-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Calculate Relative Humidity via Magnus Tetens Equation #2286
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
+231
−0
Merged
Changes from 6 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
70e0156
changed default types to float in solarposition.py
0ec0cbf
switched to floats in the docstrings and removed type hints
5eb9d00
added magnus_tetens equations
ab2de3a
line too long in solarposition.py
f62bd8e
revert solarposition.py to pre-PR state
77025c7
set default magnus coefficients per conversation in #1744
b52f9a9
moved equations to atmosphere.py, updated function names to suggested…
58b246e
moved tests to atmosphere.py tests
a06871a
changed reference to WMO
caa4417
dry-bult temperature in the docstring
edd84b1
revert pyproject.toml
9c32f6a
remove uv.lock
63ed5c7
Update pvlib/atmosphere.py
kurt-rhee 6312f92
fixing flake8 errors for tdew/rh functions
410e95f
Update pvlib/atmosphere.py
kurt-rhee d94df47
Update pvlib/atmosphere.py
kurt-rhee e6b5908
Update pvlib/atmosphere.py
kurt-rhee 63a9226
Update pvlib/atmosphere.py
kurt-rhee 459d5a6
Update pvlib/atmosphere.py
kurt-rhee 05b846f
Update pvlib/atmosphere.py
kurt-rhee 0ae31e1
added some unit tests for different coefficients and input types
kurt-rhee 337f723
refactored tests, removed magnus_tetens, updated whatsnew
kurt-rhee 8bc9b86
reference and whatsnew
kurt-rhee 9651c3b
revert line 36 (linter)
kurt-rhee b860ca5
Update pvlib/atmosphere.py
kurt-rhee 2bdc1dc
Update docs/sphinx/source/whatsnew/v0.11.2.rst
kurt-rhee c39c449
Update pvlib/atmosphere.py
kurt-rhee b5a6f09
Update pvlib/tests/test_atmosphere.py
kurt-rhee aedc835
Update pyproject.toml
kurt-rhee 4fcb9e3
Merge branch 'main' into relative-humidity
AdamRJensen c956c91
Update pvlib/atmosphere.py
kurt-rhee f682815
Update pvlib/atmosphere.py
kurt-rhee 0bfc6cb
Update pvlib/atmosphere.py
kurt-rhee 71a165e
Update pvlib/atmosphere.py
kurt-rhee 25527d6
Update pvlib/atmosphere.py
kurt-rhee 1eff53f
added a round-trip test for magnus tetens
kurt-rhee df33a31
temperature -> temp_air; dewpoint -> temp_dew
kandersolar b4802a3
miscellaneous other cleanup
kandersolar 3893692
tests: put assertions next to calculations
kandersolar 11d81c3
linter
kandersolar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def magnus_tetens_aekr(temperature, dewpoint, A=6.112, B=17.62, C=243.12): | ||
| """ | ||
| Calculate relative humidity using Magnus equation with AEKR coefficients. | ||
| This function was used by First Solar in creating their spectral model | ||
| and is therefore relevant to the first solar spectral model in pvlib. | ||
| Default magnus equation coefficients are from [2]. | ||
kurt-rhee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Parameters | ||
| ---------- | ||
| temperature : pd.Series | ||
| Air temperature in degrees Celsius | ||
kurt-rhee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| dewpoint : pd.Series | ||
| Dewpoint temperature in degrees Celsius | ||
kurt-rhee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| A: float | ||
| Magnus equation coefficient A | ||
| B: float | ||
| Magnus equation coefficient B | ||
| C: float | ||
| Magnus equation coefficient C | ||
kurt-rhee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Returns | ||
| ------- | ||
| pd.Series | ||
| Relative humidity as percentage (0-100) | ||
| Notes | ||
| ----- | ||
| Uses the AEKR coefficients which minimize errors between -40 and | ||
| 50 degrees C according to reference [1]. | ||
| References | ||
| ---------- | ||
| .. [1] https://www.osti.gov/servlets/purl/548871-PjpxAP/webviewable/ | ||
| .. [2] https://www.schweizerbart.de//papers/metz/detail/3/89544/Advancements_in_the_field_of_hygrometry?af=crossref | ||
| """ | ||
|
|
||
| # Calculate vapor pressure (e) and saturation vapor pressure (es) | ||
| e = A * np.exp((B * temperature) / (C + temperature)) | ||
| es = A * np.exp((B * dewpoint) / (C + dewpoint)) | ||
|
|
||
| # Calculate relative humidity as percentage | ||
| relative_humidity = 100 * (es / e) | ||
|
|
||
| return relative_humidity | ||
|
|
||
|
|
||
| def reverse_magnus_tetens_aekr( | ||
| temperature, relative_humidity, B=17.62, C=243.12 | ||
| ): | ||
| """ | ||
| Calculate dewpoint temperature using Magnus equation with | ||
| AEKR coefficients. This is just a reversal of the calculation | ||
| in calculate_relative_humidity. | ||
| Parameters | ||
| ---------- | ||
| temperature : pd.Series | ||
| Air temperature in degrees Celsius | ||
| relative_humidity : pd.Series | ||
| Relative humidity as percentage (0-100) | ||
| Returns | ||
| ------- | ||
| pd.Series | ||
| Dewpoint temperature in degrees Celsius | ||
| Notes | ||
| ----- | ||
| Derived by solving the Magnus equation for dewpoint given | ||
| relative humidity. | ||
| Valid for temperatures between -40 and 50 degrees C. | ||
| References | ||
| ---------- | ||
| .. [1] https://www.osti.gov/servlets/purl/548871-PjpxAP/webviewable/ | ||
| """ | ||
| # Calculate the term inside the log | ||
| # From RH = 100 * (es/e), we get es = (RH/100) * e | ||
| # Substituting the Magnus equation and solving for dewpoint | ||
|
|
||
| # First calculate ln(es/A) | ||
| ln_term = ( | ||
| (B * temperature) / (C + temperature) | ||
| + np.log(relative_humidity/100) | ||
| ) | ||
|
|
||
| # Then solve for dewpoint | ||
| dewpoint = C * ln_term / (B - ln_term) | ||
|
|
||
| return dewpoint | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
kurt-rhee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import pandas as pd | ||
| rh = magnus_tetens_aekr( | ||
| temperature=pd.Series([20.0, 25.0, 30.0, 15.0, 10.0]), | ||
| dewpoint=pd.Series([15.0, 20.0, 25.0, 12.0, 8.0]) | ||
| ) | ||
|
|
||
| dewpoint = reverse_magnus_tetens_aekr( | ||
| temperature=pd.Series([20.0, 25.0, 30.0, 15.0, 10.0]), | ||
| relative_humidity=rh | ||
| ) | ||
| print(rh) | ||
| print(dewpoint) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import pandas as pd | ||
| from pvlib.spectrum.magnus_tetens import ( | ||
| magnus_tetens_aekr, | ||
| reverse_magnus_tetens_aekr | ||
| ) | ||
|
|
||
|
|
||
| # Unit tests | ||
| def test_magnus_tetens_aekr( | ||
| spectrum_temperature, spectrum_dewpoint, spectrum_relative_humidity | ||
| ): | ||
| """ | ||
| Test the magnus_tetens_aekr function with sample data. | ||
| """ | ||
|
|
||
| # Calculate relative humidity | ||
| rh = magnus_tetens_aekr( | ||
| temperature=spectrum_temperature, | ||
| dewpoint=spectrum_dewpoint | ||
| ) | ||
|
|
||
| # test | ||
| pd.testing.assert_series_equal( | ||
| rh, | ||
| spectrum_relative_humidity, | ||
| check_names=False | ||
| ) | ||
|
|
||
|
|
||
| # Unit tests | ||
| def test_reverse_magnus_tetens_aekr( | ||
| spectrum_temperature, spectrum_dewpoint, spectrum_relative_humidity | ||
| ): | ||
| """ | ||
| Test the reverse_magnus_tetens_aekr function with sample data. | ||
| """ | ||
|
|
||
| # Calculate relative humidity | ||
| dewpoint = reverse_magnus_tetens_aekr( | ||
| temperature=spectrum_temperature, | ||
| relative_humidity=spectrum_relative_humidity | ||
| ) | ||
|
|
||
| # test | ||
| pd.testing.assert_series_equal( | ||
| dewpoint, spectrum_dewpoint, check_names=False | ||
| ) |
kurt-rhee marked this conversation as resolved.
Show resolved
Hide resolved
kurt-rhee marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" | |
|
|
||
|
|
||
| [project] | ||
| name = "pvlib" | ||
| name = "pvlib-branch" | ||
| description = "A set of functions and classes for simulating the performance of photovoltaic energy systems." | ||
| authors = [ | ||
| { name = "pvlib python Developers", email = "[email protected]" }, | ||
|
|
@@ -17,6 +17,7 @@ dependencies = [ | |
| 'requests', | ||
| 'scipy >= 1.6.0', | ||
| 'h5py', | ||
| "flake8>=7.1.1", | ||
| ] | ||
| license = { text = "BSD-3-Clause" } | ||
| classifiers = [ | ||
|
|
@@ -56,7 +57,7 @@ optional = [ | |
| ] | ||
| doc = [ | ||
| 'ipython', | ||
| 'pickleshare', # required by ipython | ||
| 'pickleshare', # required by ipython | ||
kurt-rhee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 'matplotlib', | ||
| 'sphinx == 7.3.7', | ||
| 'pydata-sphinx-theme == 0.15.4', | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.