#!/usr/bin/env python3 """Exploratory E5: CAMS 50 hPa minimum temperature (south of 60 S), August and September means of daily means, ranked 1979-2026 (rank 1 = coldest). Standard library only. Usage: python3 -I cams_t50_months.py """ import csv import hashlib import sys from collections import defaultdict from datetime import datetime from pathlib import Path V = Path(sys.argv[1]).resolve() f = next((V / "downloads" / "cams_csv_sh_t50").glob("*.csv")) print("file", f.name, hashlib.sha256(f.read_bytes()).hexdigest()) acc = defaultdict(list) with open(f, encoding="utf-8") as fh: r = csv.reader(fh) next(r) for t, v in r: d = datetime.fromisoformat(t) acc[d.date()].append(float(v)) dm = {d: sum(v) / len(v) for d, v in acc.items() if len(v) == 4} for m, name in ((8, "August"), (9, "September")): mm = {} for y in range(1979, 2027): vals = [v for d, v in dm.items() if d.year == y and d.month == m] if len(vals) >= 28: mm[y] = sum(vals) / len(vals) o = sorted(mm, key=lambda y: mm[y]) clim = [mm[y] for y in mm if y <= 2025] print(f"{name}: 2026 = {mm[2026]:.2f} K, rank {o.index(2026) + 1} coldest of {len(o)}; 1979-2025 mean " f"{sum(clim) / len(clim):.2f} K; five coldest {[(y, round(mm[y], 2)) for y in o[:5]]}")