Skip to content

Commit 03bb143

Browse files
author
Daniel Lassahn
committed
Handle timezones in timeRange
1 parent 71dc8c5 commit 03bb143

File tree

4 files changed

+33
-126
lines changed

4 files changed

+33
-126
lines changed

.stickler.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ linters:
22
flake8:
33
python: 3
44
max-line-length: 79
5-
ignore: E201,E241,E226
5+
ignore: E201,E241,E226,W503,W504
66
files:
77
ignore:
88
- 'pvlib/_version.py'

pvlib/solarposition.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from pvlib._deprecation import deprecated
2929

3030
NS_PER_HR = 1.e9 * 3600. # nanoseconds per hour
31-
JULIAN_2000 = 2451544.5
3231
JULIAN_YEARS = 365.2425
3332

3433

@@ -1488,8 +1487,8 @@ def spencer(times, latitude, longitude):
14881487
Springer Science & Business Media, 2008.
14891488
"""
14901489

1491-
julians = datetime_to_julian(times)
1492-
julians_2000 = np.asarray(julians, dtype=np.float) - JULIAN_2000
1490+
julians, julian_2000 = datetime_to_julian(times)
1491+
julians_2000 = np.asarray(julians, dtype=np.float) - julian_2000
14931492

14941493
latitude_radians = np.radians(latitude)
14951494
day_time = (julians_2000 % 1) * 24

pvlib/test/test_solarposition.py

Lines changed: 18 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -754,122 +754,25 @@ def test_sun_rise_set_transit_geometric(expected_rise_set_spa, golden_mst):
754754
atol=np.abs(expected_transit_error).max())
755755

756756

757-
def test_spencer():
757+
def test_get_solar_position_spencer(golden_mst):
758758
""" test for the calculation based on spencer 1972 """
759-
latitude = 32.2
760-
longitude = -111
761-
to_test = solarposition.spencer(times,
762-
latitude,
763-
longitude)
764-
np.testing.assert_array_almost_equal(
765-
to_test.zenith.values,
766-
np.array(
767-
[60.22983081, 63.34160549, 66.43402974, 69.50442757,
768-
72.54998112, 75.56768635, 78.55430954, 81.50634428,
769-
84.41996557, 87.29098172, 90.11478465, 92.88629624,
770-
95.5999124, 98.24944658, 100.8280723, 103.32826814,
771-
105.74177, 108.05953355, 110.2717148, 112.36767828,
772-
114.33604136, 116.16476744, 117.84132062, 119.35289073,
773-
120.68669583, 121.83036181, 122.77236599, 123.50252116,
774-
124.01246252, 124.29608957, 124.34991302, 124.17326279,
775-
123.76832862, 123.14002805, 122.29571959, 121.24479895,
776-
119.99822708, 118.56803917, 116.96687825, 115.2075839,
777-
113.30285269, 111.26497738, 109.10566098, 106.83589588,
778-
104.46589822, 102.00508468, 99.46207977, 96.84474584,
779-
94.16022733, 91.4150024, 88.61493961, 85.76535568,
780-
82.87107126, 79.93646571, 76.96552925, 73.9619112,
781-
70.92896677, 67.86980202, 64.78731633, 61.68424611,
782-
58.56321009, 55.42675712, 52.27742195, 49.11779228,
783-
45.95059256, 42.77879745, 39.60579061, 36.43559544,
784-
33.27322843, 30.12525958, 27.00073787, 23.9127918,
785-
20.88152421, 17.93949588, 15.1425391, 12.59129497,
786-
10.47023949, 9.08837272, 8.80250072, 9.71016099,
787-
11.53222951, 13.91268049, 16.61145941, 19.49478556,
788-
22.48953441, 25.55411892, 28.66354138, 31.80189434,
789-
34.95848314, 38.12572775, 41.29797352, 44.47078752,
790-
47.64052254, 50.80403616, 53.95850349, 57.10128521,
791-
60.19189057, 63.30411711, 66.39705174, 69.46801919,
792-
72.51420324, 75.53260234, 78.51998605, 81.472852,
793-
84.38737999, 87.25938384, 90.08426172, 92.85694249,
794-
95.5718297, 98.22274508, 100.80287095, 103.30469507,
795-
105.71996264, 108.03963849, 110.2538871, 112.35208031,
796-
114.32284084, 116.15413465, 117.83342459, 119.34789444,
797-
120.68475046, 121.83160023, 122.77689598, 123.51041877,
798-
124.02376646, 124.31079722, 124.36797868, 124.19459854,
799-
123.79280774, 123.16749074, 122.32598014, 121.27765375,
800-
120.03346257, 118.60543913, 117.0062299, 115.2486827,
801-
113.34550579, 111.30900578, 109.15090068, 106.88219818,
802-
104.51312938, 102.0531253, 99.51082375, 96.8940993,
803-
94.21010748, 91.46533641, 88.6656635, 85.81641338,
804-
82.92241365, 79.98804982, 77.01731753, 74.01387089,
805-
70.98106934, 67.92202265, 64.8396335, 61.73664121,
806-
58.6156671, 55.47926225, 52.3299633, 49.17035952,
807-
46.00317641, 42.83138906, 39.65838052, 36.48817188,
808-
33.32577437, 30.17774741, 27.05311932, 23.9649783,
809-
20.93334666, 17.99061869, 15.19226962, 12.63816039,
810-
10.51116169, 9.11789559, 8.81495244, 9.70524228,
811-
11.51457879, 13.88698965, 16.5808083, 19.46100386,
812-
22.45371889, 25.51695489, 28.62547834, 31.76324145,
813-
34.91946275, 38.08650657, 41.25868161, 44.43153022,
814-
47.60138827, 50.76510173, 53.91983782, 57.06295199,
815-
60.15714089]
816-
)
817-
)
818-
819-
np.testing.assert_array_almost_equal(
820-
to_test.azimuth.values,
821-
np.array(
822-
[280.45072709, 282.11633942, 283.7817931, 285.45643927,
823-
287.14893238, 288.86746085, 290.61992811, 292.41409693,
824-
294.2577037, 296.15854802, 298.12456058, 300.16384793,
825-
302.28471289, 304.49564761, 306.80529163, 309.22234814,
826-
311.75544988, 314.41296211, 317.20271279, 320.13164164,
827-
323.20536119, 326.42763345, 329.79977946, 333.32005237,
828-
336.98302936, 340.77909977, 344.69413863, 348.7094615,
829-
352.80214024, 356.94571294, 1.11126554, 5.26879407,
830-
9.38869489, 13.44320688, 17.40764042, 21.26126792,
831-
24.98782151, 28.57560944, 32.01730949, 35.30953094,
832-
38.45224149, 41.44814098, 44.3020479, 47.02034252,
833-
49.61048749, 52.08063548, 54.43932309, 56.69524105,
834-
58.85707173, 60.93338359, 62.93257078, 64.86283035,
835-
66.73217087, 68.54844619, 70.31941227, 72.05280693,
836-
73.75645209, 75.438383, 77.10701127, 78.7713305,
837-
80.44118034, 82.12759231, 83.84324978, 85.60311369,
838-
87.42529319, 89.33228209, 91.3527583, 93.52426951,
839-
95.8973478, 98.54199782, 101.55824322, 105.09379965,
840-
109.37452354, 114.75768924, 121.82299993, 131.5046235,
841-
145.14346316, 163.81906282, 186.0060095, 206.77205045,
842-
222.71272671, 234.05797936, 242.20138813, 248.2792225,
843-
253.019093, 256.86789112, 260.10425777, 262.90749472,
844-
265.39707784, 267.65536873, 269.74098333, 271.69690061,
845-
273.5555329, 275.34199145, 277.07624914, 278.77460915,
846-
280.40857909, 282.07488513, 283.74081962, 285.41575589,
847-
287.10836546, 288.8268498, 290.57912232, 292.37295337,
848-
294.21608515, 296.11632179, 298.08159771, 300.12002294,
849-
302.239904, 304.44973753, 306.75816911, 309.1739102,
850-
311.70560495, 314.36163417, 317.1498465, 320.07720857,
851-
323.14936689, 326.37012512, 329.7408537, 333.25986209,
852-
336.9217888, 340.71708646, 344.63169085, 348.64697055,
853-
352.74003696, 356.88444935, 1.05129299, 5.21054047,
854-
9.33254359, 13.38947986, 17.35658738, 21.21306193,
855-
24.94256075, 28.53332377, 31.9779703, 35.2730625,
856-
38.41853254, 41.41705548, 44.27343437, 46.99404174,
857-
49.58633845, 52.05847988, 54.4190084, 56.6766226,
858-
58.84001384, 60.91776009, 62.91826507, 64.84973506,
859-
66.72018726, 68.5374834, 70.30938637, 72.04363982,
860-
73.74807028, 75.43071613, 77.09999039, 78.7648859,
861-
80.43523879, 82.12207338, 83.83806078, 85.59814255,
862-
87.420398, 89.32727554, 91.34738372, 93.51816308,
863-
95.88997704, 98.53255847, 101.54548029, 105.07568588,
864-
109.34766226, 114.71618082, 121.75632435, 131.39424631,
865-
144.96107287, 163.54312836, 185.67160477, 206.4608716,
866-
222.464149, 233.86642744, 242.05129037, 248.1579107,
867-
252.91794505, 256.78120715, 260.02821949, 262.83947716,
868-
265.33522383, 267.59832701, 269.68774445, 271.64669179,
869-
273.50774886, 275.29614781, 277.03195029, 278.73152558,
870-
280.35936035]
871-
)
872-
)
759+
times = pd.date_range(datetime.datetime(2003, 10, 17, 12, 30, 30),
760+
periods=1, freq='D')
761+
spencer_data = solarposition.spencer(times,
762+
golden_mst.latitude,
763+
golden_mst.longitude)
764+
765+
expected_solpos = golden_mst.get_solarposition(times)
766+
767+
np.testing.assert_array_almost_equal(spencer_data.zenith.values,
768+
expected_solpos.zenith.values,
769+
decimal=0)
770+
np.testing.assert_array_almost_equal(spencer_data.azimuth.values,
771+
expected_solpos.azimuth.values,
772+
decimal=0)
773+
np.testing.assert_array_almost_equal(spencer_data.elevation.values,
774+
expected_solpos.elevation.values,
775+
decimal=0)
873776

874777

875778
# put numba tests at end of file to minimize reloading

pvlib/tools.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import pandas as pd
1010
import pytz
1111

12-
JULIAN_2000 = 2451544.5
13-
DT_2000 = dt.datetime(2000, 1, 1)
12+
JULIAN_2000 = 2451545
1413
DAY_SECONDS = 60 * 60 * 24
14+
DT_2000 = pd.to_datetime(dt.datetime(2000, 1, 1)).tz_localize('UTC')
1515

1616

1717
def cosd(angle):
@@ -437,17 +437,22 @@ def datetime_to_julian(times):
437437
438438
Parameters
439439
----------
440-
times : :class:`pandas.DatetimeIndex`
440+
times : pandas.DatetimeIndex
441441
Corresponding timestamps, must be localized to the timezone for the
442442
``latitude`` and ``longitude``
443443
Returns
444444
-------
445445
Float64Index
446446
The float index contains julian dates
447+
Float
448+
julian 2000 in UTC referenced to local time
447449
"""
448-
449-
delta = times - DT_2000
450+
dt_2000 = DT_2000.tz_convert(times.tzinfo)
451+
hours_difference = (DT_2000.tz_localize(None) -
452+
dt_2000.tz_localize(None)).seconds / 3600
453+
julian_2000 = JULIAN_2000 - (hours_difference/24)
454+
delta = times - dt_2000
450455
delta_julians = (delta.seconds + delta.microseconds / 1e6)
451456
return (
452-
JULIAN_2000 + delta.days + delta_julians / DAY_SECONDS
453-
)
457+
julian_2000 + delta.days + delta_julians / DAY_SECONDS
458+
), julian_2000

0 commit comments

Comments
 (0)