Skip to content

Commit

Permalink
rename spa.py to _spa.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kandersolar committed Jun 12, 2023
1 parent f02266f commit eb2e23a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 22 deletions.
8 changes: 0 additions & 8 deletions docs/sphinx/source/reference/solarposition.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ Functions for calculating sunrise, sunset and transit times.
solarposition.sun_rise_set_transit_geometric


The spa module contains the implementation of the built-in NREL SPA
algorithm.

.. autosummary::
:toctree: generated/

spa

Correlations and analytical expressions for low precision solar position
calculations.

Expand Down
1 change: 0 additions & 1 deletion pvlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
snow,
soiling,
solarposition,
spa,
temperature,
tools,
tracking,
Expand Down
1 change: 0 additions & 1 deletion pvlib/spa.py → pvlib/_spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,4 +1245,3 @@ def earthsun_distance(unixtime, delta_t, numthreads):

return R


10 changes: 5 additions & 5 deletions pvlib/solarposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,10 @@ def spa_c(time, latitude, longitude, pressure=101325, altitude=0,
def _spa_python_import(how):
"""Compile spa.py appropriately"""

from pvlib import spa
from pvlib import _spa

# check to see if the spa module was compiled with numba
using_numba = spa.USE_NUMBA
using_numba = _spa.USE_NUMBA

if how == 'numpy' and using_numba:
# the spa module was compiled to numba code, so we need to
Expand All @@ -259,19 +259,19 @@ def _spa_python_import(how):
# to not compile with numba
warnings.warn('Reloading spa to use numpy')
os.environ['PVLIB_USE_NUMBA'] = '0'
spa = reload(spa)
_spa = reload(_spa)
del os.environ['PVLIB_USE_NUMBA']
elif how == 'numba' and not using_numba:
# The spa module was not compiled to numba code, so set
# PVLIB_USE_NUMBA so it does compile to numba on reload.
warnings.warn('Reloading spa to use numba')
os.environ['PVLIB_USE_NUMBA'] = '1'
spa = reload(spa)
_spa = reload(_spa)
del os.environ['PVLIB_USE_NUMBA']
elif how != 'numba' and how != 'numpy':
raise ValueError("how must be either 'numba' or 'numpy'")

return spa
return _spa


def spa_python(time, latitude, longitude,
Expand Down
43 changes: 38 additions & 5 deletions pvlib/tests/test_solarposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest

from pvlib.location import Location
from pvlib import solarposition, spa
from pvlib import solarposition, _spa

from .conftest import requires_ephem, requires_spa_c, requires_numba

Expand Down Expand Up @@ -560,11 +560,11 @@ def test_declination():
times = pd.date_range(start="1/1/2015 0:00", end="12/31/2015 23:00",
freq="H")
atmos_refract = 0.5667
delta_t = spa.calculate_deltat(times.year, times.month)
delta_t = solarposition.calculate_deltat(times.year, times.month)
unixtime = np.array([calendar.timegm(t.timetuple()) for t in times])
_, _, declination = spa.solar_position(unixtime, 37.8, -122.25, 100,
1013.25, 25, delta_t, atmos_refract,
sst=True)
_, _, declination = _spa.solar_position(
unixtime, 37.8, -122.25, 100, 1013.25, 25, delta_t, atmos_refract,
sst=True)
declination = np.deg2rad(declination)
declination_rng = declination.max() - declination.min()
declination_1 = solarposition.declination_cooper69(times.dayofyear)
Expand Down Expand Up @@ -769,3 +769,36 @@ def test_spa_python_numba_physical_dst(expected_solpos, golden):
temperature=11, delta_t=67,
atmos_refract=0.5667,
how='numpy', numthreads=1)


def test_calculate_deltat():
year_array = np.array([-499, 500, 1000, 1500, 1800, 1860, 1900, 1950,
1970, 1985, 1990, 2000, 2005, 2050, 2150])
# `month_array` is used with `year_array` in `test_calculate_deltat`.
# Both arrays need to have the same length for the test, hence the duplicates.
month_array = np.array([1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12, 12, 12])
dt_actual = 54.413442486
dt_actual_array = np.array([1.7184831e+04, 5.7088051e+03, 1.5730419e+03,
1.9801820e+02, 1.3596506e+01, 7.8316585e+00,
-2.1171894e+00, 2.9289261e+01, 4.0824887e+01,
5.4724581e+01, 5.7426651e+01, 6.4108015e+01,
6.5038015e+01, 9.4952955e+01, 3.3050693e+02])
year = 1985
month = 2

mix_year_array = np.full((10), year)
mix_month_array = np.full((10), month)
mix_year_actual = np.full((10), dt_actual)
mix_month_actual = mix_year_actual

result_mix_year = solarposition.calculate_deltat(mix_year_array, month)
np.testing.assert_almost_equal(mix_year_actual, result_mix_year)

result_mix_month = solarposition.calculate_deltat(year, mix_month_array)
np.testing.assert_almost_equal(mix_month_actual, result_mix_month)

result_array = solarposition.calculate_deltat(year_array, month_array)
np.testing.assert_almost_equal(dt_actual_array, result_array, 3)

result_scalar = solarposition.calculate_deltat(year, month)
np.testing.assert_almost_equal(dt_actual, result_scalar)
4 changes: 2 additions & 2 deletions pvlib/tests/test_spa.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class NumpySpaTest(unittest.TestCase, SpaBase):
@classmethod
def setUpClass(self):
os.environ['PVLIB_USE_NUMBA'] = '0'
import pvlib.spa as spa
import pvlib._spa as spa
spa = reload(spa)
self.spa = spa

Expand All @@ -398,7 +398,7 @@ class NumbaSpaTest(unittest.TestCase, SpaBase):
def setUpClass(self):
os.environ['PVLIB_USE_NUMBA'] = '1'
if numba_version_int >= 17:
import pvlib.spa as spa
import pvlib._spa as spa
spa = reload(spa)
self.spa = spa

Expand Down

0 comments on commit eb2e23a

Please sign in to comment.