Skip to content

Commit f118175

Browse files
authored
Fix year limits in SG2 algorithm (#118)
1 parent 4bf7275 commit f118175

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/solposx/solarposition/sg2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def sg2(times, latitude, longitude, elevation=0, *, pressure=101325,
8484
]),
8585
columns=['y', 'a_0', 'a_1', 'a_2', 'a_3', 'a_4', 'a_5'])
8686

87-
if (year_dec < 1980) | (year_dec > 2030):
87+
if (year_dec.min() < 1980) | (year_dec.max() > 2030):
8888
raise ValueError("The algorithm is valid only between 1980 and 2030")
8989

9090
row = np.where(

tests/test_solarposition.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,16 +429,18 @@ def _generate_solarposition_dataframe(inputs, algorithm, **kwargs):
429429
if row['elevation'] is not None:
430430
if algorithm.__name__ in ['sg2', 'sg2_c', 'spa']:
431431
elevation_dict['elevation'] = row['elevation']
432+
# calculate for two identical indexes to make sure
433+
# indexes with more than one element works
432434
dfi = algorithm(
433-
pd.DatetimeIndex([index]),
435+
pd.DatetimeIndex([index, index]),
434436
row['latitude'],
435437
row['longitude'],
436438
**{**kwargs, **elevation_dict},
437439
)
438440
except ValueError:
439441
# Add empty row (nans)
440442
dfi = pd.DataFrame(index=[index])
441-
dfs.append(dfi)
443+
dfs.append(dfi.iloc[0:1])
442444
df = pd.concat(dfs, axis='rows')
443445
return df
444446

@@ -651,3 +653,18 @@ def test_site_elevation_sg2():
651653
none_elevation = sg2(times, latitude, longitude)
652654
assert zero_elevation.iloc[0]['elevation'] != high_elevation.iloc[0]['elevation']
653655
assert zero_elevation.iloc[0]['elevation'] == none_elevation.iloc[0]['elevation']
656+
657+
658+
def test_sg2_year_out_of_range():
659+
with pytest.raises(ValueError,
660+
match='valid only between 1980 and 2030'):
661+
_ = sg2(
662+
times=pd.date_range('1960-01-01', '1960-01-02', tz='UTC'),
663+
latitude=50, longitude=10,
664+
)
665+
with pytest.raises(ValueError,
666+
match='valid only between 1980 and 2030'):
667+
_ = sg2(
668+
times=pd.date_range('2035-01-01', '2035-01-02', tz='UTC'),
669+
latitude=50, longitude=10,
670+
)

0 commit comments

Comments
 (0)