File size: 998 Bytes
5311776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from __future__ import annotations
import os
import math
from dataclasses import dataclass
from typing import Dict, Any, Optional, List

import pandas as pd
import numpy as np


def get_env(name: str, default: Optional[str] = None) -> Optional[str]:
    # HF Spaces: secrets mounted as env
    return os.getenv(name, default)


def heat_index_c(temp_c: float, rh: float) -> float:
    """Compute heat index (°C) using Rothfusz regression.
    Input temp in °C, RH in %.
    """
    T = temp_c * 9/5 + 32
    R = rh
    HI_f = (-42.379 + 2.04901523*T + 10.14333127*R - 0.22475541*T*R
            - 6.83783e-3*T*T - 5.481717e-2*R*R + 1.22874e-3*T*T*R
            + 8.5282e-4*T*R*R - 1.99e-6*T*T*R*R)
    return (HI_f - 32) * 5/9


def bucketize(val: float, bins: List[float], labels: List[str]) -> str:
    idx = np.digitize([val], bins)[0]
    return labels[min(idx, len(labels)-1)]


def safe_float(x, default=np.nan):
    try:
        return float(x)
    except Exception:
        return default