Upload calculate_observed_wbgt.py with huggingface_hub
Browse files- calculate_observed_wbgt.py +142 -0
calculate_observed_wbgt.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Compute observed outdoor WBGT from Dortmund d2r_advanced station CSVs.
|
| 3 |
+
|
| 4 |
+
The equations match /perm/nkaz/GLIDE-SOL-private/src/calculate_wbgt.py:
|
| 5 |
+
Stull wet-bulb approximation, Leroyer black-globe temperature, and
|
| 6 |
+
ISO outdoor WBGT = 0.7*Twb + 0.2*Tg + 0.1*Ta.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import argparse
|
| 12 |
+
import csv
|
| 13 |
+
import math
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
SBOLTZ = 5.670374419e-8
|
| 18 |
+
MISSING = -999.0
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def parse_float(value: str) -> float:
|
| 22 |
+
if value is None:
|
| 23 |
+
return math.nan
|
| 24 |
+
text = str(value).strip()
|
| 25 |
+
if not text:
|
| 26 |
+
return math.nan
|
| 27 |
+
try:
|
| 28 |
+
return float(text)
|
| 29 |
+
except ValueError:
|
| 30 |
+
return math.nan
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def twb_stull(ta: float, rh: float) -> float:
|
| 34 |
+
"""Wet-bulb temperature in degC using the Stull (2011) approximation."""
|
| 35 |
+
rh = min(100.0, max(0.0, rh))
|
| 36 |
+
return (
|
| 37 |
+
ta * math.atan(0.151977 * math.sqrt(rh + 8.313659))
|
| 38 |
+
+ math.atan(ta + rh)
|
| 39 |
+
- math.atan(rh - 1.676331)
|
| 40 |
+
+ 0.00391838 * (rh**1.5) * math.atan(0.023101 * rh)
|
| 41 |
+
- 4.686035
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def tg_leroyer(
|
| 46 |
+
ta: float,
|
| 47 |
+
tmrt: float,
|
| 48 |
+
wind_speed: float,
|
| 49 |
+
globe_diameter: float = 0.15,
|
| 50 |
+
globe_emissivity: float = 0.95,
|
| 51 |
+
) -> float:
|
| 52 |
+
"""Black-globe temperature in degC using the Leroyer formulation."""
|
| 53 |
+
ws = max(float(wind_speed), 0.15)
|
| 54 |
+
hcg = 6.3 * (ws**0.6) / (globe_diameter**0.4)
|
| 55 |
+
a = hcg / (globe_emissivity * SBOLTZ)
|
| 56 |
+
b = (tmrt + 273.15) ** 4.0 + a * (ta + 273.15)
|
| 57 |
+
m = 9.0 * (a**2.0)
|
| 58 |
+
n = 27.0 * (a**4.0)
|
| 59 |
+
p = 256.0 * (b**3.0)
|
| 60 |
+
e = (m + 1.73205 * math.sqrt(max(n + p, 0.0))) ** (1.0 / 3.0)
|
| 61 |
+
q = 3.4943 * b
|
| 62 |
+
k = 0.381571 * e - q / max(e, 1.0e-12)
|
| 63 |
+
k = max(k, 1.0e-12)
|
| 64 |
+
i_term = 0.5 * math.sqrt(max((2.0 * a / math.sqrt(k)) - k, 0.0))
|
| 65 |
+
j_term = math.sqrt(k) / 2.0
|
| 66 |
+
return i_term - j_term - 273.15
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def wbgt_observed(ta: float, rh: float, tmrt: float, wind_speed: float) -> float:
|
| 70 |
+
if not all(math.isfinite(x) for x in (ta, rh, tmrt, wind_speed)):
|
| 71 |
+
return MISSING
|
| 72 |
+
twb = twb_stull(ta, rh)
|
| 73 |
+
tg = tg_leroyer(ta, tmrt, wind_speed)
|
| 74 |
+
return 0.7 * twb + 0.2 * tg + 0.1 * ta
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def process_file(input_csv: Path, output_csv: Path) -> tuple[int, int]:
|
| 78 |
+
output_csv.parent.mkdir(parents=True, exist_ok=True)
|
| 79 |
+
total = 0
|
| 80 |
+
valid = 0
|
| 81 |
+
|
| 82 |
+
with input_csv.open(newline="", encoding="utf-8") as src, output_csv.open(
|
| 83 |
+
"w", newline="", encoding="utf-8"
|
| 84 |
+
) as dst:
|
| 85 |
+
reader = csv.DictReader(src)
|
| 86 |
+
writer = csv.DictWriter(
|
| 87 |
+
dst,
|
| 88 |
+
fieldnames=["station_id", "measured_at", "WBGT_observed"],
|
| 89 |
+
)
|
| 90 |
+
writer.writeheader()
|
| 91 |
+
|
| 92 |
+
required = {"air_temperature", "relative_humidity", "mrt", "wind_speed"}
|
| 93 |
+
missing_cols = required.difference(reader.fieldnames or [])
|
| 94 |
+
if missing_cols:
|
| 95 |
+
raise ValueError(f"{input_csv} missing columns: {sorted(missing_cols)}")
|
| 96 |
+
|
| 97 |
+
for row in reader:
|
| 98 |
+
total += 1
|
| 99 |
+
ta = parse_float(row.get("air_temperature"))
|
| 100 |
+
rh = parse_float(row.get("relative_humidity"))
|
| 101 |
+
tmrt = parse_float(row.get("mrt"))
|
| 102 |
+
ws = parse_float(row.get("wind_speed"))
|
| 103 |
+
wbgt = wbgt_observed(ta, rh, tmrt, ws)
|
| 104 |
+
if wbgt != MISSING:
|
| 105 |
+
valid += 1
|
| 106 |
+
writer.writerow(
|
| 107 |
+
{
|
| 108 |
+
"station_id": row.get("station_id", ""),
|
| 109 |
+
"measured_at": row.get("measured_at", ""),
|
| 110 |
+
"WBGT_observed": f"{wbgt:.6f}" if wbgt != MISSING else f"{MISSING:.1f}",
|
| 111 |
+
}
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
return total, valid
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def main() -> None:
|
| 118 |
+
parser = argparse.ArgumentParser()
|
| 119 |
+
parser.add_argument(
|
| 120 |
+
"--input-dir",
|
| 121 |
+
type=Path,
|
| 122 |
+
default=Path("/perm/nkaz/SOLWEIG_POSTPROC/DORTMUND/d2r_advanced"),
|
| 123 |
+
)
|
| 124 |
+
parser.add_argument(
|
| 125 |
+
"--output-dir",
|
| 126 |
+
type=Path,
|
| 127 |
+
default=Path("/perm/nkaz/SOLWEIG_POSTPROC/DORTMUND/d2r_advanced_wbgt_observed"),
|
| 128 |
+
)
|
| 129 |
+
args = parser.parse_args()
|
| 130 |
+
|
| 131 |
+
files = sorted(args.input_dir.glob("*.csv"))
|
| 132 |
+
if not files:
|
| 133 |
+
raise SystemExit(f"No CSV files found in {args.input_dir}")
|
| 134 |
+
|
| 135 |
+
for input_csv in files:
|
| 136 |
+
output_csv = args.output_dir / input_csv.name
|
| 137 |
+
total, valid = process_file(input_csv, output_csv)
|
| 138 |
+
print(f"{input_csv.name}: wrote {output_csv} ({valid}/{total} valid WBGT)")
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
main()
|