Untitled
unknown
python
a year ago
3.2 kB
7
Indexable
import math
class GHICalculator:
# Dew point ranges and their corresponding comfort levels and colors
COMFORT_RANGES = [
{'max': 10, 'label': 'dry', 'color': '#5ECBFC'},
{'max': 15, 'label': 'very comfortable', 'color': '#62FF3B'},
{'max': 18, 'label': 'comfortable', 'color': '#CBFF73'},
{'max': 20, 'label': 'alright', 'color': '#FFFF9E'},
{'max': 24, 'label': 'uncomfortable', 'color': '#FFD239'},
{'max': 26, 'label': 'very uncomfortable', 'color': '#FF981E'},
{'max': float('inf'), 'label': 'severely uncomfortable', 'color': '#FF6610'}
]
@staticmethod
def calculate_relative_humidity(temperature: float, dew_point: float) -> float:
"""
Calculate relative humidity using Magnus-Tetens formula
Args:
temperature (float): Temperature in Celsius
dew_point (float): Dew point in Celsius
Returns:
float: Relative humidity as a percentage
"""
# Constants for Magnus-Tetens formula
a = 17.27
b = 237.7
# Calculate saturation vapor pressure at temperature
sat_vapor_pressure = 6.112 * pow(2.71828, (a * temperature) / (temperature + b))
# Calculate actual vapor pressure using dew point
act_vapor_pressure = 6.112 * pow(2.71828, (a * dew_point) / (dew_point + b))
# Calculate relative humidity
rh = (act_vapor_pressure / sat_vapor_pressure) * 100
return min(100, max(0, rh)) # Ensure RH is between 0-100%
@staticmethod
def calculate_ghi(dew_point: float, temperature: float = None) -> dict:
"""
Calculate the Global Humidity Index (GHI) based on dew point.
Args:
dew_point (float): Dew point temperature in Celsius
temperature (float): Not used in new calculation, kept for backwards compatibility
Returns:
dict: Contains GHI value, comfort level label, and color code
"""
# Normalize dew point to 0-100 scale
normalized_ghi = math.ceil((dew_point / 38) * 100)
normalized_ghi = max(0, min(100, normalized_ghi)) # Ensure it's between 0-100
# Determine comfort level and color based on dew point
comfort_info = None
for range_info in GHICalculator.COMFORT_RANGES:
if dew_point <= range_info['max']:
comfort_info = range_info
break
return {
'ghi': normalized_ghi,
'comfort_level': comfort_info['label'],
'color': comfort_info['color']
}
@staticmethod
def get_comfort_level(ghi: float) -> str:
"""
Get comfort level based on dew point (GHI value).
Args:
ghi (float): Dew point value in Celsius
Returns:
str: Comfort level description
"""
for range_info in GHICalculator.COMFORT_RANGES:
if ghi <= range_info['max']:
return range_info['label']
return GHICalculator.COMFORT_RANGES[-1]['label'] Editor is loading...
Leave a Comment