#!/usr/bin/env python3
"""ISOTHERM starter kit: read the ISOTHERM settlement dataset and answer three questions.

  python isotherm_kit.py <folder with the unzipped CSV files> [ICAO]

Works on the free Tokyo sample (https://isotherm-82f.pages.dev/data.html) and on the paid sets.
Standard library only. MIT licence. Information only, not financial or betting advice.

1. rounding   How often would cutting off the decimal (truncating) name a different whole degree
              than rounding half up? Markets resolve on the whole degree, so this is the share of
              days a wrong counting rule puts you in the wrong range.
2. hours      By each local hour, on what share of days had the day's final high already been reached?
3. forecast   How far was each model's last forecast before the day from the settled high?
"""
import csv, math, os, sys
from collections import defaultdict


def rows(folder, name):
    path = os.path.join(folder, name)
    if not os.path.exists(path):
        return []
    with open(path, newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def half_up(x):
    return int(math.floor(x + 0.5))


def in_unit(c, unit):
    return round(c * 9 / 5 + 32, 6) if unit == "F" else round(c, 6)


def rounding(daily):
    out = defaultdict(lambda: [0, 0])
    for r in daily:
        if r.get("complete") != "yes":
            continue
        v = in_unit(float(r["high_c"]), r["unit"])
        out[r["icao"]][0] += half_up(v) != math.floor(v)
        out[r["icao"]][1] += 1
    return out


def hours(running):
    out = defaultdict(lambda: [[0, 0] for _ in range(24)])
    for r in running:
        for h in range(24):
            v = r.get(f"h{h:02d}")
            if v in (None, ""):
                continue
            out[r["icao"]][h][0] += v == r["settled_high"]
            out[r["icao"]][h][1] += 1
    return out


def forecast(fc, daily):
    settled = {(r["icao"], r["local_date"]): float(r["high_c"]) for r in daily if r.get("complete") == "yes"}
    last = {}
    for r in fc:
        try:
            lead = float(r["lead_hours"])
        except ValueError:
            continue
        key = (r["model"], r["icao"], r["local_date"])
        if lead >= 12 and (key not in last or lead < last[key][0]):      # the last run at least 12 h ahead
            last[key] = (lead, float(r["forecast_high_c"]))
    out = defaultdict(list)
    for (model, icao, day), (_, f) in last.items():
        if (icao, day) in settled:
            out[(icao, model)].append(f - settled[(icao, day)])
    return out


def main():
    if len(sys.argv) < 2:
        print(__doc__)
        return 1
    folder, only = sys.argv[1], (sys.argv[2].upper() if len(sys.argv) > 2 else None)
    daily = [r for r in rows(folder, "settled_daily_highs.csv") if not only or r["icao"] == only]
    if not daily:
        print("settled_daily_highs.csv not found or empty in", folder)
        return 1
    running = [r for r in rows(folder, "running_high_by_hour.csv") if not only or r["icao"] == only]
    fc = [r for r in rows(folder, "forecast_archive.csv") if not only or r["icao"] == only]

    print("1. rounding: days where truncating names a different whole degree than rounding half up")
    for icao, (n, d) in sorted(rounding(daily).items()):
        print(f"   {icao}: {n} of {d} days ({100 * n / max(d, 1):.1f}%)")
    print("   (0% is normal where the airport reports whole degrees; the gap shows up at airports that report 0.1 degrees)")

    print("\n2. hours: share of days on which the final high had been reached by each local hour")
    for icao, hs in sorted(hours(running).items()):
        line = "  ".join(f"{h:02d}h {100 * a / n:.0f}%" for h, (a, n) in enumerate(hs) if n and h in (9, 11, 12, 13, 14, 15, 16, 18))
        print(f"   {icao}: {line}")

    print("\n3. forecast: last run at least 12 h before the day, forecast minus settled high (deg C)")
    for (icao, model), errs in sorted(forecast(fc, daily).items()):
        mean = sum(errs) / len(errs)
        mae = sum(abs(e) for e in errs) / len(errs)
        print(f"   {icao} {model}: {len(errs)} days, mean {mean:+.2f}, mean absolute {mae:.2f}")
    return 0


if __name__ == "__main__":
    sys.exit(main())
