#!/usr/bin/env python3 """Test 2: rebuild the note's storm lists from GFZ's Kp and Kyoto's Dst. Rules: RULES_storm_lists.txt. Fact-Checked Science, verification of florida-aurora-cycle-25. Standard library only; reads the downloads as data. python3 -I storm_lists.py Writes /storm_lists.txt (the comparison) and /our_storms_24h.csv, our_storms_48h.csv (our lists, reading A). Dst is the World Data Center for Geomagnetism, Kyoto's (final and provisional values only), for scientific, non-commercial use, with acknowledgement of the WDC and the observatories that supply its data. """ import csv import datetime as dt import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # our own code folder; -I leaves it out import fclib as F # noqa: E402 def main(): dl, outdir = sys.argv[1], sys.argv[2] lines = [] out = lines.append kp, _ = F.read_kp(os.path.join(dl, "gfz-kp", "Kp_ap_Ap_SN_F107_since_1932.txt")) dst, kinds = F.read_dst_folder(os.path.join(dl, "kyoto-dst")) A = F.read_events(os.path.join(dl, "s0-note-004-drive", "PathA_events.csv")) B = F.read_events(os.path.join(dl, "s0-note-004-drive", "PathB_events.csv")) Ajx = [e for e in A if not e["date"].startswith("2026-01")] out("TEST 2. The note's storm lists, rebuilt from GFZ's Kp and Kyoto's Dst (rules: RULES_storm_lists.txt)") out("") nf = sum(1 for k in kinds.values() if k == "final") out(f"Kyoto months read: {len(kinds)} ({nf} final, {len(kinds) - nf} provisional); none real-time. Last Kp day in GFZ's file: {max(kp).date()}") last_kp8 = max(t for t, v in kp.items() if v >= 7.667 - 1e-3) out(f"Latest three-hour Kp of 8- or more in GFZ's file: {last_kp8} ({F.kpname(kp[last_kp8])})") after = [(t, v) for t, v in kp.items() if dt.datetime(2026, 2, 1) <= t <= F.FREEZE] tmax = max(after, key=lambda x: x[1]) out(f"Highest Kp from 2026-02-01 to the note's freeze (2026-10-30 14 UT): {F.kpname(tmax[1])} at {tmax[0]}; " f"in October 2026 to the freeze: {F.kpname(max(v for t, v in after if t >= dt.datetime(2026, 10, 1)))}") since = [(t, v) for t, v in kp.items() if t > F.FREEZE] out(f"Highest Kp after the freeze, to {max(kp).date()}: {F.kpname(max(v for t, v in since))}") out("") def in_era(t, stratum): if F.ERA0[0] <= t < F.ERA0[1]: return "1986-2019Nov" end = F.FREEZE if stratum.startswith("Kp") else F.DST_LAST if F.C25_START <= t <= end: return "C25" return None def stamps(stratum): if stratum in F.KP_THR: return [t for t, v in kp.items() if v >= F.KP_THR[stratum] - 1e-3], 3 return [t for t, v in dst.items() if v <= F.DST_THR[stratum]], 1 def attrs(storm, step): s0, s1 = storm[0], storm[1] + dt.timedelta(hours=step) ks = [v for t, v in kp.items() if t < s1 and t + dt.timedelta(hours=3) > s0] ds = [v for t, v in dst.items() if s0 <= t < s1 + dt.timedelta(hours=24)] return (max(ks) if ks else None), (min(ds) if ds else None) note = {} for gap in (24, 48): with open(os.path.join(dl, "s0-note-004-drive", f"Storms_all_strata_{gap}h.csv"), encoding="utf-8-sig", newline="") as f: note[gap] = list(csv.DictReader(f)) want = {24: {"1986-2019Nov": [19, 76, 52, 30, 8], "C25": [4, 13, 9, 6, 2]}, 48: {"1986-2019Nov": [18, 71, 49, 29, 8], "C25": [4, 13, 9, 6, 2]}} ours_all = {} out("Counts per stratum and era: ours under readings A (chain), B (anchor), C (runs), against the note's") for gap in (24, 48): for i, s in enumerate(F.STRATA): st, step = stamps(s) row = [] for reading in "ABC": storms = F.build_storms(st, step, gap, reading) c = {"1986-2019Nov": 0, "C25": 0} for x in storms: e = in_era(x[0], s) if e: c[e] += 1 row.append((c["1986-2019Nov"], c["C25"])) if reading == "A": ours_all[(gap, s)] = [(x, in_era(x[0], s)) for x in storms if in_era(x[0], s)] w = (want[gap]["1986-2019Nov"][i], want[gap]["C25"][i]) verdict = "reproduces (A)" if row[0] == w else ("reproduces under " + "/".join(r for r, v in zip("ABC", row) if v == w) if w in row else "DOES NOT reproduce") out(f" {gap} h, {s}: A {row[0][0]}+{row[0][1]}, B {row[1][0]}+{row[1][1]}, C {row[2][0]}+{row[2][1]}; note {w[0]}+{w[1]}: {verdict}") out("") out("Storm by storm (reading A): unmatched storms, and differences in hit flags and attributes") for gap in (24, 48): rows_out = [] n_unmatched = n_flag = n_attr_kp = n_attr_dst = n_time = 0 for s in F.STRATA: step = 3 if s in F.KP_THR else 1 tol = dt.timedelta(hours=3 if s in F.KP_THR else 1) theirs = [r for r in note[gap] if r["stratum"] == s] used = set() for storm, era in ours_all[(gap, s)]: kmax, dmin = attrs(storm, step) hits = tuple(int(F.is_hit(storm, step, ev)) for ev in (A, B, Ajx)) match = None for j, r in enumerate(theirs): if j in used: continue t0 = dt.datetime.fromisoformat(r["start_UT"]) if t0 == storm[0]: match = j break if match is None: for j, r in enumerate(theirs): if j not in used and abs(dt.datetime.fromisoformat(r["start_UT"]) - storm[0]) <= tol: match = j break rows_out.append([s, storm[0].isoformat(), storm[1].isoformat(), era, f"{kmax:.3f}" if kmax is not None else "", dmin if dmin is not None else "", *hits]) if match is None: n_unmatched += 1 out(f" {gap} h, {s}: OURS ONLY {storm[0]} to {storm[1]} ({era}), Kp max {kmax}, Dst min {dmin}, hits {hits}") continue used.add(match) r = theirs[match] if dt.datetime.fromisoformat(r["start_UT"]) != storm[0] or dt.datetime.fromisoformat(r["end_UT"]) != storm[1]: n_time += 1 out(f" {gap} h, {s}: times differ: ours {storm[0]} to {storm[1]}; note {r['start_UT']} to {r['end_UT']}") if r["era"] != era: out(f" {gap} h, {s}: era differs for {storm[0]}: ours {era}, note {r['era']}") theirs_hits = (int(r["FL_hit_PathA_primary"]), int(r["FL_hit_PathB"]), int(r["FL_hit_PathA_JanExcl_sens"])) if theirs_hits != hits: n_flag += 1 out(f" {gap} h, {s}: HIT FLAGS differ for {storm[0]}: ours {hits}, note {theirs_hits}") if kmax is not None and abs(float(r["Kp_max_GFZ"]) - kmax) > 0.01: n_attr_kp += 1 out(f" {gap} h, {s}: Kp max differs for {storm[0]}: ours {kmax:.3f}, note {r['Kp_max_GFZ']}") if dmin is not None and int(r["Dst_min_nT_OMNI2/Kyoto"]) != dmin: n_attr_dst += 1 out(f" {gap} h, {s}: Dst min differs for {storm[0]}: ours {dmin} (to 24 h after the storm's end), note {r['Dst_min_nT_OMNI2/Kyoto']}") for j, r in enumerate(theirs): if j not in used: n_unmatched += 1 ym = r["start_UT"][:4] + r["start_UT"][5:7] out(f" {gap} h, {s}: NOTE ONLY {r['start_UT']} to {r['end_UT']} ({r['era']}), Kp {r['Kp_max_GFZ']}, Dst {r['Dst_min_nT_OMNI2/Kyoto']}" f"{'' if ym in kinds else ' [month not among ours]'}") out(f" {gap} h summary: unmatched {n_unmatched}; start or end differs {n_time}; hit flags differ {n_flag}; Kp max differs {n_attr_kp}; Dst min differs {n_attr_dst}") with open(os.path.join(outdir, f"our_storms_{gap}h.csv"), "w", encoding="utf-8", newline="\n") as f: f.write("# Our rebuild of the storm lists of S0 News research note 004 (reading A of RULES_storm_lists.txt), " + f"merge gap {gap} h.\n") f.write("# Kp: GFZ Helmholtz Centre for Geosciences, CC BY 4.0. Dst: WDC for Geomagnetism, Kyoto, final and provisional values;\n") f.write("# scientific, non-commercial use, with acknowledgement of the WDC and the observatories that supply its data.\n") w = csv.writer(f, lineterminator="\n") w.writerow(["stratum", "start_UT", "end_UT", "era", "kp_max", "dst_min_nT", "hit_path_a", "hit_path_b", "hit_path_a_jan_excluded"]) w.writerows(rows_out) out("") out("The note's Dst storms whose month is not among our 86: " + (", ".join(sorted({r["start_UT"][:7] for g in note for r in note[g] if r["stratum"].startswith("Dst") and (r["start_UT"][:4] + r["start_UT"][5:7]) not in kinds})) or "none")) with open(os.path.join(outdir, "storm_lists.txt"), "w", encoding="utf-8", newline="\n") as f: f.write("\n".join(lines) + "\n") sys.stdout.reconfigure(encoding="utf-8", newline="\n") print("\n".join(lines)) if __name__ == "__main__": main()