|
| 1 | +import pytest |
| 2 | +from numpy.testing import assert_allclose, assert_approx_equal, assert_equal |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +from pvlib import spectrum |
| 6 | +from pvlib._deprecation import pvlibDeprecationWarning |
| 7 | + |
| 8 | +from ..conftest import assert_series_equal, fail_on_pvlib_version |
| 9 | + |
| 10 | + |
| 11 | +@fail_on_pvlib_version('0.12') |
| 12 | +def test_get_am15g(): |
| 13 | + # test that the reference spectrum is read and interpolated correctly |
| 14 | + with pytest.warns(pvlibDeprecationWarning, |
| 15 | + match="get_reference_spectra instead"): |
| 16 | + e = spectrum.get_am15g() |
| 17 | + assert_equal(len(e), 2002) |
| 18 | + assert_equal(np.sum(e.index), 2761442) |
| 19 | + assert_approx_equal(np.sum(e), 1002.88, significant=6) |
| 20 | + |
| 21 | + wavelength = [270, 850, 950, 1200, 1201.25, 4001] |
| 22 | + expected = [0.0, 0.893720, 0.147260, 0.448250, 0.4371025, 0.0] |
| 23 | + |
| 24 | + with pytest.warns(pvlibDeprecationWarning, |
| 25 | + match="get_reference_spectra instead"): |
| 26 | + e = spectrum.get_am15g(wavelength) |
| 27 | + assert_equal(len(e), len(wavelength)) |
| 28 | + assert_allclose(e, expected, rtol=1e-6) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize( |
| 32 | + "reference_identifier,expected_sums", |
| 33 | + [ |
| 34 | + ( |
| 35 | + "ASTM G173-03", # reference_identifier |
| 36 | + { # expected_sums |
| 37 | + "extraterrestrial": 1356.15, |
| 38 | + "global": 1002.88, |
| 39 | + "direct": 887.65, |
| 40 | + }, |
| 41 | + ), |
| 42 | + ], |
| 43 | +) |
| 44 | +def test_get_reference_spectra(reference_identifier, expected_sums): |
| 45 | + # test reading of a standard spectrum |
| 46 | + standard = spectrum.get_reference_spectra(standard=reference_identifier) |
| 47 | + assert set(standard.columns) == expected_sums.keys() |
| 48 | + assert standard.index.name == "wavelength" |
| 49 | + assert standard.index.is_monotonic_increasing is True |
| 50 | + expected_sums = pd.Series(expected_sums) # convert prior to comparison |
| 51 | + assert_series_equal(np.sum(standard, axis=0), expected_sums, atol=1e-2) |
| 52 | + |
| 53 | + |
| 54 | +def test_get_reference_spectra_custom_wavelengths(): |
| 55 | + # test that the spectrum is interpolated correctly when custom wavelengths |
| 56 | + # are specified |
| 57 | + # only checked for ASTM G173-03 reference spectrum |
| 58 | + wavelength = [270, 850, 951.634, 1200, 4001] |
| 59 | + expected_sums = pd.Series( |
| 60 | + {"extraterrestrial": 2.23266, "global": 1.68952, "direct": 1.58480} |
| 61 | + ) # for given ``wavelength`` |
| 62 | + standard = spectrum.get_reference_spectra( |
| 63 | + wavelength, standard="ASTM G173-03" |
| 64 | + ) |
| 65 | + assert_equal(len(standard), len(wavelength)) |
| 66 | + # check no NaN values were returned |
| 67 | + assert not standard.isna().any().any() # double any to return one value |
| 68 | + assert_series_equal(np.sum(standard, axis=0), expected_sums, atol=1e-4) |
| 69 | + |
| 70 | + |
| 71 | +def test_get_reference_spectra_invalid_reference(): |
| 72 | + # test that an invalid reference identifier raises a ValueError |
| 73 | + with pytest.raises(ValueError, match="Invalid standard identifier"): |
| 74 | + spectrum.get_reference_spectra(standard="invalid") |
0 commit comments