"""kp_ratio.py FOLDER -- RULES_kp_ratio.txt: is a Kp 8+ storm "1,000 to 10,000 times more powerful" than Kp 5? Fetches NOAA NCEI's page with the standard Kp-to-ap table under a plain client name, checks it against the copy we used (SHA-256 below), reads the table and computes the ratios fixed in RULES_kp_ratio.txt. ap is the 3-hour equivalent range of the disturbance, in units of 2 nT. Standard library only. After the registered test it prints one check the rules did not fix, added after a fresh review on 2026-11-13: the same ratios with "Kp 5" read as 5- (4 2/3 on NCEI's scale of thirds), the lowest reading of Kp 5. Usage: python3 -I kp_ratio.py (the output should match kp_ratio.txt) """ import hashlib import html import re import sys import urllib.request from pathlib import Path URL = "https://www.ngdc.noaa.gov/stp/GEOMAG/kp_ap.html" OUR_SHA256 = "dccec2ec128af92915fd6194bbfea84c4459ae312216f06afc6b6e443bffa1fd" # fetched 2026-11-13 16:14 UTC def main(folder: Path) -> None: folder.mkdir(parents=True, exist_ok=True) req = urllib.request.Request(URL, headers={"User-Agent": "factchecked-research/1.0"}) with urllib.request.urlopen(req, timeout=60) as r: data = r.read() (folder / "kp_ap.html").write_bytes(data) sha = hashlib.sha256(data).hexdigest() print(f"fetched {len(data):,} bytes; SHA-256 {sha}") print("same file as ours" if sha == OUR_SHA256 else "DIFFERENT from the copy we used: check the table by eye") text = html.unescape(re.sub(r"<[^>]+>", " ", data.decode("utf-8", "replace"))) table = {} for kps, aps in re.findall(r"Kp =([^\n]+)\n\s*ap =([^\n]+)", text): for k, a in zip(kps.split(), aps.split()): table[k] = int(a) print("Kp->ap table read:", table) ap5, ap8p, ap9m, ap9 = table["5o"], table["8+"], table["9-"], table["9o"] r1 = {"8+/5o": ap8p / ap5, "9-/5o": ap9m / ap5, "9o/5o": ap9 / ap5} r2 = {k: v ** 2 for k, v in r1.items()} r3 = 400 / 50 # |Dst| of May 2024 (about -400 nT, Kyoto provisional) over the moderate-storm threshold (-50 nT) print("R1 amplitude ratios (ap):", {k: round(v, 2) for k, v in r1.items()}) print("R2 energy-like ratios (ap squared):", {k: round(v, 1) for k, v in r2.items()}) print("R3 ring-current energy ratio (|Dst| 400/50):", r3) allr = list(r1.values()) + list(r2.values()) + [r3] if max(allr) >= 1000: decision = "holds" elif max(allr) < 100: decision = "contradicted" else: decision = "overstated" print("largest ratio:", round(max(allr), 1), "-> decision:", decision) # Not registered: added after the fresh review. NCEI's page gives Kp in thirds ("5- is 4 2/3"), so a storm # called "Kp 5" can be 5- as well as 5o. Same ratios and the same decision rule, with Kp 5 read as 5-. ap5m = table["5-"] s1 = {"8+/5-": ap8p / ap5m, "9-/5-": ap9m / ap5m, "9o/5-": ap9 / ap5m} s2 = {k: v ** 2 for k, v in s1.items()} print("Not registered: Kp 5 read as 5- (ap %d)" % ap5m) print(" R1 amplitude ratios (ap):", {k: round(v, 2) for k, v in s1.items()}) print(" R2 energy-like ratios (ap squared):", {k: round(v, 1) for k, v in s2.items()}) alls = list(s1.values()) + list(s2.values()) + [r3] smax = max(alls) sdec = "holds" if smax >= 1000 else ("contradicted" if smax < 100 else "overstated") print(" largest ratio:", round(smax, 1), "-> the same rule would give:", sdec) print(" the glossary's 1,000-10,000 is %.0f to %.0f times this largest ratio" % (1000 / smax, 10000 / smax)) if __name__ == "__main__": if len(sys.argv) != 2: sys.exit(__doc__) main(Path(sys.argv[1]))