Skip to content

Commit 5eb9d00

Browse files
Kurt RheeKurt Rhee
authored andcommitted
added magnus_tetens equations
1 parent 0ec0cbf commit 5eb9d00

File tree

5 files changed

+2150
-2
lines changed

5 files changed

+2150
-2
lines changed

pvlib/spectrum/magnus_tetens.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import numpy as np
2+
3+
4+
def magnus_tetens_aekr(temperature, dewpoint):
5+
"""
6+
Calculate relative humidity using Magnus equation with AEKR coefficients.
7+
This function was used by First Solar in creating their spectral model
8+
and is therefore relevant to the first solar spectral model in pvlib.
9+
10+
Parameters
11+
----------
12+
temperature : pd.Series
13+
Air temperature in degrees Celsius
14+
dewpoint : pd.Series
15+
Dewpoint temperature in degrees Celsius
16+
17+
Returns
18+
-------
19+
pd.Series
20+
Relative humidity as percentage (0-100)
21+
22+
Notes
23+
-----
24+
Uses the AEKR coefficients which minimize errors between -40 and
25+
50 degrees C according to reference [1].
26+
27+
References
28+
----------
29+
.. [1] https://www.osti.gov/servlets/purl/548871-PjpxAP/webviewable/
30+
"""
31+
# Magnus equation coefficients (AEKR)
32+
MAGNUS_A = 6.1094
33+
MAGNUS_B = 17.625
34+
MAGNUS_C = 243.04
35+
36+
# Calculate vapor pressure (e) and saturation vapor pressure (es)
37+
e = MAGNUS_A * np.exp((MAGNUS_B * temperature) / (MAGNUS_C + temperature))
38+
es = MAGNUS_A * np.exp((MAGNUS_B * dewpoint) / (MAGNUS_C + dewpoint))
39+
40+
# Calculate relative humidity as percentage
41+
relative_humidity = 100 * (es / e)
42+
43+
return relative_humidity
44+
45+
46+
def reverse_magnus_tetens_aekr(temperature, relative_humidity):
47+
"""
48+
Calculate dewpoint temperature using Magnus equation with
49+
AEKR coefficients. This is just a reversal of the calculation
50+
in calculate_relative_humidity.
51+
52+
Parameters
53+
----------
54+
temperature : pd.Series
55+
Air temperature in degrees Celsius
56+
relative_humidity : pd.Series
57+
Relative humidity as percentage (0-100)
58+
59+
Returns
60+
-------
61+
pd.Series
62+
Dewpoint temperature in degrees Celsius
63+
64+
Notes
65+
-----
66+
Derived by solving the Magnus equation for dewpoint given
67+
relative humidity.
68+
Valid for temperatures between -40 and 50 degrees C.
69+
70+
References
71+
----------
72+
.. [1] https://www.osti.gov/servlets/purl/548871-PjpxAP/webviewable/
73+
"""
74+
# Magnus equation coefficients (AEKR)
75+
MAGNUS_B = 17.625
76+
MAGNUS_C = 243.04
77+
78+
# Calculate the term inside the log
79+
# From RH = 100 * (es/e), we get es = (RH/100) * e
80+
# Substituting the Magnus equation and solving for dewpoint
81+
82+
# First calculate ln(es/MAGNUS_A)
83+
ln_term = (
84+
(MAGNUS_B * temperature) / (MAGNUS_C + temperature)
85+
+ np.log(relative_humidity/100)
86+
)
87+
88+
# Then solve for dewpoint
89+
dewpoint = MAGNUS_C * ln_term / (MAGNUS_B - ln_term)
90+
91+
return dewpoint
92+
93+
94+
if __name__ == "__main__":
95+
import pandas as pd
96+
rh = magnus_tetens_aekr(
97+
temperature=pd.Series([20.0, 25.0, 30.0, 15.0, 10.0]),
98+
dewpoint=pd.Series([15.0, 20.0, 25.0, 12.0, 8.0])
99+
)
100+
101+
dewpoint = reverse_magnus_tetens_aekr(
102+
temperature=pd.Series([20.0, 25.0, 30.0, 15.0, 10.0]),
103+
relative_humidity=rh
104+
)
105+
print(rh)
106+
print(dewpoint)

pvlib/tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,22 @@ def sapm_module_params():
474474
'IXXO': 3.18803,
475475
'FD': 1}
476476
return parameters
477+
478+
479+
@pytest.fixture(scope='function')
480+
def spectrum_temperature():
481+
temperature = pd.Series([20.0, 25.0, 30.0, 15.0, 10.0])
482+
return temperature
483+
484+
485+
@pytest.fixture(scope='function')
486+
def spectrum_dewpoint():
487+
dewpoint = pd.Series([15.0, 20.0, 25.0, 12.0, 8.0])
488+
return dewpoint
489+
490+
@pytest.fixture(scope='function')
491+
def spectrum_relative_humidity():
492+
relative_humidity = pd.Series([
493+
72.938767, 73.802512, 74.628205, 82.261353, 87.383237
494+
])
495+
return relative_humidity
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pandas as pd
2+
from pvlib.spectrum.magnus_tetens import (
3+
magnus_tetens_aekr,
4+
reverse_magnus_tetens_aekr
5+
)
6+
7+
8+
# Unit tests
9+
def test_magnus_tetens_aekr(
10+
spectrum_temperature, spectrum_dewpoint, spectrum_relative_humidity
11+
):
12+
"""
13+
Test the magnus_tetens_aekr function with sample data.
14+
"""
15+
16+
# Calculate relative humidity
17+
rh = magnus_tetens_aekr(
18+
temperature=spectrum_temperature,
19+
dewpoint=spectrum_dewpoint
20+
)
21+
22+
# test
23+
pd.testing.assert_series_equal(
24+
rh,
25+
spectrum_relative_humidity,
26+
check_names=False
27+
)
28+
29+
30+
# Unit tests
31+
def test_reverse_magnus_tetens_aekr(
32+
spectrum_temperature, spectrum_dewpoint, spectrum_relative_humidity
33+
):
34+
"""
35+
Test the reverse_magnus_tetens_aekr function with sample data.
36+
"""
37+
38+
# Calculate relative humidity
39+
dewpoint = reverse_magnus_tetens_aekr(
40+
temperature=spectrum_temperature,
41+
relative_humidity=spectrum_relative_humidity
42+
)
43+
44+
# test
45+
pd.testing.assert_series_equal(
46+
dewpoint, spectrum_dewpoint, check_names=False
47+
)

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55

66
[project]
7-
name = "pvlib"
7+
name = "pvlib-branch"
88
description = "A set of functions and classes for simulating the performance of photovoltaic energy systems."
99
authors = [
1010
{ name = "pvlib python Developers", email = "[email protected]" },
@@ -17,6 +17,7 @@ dependencies = [
1717
'requests',
1818
'scipy >= 1.6.0',
1919
'h5py',
20+
"flake8>=7.1.1",
2021
]
2122
license = { text = "BSD-3-Clause" }
2223
classifiers = [
@@ -56,7 +57,7 @@ optional = [
5657
]
5758
doc = [
5859
'ipython',
59-
'pickleshare', # required by ipython
60+
'pickleshare', # required by ipython
6061
'matplotlib',
6162
'sphinx == 7.3.7',
6263
'pydata-sphinx-theme == 0.15.4',

0 commit comments

Comments
 (0)