Skip to content

Commit b2a82f1

Browse files
authored
Split test_spectrum.py (#2151)
* Split test_spectrum.py files Restructure tests in line with mismatch.py restructure (#2136) * fix conftest import * Update test_mismatch.py add spectrl2_data function to mismatch.py * Update v0.11.1.rst * create pvlib\tests\spectrum\conftest.py
1 parent 72185cb commit b2a82f1

File tree

7 files changed

+314
-292
lines changed

7 files changed

+314
-292
lines changed

docs/sphinx/source/whatsnew/v0.11.1.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Enhancements
1919
* Restructured the pvlib/spectrum folder by breaking up the contents of
2020
pvlib/spectrum/mismatch.py into pvlib/spectrum/mismatch.py,
2121
pvlib/spectrum/irradiance.py, and
22-
pvlib/spectrum/response.py. (:issue:`2125`, :pull:`2136`)
22+
pvlib/spectrum/response.py. (:issue:`2125`, :pull:`2136`, :pull:`2151`)
2323
* Added function for calculating wind speed at different heights,
2424
:py:func:`pvlib.atmosphere.windspeed_powerlaw`.
2525
(:issue:`2118`, :pull:`2124`)

pvlib/tests/spectrum/__init__.py

Whitespace-only changes.

pvlib/tests/spectrum/conftest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import numpy as np
2+
import pytest
3+
import pandas as pd
4+
5+
from ..conftest import DATA_DIR
6+
7+
SPECTRL2_TEST_DATA = DATA_DIR / 'spectrl2_example_spectra.csv'
8+
9+
10+
@pytest.fixture
11+
def spectrl2_data():
12+
# reference spectra generated with solar_utils==0.3
13+
"""
14+
expected = solar_utils.spectrl2(
15+
units=1,
16+
location=[40, -80, -5],
17+
datetime=[2020, 3, 15, 10, 45, 59],
18+
weather=[1013, 15],
19+
orientation=[0, 180],
20+
atmospheric_conditions=[1.14, 0.65, 0.344, 0.1, 1.42],
21+
albedo=[0.3, 0.7, 0.8, 1.3, 2.5, 4.0] + [0.2]*6,
22+
)
23+
"""
24+
kwargs = {
25+
'surface_tilt': 0,
26+
'relative_airmass': 1.4899535986910446,
27+
'apparent_zenith': 47.912086486816406,
28+
'aoi': 47.91208648681641,
29+
'ground_albedo': 0.2,
30+
'surface_pressure': 101300,
31+
'ozone': 0.344,
32+
'precipitable_water': 1.42,
33+
'aerosol_turbidity_500nm': 0.1,
34+
'dayofyear': 75
35+
}
36+
df = pd.read_csv(SPECTRL2_TEST_DATA, index_col=0)
37+
# convert um to nm
38+
df['wavelength'] = np.round(df['wavelength'] * 1000, 1)
39+
df[['specdif', 'specdir', 'specetr', 'specglo']] /= 1000
40+
return kwargs, df
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)