"""RULES_storms.txt: GFZ and NOAA SWPC records for 2026-10-28 .. 2026-11-06. Reads downloads as data.""" import sys, json, re, csv, io, hashlib, os sys.stdout.reconfigure(encoding="utf-8") D = sys.argv[1] # downloads folder (Windows path) OUT = sys.argv[2] # results file SCR = sys.argv[3] # coordinator's nowcast copy days = ["2026-10-%02d" % d for d in range(28, 32)] + ["2026-11-%02d" % d for d in range(1, 7)] lines = [] def p(*a): s = " ".join(str(x) for x in a) lines.append(s) print(s) def sha(path): return hashlib.sha256(open(path, "rb").read()).hexdigest() def thirds(k): # GFZ notation: 5.667 -> 6-, 5.0 -> 5o, 5.333 -> 5+ n = round(k * 3) base, r = divmod(n, 3) return {0: "%do" % base, 1: "%d+" % base, 2: "%d-" % (base + 1)}[r] def g_thirds(k): # N- counts as N for g, t in ((5, 8.667), (4, 7.667), (3, 6.667), (2, 5.667), (1, 4.667)): if k >= t - 1e-6: return "G%d" % g return "-" def g_integer(k): for g, t in ((5, 9.0), (4, 8.0), (3, 7.0), (2, 6.0), (1, 5.0)): if k >= t - 1e-6: return "G%d" % g return "-" # 1. GFZ nowcast (3-hourly lines) def read_nowcast(path): out = {} for ln in open(path, "rb").read().decode("utf-8").splitlines(): if ln.startswith("#") or not ln.strip(): continue f = ln.split() day = "%s-%s-%s" % (f[0], f[1], f[2]) out.setdefault(day, []).append((float(f[3]), float(f[7]), int(f[8]), int(f[9]))) return out now = read_nowcast(os.path.join(D, "gfz", "Kp_ap_nowcast.txt")) coord = read_nowcast(SCR) p("GFZ Kp_ap_nowcast.txt sha256", sha(os.path.join(D, "gfz", "Kp_ap_nowcast.txt")), "; coordinator copy", sha(SCR)) p("D flag per file header: D = 1 definitive, D = 0 preliminary") p("day | eight 3-h Kp (GFZ nowcast) | max notation | G thirds | G integer | D flags | coordinator copy max") for d in days: v = now.get(d, []) ks = [k for _, k, _, _ in v] valid = [k for k in ks if k >= 0] mx = max(valid) if valid else None cv = [k for _, k, _, _ in coord.get(d, []) if k >= 0] p(d, "|", " ".join("%5.3f" % k for k in ks), "|", mx, thirds(mx) if mx is not None else "", "|", g_thirds(mx) if mx is not None else "", "|", g_integer(mx) if mx is not None else "", "|", sorted(set(x[3] for x in v)), "|", max(cv) if cv else None) # GFZ main file (daily lines) p("") p("GFZ Kp_ap_Ap_SN_F107_since_1932.txt sha256", sha(os.path.join(D, "gfz", "Kp_ap_Ap_SN_F107_since_1932.txt"))) main = {} for ln in open(os.path.join(D, "gfz", "Kp_ap_Ap_SN_F107_since_1932.txt"), "rb").read().decode("utf-8").splitlines(): if ln.startswith("#"): continue f = ln.split() day = "%s-%s-%s" % (f[0], f[1], f[2]) if day in days: ks = [float(x) for x in f[7:15]] main[day] = (ks, int(f[23]), f[-1]) for d in days: if d in main: ks, Ap, flag = main[d] p(d, "| main file Kp:", " ".join("%5.3f" % k for k in ks), "| max", max(ks), thirds(max(ks)), "| Ap", Ap, "| D", flag) # note 003's own daily_ee_kp.csv p("") rows = list(csv.DictReader(io.StringIO(open(os.path.join(D, "drive003", "daily_ee_kp.csv"), "rb").read().decode("utf-8")))) bad = [r["date_utc"] for r in rows if (float(r["kp_max"]) >= 6.0 - 1e-9) != (r["Kp6_storm_day"] == "1")] p("daily_ee_kp.csv: days where Kp6_storm_day disagrees with kp_max >= 6.0:", len(bad), bad[:5]) bad2 = [r["date_utc"] for r in rows if (float(r["kp_max"]) >= 5.667 - 1e-9) != (r["Kp6_storm_day"] == "1")] p("daily_ee_kp.csv: days where Kp6_storm_day disagrees with kp_max >= 5.667:", len(bad2)) n6m = sum(1 for r in rows if 5.667 - 1e-9 <= float(r["kp_max"]) < 6.0 - 1e-9) p("days with kp_max = 5.667 exactly (6-) in daily_ee_kp.csv:", n6m) for r in rows: if r["date_utc"] in days: p(r["date_utc"], "note003 kp_max", r["kp_max"], "Kp6_storm_day", r["Kp6_storm_day"], "G3plus_peak_day", r["G3plus_peak_day"]) # 2. NOAA DGD p("") p("NOAA SWPC daily-geomagnetic-indices.txt sha256", sha(os.path.join(D, "swpc", "daily-geomagnetic-indices.txt"))) for ln in open(os.path.join(D, "swpc", "daily-geomagnetic-indices.txt"), "rb").read().decode("utf-8").splitlines(): m = re.match(r"^(\d{4}) (\d\d) (\d\d)\s+(.*)$", ln) if not m: continue day = "%s-%s-%s" % m.group(1, 2, 3) if day not in days: continue f = m.group(4).split() # Planetary A + 8 K are the last nine tokens (always blank-separated); station columns can fuse ("-1-1-1") pl = f[-9:] pk = [float(x) for x in pl[1:] if float(x) >= 0] st = "" if len(f) == 27: st = "| Fredericksburg max K %d | College max K %d" % (max(int(x) for x in f[1:9]), max(int(x) for x in f[10:18])) else: st = "| station columns partly missing (-1)" p(day, "| Planetary A", pl[0], "| K", " ".join(pl[1:]), "| max", max(pk), thirds(max(pk)), g_thirds(max(pk)), g_integer(max(pk)), st) # 3. NOAA planetary K json p("") p("NOAA noaa-planetary-k-index.json sha256", sha(os.path.join(D, "swpc", "noaa-planetary-k-index.json"))) j = json.load(open(os.path.join(D, "swpc", "noaa-planetary-k-index.json"), "rb")) byday = {} for e in j: byday.setdefault(e["time_tag"][:10], []).append(e["Kp"]) for d in days: if d in byday: p(d, "| json Kp", byday[d], "| max", max(byday[d])) # 4. NOAA alerts p("") p("NOAA alerts.json sha256", sha(os.path.join(D, "swpc", "alerts.json"))) a = json.load(open(os.path.join(D, "swpc", "alerts.json"), "rb")) for e in sorted(a, key=lambda e: e["issue_datetime"]): t = e["issue_datetime"] if not ("2026-10-29" <= t[:10] <= "2026-11-06"): continue msg = e["message"] if not re.search(r"K-index|Geomagnetic|G[1-5]", msg): continue head = [l.strip() for l in msg.replace("\r", "").split("\n") if l.strip()] keep = [l for l in head if re.match(r"(ALERT|WARNING|WATCH|SUMMARY|EXTENDED|CANCEL|Threshold|Valid|Observed|Highest|NOAA Scale|Synoptic|Begin|End|Station|Now Valid|Original|Comment)", l)] p("---", e["product_id"], t) for l in keep[:14]: p(" ", l) open(OUT, "w", encoding="utf-8", newline="\n").write("\n".join(lines) + "\n")