"""NOT in the sealed rule, added after the verdict. Ground frost (TGK, 5 cm above the ground) after 15 May,
same 32 stations at or below 400 m, and the daily mean line for the chart. The Ice Saints rule never read these."""
import io, re, sys, json, zipfile, urllib.request, datetime as dt
from pathlib import Path
import numpy as np, pandas as pd
sys.path.insert(0, str(Path(__file__).parent))
import rule
U = "https://opendata.dwd.de/climate_environment/CDC/observations_germany/climate/daily/kl/historical/"
idx = urllib.request.urlopen(U, timeout=60).read().decode()
sta = {}
for l in open(Path.home() / "data/ice-saints/stations.txt", encoding="latin1").read().split("\n")[2:]:
    p = l.split()
    if len(p) > 5 and p[0] in rule.FRAME:
        sta[p[0]] = float(p[3])
low = [s for s in rule.FRAME if sta[s] <= 400]
per = ((1926, 1955), (1956, 1985), (1986, 2015), (1996, 2025))
hit = {p: [0, 0, 0] for p in per}
for sid in low:
    name = re.search(rf"tageswerte_KL_{sid}_\d+_\d+_hist\.zip", idx).group(0)
    z = zipfile.ZipFile(io.BytesIO(urllib.request.urlopen(U + name, timeout=180).read()))
    f = [n for n in z.namelist() if n.startswith("produkt_klima_tag")][0]
    df = pd.read_csv(z.open(f), sep=";", skipinitialspace=True, dtype=str, encoding="latin1")
    df.columns = [c.strip() for c in df.columns]
    d = pd.to_datetime(df["MESS_DATUM"].str.strip(), format="%Y%m%d")
    g = pd.to_numeric(df["TGK"], errors="coerce").where(lambda s: s > -900)
    s = pd.Series(g.values, index=d)
    for p in per:
        for y in range(p[0], p[1] + 1):
            x = s[f"{y}-05-16":f"{y}-06-30"]
            if len(x) < 40 or x.isna().sum() > 3:
                continue
            hit[p][0] += 1
            hit[p][1] += bool((x < 0).any())
            hit[p][2] += bool((x[10:] < 0).any())
out = {f"{a}-{b}": dict(station_years=v[0], ground_frost_after_may_15=round(v[1] / max(v[0], 1), 4), after_may_25=round(v[2] / max(v[0], 1), 4)) for (a, b), v in hit.items()}
print(out)
d = pd.read_parquet(Path.home() / "data/ice-saints/daily.parquet")
d = d[(d.date.dt.month.isin([4, 5, 6])) & (d.date.dt.year.between(1925, 2025))]
d["md"] = d.date.dt.strftime("%m-%d")
line = d.groupby("md").tmk.mean().round(3)
line = line["04-20":"06-10"]
json.dump(dict(ground=out, daily_mean=line.to_dict()), open(Path(__file__).parent / "ground-and-line.json", "w"), indent=1)
print(line["05-01":"05-25"].to_dict())
