Skip to content

Commit fe5bf63

Browse files
author
Daniel Lassahn
committed
Rename function, use existend equation of time function, whatsnew in 7.0.0.
1 parent b20ca53 commit fe5bf63

File tree

6 files changed

+127
-147
lines changed

6 files changed

+127
-147
lines changed

docs/sphinx/source/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ unless you know that you need a different function.
4545
solarposition.ephemeris
4646
solarposition.pyephem
4747
solarposition.spa_c
48-
solarposition.spencer_mc
48+
solarposition.spencer
4949

5050

5151
Additional functions for quantities closely related to solar position.

docs/sphinx/source/whatsnew/v0.7.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ recommend all users of v0.6.3 upgrade to this release.
88

99
**Python 2.7 support ended on June 1, 2019**. (:issue:`501`)
1010

11+
- Add a faster way to calculate the solar position (`spencer`)
12+
1113

1214
Contributors
1315
~~~~~~~~~~~~
1416
* Mark Campanellli (:ghuser:`markcampanelli`)
1517
* Will Holmgren (:ghuser:`wholmgren`)
18+
* Daniel Lassahn (:ghuser:`meteoDaniel`)

docs/sphinx/source/whatsnew/v0.7.1.rst

Lines changed: 0 additions & 11 deletions
This file was deleted.

pvlib/solarposition.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def get_solarposition(time, latitude, longitude,
6868
6969
'nrel_c' uses the NREL SPA C code [3]: :py:func:`spa_c`
7070
71-
'spencer_mc' uses the Spencer formula [4] :py:func:`spencer_mc`
71+
'spencer' uses the Spencer formula [4] :py:func:`spencer`
7272
7373
7474
temperature : float, default 12
@@ -120,8 +120,8 @@ def get_solarposition(time, latitude, longitude,
120120
temperature=temperature, **kwargs)
121121
elif method == 'ephemeris':
122122
ephem_df = ephemeris(time, latitude, longitude, pressure, temperature)
123-
elif method == 'spencer_mc':
124-
ephem_df = spencer_mc(time, latitude, longitude)
123+
elif method == 'spencer':
124+
ephem_df = spencer(time, latitude, longitude)
125125

126126
else:
127127
raise ValueError('Invalid solar position method')
@@ -1456,14 +1456,13 @@ def sun_rise_set_transit_geometric(times, latitude, longitude, declination,
14561456
return sunrise, sunset, transit
14571457

14581458

1459-
def spencer_mc(times, latitude, longitude):
1459+
def spencer(times, latitude, longitude):
14601460
"""
1461-
Calculate the solar position using a python implementation of the
1462-
Spencer (1972) formulation provided by meteocontrol
1461+
Calculate the solar position using a formulation by Spencer 1971/1972.
14631462
14641463
Parameters
14651464
----------
1466-
times : :class:`pandas.DatetimeIndex`
1465+
times : pandas.DatetimeIndex`
14671466
Corresponding timestamps, must be localized to the timezone for the
14681467
``latitude`` and ``longitude``.
14691468
latitude : float
@@ -1479,7 +1478,6 @@ def spencer_mc(times, latitude, longitude):
14791478
elevation (degrees),
14801479
azimuth (degrees),
14811480
equation_of_time (seconds),
1482-
eccentricity,
14831481
declination (degrees).
14841482
14851483
References
@@ -1493,28 +1491,13 @@ def spencer_mc(times, latitude, longitude):
14931491
julians = datetime_to_julian(times)
14941492
julians_2000 = np.asarray(julians, dtype=np.float) - JULIAN_2000
14951493

1496-
lat = np.radians(latitude)
1497-
1498-
# Compute fractional year (gamma) in radians
1499-
gamma = 2 * np.pi * (julians_2000 % JULIAN_YEARS) / JULIAN_YEARS
1500-
cos_gamma = np.cos(gamma), np.cos(gamma * 2), np.cos(gamma * 3)
1501-
sin_gamma = np.sin(gamma), np.sin(gamma * 2), np.sin(gamma * 3)
1494+
latitude_radians = np.radians(latitude)
15021495
day_time = (julians_2000 % 1) * 24
15031496

1504-
# Eccentricity: correction factor of the earth's orbit.
1505-
eccentricity = 1.00011 + 0.034221 * cos_gamma[0]
1506-
eccentricity += 0.001280 * sin_gamma[0]
1507-
eccentricity += 0.000719 * cos_gamma[1]
1508-
eccentricity += 0.000077 * sin_gamma[1]
1509-
15101497
declination = np.array(declination_spencer71(times.dayofyear))
15111498

15121499
# Equation of time (difference between standard time and solar time).
1513-
eot = 0.000075 + 0.001868 * cos_gamma[0]
1514-
eot -= 0.032077 * sin_gamma[0]
1515-
eot -= 0.014615 * cos_gamma[1]
1516-
eot -= 0.040849 * sin_gamma[1]
1517-
eot *= 229.18
1500+
eot = np.array(equation_of_time_spencer71(times.dayofyear))
15181501

15191502
# True local time
15201503
tlt = (day_time + longitude / 15 + eot / 60) % 24 - 12
@@ -1523,16 +1506,18 @@ def spencer_mc(times, latitude, longitude):
15231506
ha = np.radians(tlt * 15)
15241507

15251508
# Calculate sun elevation.
1526-
sin_sun_elevation = np.sin(declination) * np.sin(lat)
1527-
sin_sun_elevation += np.cos(declination) * np.cos(lat) * np.cos(ha)
1509+
sin_sun_elevation = (
1510+
np.sin(declination) * np.sin(latitude_radians) +
1511+
np.cos(declination) * np.cos(latitude_radians) * np.cos(ha)
1512+
)
15281513

15291514
# Compute the sun's elevation and zenith angle.
15301515
elevation = np.arcsin(sin_sun_elevation)
15311516
zenith = np.pi / 2 - elevation
15321517

15331518
# Compute the sun's azimuth angle.
1534-
y = -(np.sin(lat) * np.sin(elevation) - np.sin(declination)) \
1535-
/ (np.cos(lat) * np.cos(elevation))
1519+
y = -(np.sin(latitude_radians) * np.sin(elevation) - np.sin(declination)) \
1520+
/ (np.cos(latitude_radians) * np.cos(elevation))
15361521
azimuth = np.arccos(y)
15371522

15381523
# Convert azimuth angle from 0-pi to 0-2pi.
@@ -1542,7 +1527,6 @@ def spencer_mc(times, latitude, longitude):
15421527
result = pd.DataFrame({'zenith': np.degrees(zenith),
15431528
'elevation': np.degrees(elevation),
15441529
'azimuth': np.degrees(azimuth),
1545-
'eccentricity': eccentricity,
15461530
'declination': declination,
15471531
'equation_of_time': eot},
15481532
index=times)

pvlib/test/test_solarposition.py

Lines changed: 104 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -754,120 +754,120 @@ 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_mc():
757+
def test_spencer():
758758
""" test for the calculation based on spencer 1972 """
759-
latitude = 48.367073
760-
longitude = 10.868378
761-
to_test = solarposition.spencer_mc(times,
762-
latitude,
763-
longitude)
759+
latitude = 32.2
760+
longitude = -111
761+
to_test = solarposition.spencer(times,
762+
latitude,
763+
longitude)
764764
np.testing.assert_array_almost_equal(
765765
to_test.zenith.values,
766766
np.array(
767-
[107.5944161, 107.08772172, 106.43116829, 105.62900042,
768-
104.68621177, 103.60841558, 102.40171152, 101.07255494,
769-
99.62763476, 98.07376382, 96.41778336, 94.66648432,
770-
92.82654495, 90.90448339, 88.90662551, 86.83908587,
771-
84.70775942, 82.51832403, 80.27625186, 77.98682746,
772-
75.65517374, 73.28628434, 70.88506165, 68.45636234,
773-
66.0050506, 63.53605921, 61.05446218, 58.56556086,
774-
56.07498558, 53.58881946, 51.11374884, 48.65724635,
775-
46.22779685, 43.83517458, 41.49078009, 39.20804631,
776-
37.00291337, 34.89435623, 32.90492124, 31.06117096,
777-
29.39385375, 27.93751303, 26.72916357, 25.80569641,
778-
25.19996401, 24.93609994, 25.02533181, 25.46381181,
779-
26.23337616, 27.30490594, 28.64295418, 30.21015627,
780-
31.97050468, 33.89127116, 35.94381137, 38.10363121,
781-
40.35004899, 42.66568743, 45.03593362, 47.44843417,
782-
49.89265446, 52.35950849, 54.8410528, 57.33023629,
783-
59.8206975, 62.30659851, 64.78248863, 67.24319292,
784-
69.68371835, 72.09917489, 74.4847094, 76.83544826,
785-
79.14644817, 81.41265486, 83.62886755, 85.78970975,
786-
87.88960763, 89.92277472, 91.88320492, 93.76467562,
787-
95.5607608, 97.26485655, 98.8702211, 100.37002926,
788-
101.7574431, 103.02569948, 104.16821258, 105.17869036,
789-
106.05126175, 106.78060921, 107.36210092, 107.79191584,
790-
108.06715341, 108.18592116, 108.14739387, 107.9518398,
791-
107.61767954, 107.11306117, 106.45849746, 105.65822415,
792-
104.71722821, 103.64111837, 102.43599187, 101.10830346,
793-
99.66474308, 98.11212592, 96.45729665, 94.7070504,
794-
92.86807018, 90.9468792, 88.94980851, 86.88297787,
795-
84.75228725, 82.56341928, 80.32185056, 78.03286967,
796-
75.70160305, 73.33304741, 70.93210757, 68.50364202,
797-
66.05251605, 63.58366268, 61.10215525, 58.61329329,
798-
56.12270393, 53.63646542, 51.16125711, 48.70454205,
799-
46.27479211, 43.88176429, 41.53683649, 39.25341219,
800-
37.04739373, 34.93770849, 32.94684449, 31.10129575,
801-
29.43173548, 27.97263389, 26.7609499, 25.83355775,
802-
25.22335515, 24.95459651, 25.03869819, 25.47203708,
803-
26.23666504, 27.30363156, 28.63759291, 30.20122571,
804-
31.95851686, 33.87670365, 35.92709343, 38.08514007,
805-
40.33011209, 42.64458753, 45.01391492, 47.42570839,
806-
49.86940645, 52.33590108, 54.81723096, 57.30633064,
807-
59.79682723, 62.2828738, 64.75901269, 67.22006367,
808-
69.66102988, 72.0770186, 74.46317504, 76.81462477,
809-
79.12642433, 81.39351992, 83.61071161, 85.77262406,
810-
87.87368475, 89.90810859, 91.86989066, 93.75280924,
811-
95.55043872, 97.25617485, 98.86327462, 100.36491047,
812-
101.75424074, 103.024497, 104.16908649, 105.1817084,
813-
106.05648115, 106.78807492, 107.3718441, 107.80395262,
814-
108.08148404, 108.20252959, 108.1662479, 107.97289173,
815-
107.64770976]
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]
816816
)
817817
)
818818

819819
np.testing.assert_array_almost_equal(
820820
to_test.azimuth.values,
821821
np.array(
822-
[9.95354884, 13.51797616, 17.04447274, 20.52491225,
823-
23.95238719, 27.32131453, 30.62748235, 33.86804529,
824-
37.04147561, 40.14748222, 43.18691015, 46.1616283,
825-
49.07441566, 51.92885441, 54.72923301, 57.48046422,
826-
60.18802201, 62.85789652, 65.49656941, 68.11101133,
827-
70.70870005, 73.29766135, 75.88653487, 78.48466488,
828-
81.10221958, 83.75034334, 86.44134434, 89.18892358,
829-
92.00845226, 94.91730112, 97.93522718, 101.08482003,
830-
104.39199808, 107.88653257, 111.60254901, 115.57890087,
831-
119.8592317, 124.49141579, 129.52588659, 135.01216661,
832-
140.99278628, 147.49394635, 154.51314061, 162.00583842,
833-
169.87594269, 177.97643578, 186.12466927, 194.13016535,
834-
201.82539566, 209.08806319, 215.84877252, 222.08574604,
835-
227.81263221, 233.06524098, 237.89058053, 242.3392101,
836-
246.46057573, 250.30054442, 253.90036735, 257.29648187,
837-
260.52076061, 263.60096725, 266.56127763, 269.42279422,
838-
272.20401931, 274.92127106, 277.58904086, 280.22029634,
839-
282.82673312, 285.41898225, 288.00677995, 290.59910223,
840-
293.20426943, 295.83002468, 298.48358643, 301.17167776,
841-
303.9005345, 306.67589094, 309.50294481, 312.38630353,
842-
315.32991074, 318.33695647, 321.40977511, 324.54973374,
843-
327.75711805, 331.0310254, 334.36927221, 337.76832766,
844-
341.22328647, 344.72788848, 348.27459447, 351.85472406,
845-
355.45865247, 359.07606104, 2.69623101, 6.30836085,
846-
9.90413618, 13.46983714, 16.99770579, 20.47959425,
847-
23.90857277, 27.27903624, 30.58675167, 33.82885422,
848-
37.00379863, 40.1112785, 43.15212578, 46.12819847,
849-
49.04226661, 51.89790514, 54.69939668, 57.45164925,
850-
60.1601328, 62.83083386, 65.47023059, 68.08528982,
851-
70.68348498, 73.27283649, 75.86197737, 78.46024348,
852-
81.07779237, 83.72575502, 86.41642271, 89.16347527,
853-
91.9822575, 94.8901073, 97.90674111, 101.05469855,
854-
104.35983703, 107.85185424, 111.56478847, 115.53739258,
855-
119.81319937, 124.43997055, 129.46804248, 134.94688671,
856-
140.91907623, 147.41101064, 154.42060141, 161.90398226,
857-
169.76591074, 177.86024433, 186.00498616, 194.00988407,
858-
201.70716439, 208.97394261, 215.74010434, 221.98320309,
859-
227.71636618, 232.97505067, 237.80605645, 242.2598406,
860-
246.38581686, 250.22986166, 253.83325807, 257.23248545,
861-
260.45946126, 263.54199277, 266.50429617, 269.36751012,
862-
272.15016893, 274.86861866, 277.53737498, 280.1694266,
863-
282.77648747, 285.3692047, 287.95732871, 290.54984827,
864-
293.15509539, 295.78082413, 298.43426346, 301.12214686,
865-
303.85072078, 306.62573055, 309.45238569, 312.3353063,
866-
315.27844985, 318.28502138, 321.35737159, 324.49688503,
867-
327.70386592, 330.97743082, 334.31541557, 337.71430836,
868-
341.1692218, 344.67391164, 348.22085175, 351.80137124,
869-
355.40585053, 359.02397146, 2.64501087, 6.2581581,
870-
9.85597076]
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]
871871
)
872872
)
873873

pvlib/test/test_tools.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import datetime
2+
13
import numpy as np
24
import pandas as pd
35
import pytest
46

57
import pvlib.tools
68
from pvlib import tools
7-
from pvlib.test.test_solarposition import times
9+
10+
times = pd.date_range(start=datetime.datetime(2014, 6, 24),
11+
end=datetime.datetime(2014, 6, 26), freq='15Min')
812

913

1014
@pytest.mark.parametrize('keys, input_dict, expected', [

0 commit comments

Comments
 (0)