Skip to content

Instantly share code, notes, and snippets.

@JBlond
Created October 18, 2024 07:34
Show Gist options
  • Save JBlond/66da96db3d8215a09fa3ea5c686a3fc1 to your computer and use it in GitHub Desktop.
Save JBlond/66da96db3d8215a09fa3ea5c686a3fc1 to your computer and use it in GitHub Desktop.

Revisions

  1. JBlond renamed this gist Oct 18, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. JBlond created this gist Oct 18, 2024.
    64 changes: 64 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    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))