#!/usr/bin/env python3 """How much oxygen did Earth lose to space in the May 2024 storm, and what is that next to the air? Fact-Checked Science, November 2026, for the paper check "ring-current-oxygen" ("Our analysis"). The rules were written before any number was computed: RULES_oxygen_budget.txt, beside this script. Standard library only. Run it python3 oxygen_budget.py [folder] The folder (default: ./ring-current-oxygen-data) must hold, or will receive, the inputs below. A missing file is downloaded from its maker under a plain client name; every file is then checked against the SHA-256 of the copy we used on November 13, 2026. A different hash means the maker has updated the file, so numbers may differ a little: Scripps adds months, and GFZ's Kp file grows every day (its values for 2016-2025 and May 2024, flagged definitive, do not change). The script prints its log and writes oxygen-budget.json into the folder. Inputs monthly_o2_alt.csv, monthly_o2_ljo.csv, monthly_o2_cgo.csv, monthly_o2_spo.csv Scripps O2 Program, Scripps Institution of Oceanography, UC San Diego: monthly O2/N2 ratios (per meg, SIO2023 scale) at Alert, La Jolla, Cape Grim and the South Pole. CC BY 4.0, credit to Scripps Institution of Oceanography at UC San Diego. https://keelinglabsites.ucsd.edu/websitedatao2/monthly_o2_.csv Kp_ap_Ap_SN_F107_since_1932.txt GFZ Helmholtz Centre for Geosciences: the Kp index (Matzka et al. 2021, doi:10.5880/Kp.0001), CC BY 4.0. https://kp.gfz.de/app/files/Kp_ap_Ap_SN_F107_since_1932.txt What it computes (all as RULES_oxygen_budget.txt fixes them) - The air's O2: 3.706e19 mol (Scripps O2 Program FAQ) x 31.9988 g/mol; 1 per meg = 1e-6 of it. - The measured O2 decline: least-squares slopes of the seasonally adjusted O2/N2 (measured months only) at the four stations over 2016-2025 (headline: their mean) and 1992-2025, and La Jolla over January 1992 to January 2009 (the Scripps FAQ's 19 per meg a year), converted to kg of O2 a year. - O+ escape from Slapak et al. (2017, Ann. Geophys. 35, 721): 3.9e24 exp(0.45 Kp) per second through the plasma mantle, plus a third for the cusp-to-magnetosheath route (this reproduces their 3e26 at Kp 9). A: summed over the 28 three-hour Kp values of 10 May 12 UT to 14 May 00 UT 2024; B: 1e27 per second (their top-20% estimate for Kp 9) for those 3.5 days; C: 1e28 per second for 7 days (a ceiling beyond anything measured); D: the oxygen ions held in the ring current at the storm's peak (Dessler-Parker-Sckopke energy at 400-600 nT, heavy-ion share 0.70-0.90, 30-70 keV per ion, from Kitamura et al. 2026); E: Slapak's rate averaged over every three-hour Kp of 2016-2025; F: Gunell et al.'s (2018) 0.5-2 kg/s; G: Wei et al.'s (2014) 6.5e29 per second, converted. - Each against the air's O2, against 1 per meg, and as the time the measured decline takes to remove as much. - Added after the fresh review, outside the registered rules: D2, the ring current's oxygen with the mean energy per ion a Maxwellian would give, since the paper's central energies are energy-weighted medians. """ import datetime as dt import hashlib import json import math import os import sys import urllib.request UA = "factchecked-research/1.0" RULES_SHA256 = "1fa3b8f0c82aa2afbb12bbd361e93cf434e9a959c9b1f10ec440f991baeb14d8" STATIONS = ["alt", "ljo", "cgo", "spo"] INPUTS = { "monthly_o2_alt.csv": ("https://keelinglabsites.ucsd.edu/websitedatao2/monthly_o2_alt.csv", "0da1663e89058ed6255c38844dc78c26ca3bae76165632da8930eb7681ed4561"), "monthly_o2_ljo.csv": ("https://keelinglabsites.ucsd.edu/websitedatao2/monthly_o2_ljo.csv", "76dfbb54b09b05729b53d0b21aec30bc1e733bf57f15762ae7f949a0d693e769"), "monthly_o2_cgo.csv": ("https://keelinglabsites.ucsd.edu/websitedatao2/monthly_o2_cgo.csv", "74f921ada8095940a39d389875f3301df45b79126d5bf57fe7550be62e593c6d"), "monthly_o2_spo.csv": ("https://keelinglabsites.ucsd.edu/websitedatao2/monthly_o2_spo.csv", "e7dfc16451ce72c6b66776d2f6bda49ca5adefcaf607583db0ccc82fa79fff0d"), "Kp_ap_Ap_SN_F107_since_1932.txt": ("https://kp.gfz.de/app/files/Kp_ap_Ap_SN_F107_since_1932.txt", "cc76277f192590b5918d4148e6d22d73608568032fb84f2bab18fb4268cd377e"), } # Fixed inputs (RULES_oxygen_budget.txt) M_O2_MOL = 3.706e19 # Scripps O2 Program FAQ MW_O2 = 31.9988e-3 # kg/mol DRY_AIR_KG = 5.1352e18 # Trenberth & Smith (2005), cross-check only X_O2 = 0.2095 MW_AIR = 28.9647e-3 U = 1.66054e-27 M_OPLUS = 15.9949 * U # kg per O+ ion SLAPAK_A, SLAPAK_B = 3.9e24, 0.45 ROUTE_FACTOR = 1.0 + 1.0 / 3.0 UPPER_KP9 = 1e27 CEILING = 1e28 YEAR_S = 365.25 * 86400 MU0 = 4e-7 * math.pi B0 = 3.1e-5 RE = 6.371e6 KEV = 1.602176634e-16 WEI_RATE = 6.5e29 def sha256(path): h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def fetch_inputs(folder, say): os.makedirs(folder, exist_ok=True) for name, (url, want) in INPUTS.items(): path = os.path.join(folder, name) if not os.path.exists(path): say("downloading %s" % url) req = urllib.request.Request(url, headers={"User-Agent": UA}) with urllib.request.urlopen(req, timeout=300) as r, open(path, "wb") as f: f.write(r.read()) got = sha256(path) say("%s SHA-256 %s (%s)" % (name, got, "same as the copy we used" if got == want else "DIFFERENT from the copy we used, %s: the maker has updated it" % want[:12])) def phi(kp): return ROUTE_FACTOR * SLAPAK_A * math.exp(SLAPAK_B * kp) def gamma_p(a, x): """Regularized lower incomplete gamma function P(a, x), by its power series.""" term = 1.0 / a total = term n = 0 while term > 1e-16 * total: n += 1 term *= x / (a + n) total += term return total * math.exp(-x + a * math.log(x) - math.lgamma(a)) def maxwell_mean_over_edens_median(): """For a Maxwellian, the energy density is distributed in energy as a gamma distribution of shape 2.5 (in units of kT), and the mean energy per particle is 1.5 kT; return 1.5 over that distribution's median.""" lo, hi = 0.0, 20.0 for _ in range(200): mid = (lo + hi) / 2 if gamma_p(2.5, mid) < 0.5: lo = mid else: hi = mid return 1.5 / ((lo + hi) / 2) def read_scripps(path): rows = [] with open(path, encoding="utf-8", errors="replace") as f: for line in f: parts = [p.strip() for p in line.split(",")] if len(parts) < 10: continue try: yr, mn, dec = int(parts[0]), int(parts[1]), float(parts[3]) except ValueError: continue if parts[4].lower() == "nan" or parts[5].lower() == "nan": continue rows.append((yr, mn, dec, float(parts[5]))) return rows def ols(xs, ys): n = len(xs) mx, my = sum(xs) / n, sum(ys) / n sxx = sum((x - mx) ** 2 for x in xs) b = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sxx a = my - b * mx s2 = sum((y - (a + b * x)) ** 2 for x, y in zip(xs, ys)) / (n - 2) return b, math.sqrt(s2 / sxx), n def read_kp(path): out = {} with open(path, encoding="utf-8") as f: for line in f: if line.startswith("#") or not line.strip(): continue p = line.split() out[dt.date(int(p[0]), int(p[1]), int(p[2]))] = ([float(v) for v in p[7:15]], int(p[-1])) return out def main(): folder = sys.argv[1] if len(sys.argv) > 1 else "ring-current-oxygen-data" log = [] def say(s): log.append(s) print(s) say("oxygen_budget.py, run at %s UTC" % dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%d %H:%M:%S")) rules = os.path.join(os.path.dirname(os.path.abspath(__file__)), "RULES_oxygen_budget.txt") if os.path.exists(rules): got = sha256(rules) say("rules SHA-256 %s (%s)" % (got, "as registered" if got == RULES_SHA256 else "NOT the registered file")) fetch_inputs(folder, say) res = {} o2_kg = M_O2_MOL * MW_O2 per_meg_kg = o2_kg * 1e-6 res["o2_inventory_kg"] = o2_kg res["o2_inventory_check_kg"] = DRY_AIR_KG * X_O2 * MW_O2 / MW_AIR res["one_per_meg_kg"] = per_meg_kg say("the air's O2: %.4g kg (cross-check %.4g kg); 1 per meg = %.3g kg" % (o2_kg, res["o2_inventory_check_kg"], per_meg_kg)) trends = {} for st in STATIONS: rows = read_scripps(os.path.join(folder, "monthly_o2_%s.csv" % st)) t = {} for name, (y0, y1) in {"W1_2016_2025": (2016, 2025), "W2_1992_2025": (1992, 2025)}.items(): sel = [(r[2], r[3]) for r in rows if y0 <= r[0] <= y1] b, se, n = ols([s[0] for s in sel], [s[1] for s in sel]) t[name] = {"slope_per_meg_per_yr": b, "se": se, "n_months": n} sel = [(r[2], r[3]) for r in rows if (1992, 1) <= (r[0], r[1]) <= (2009, 1)] b, se, n = ols([s[0] for s in sel], [s[1] for s in sel]) t["FAQ_1992_2009"] = {"slope_per_meg_per_yr": b, "se": se, "n_months": n} trends[st] = t say("%s: 2016-2025 %.2f +/- %.2f per meg a year (%d months); 1992-2025 %.2f; Jan 1992-Jan 2009 %.2f" % ( st, t["W1_2016_2025"]["slope_per_meg_per_yr"], t["W1_2016_2025"]["se"], t["W1_2016_2025"]["n_months"], t["W2_1992_2025"]["slope_per_meg_per_yr"], t["FAQ_1992_2009"]["slope_per_meg_per_yr"])) head = sum(trends[s]["W1_2016_2025"]["slope_per_meg_per_yr"] for s in STATIONS) / 4 head2 = sum(trends[s]["W2_1992_2025"]["slope_per_meg_per_yr"] for s in STATIONS) / 4 decline_kg = abs(head) * 1e-6 * M_O2_MOL * MW_O2 res.update({"scripps_trends": trends, "decline_per_meg_per_yr_W1": head, "decline_kg_per_yr_W1": decline_kg, "decline_per_meg_per_yr_W2": head2, "decline_kg_per_yr_W2": abs(head2) * 1e-6 * M_O2_MOL * MW_O2}) say("measured decline, 2016-2025: %.2f per meg a year = %.3g kg of O2 a year (1992-2025: %.2f)" % (head, decline_kg, head2)) kp = read_kp(os.path.join(folder, "Kp_ap_Ap_SN_F107_since_1932.txt")) start, end = dt.datetime(2024, 5, 10, 12), dt.datetime(2024, 5, 14, 0) intervals, t0 = [], start while t0 < end: kps, flag = kp[t0.date()] intervals.append((t0.isoformat(), kps[t0.hour // 3], flag)) t0 += dt.timedelta(hours=3) say("storm window: %d three-hour intervals, Kp max %.3f, definitive flags %s" % ( len(intervals), max(i[1] for i in intervals), sorted(set(i[2] for i in intervals)))) dur = len(intervals) * 10800 A = sum(phi(i[1]) * 10800 for i in intervals) * M_OPLUS B = UPPER_KP9 * dur * M_OPLUS C = CEILING * 7 * 86400 * M_OPLUS res["storm_window"] = {"start": start.isoformat(), "end": end.isoformat(), "intervals": intervals} e_m = (4 * math.pi / (3 * MU0)) * B0 ** 2 * RE ** 3 corners = [] for db in (400e-9, 600e-9): for f in (0.70, 0.90): for e_kev in (30.0, 70.0): corners.append(f * (1.5 * db / B0 * e_m) / (e_kev * KEV) * M_OPLUS) d_mid = 0.85 * (1.5 * 518e-9 / B0 * e_m) / (50 * KEV) * M_OPLUS res["D_inventory"] = {"E_M_J": e_m, "E_RC_518nT_J": 1.5 * 518e-9 / B0 * e_m, "min_kg": min(corners), "max_kg": max(corners), "mid_kg": d_mid} def compare(label, kg): secs = kg / decline_kg * YEAR_S say("%s: %.3g kg = %.2g of the air's O2; %.2g times below 1 per meg; as much as the measured decline removes in %.2g s" % ( label, kg, kg / o2_kg, per_meg_kg / kg, secs)) return {"kg": kg, "fraction_of_O2": kg / o2_kg, "per_meg": kg / o2_kg * 1e6, "below_1_per_meg": kg < per_meg_kg, "factor_below_1_per_meg": per_meg_kg / kg, "seconds_of_measured_decline": secs} res["A_central"] = compare("A, central (Slapak fit over the storm's Kp)", A) res["A_central"]["mean_rate_ions_per_s"] = A / M_OPLUS / dur res["B_generous"] = compare("B, generous (1e27 per second for 3.5 days)", B) res["C_ceiling"] = compare("C, ceiling (1e28 per second for 7 days)", C) res["D_min"] = compare("D, ring current at the peak, low corner", min(corners)) res["D_max"] = compare("D, ring current at the peak, high corner", max(corners)) # Added after the fresh review, outside the registered rules: the paper's central energies are medians of the # energy-density distribution, so the mean energy per ion is lower and D's count is a lower estimate. D2 repeats # D's corners with the mean energy a Maxwellian would give (1.5 kT over the median of its energy density). ratio = maxwell_mean_over_edens_median() corners2 = [c / ratio for c in corners] res["D2_inventory_mean_energy"] = {"mean_energy_over_central_energy": ratio, "min_kg": min(corners2), "max_kg": max(corners2), "reported_range_kg": [min(corners), max(corners2)]} say("D2 (added after the fresh review): mean energy per ion %.3f of the central energy; ring current %.3g to %.3g kg; range reported %.3g to %.3g kg" % ( ratio, min(corners2), max(corners2), min(corners), max(corners2))) res["D2_max"] = compare("D2, ring current at the peak, high corner with the Maxwellian mean energy", max(corners2)) rates = [phi(v) for day, (kps, flag) in kp.items() if dt.date(2016, 1, 1) <= day <= dt.date(2025, 12, 31) for v in kps if v >= 0] mean_rate = sum(rates) / len(rates) e_kg = mean_rate * YEAR_S * M_OPLUS res["E_annual"] = {"mean_rate_ions_per_s": mean_rate, "n_intervals": len(rates), "kg_per_yr": e_kg, "ratio_to_decline": e_kg / decline_kg, "years_to_lose_1pct": 0.01 * o2_kg / e_kg} res["F_gunell"] = {"kg_per_yr_low": 0.5 * YEAR_S, "kg_per_yr_high": 2.0 * YEAR_S, "ratio_to_decline_low": 0.5 * YEAR_S / decline_kg, "ratio_to_decline_high": 2.0 * YEAR_S / decline_kg} say("E, escape in a typical year (2016-2025): %.3g kg = %.2g of the measured decline; 1%% of the air's O2 would take %.2g years" % ( e_kg, e_kg / decline_kg, 0.01 * o2_kg / e_kg)) say("F, Gunell et al.'s 0.5-2 kg/s: %.3g to %.3g kg a year" % (0.5 * YEAR_S, 2.0 * YEAR_S)) for lab, r in (("outflow 1.5e26 per second (Yau et al. 1985)", 1.5e26), ("outflow 6e26 per second (Wei et al. 2014, citing Yau & Andre 1997)", 6e26)): v = r * YEAR_S * M_OPLUS res.setdefault("outflow_if_all_escaped", {})[lab] = {"kg_per_yr": v, "ratio_to_decline": v / decline_kg} say("%s, as if all of it escaped: %.3g kg a year = %.2g of the measured decline" % (lab, v, v / decline_kg)) g_yr = WEI_RATE * YEAR_S * M_OPLUS res["G_wei2014"] = {"kg_per_yr": g_yr, "ratio_to_decline": g_yr / decline_kg, "kg_per_1e4_yr": g_yr * 1e4, "fraction_of_O2_per_reversal": g_yr * 1e4 / o2_kg} say("G, Wei et al.'s 6.5e29 per second: %.3g kg a year = %.2g of the measured decline; over 1e4 years %.2g of the air's O2" % ( g_yr, g_yr / decline_kg, g_yr * 1e4 / o2_kg)) res["decision_1"] = "C below 1 per meg" if C < per_meg_kg else ("only C reaches 1 per meg" if B < per_meg_kg else "B exceeds 1 per meg") res["decision_2_E_and_F_below_1pct"] = (e_kg < 0.01 * decline_kg) and (2.0 * YEAR_S < 0.01 * decline_kg) say("decision 1: %s; decision 2 (E and F below 1%% of the measured decline): %s" % (res["decision_1"], res["decision_2_E_and_F_below_1pct"])) out = os.path.join(folder, "oxygen-budget.json") with open(out, "w", encoding="utf-8", newline="\n") as f: json.dump(res, f, indent=1) f.write("\n") say("wrote %s" % out) if __name__ == "__main__": main()