@@ -907,6 +907,7 @@ def solar_position_loop(unixtime, loc_args, out):
907
907
delta_t = loc_args [5 ]
908
908
atmos_refract = loc_args [6 ]
909
909
sst = loc_args [7 ]
910
+ esd = loc_args [8 ]
910
911
911
912
for i in range (unixtime .shape [0 ]):
912
913
utime = unixtime [i ]
@@ -918,6 +919,9 @@ def solar_position_loop(unixtime, loc_args, out):
918
919
L = heliocentric_longitude (jme )
919
920
B = heliocentric_latitude (jme )
920
921
R = heliocentric_radius_vector (jme )
922
+ if esd :
923
+ out [0 , i ] = R
924
+ continue
921
925
Theta = geocentric_longitude (L )
922
926
beta = geocentric_latitude (B )
923
927
x0 = mean_elongation (jce )
@@ -970,14 +974,24 @@ def solar_position_loop(unixtime, loc_args, out):
970
974
971
975
972
976
def solar_position_numba (unixtime , lat , lon , elev , pressure , temp , delta_t ,
973
- atmos_refract , numthreads , sst = False ):
977
+ atmos_refract , numthreads , sst = False , esd = False ):
974
978
"""Calculate the solar position using the numba compiled functions
975
979
and multiple threads. Very slow if functions are not numba compiled.
976
980
"""
981
+ # these args are the same for each thread
977
982
loc_args = np .array ([lat , lon , elev , pressure , temp , delta_t ,
978
- atmos_refract , sst ])
983
+ atmos_refract , sst , esd ])
984
+
985
+ # construct dims x ulength array to put the results in
979
986
ulength = unixtime .shape [0 ]
980
- result = np .empty ((6 , ulength ), dtype = np .float64 )
987
+ if sst :
988
+ dims = 3
989
+ elif esd :
990
+ dims = 1
991
+ else :
992
+ dims = 6
993
+ result = np .empty ((dims , ulength ), dtype = np .float64 )
994
+
981
995
if unixtime .dtype != np .float64 :
982
996
unixtime = unixtime .astype (np .float64 )
983
997
@@ -992,6 +1006,7 @@ def solar_position_numba(unixtime, lat, lon, elev, pressure, temp, delta_t,
992
1006
solar_position_loop (unixtime , loc_args , result )
993
1007
return result
994
1008
1009
+ # split the input and output arrays into numthreads chunks
995
1010
split0 = np .array_split (unixtime , numthreads )
996
1011
split2 = np .array_split (result , numthreads , axis = 1 )
997
1012
chunks = [[a0 , loc_args , split2 [i ]] for i , a0 in enumerate (split0 )]
@@ -1006,7 +1021,7 @@ def solar_position_numba(unixtime, lat, lon, elev, pressure, temp, delta_t,
1006
1021
1007
1022
1008
1023
def solar_position_numpy (unixtime , lat , lon , elev , pressure , temp , delta_t ,
1009
- atmos_refract , numthreads , sst = False ):
1024
+ atmos_refract , numthreads , sst = False , esd = False ):
1010
1025
"""Calculate the solar position assuming unixtime is a numpy array. Note
1011
1026
this function will not work if the solar position functions were
1012
1027
compiled with numba.
@@ -1020,6 +1035,8 @@ def solar_position_numpy(unixtime, lat, lon, elev, pressure, temp, delta_t,
1020
1035
L = heliocentric_longitude (jme )
1021
1036
B = heliocentric_latitude (jme )
1022
1037
R = heliocentric_radius_vector (jme )
1038
+ if esd :
1039
+ return (R , )
1023
1040
Theta = geocentric_longitude (L )
1024
1041
beta = geocentric_latitude (B )
1025
1042
x0 = mean_elongation (jce )
@@ -1063,7 +1080,7 @@ def solar_position_numpy(unixtime, lat, lon, elev, pressure, temp, delta_t,
1063
1080
1064
1081
1065
1082
def solar_position (unixtime , lat , lon , elev , pressure , temp , delta_t ,
1066
- atmos_refract , numthreads = 8 , sst = False ):
1083
+ atmos_refract , numthreads = 8 , sst = False , esd = False ):
1067
1084
1068
1085
"""
1069
1086
Calculate the solar position using the
@@ -1100,6 +1117,11 @@ def solar_position(unixtime, lat, lon, elev, pressure, temp, delta_t,
1100
1117
numthreads: int, optional
1101
1118
Number of threads to use for computation if numba>=0.17
1102
1119
is installed.
1120
+ sst : bool
1121
+ If True, return only data needed for sunrise, sunset, and transit
1122
+ calculations.
1123
+ esd : bool
1124
+ If True, return only Earth-Sun distance in AU
1103
1125
1104
1126
Returns
1105
1127
-------
@@ -1126,7 +1148,7 @@ def solar_position(unixtime, lat, lon, elev, pressure, temp, delta_t,
1126
1148
1127
1149
result = do_calc (unixtime , lat , lon , elev , pressure ,
1128
1150
temp , delta_t , atmos_refract , numthreads ,
1129
- sst )
1151
+ sst , esd )
1130
1152
1131
1153
if not isinstance (result , np .ndarray ):
1132
1154
try :
@@ -1241,3 +1263,31 @@ def transit_sunrise_sunset(dates, lat, lon, delta_t, numthreads):
1241
1263
sunset = S + utday
1242
1264
1243
1265
return transit , sunrise , sunset
1266
+
1267
+
1268
+ def earthsun_distance (unixtime , delta_t , numthreads ):
1269
+ """
1270
+ Calculate the sun transit, sunrise, and sunset
1271
+ for a set of dates at a given location.
1272
+
1273
+ Parameters
1274
+ ----------
1275
+ unixtime : numpy array
1276
+ Array of unix/epoch timestamps to calculate solar position for.
1277
+ Unixtime is the number of seconds since Jan. 1, 1970 00:00:00 UTC.
1278
+ A pandas.DatetimeIndex is easily converted using .astype(np.int64)/10**9
1279
+ delta_t : float
1280
+ Difference between terrestrial time and UT. USNO has tables.
1281
+ numthreads : int
1282
+ Number to threads to use for calculation (if using numba)
1283
+
1284
+ Returns
1285
+ -------
1286
+ R : array
1287
+ Earth-Sun distance in AU.
1288
+ """
1289
+
1290
+ R = solar_position (unixtime , 0 , 0 , 0 , 0 , 0 , delta_t ,
1291
+ 0 , numthreads , esd = True )[0 ]
1292
+
1293
+ return R
0 commit comments