|
1 | 1 | import pandas as pd
|
| 2 | +import numpy as np |
2 | 3 | import pytest
|
3 | 4 | import pvlib
|
4 | 5 | from tests.conftest import RERUNS, RERUNS_DELAY
|
| 6 | +from requests.exceptions import HTTPError |
5 | 7 |
|
6 | 8 |
|
7 | 9 | @pytest.fixture
|
@@ -29,11 +31,13 @@ def expected_meta():
|
29 | 31 | {'aggregation_method': 'average',
|
30 | 32 | 'description': 'Global horizontal irradiance',
|
31 | 33 | 'name': 'global_horizontal_irradiance',
|
32 |
| - 'unit': {'description': 'Watt per square meter', 'name': 'W/m**2'}}, |
| 34 | + 'unit': { |
| 35 | + 'description': 'Watt per square meter', 'name': 'W/m**2'}}, |
33 | 36 | {'aggregation_method': 'average',
|
34 |
| - 'description': 'Global horizontal irradiance with shading taken into account', |
| 37 | + 'description': 'Global horizontal irradiance with shading taken into account', # noqa: E501 |
35 | 38 | 'name': 'global_horizontal_irradiance_with_shading',
|
36 |
| - 'unit': {'description': 'Watt per square meter', 'name': 'W/m**2'}}, |
| 39 | + 'unit': {'description': 'Watt per square meter', |
| 40 | + 'name': 'W/m**2'}}, |
37 | 41 | ],
|
38 | 42 | 'surface_azimuth': 180,
|
39 | 43 | 'surface_tilt': 0,
|
@@ -186,3 +190,115 @@ def test_get_meteonorm_forecast_precision(demo_api_key, demo_url):
|
186 | 190 | assert data.index[1] - data.index[0] == pd.Timedelta(minutes=15)
|
187 | 191 | assert data.shape == (5, 1)
|
188 | 192 | assert meta['frequency'] == '15_minutes'
|
| 193 | + |
| 194 | + |
| 195 | +@pytest.mark.remote_data |
| 196 | +@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) |
| 197 | +def test_get_meteonorm_custom_horizon(demo_api_key, demo_url): |
| 198 | + data, meta = pvlib.iotools.get_meteonorm( |
| 199 | + latitude=50, longitude=10, |
| 200 | + start=pd.Timestamp.now(tz='UTC'), |
| 201 | + end=pd.Timestamp.now(tz='UTC') + pd.Timedelta(hours=5), |
| 202 | + api_key=demo_api_key, |
| 203 | + parameters='ghi', |
| 204 | + endpoint='forecast/basic', |
| 205 | + horizon=list(np.ones(360).astype(int)*80), |
| 206 | + url=demo_url) |
| 207 | + |
| 208 | + |
| 209 | +@pytest.mark.remote_data |
| 210 | +@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) |
| 211 | +def test_get_meteonorm_HTTPError(demo_api_key, demo_url): |
| 212 | + with pytest.raises( |
| 213 | + HTTPError, match="unknown parameter: not_a_real_parameter'"): |
| 214 | + _ = pvlib.iotools.get_meteonorm( |
| 215 | + latitude=50, longitude=10, |
| 216 | + start=pd.Timestamp.now(tz='UTC'), |
| 217 | + end=pd.Timestamp.now(tz='UTC') + pd.Timedelta(hours=5), |
| 218 | + api_key=demo_api_key, |
| 219 | + parameters='not_a_real_parameter', |
| 220 | + endpoint='forecast/basic', |
| 221 | + url=demo_url) |
| 222 | + |
| 223 | + |
| 224 | +@pytest.fixture |
| 225 | +def expected_meteonorm_tmy_meta(): |
| 226 | + meta = { |
| 227 | + 'altitude': 290, |
| 228 | + 'frequency': '1_hour', |
| 229 | + 'parameters': [{ |
| 230 | + 'aggregation_method': 'average', |
| 231 | + 'description': 'Diffuse horizontal irradiance', |
| 232 | + 'name': 'diffuse_horizontal_irradiance', |
| 233 | + 'unit': {'description': 'Watt per square meter', |
| 234 | + 'name': 'W/m**2'}, |
| 235 | + }], |
| 236 | + 'surface_azimuth': 90, |
| 237 | + 'surface_tilt': 20, |
| 238 | + 'time_zone': 1, |
| 239 | + 'latitude': 50, |
| 240 | + 'longitude': 10, |
| 241 | + } |
| 242 | + return meta |
| 243 | + |
| 244 | + |
| 245 | +@pytest.fixture |
| 246 | +def expected_meteonorm_tmy_index(): |
| 247 | + index = pd.date_range( |
| 248 | + '2005-01-01', periods=8760, freq='1h', tz=3600) |
| 249 | + index.freq = None |
| 250 | + return index |
| 251 | + |
| 252 | + |
| 253 | +@pytest.fixture |
| 254 | +def expected_metenorm_tmy_data(): |
| 255 | + # The first 12 rows of data |
| 256 | + columns = ['diffuse_horizontal_irradiance'] |
| 257 | + expected = [ |
| 258 | + [0.], |
| 259 | + [0.], |
| 260 | + [0.], |
| 261 | + [0.], |
| 262 | + [0.], |
| 263 | + [0.], |
| 264 | + [0.], |
| 265 | + [0.], |
| 266 | + [9.], |
| 267 | + [8.4], |
| 268 | + [86.6], |
| 269 | + [110.5], |
| 270 | + ] |
| 271 | + index = pd.date_range( |
| 272 | + '2005-01-01', periods=12, freq='1h', tz=3600) |
| 273 | + index.freq = None |
| 274 | + expected = pd.DataFrame(expected, index=index, columns=columns) |
| 275 | + return expected |
| 276 | + |
| 277 | + |
| 278 | +# @pytest.mark.remote_data |
| 279 | +# @pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY) |
| 280 | +def test_get_meteonorm_tmy( |
| 281 | + demo_api_key, demo_url, expected_meteonorm_tmy_meta, |
| 282 | + expected_metenorm_tmy_data, expected_meteonorm_tmy_index): |
| 283 | + data, meta = pvlib.iotools.get_meteonorm_tmy( |
| 284 | + latitude=50, longitude=10, |
| 285 | + api_key=demo_api_key, |
| 286 | + parameters='dhi', |
| 287 | + surface_tilt=20, |
| 288 | + surface_azimuth=90, |
| 289 | + time_step='1h', |
| 290 | + horizon=list(np.ones(360).astype(int)*2), |
| 291 | + terrain='open', |
| 292 | + albedo=0.5, |
| 293 | + turbidity='auto', |
| 294 | + random_seed=100, |
| 295 | + clear_sky_radiation_model='solis', |
| 296 | + data_version='v9.0', # fix version |
| 297 | + future_scenario='ssp1_26', |
| 298 | + future_year=2030, |
| 299 | + interval_index=True, |
| 300 | + map_variables=False, |
| 301 | + url=demo_url) |
| 302 | + assert meta == expected_meteonorm_tmy_meta |
| 303 | + pd.testing.assert_frame_equal(data.iloc[:12], expected_metenorm_tmy_data) |
| 304 | + pd.testing.assert_index_equal(data.index, expected_meteonorm_tmy_index) |
0 commit comments