forked from pvlib/pvlib-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_solaranywhere.py
More file actions
292 lines (254 loc) · 12.2 KB
/
test_solaranywhere.py
File metadata and controls
292 lines (254 loc) · 12.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import pandas as pd
import pytest
import pvlib
import os
from tests.conftest import (
TESTS_DATA_DIR,
RERUNS,
RERUNS_DELAY,
requires_solaranywhere_credentials,
)
# High spatial resolution and 5-min data, true dynamics enabled
TESTFILE_HIGH_RESOLUTION = TESTS_DATA_DIR / 'Burlington, United States SolarAnywhere Time Series 20210101 to 20210103 Lat_44_4675 Lon_-73_2075 SA format.csv' # noqa: E501
# TGY test file (v3.6) containing GHI/DHI and temperature.
# Note, the test file only contains the first three days.
TESTFILE_TMY = TESTS_DATA_DIR / 'Burlington, United States SolarAnywhere Typical GHI Year Lat_44_465 Lon_-73_205 SA format.csv' # noqa: E501
@pytest.fixture(scope="module")
def solaranywhere_api_key():
"""Supplies the pvlib's SolarAnywhere API key for testing purposes.
Users can freely register for an API key."""
solaranywhere_api_key = os.environ["SOLARANYWHERE_API_KEY"]
return solaranywhere_api_key
@pytest.fixture
def high_resolution_index():
index = pd.date_range(start='2021-01-01 00:05-0500',
end='2021-01-03 00:00-0500', freq='5min')
index.name = 'ObservationTime'
return index
@pytest.fixture
def tmy_index():
index = pd.date_range(
start='2000-01-01 01:00-0500', periods=3*24, freq='1h')
index.name = 'ObservationTime'
index.freq = None
return index
@pytest.fixture
def tmy_ghi_series(tmy_index):
ghi = [
0, 0, 0, 0, 0, 0, 0, 3, 50, 171, 234, 220, 202, 122, 141, 65, 2, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 48, 105, 161, 135, 108, 72, 58,
33, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 47, 124, 99, 116,
130, 165, 110, 36, 1, 0, 0, 0, 0, 0, 0, 0
]
return pd.Series(data=ghi, index=tmy_index, name='ghi')
def test_read_solaranywhere_high_resolution(high_resolution_index):
data, meta = pvlib.iotools.read_solaranywhere(TESTFILE_HIGH_RESOLUTION,
map_variables=False)
# Check that metadata is parsed correctly
assert meta['latitude'] == 44.4675
assert meta['longitude'] == -73.2075
assert meta['altitude'] == 41.0
assert meta['name'] == 'Burlington United States'
assert meta['TZ'] == -5.0
assert meta['Data Version'] == '3.6'
assert meta['LatLon Resolution'] == 0.005
# Check that columns are parsed correctly
assert 'Albedo' in data.columns
assert 'Global Horizontal Irradiance (GHI) W/m2' in data.columns
assert 'Direct Normal Irradiance (DNI) W/m2' in data.columns
assert 'WindSpeed (m/s)' in data.columns
assert 'WindSpeedObservationType' in data.columns
assert 'Particulate Matter 10 (µg/m3)' in data.columns
# Check that data is parsed correctly
assert data.loc['2021-01-01 07:00:00-05:00', 'Albedo'] == 0.6
assert data.loc['2021-01-01 07:00:00-05:00', 'WindSpeed (m/s)'] == 0
# Assert that the index is parsed correctly
pd.testing.assert_index_equal(data.index, high_resolution_index)
def test_read_solaranywhere_map_variables():
# Check that variables are mapped by default to pvlib names
data, meta = pvlib.iotools.read_solaranywhere(TESTFILE_HIGH_RESOLUTION)
mapped_column_names = ['ghi', 'dni', 'dhi', 'temp_air', 'wind_speed',
'relative_humidity', 'ghi_clear', 'dni_clear',
'dhi_clear', 'albedo']
for c in mapped_column_names:
assert c in data.columns
assert meta['latitude'] == 44.4675
assert meta['longitude'] == -73.2075
assert meta['altitude'] == 41.0
def test_read_solaranywhere_tmy(tmy_index, tmy_ghi_series):
# Check that TMY files are correctly parsed
data, meta = pvlib.iotools.read_solaranywhere(TESTFILE_TMY)
# Check that columns names are correct and mapped to pvlib names
assert 'ghi' in data.columns
assert 'dni' in data.columns
assert 'dhi' in data.columns
assert 'temp_air' in data.columns
# Check that metadata is parsed correctly
assert meta['latitude'] == 44.465
assert meta['longitude'] == -73.205
assert meta['altitude'] == 41.0
assert meta['name'] == 'Burlington United States'
assert meta['TZ'] == -5.0
assert meta['Data Version'] == '3.6'
assert meta['LatLon Resolution'] == 0.010
assert meta['Time Resolution'] == '60 minutes'
# Assert that the index is parsed correctly
pd.testing.assert_index_equal(data.index, tmy_index)
# Test one column
pd.testing.assert_series_equal(data['ghi'], tmy_ghi_series)
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_bad_probability_of_exceedance():
# Test if ValueError is raised if probability_of_exceedance is not integer
with pytest.raises(ValueError, match="must be an integer"):
pvlib.iotools.get_solaranywhere(
latitude=44, longitude=-73, api_key='empty',
source='SolarAnywherePOELatest', probability_of_exceedance=0.5)
@pytest.mark.remote_data
@requires_solaranywhere_credentials
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_missing_start_end(solaranywhere_api_key):
# Test if ValueError is raised if start/end is missing for non-TMY request
with pytest.raises(ValueError, match="simulation start and end time"):
pvlib.iotools.get_solaranywhere(
latitude=44, longitude=-73, api_key=solaranywhere_api_key,
source='SolarAnywhereLatest')
@pytest.fixture
def time_series_index():
index = pd.date_range(start='2019-12-31 19:02:30-05:00', periods=288,
freq='5min')
index.name = 'ObservationTime'
index.freq = None
return index
@pytest.fixture
def timeseries_temp_air(time_series_index):
temp_air = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
]
return pd.Series(data=temp_air, index=time_series_index, name='temp_air')
@requires_solaranywhere_credentials
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_no_timezone(
solaranywhere_api_key, time_series_index, timeseries_temp_air):
# Test if data can be retrieved. This test only retrieves one day of data
# to minimize the request time.
data, meta = pvlib.iotools.get_solaranywhere(
latitude=44.4675, longitude=-73.2075, api_key=solaranywhere_api_key,
# specify start/end without timezone information
start=pd.Timestamp(2020, 1, 1), end=pd.Timestamp(2020, 1, 2),
# test specific version of SolarAnywhere
source='SolarAnywhere3_6',
spatial_resolution=0.005, time_resolution=5, true_dynamics=True)
# Check metadata, including that true-dynamics is set
assert meta['WeatherSiteName'] == 'SolarAnywhere3_6'
assert meta['ApplyTrueDynamics'] is True
assert meta['time_resolution'] == 5
assert meta['spatial_resolution'] == 0.005
assert meta['latitude'] == 44.4675
assert meta['longitude'] == -73.2075
assert meta['altitude'] == 41.0
# Check that variables have been mapped (default convention)
assert 'StartTime' in data.columns
assert 'ObservationTime' in data.columns
assert 'EndTime' in data.columns
assert 'ghi' in data.columns
assert 'dni' in data.columns
assert 'dhi' in data.columns
assert 'temp_air' in data.columns
assert 'wind_speed' in data.columns
assert 'albedo' in data.columns
assert 'DataVersion' in data.columns
# Assert index (checks that time resolution is 5 min)
pd.testing.assert_index_equal(data.index, time_series_index)
# Test one column
pd.testing.assert_series_equal(data['temp_air'], timeseries_temp_air)
@requires_solaranywhere_credentials
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_other_options(
solaranywhere_api_key, time_series_index, timeseries_temp_air):
# Test if data can be retrieved. This test only retrieves one day of data
# to minimize the request time.
data, meta = pvlib.iotools.get_solaranywhere(
latitude=44.4675, longitude=-73.2075, api_key=solaranywhere_api_key,
# specify start/end as str with timezone information
start='2020-01-01 00:00:00+0000',
end='2020-01-02 00:00:00+0000',
# test specific version of SolarAnywhere
source='SolarAnywhere3_7',
# test fewer variables
variables=[
'ObservationTime',
'GlobalHorizontalIrradiance_WattsPerMeterSquared',
],
map_variables=False)
# Check metadata
assert meta['WeatherSiteName'] == 'SolarAnywhere3_7'
assert meta['ApplyTrueDynamics'] is False # default setting
assert meta['time_resolution'] == 60 # default resolution
assert meta['spatial_resolution'] == 0.01 # default resolution
assert meta['latitude'] == 44.4675
assert meta['longitude'] == -73.2075
assert meta['altitude'] == 41.0
# Check that variables have been mapped (default convention)
assert 'StartTime' not in data.columns
assert 'ObservationTime' in data.columns
assert 'EndTime' not in data.columns
# Check that ghi is not mapped
assert 'ghi' not in data.columns
assert 'GlobalHorizontalIrradiance_WattsPerMeterSquared' in data.columns
assert 'dni' not in data.columns
assert 'dhi' not in data.columns
assert 'temp_air' not in data.columns
assert 'wind_speed' not in data.columns
assert 'albedo' not in data.columns
assert 'DataVersion' not in data.columns
@requires_solaranywhere_credentials
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_probability_exceedance_error(solaranywhere_api_key):
# Test if ValueError is raised when passing start/end to typical year
with pytest.raises(ValueError, match="start and end time must be null"):
data, meta = pvlib.iotools.get_solaranywhere(
latitude=44.4675, longitude=-73.2075,
api_key=solaranywhere_api_key,
# Probabiliy of exceedance year should not have start/end specified
start=pd.Timestamp('2020-01-01 00:00:00+0000'),
end=pd.Timestamp('2020-01-05 12:00:00+0000'),
source='SolarAnywherePOELatest',
probability_of_exceedance=20)
@requires_solaranywhere_credentials
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_timeout_tgy(solaranywhere_api_key):
# Test if the service times out when the timeout parameter is close to zero
with pytest.raises(TimeoutError, match="Time exceeded"):
pvlib.iotools.get_solaranywhere(
latitude=44.4675, longitude=-73.2075,
api_key=solaranywhere_api_key,
source='SolarAnywhereTGYLatest',
timeout=0.00001)
@requires_solaranywhere_credentials
@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solaranywhere_not_available(solaranywhere_api_key):
# Test if RuntimeError is raised if location in the ocean is requested
with pytest.raises(RuntimeError,
match="Location is outside of our coverage area"):
pvlib.iotools.get_solaranywhere(
latitude=40, longitude=-70,
api_key=solaranywhere_api_key,
start=pd.Timestamp('2020-01-01 00:00:00+0000'),
end=pd.Timestamp('2020-01-05 12:00:00+0000'))