"""Join the MeteoSwiss hourly weather (work/weather_geneve.csv) with table.py's hourly output (work/table_1h.txt): prints the joined table (every N hours) and correlations of dx_RD, temp (housing) with the weather columns, plus lags. usage: python3 weather.py [EVERY] """ import csv, statistics, sys D = "labeling/auto/analyses/WYOMING_2026-11-06T0805_2026-11-16T0627/work/" every = int(sys.argv[1]) if len(sys.argv) > 1 else 1 w = {r["time_utc"][5:16]: r for r in csv.DictReader(open(D + "weather_geneve.csv"))} rows = [l.rstrip("\n").split("\t") for l in open(D + "table_1h.txt")] head, rows = rows[0], rows[1:] c = {h: i for i, h in enumerate(head)} W = ["temperature_c", "sunshine_min", "global_radiation_wm2", "precipitation_mm", "humidity_pct", "wind_speed_kmh", "wind_direction_deg"] joined = [] for r in rows: k = r[0] if k in w: def f(x): try: return float(x) except ValueError: return None joined.append((k, f(r[c["temp"]]), f(r[c["dx_RD"]]), f(r[c["cx14"]]), *[f(w[k][x]) for x in W])) print("time housing dx_RD cx14 air sun rad rain hum wind wdir") for i, j in enumerate(joined): if i % every == 0 and j[0] >= "11-06T08": print(" ".join("-" if v is None else (v if isinstance(v, str) else "%.1f" % v) for v in j)) def corr(a, b): p = [(x, y) for x, y in zip(a, b) if x is not None and y is not None] xs, ys = zip(*p) return statistics.correlation(xs, ys), len(p) sel = [j for j in joined if j[0] >= "11-06T08"] cols = {"housing": 1, "dx_RD": 2, "air": 4, "sun": 5, "rad": 6, "hum": 8, "wind": 9} for a in ("housing", "dx_RD"): for b in ("air", "rad", "sun", "hum", "wind") + (("housing",) if a == "dx_RD" else ()): r, n = corr([s[cols[a]] for s in sel], [s[cols[b]] for s in sel]) print(f"r({a}, {b}) = {r:+.2f} (n={n})") # lag: dx_RD against air temperature shifted by L hours (air earlier) for L in (0, 1, 2, 3, 4, 6): a = [s[2] for s in sel[L:]]; b = [s[4] for s in sel[:len(sel) - L]] print(f"lag {L} h: r(dx_RD, air earlier) = {corr(a, b)[0]:+.2f}; r(housing, air earlier) = {corr([s[1] for s in sel[L:]], b)[0]:+.2f}")