Skip to content

Commit 745eb0f

Browse files
committed
code changes: allow delta_t to be array
1 parent b5e89be commit 745eb0f

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

pvlib/solarposition.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,11 @@ def spa_python(time, latitude, longitude,
312312
avg. yearly air pressure in Pascals.
313313
temperature : int or float, optional, default 12
314314
avg. yearly air temperature in degrees C.
315-
delta_t : float, optional, default 67.0
315+
delta_t : float or array, optional, default 67.0
316316
Difference between terrestrial time and UT1.
317317
If delta_t is None, uses spa.calculate_deltat
318318
using time.year and time.month from pandas.DatetimeIndex.
319319
For most simulations the default delta_t is sufficient.
320-
*Note: delta_t = None will break code using nrel_numba,
321-
this will be fixed in a future version.*
322320
The USNO has historical and forecasted delta_t [3]_.
323321
atmos_refrac : float, optional
324322
The approximate atmospheric refraction (in degrees)
@@ -378,7 +376,8 @@ def spa_python(time, latitude, longitude,
378376

379377
spa = _spa_python_import(how)
380378

381-
delta_t = delta_t or spa.calculate_deltat(time.year, time.month)
379+
if delta_t is None:
380+
delta_t = spa.calculate_deltat(time.year, time.month)
382381

383382
app_zenith, zenith, app_elevation, elevation, azimuth, eot = \
384383
spa.solar_position(unixtime, lat, lon, elev, pressure, temperature,
@@ -418,13 +417,11 @@ def sun_rise_set_transit_spa(times, latitude, longitude, how='numpy',
418417
Options are 'numpy' or 'numba'. If numba >= 0.17.0
419418
is installed, how='numba' will compile the spa functions
420419
to machine code and run them multithreaded.
421-
delta_t : float, optional, default 67.0
420+
delta_t : float or array, optional, default 67.0
422421
Difference between terrestrial time and UT1.
423422
If delta_t is None, uses spa.calculate_deltat
424423
using times.year and times.month from pandas.DatetimeIndex.
425424
For most simulations the default delta_t is sufficient.
426-
*Note: delta_t = None will break code using nrel_numba,
427-
this will be fixed in a future version.*
428425
numthreads : int, optional, default 4
429426
Number of threads to use if how == 'numba'.
430427
@@ -457,7 +454,8 @@ def sun_rise_set_transit_spa(times, latitude, longitude, how='numpy',
457454

458455
spa = _spa_python_import(how)
459456

460-
delta_t = delta_t or spa.calculate_deltat(times.year, times.month)
457+
if delta_t is None:
458+
delta_t = spa.calculate_deltat(times.year, times.month)
461459

462460
transit, sunrise, sunset = spa.transit_sunrise_sunset(
463461
unixtime, lat, lon, delta_t, numthreads)
@@ -980,13 +978,11 @@ def nrel_earthsun_distance(time, how='numpy', delta_t=67.0, numthreads=4):
980978
is installed, how='numba' will compile the spa functions
981979
to machine code and run them multithreaded.
982980
983-
delta_t : float, optional, default 67.0
981+
delta_t : float or array, optional, default 67.0
984982
Difference between terrestrial time and UT1.
985983
If delta_t is None, uses spa.calculate_deltat
986984
using time.year and time.month from pandas.DatetimeIndex.
987985
For most simulations the default delta_t is sufficient.
988-
*Note: delta_t = None will break code using nrel_numba,
989-
this will be fixed in a future version.*
990986
991987
numthreads : int, optional, default 4
992988
Number of threads to use if how == 'numba'.
@@ -1013,7 +1009,8 @@ def nrel_earthsun_distance(time, how='numpy', delta_t=67.0, numthreads=4):
10131009

10141010
spa = _spa_python_import(how)
10151011

1016-
delta_t = delta_t or spa.calculate_deltat(time.year, time.month)
1012+
if delta_t is None:
1013+
delta_t = spa.calculate_deltat(time.year, time.month)
10171014

10181015
dist = spa.earthsun_distance(unixtime, delta_t, numthreads)
10191016

pvlib/spa.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -835,24 +835,24 @@ def equation_of_time(sun_mean_longitude, geocentric_sun_right_ascension,
835835
return E
836836

837837

838-
@jcompile('void(float64[:], float64[:], float64[:,:])', nopython=True,
839-
nogil=True)
840-
def solar_position_loop(unixtime, loc_args, out):
838+
@jcompile('void(float64[:], float64[:], float64[:], float64[:,:])',
839+
nopython=True, nogil=True)
840+
def solar_position_loop(unixtime, delta_t, loc_args, out):
841841
"""Loop through the time array and calculate the solar position"""
842842
lat = loc_args[0]
843843
lon = loc_args[1]
844844
elev = loc_args[2]
845845
pressure = loc_args[3]
846846
temp = loc_args[4]
847-
delta_t = loc_args[5]
848-
atmos_refract = loc_args[6]
849-
sst = loc_args[7]
850-
esd = loc_args[8]
847+
atmos_refract = loc_args[5]
848+
sst = loc_args[6]
849+
esd = loc_args[7]
851850

852851
for i in range(unixtime.shape[0]):
853852
utime = unixtime[i]
853+
dT = delta_t[i]
854854
jd = julian_day(utime)
855-
jde = julian_ephemeris_day(jd, delta_t)
855+
jde = julian_ephemeris_day(jd, dT)
856856
jc = julian_century(jd)
857857
jce = julian_ephemeris_century(jde)
858858
jme = julian_ephemeris_millennium(jce)
@@ -920,8 +920,11 @@ def solar_position_numba(unixtime, lat, lon, elev, pressure, temp, delta_t,
920920
and multiple threads. Very slow if functions are not numba compiled.
921921
"""
922922
# these args are the same for each thread
923-
loc_args = np.array([lat, lon, elev, pressure, temp, delta_t,
924-
atmos_refract, sst, esd])
923+
loc_args = np.array([lat, lon, elev, pressure, temp,
924+
atmos_refract, sst, esd], dtype=np.float64)
925+
926+
# turn delta_t into an array if it isn't already
927+
delta_t = np.full_like(unixtime, delta_t)
925928

926929
# construct dims x ulength array to put the results in
927930
ulength = unixtime.shape[0]
@@ -942,13 +945,17 @@ def solar_position_numba(unixtime, lat, lon, elev, pressure, temp, delta_t,
942945
numthreads = ulength
943946

944947
if numthreads <= 1:
945-
solar_position_loop(unixtime, loc_args, result)
948+
solar_position_loop(unixtime, delta_t, loc_args, result)
946949
return result
947950

948951
# split the input and output arrays into numthreads chunks
949952
split0 = np.array_split(unixtime, numthreads)
953+
split1 = np.array_split(delta_t, numthreads)
950954
split2 = np.array_split(result, numthreads, axis=1)
951-
chunks = [[a0, loc_args, split2[i]] for i, a0 in enumerate(split0)]
955+
chunks = [
956+
[a0, a1, loc_args, a2]
957+
for i, (a0, a1, a2) in enumerate(zip(split0, split1, split2))
958+
]
952959
# Spawn one thread per chunk
953960
threads = [threading.Thread(target=solar_position_loop, args=chunk)
954961
for chunk in chunks]
@@ -1048,7 +1055,7 @@ def solar_position(unixtime, lat, lon, elev, pressure, temp, delta_t,
10481055
temp : int or float
10491056
avg. yearly temperature at location in
10501057
degrees C; used for atmospheric correction
1051-
delta_t : float
1058+
delta_t : float or array
10521059
Difference between terrestrial time and UT1.
10531060
atmos_refrac : float
10541061
The approximate atmospheric refraction (in degrees)
@@ -1113,7 +1120,7 @@ def transit_sunrise_sunset(dates, lat, lon, delta_t, numthreads):
11131120
Latitude of location to perform calculation for
11141121
lon : float
11151122
Longitude of location
1116-
delta_t : float
1123+
delta_t : float or array
11171124
Difference between terrestrial time and UT. USNO has tables.
11181125
numthreads : int
11191126
Number to threads to use for calculation (if using numba)
@@ -1215,7 +1222,7 @@ def earthsun_distance(unixtime, delta_t, numthreads):
12151222
Array of unix/epoch timestamps to calculate solar position for.
12161223
Unixtime is the number of seconds since Jan. 1, 1970 00:00:00 UTC.
12171224
A pandas.DatetimeIndex is easily converted using .astype(np.int64)/10**9
1218-
delta_t : float
1225+
delta_t : float or array
12191226
Difference between terrestrial time and UT. USNO has tables.
12201227
numthreads : int
12211228
Number to threads to use for calculation (if using numba)
@@ -1242,9 +1249,6 @@ def calculate_deltat(year, month):
12421249
"""Calculate the difference between Terrestrial Dynamical Time (TD)
12431250
and Universal Time (UT).
12441251
1245-
Note: This function is not yet compatible for calculations using
1246-
Numba.
1247-
12481252
Equations taken from http://eclipse.gsfc.nasa.gov/SEcat5/deltatpoly.html
12491253
"""
12501254

0 commit comments

Comments
 (0)