NREL / rex

REsource eXtraction Tool (rex)
https://nrel.github.io/rex
BSD 3-Clause "New" or "Revised" License
19 stars 10 forks source link

Cast array to float before computation #179

Open ppinchuk opened 1 month ago

ppinchuk commented 1 month ago

We should cast to numpy float to avoid the following error:

In [8]: spa_test.azimuth()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
AttributeError: 'numpy.float64' object has no attribute 'radians'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 spa_test.azimuth()

File ~/miniconda3/envs/py312/lib/python3.12/site-packages/rex/utilities/solar_position.py:405, in SolarPosition.azimuth(self)
    395 @property
    396 def azimuth(self):
    397     """
    398     Solar azimuth angle
    399
   (...)
    403         Solar azimuth angle in degrees
    404     """
--> 405     azimuth = self._azimuth(self.time_index, self.latitude, self.longitude)
    407     return self._format_output(azimuth)

File ~/miniconda3/envs/py312/lib/python3.12/site-packages/rex/utilities/solar_position.py:316, in SolarPosition._azimuth(time_index, lat, lon)
    314 n, zulu = SolarPosition._parse_time(time_index)
    315 ra, dec = SolarPosition._calc_sun_pos(n)
--> 316 ha = SolarPosition._calc_hour_angle(n, zulu, ra, lon)
    317 azimuth = SolarPosition._calc_azimuth(dec, ha, lat)
    318 return azimuth

File ~/miniconda3/envs/py312/lib/python3.12/site-packages/rex/utilities/solar_position.py:175, in SolarPosition._calc_hour_angle(n, zulu, ra, lon)
    173 gmst = (6.697375 + 0.06570982441908 * n + 1.00273790935 * zulu) * 15
    174 # Local mean sidereal time in radians
--> 175 lmst = np.radians(np.remainder(gmst + lon, 360))
    176 # Hour angle in radians
    177 ha = lmst - ra

TypeError: loop of ufunc does not support argument 0 of type numpy.float64 which has no callable radians method

Error produced with pandas = 2.2.2, numpy = 1.26.4.

spa_test = spa.SolarPosition(sam_df['datetime'].values, sites.iloc[0, 1:3].values)

Fixed with spa_test = spa.SolarPosition(sam_df['datetime'].values, sites.iloc[0, 1:3].values.astype('float64'))

ALSO! Azimuth calculation implementation is bugged. Missing these lines from the SAM code: https://github.com/NREL/ssc/blob/20cc2e75b0f5b77aacc9ae3200a327dfe54abf92/shared/lib_irradproc.cpp#L181-L184

This can be fixed on the user side by:

test_ar = np.array((((ha <= 0.0) & (ha >= -np.pi), (ha >= np.pi))))

azm_adj = np.where(np.any(test_ar, axis=0), np.pi - azm, np.pi + azm)

But this should be fixed in the code instead (and add tests for this calculation!)