forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_acis.py
More file actions
213 lines (194 loc) · 8.25 KB
/
test_acis.py
File metadata and controls
213 lines (194 loc) · 8.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
tests for :mod:`pvlib.iotools.acis`
"""
import pandas as pd
import numpy as np
import pytest
from pvlib.iotools import (
get_acis_prism, get_acis_nrcc, get_acis_mpe,
get_acis_station_data, get_acis_available_stations
)
from tests.conftest import RERUNS, RERUNS_DELAY, assert_frame_equal
from requests import HTTPError
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_acis_prism():
# map_variables=True
df, meta = get_acis_prism(40.001, -80.001, '2020-01-01', '2020-01-02')
expected = pd.DataFrame(
[
[0.5, 5, 0, 2.5, 0, 62, 0],
[0, 5, -3, 1, 0, 64, 0]
],
columns=['precipitation', 'temp_air_max', 'temp_air_min',
'temp_air_average', 'cooling_degree_days',
'heating_degree_days', 'growing_degree_days'],
index=pd.to_datetime(['2020-01-01', '2020-01-02']),
)
assert_frame_equal(df, expected)
expected_meta = {'latitude': 40, 'longitude': -80, 'altitude': 298.0944}
assert meta == expected_meta
# map_variables=False
df, meta = get_acis_prism(40.001, -80.001, '2020-01-01', '2020-01-02',
map_variables=False)
expected.columns = ['pcpn', 'maxt', 'mint', 'avgt', 'cdd', 'hdd', 'gdd']
assert_frame_equal(df, expected)
expected_meta = {'lat': 40, 'lon': -80, 'elev': 298.0944}
assert meta == expected_meta
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
@pytest.mark.parametrize('grid, expected', [
(1, [[0.51, 5, 0, 2.5, 0, 62, 0]]),
(3, [[0.51, 5, -1, 2.0, 0, 63, 0]])
])
def test_get_acis_nrcc(grid, expected):
# map_variables=True
df, meta = get_acis_nrcc(40.001, -80.001, '2020-01-01', '2020-01-01', grid)
expected = pd.DataFrame(
expected,
columns=['precipitation', 'temp_air_max', 'temp_air_min',
'temp_air_average', 'cooling_degree_days',
'heating_degree_days', 'growing_degree_days'],
index=pd.to_datetime(['2020-01-01']),
)
assert_frame_equal(df, expected)
expected_meta = {'latitude': 40., 'longitude': -80., 'altitude': 356.9208}
assert meta == pytest.approx(expected_meta)
# map_variables=False
df, meta = get_acis_nrcc(40.001, -80.001, '2020-01-01', '2020-01-01', grid,
map_variables=False)
expected.columns = ['pcpn', 'maxt', 'mint', 'avgt', 'cdd', 'hdd', 'gdd']
assert_frame_equal(df, expected)
expected_meta = {'lat': 40., 'lon': -80., 'elev': 356.9208}
assert meta == pytest.approx(expected_meta)
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_acis_nrcc_error():
with pytest.raises(HTTPError, match='invalid grid'):
# 50 is not a valid dataset (or "grid", in ACIS lingo)
_ = get_acis_nrcc(40, -80, '2012-01-01', '2012-01-01', 50)
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_acis_mpe():
# map_variables=True
df, meta = get_acis_mpe(40.001, -80.001, '2020-01-01', '2020-01-02')
expected = pd.DataFrame(
{'precipitation': [0.4, 0.0]},
index=pd.to_datetime(['2020-01-01', '2020-01-02']),
)
assert_frame_equal(df, expected)
expected_meta = {'latitude': 40.0083, 'longitude': -79.9653}
assert meta == expected_meta
# map_variables=False
df, meta = get_acis_mpe(40.001, -80.001, '2020-01-01', '2020-01-02',
map_variables=False)
expected.columns = ['pcpn']
assert_frame_equal(df, expected)
expected_meta = {'lat': 40.0083, 'lon': -79.9653}
assert meta == expected_meta
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_acis_station_data():
# map_variables=True
df, meta = get_acis_station_data('ORD', '2020-01-10', '2020-01-12',
trace_val=-99)
expected = pd.DataFrame(
[[10., 2., 6., np.nan, 21.34, 0., 0., 0., 59., 0.],
[3., -4., -0.5, np.nan, 9.4, 5.3, 0., 0., 65., 0.],
[-1., -5., -3., np.nan, -99, -99, 5., 0., 68., 0.]],
columns=['temp_air_max', 'temp_air_min', 'temp_air_average',
'temp_air_observation', 'precipitation', 'snowfall',
'snowdepth', 'cooling_degree_days',
'heating_degree_days', 'growing_degree_days'],
index=pd.to_datetime(['2020-01-10', '2020-01-11', '2020-01-12']),
)
assert_frame_equal(df, expected)
expected_meta = {
'uid': 48,
'state': 'IL',
'name': 'CHICAGO OHARE INTL AP',
'altitude': 204.8256,
'latitude': 41.96017,
'longitude': -87.93164
}
expected_meta = {
'valid_daterange': [
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15'],
[],
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15'],
['1958-11-01', '2023-06-15']
],
'name': 'CHICAGO OHARE INTL AP',
'sids': ['94846 1', '111549 2', 'ORD 3', '72530 4', 'KORD 5',
'USW00094846 6', 'ORD 7', 'USW00094846 32'],
'county': '17031',
'state': 'IL',
'climdiv': 'IL02',
'uid': 48,
'tzo': -6.0,
'sid_dates': [
['94846 1', '1989-01-19', '9999-12-31'],
['94846 1', '1958-10-30', '1989-01-01'],
['111549 2', '1989-01-19', '9999-12-31'],
['111549 2', '1958-10-30', '1989-01-01'],
['ORD 3', '1989-01-19', '9999-12-31'],
['ORD 3', '1958-10-30', '1989-01-01'],
['72530 4', '1989-01-19', '9999-12-31'],
['72530 4', '1958-10-30', '1989-01-01'],
['KORD 5', '1989-01-19', '9999-12-31'],
['KORD 5', '1958-10-30', '1989-01-01'],
['USW00094846 6', '1989-01-19', '9999-12-31'],
['USW00094846 6', '1958-10-30', '1989-01-01'],
['ORD 7', '1989-01-19', '9999-12-31'],
['ORD 7', '1958-10-30', '1989-01-01'],
['USW00094846 32', '1989-01-19', '9999-12-31'],
['USW00094846 32', '1958-10-30', '1989-01-01']],
'altitude': 204.8256,
'longitude': -87.93164,
'latitude': 41.96017
}
# don't check valid dates since they get extended every day
meta.pop("valid_daterange")
expected_meta.pop("valid_daterange")
assert meta == expected_meta
# map_variables=False
df, meta = get_acis_station_data('ORD', '2020-01-10', '2020-01-12',
trace_val=-99, map_variables=False)
expected.columns = ['maxt', 'mint', 'avgt', 'obst', 'pcpn', 'snow',
'snwd', 'cdd', 'hdd', 'gdd']
assert_frame_equal(df, expected)
expected_meta['lat'] = expected_meta.pop('latitude')
expected_meta['lon'] = expected_meta.pop('longitude')
expected_meta['elev'] = expected_meta.pop('altitude')
meta.pop("valid_daterange")
assert meta == expected_meta
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_acis_available_stations():
# use a very narrow bounding box to hopefully make this test less likely
# to fail due to new stations being added in the future
lat, lon = 39.8986, -80.1656
stations = get_acis_available_stations([lat - 0.0001, lat + 0.0001],
[lon - 0.0001, lon + 0.0001])
assert len(stations) == 1
station = stations.iloc[0]
# test the more relevant values
assert station['name'] == 'WAYNESBURG 1 E'
assert station['sids'] == ['369367 2', 'USC00369367 6', 'WYNP1 7']
assert station['state'] == 'PA'
assert station['altitude'] == 940.
assert station['tzo'] == -5.0
assert station['latitude'] == lat
assert station['longitude'] == lon
# check that start/end work as filters
stations = get_acis_available_stations([lat - 0.0001, lat + 0.0001],
[lon - 0.0001, lon + 0.0001],
start='1900-01-01',
end='1900-01-02')
assert stations.empty