Created
October 18, 2024 07:34
-
-
Save JBlond/66da96db3d8215a09fa3ea5c686a3fc1 to your computer and use it in GitHub Desktop.
HumidityCalculator.py
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
import math | |
class HumidityCalculator: | |
def calculate_absolute_humidity( | |
self, | |
relative_humidity: float, | |
temperature: float, | |
temperature_in_fahrenheit: bool = False, | |
is_relative_humidity_in_percent: bool = True | |
) -> float: | |
""" | |
Computes absolute humidity from relative humidity and temperature. | |
Based on the August-Roche-Magnus approximation. | |
Considered valid when: | |
0 < temperature < 60 degrees Celsius | |
1% < relative humidity < 100% | |
0 < dew point < 50 degrees Celsius | |
Args: | |
relative_humidity: The relative humidity value to be converted. | |
temperature: Temperature associated with given relative humidity value in Fahrenheit or Celsius. | |
temperature_in_fahrenheit: Is the given temperature in Fahrenheit or Celsius? Default is Celsius. | |
is_relative_humidity_in_percent: Is the given relative humidity in percent or decimal form? | |
Returns: | |
The absolute humidity in grams per cubic meter (g/m³). | |
Raises: | |
ValueError: If the input values are outside the acceptable range. | |
""" | |
# Constants | |
kSVP = 6.112 # Saturated vapor pressure in millibars | |
kMolecularWeight = 18.01528 # Molecular weight of water in g/mol | |
kA = 17.625 # Alduchov-Eskeridge coefficient A | |
kB = 243.05 # Alduchov-Eskeridge coefficient B | |
# Check and convert relative humidity if in percent | |
if is_relative_humidity_in_percent: | |
if relative_humidity < 1 or relative_humidity > 100: | |
raise ValueError("Relative Humidity In Percent must be between 1 and 100") | |
relative_humidity /= 100.0 | |
elif relative_humidity < 0.01 or relative_humidity > 1: | |
raise ValueError("Relative Humidity must be between 0.01 and 1.0") | |
# Convert temperature to Celsius if in Fahrenheit | |
temperature_in_celsius = temperature | |
if temperature_in_fahrenheit: | |
temperature_in_celsius = (temperature - 32) / 1.8000 | |
# Validate temperature range | |
if temperature_in_celsius < 1 or temperature_in_celsius > 60: | |
raise ValueError("Temperature In Celsius must be between 1 and 60") | |
# Calculate absolute humidity | |
temperature_in_kelvin = temperature_in_celsius + 273.15 | |
pressure = kSVP * math.exp((kA * temperature_in_celsius) / (temperature_in_celsius + kB)) * relative_humidity | |
water_vapor_in_mols = pressure / (temperature_in_kelvin * 0.08314) | |
return water_vapor_in_mols * kMolecularWeight # g/m³ | |
if __name__ == "__main__": | |
humcalc = HumidityCalculator() | |
print(humcalc.calculate_absolute_humidity(50.0, 30.0, False, True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment