Skip to content

Split test_spectrum.py #2151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/sphinx/source/whatsnew/v0.11.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Enhancements
* Restructured the pvlib/spectrum folder by breaking up the contents of
pvlib/spectrum/mismatch.py into pvlib/spectrum/mismatch.py,
pvlib/spectrum/irradiance.py, and
pvlib/spectrum/response.py. (:issue:`2125`, :pull:`2136`)
pvlib/spectrum/response.py. (:issue:`2125`, :pull:`2136`, :pull:`2151`)
* Added function for calculating wind speed at different heights,
:py:func:`pvlib.atmosphere.windspeed_powerlaw`.
(:issue:`2118`, :pull:`2124`)
Expand Down
Empty file.
40 changes: 40 additions & 0 deletions pvlib/tests/spectrum/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
import pytest
import pandas as pd

from ..conftest import DATA_DIR

SPECTRL2_TEST_DATA = DATA_DIR / 'spectrl2_example_spectra.csv'


@pytest.fixture
def spectrl2_data():
# reference spectra generated with solar_utils==0.3
"""
expected = solar_utils.spectrl2(
units=1,
location=[40, -80, -5],
datetime=[2020, 3, 15, 10, 45, 59],
weather=[1013, 15],
orientation=[0, 180],
atmospheric_conditions=[1.14, 0.65, 0.344, 0.1, 1.42],
albedo=[0.3, 0.7, 0.8, 1.3, 2.5, 4.0] + [0.2]*6,
)
"""
kwargs = {
'surface_tilt': 0,
'relative_airmass': 1.4899535986910446,
'apparent_zenith': 47.912086486816406,
'aoi': 47.91208648681641,
'ground_albedo': 0.2,
'surface_pressure': 101300,
'ozone': 0.344,
'precipitable_water': 1.42,
'aerosol_turbidity_500nm': 0.1,
'dayofyear': 75
}
df = pd.read_csv(SPECTRL2_TEST_DATA, index_col=0)
# convert um to nm
df['wavelength'] = np.round(df['wavelength'] * 1000, 1)
df[['specdif', 'specdir', 'specetr', 'specglo']] /= 1000
return kwargs, df
74 changes: 74 additions & 0 deletions pvlib/tests/spectrum/test_irradiance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest
from numpy.testing import assert_allclose, assert_approx_equal, assert_equal
import pandas as pd
import numpy as np
from pvlib import spectrum
from pvlib._deprecation import pvlibDeprecationWarning

from ..conftest import assert_series_equal, fail_on_pvlib_version


@fail_on_pvlib_version('0.12')
def test_get_am15g():
# test that the reference spectrum is read and interpolated correctly
with pytest.warns(pvlibDeprecationWarning,
match="get_reference_spectra instead"):
e = spectrum.get_am15g()
assert_equal(len(e), 2002)
assert_equal(np.sum(e.index), 2761442)
assert_approx_equal(np.sum(e), 1002.88, significant=6)

wavelength = [270, 850, 950, 1200, 1201.25, 4001]
expected = [0.0, 0.893720, 0.147260, 0.448250, 0.4371025, 0.0]

with pytest.warns(pvlibDeprecationWarning,
match="get_reference_spectra instead"):
e = spectrum.get_am15g(wavelength)
assert_equal(len(e), len(wavelength))
assert_allclose(e, expected, rtol=1e-6)


@pytest.mark.parametrize(
"reference_identifier,expected_sums",
[
(
"ASTM G173-03", # reference_identifier
{ # expected_sums
"extraterrestrial": 1356.15,
"global": 1002.88,
"direct": 887.65,
},
),
],
)
def test_get_reference_spectra(reference_identifier, expected_sums):
# test reading of a standard spectrum
standard = spectrum.get_reference_spectra(standard=reference_identifier)
assert set(standard.columns) == expected_sums.keys()
assert standard.index.name == "wavelength"
assert standard.index.is_monotonic_increasing is True
expected_sums = pd.Series(expected_sums) # convert prior to comparison
assert_series_equal(np.sum(standard, axis=0), expected_sums, atol=1e-2)


def test_get_reference_spectra_custom_wavelengths():
# test that the spectrum is interpolated correctly when custom wavelengths
# are specified
# only checked for ASTM G173-03 reference spectrum
wavelength = [270, 850, 951.634, 1200, 4001]
expected_sums = pd.Series(
{"extraterrestrial": 2.23266, "global": 1.68952, "direct": 1.58480}
) # for given ``wavelength``
standard = spectrum.get_reference_spectra(
wavelength, standard="ASTM G173-03"
)
assert_equal(len(standard), len(wavelength))
# check no NaN values were returned
assert not standard.isna().any().any() # double any to return one value
assert_series_equal(np.sum(standard, axis=0), expected_sums, atol=1e-4)


def test_get_reference_spectra_invalid_reference():
# test that an invalid reference identifier raises a ValueError
with pytest.raises(ValueError, match="Invalid standard identifier"):
spectrum.get_reference_spectra(standard="invalid")
Loading
Loading