|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | +import pvlib |
| 4 | +from requests.exceptions import HTTPError |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def data_index(): |
| 9 | + index = pd.date_range(start='2025-02-02 00:00+00:00', |
| 10 | + end='2025-02-02 23:00+00:00', freq='1h') |
| 11 | + return index |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def ghi_series(data_index): |
| 16 | + ghi = [ |
| 17 | + 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 50.25, 184.2, 281.55, 368.3, 406.48, |
| 18 | + 386.45, 316.05, 210.1, 109.05, 12.9, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 |
| 19 | + ] |
| 20 | + return pd.Series(data=ghi, index=data_index, name='ghi') |
| 21 | + |
| 22 | + |
| 23 | +def test_get_nasa_power(data_index, ghi_series): |
| 24 | + data, meta = pvlib.iotools.get_nasa_power(latitude=44.76, |
| 25 | + longitude=7.64, |
| 26 | + start=data_index[0], |
| 27 | + end=data_index[-1], |
| 28 | + parameters=['ALLSKY_SFC_SW_DWN'], |
| 29 | + map_variables=False) |
| 30 | + # Check that metadata is correct |
| 31 | + assert meta['latitude'] == 44.76 |
| 32 | + assert meta['longitude'] == 7.64 |
| 33 | + assert meta['altitude'] == 705.88 |
| 34 | + assert meta['header']['start'] == '20250202' |
| 35 | + assert meta['header']['end'] == '20250202' |
| 36 | + assert meta['header']['time_standard'] == 'UTC' |
| 37 | + assert meta['header']['title'] == 'NASA/POWER Source Native Resolution Hourly Data' # noqa: E501 |
| 38 | + assert meta['header']['sources'][0] == 'SYN1DEG' |
| 39 | + # Check that columns are parsed correctly |
| 40 | + assert 'ALLSKY_SFC_SW_DWN' in data.columns |
| 41 | + # Assert that the index is parsed correctly |
| 42 | + pd.testing.assert_index_equal(data.index, data_index) |
| 43 | + # Test one column |
| 44 | + pd.testing.assert_series_equal(data['ALLSKY_SFC_SW_DWN'], ghi_series, |
| 45 | + check_freq=False, check_names=False) |
| 46 | + |
| 47 | + |
| 48 | +def test_get_nasa_power_map_variables(data_index): |
| 49 | + # Check that variables are mapped by default to pvlib names |
| 50 | + data, meta = pvlib.iotools.get_nasa_power(latitude=44.76, |
| 51 | + longitude=7.64, |
| 52 | + start=data_index[0], |
| 53 | + end=data_index[-1], |
| 54 | + parameters=['ALLSKY_SFC_SW_DWN', |
| 55 | + 'ALLSKY_SFC_SW_DIFF', |
| 56 | + 'ALLSKY_SFC_SW_DNI', |
| 57 | + 'CLRSKY_SFC_SW_DWN', |
| 58 | + 'T2M', 'WS2M', |
| 59 | + 'WS10M' |
| 60 | + ]) |
| 61 | + mapped_column_names = ['ghi', 'dni', 'dhi', 'temp_air_2m', 'wind_speed_2m', |
| 62 | + 'wind_speed_10m', 'ghi_clear'] |
| 63 | + for c in mapped_column_names: |
| 64 | + assert c in data.columns |
| 65 | + assert meta['latitude'] == 44.76 |
| 66 | + assert meta['longitude'] == 7.64 |
| 67 | + assert meta['altitude'] == 705.88 |
| 68 | + |
| 69 | + |
| 70 | +def test_get_nasa_power_wrong_parameter_name(data_index): |
| 71 | + # Test if HTTPError is raised if a wrong parameter name is asked |
| 72 | + with pytest.raises(HTTPError, match=r"ALLSKY_SFC_SW_DLN"): |
| 73 | + pvlib.iotools.get_nasa_power(latitude=44.76, |
| 74 | + longitude=7.64, |
| 75 | + start=data_index[0], |
| 76 | + end=data_index[-1], |
| 77 | + parameters=['ALLSKY_SFC_SW_DLN']) |
| 78 | + |
| 79 | + |
| 80 | +def test_get_nasa_power_duplicate_parameter_name(data_index): |
| 81 | + # Test if HTTPError is raised if a duplicate parameter is asked |
| 82 | + with pytest.raises(HTTPError, match=r"ALLSKY_SFC_SW_DWN"): |
| 83 | + pvlib.iotools.get_nasa_power(latitude=44.76, |
| 84 | + longitude=7.64, |
| 85 | + start=data_index[0], |
| 86 | + end=data_index[-1], |
| 87 | + parameters=2*['ALLSKY_SFC_SW_DWN']) |
0 commit comments