"""amounts.py -- RULES_amounts.txt. Standard library only. python3 -I amounts.py """ import hashlib import json import math import os import sys dig_path, rco_path, scripps_dir, rules_path, out_dir = sys.argv[1:6] U = 1.66053907e-27 M_OP = 16 * U # kg per O+ ion (16 u, as the brief says) YR = 365.25 * 86400 DUR = {"L1 700 yr (Laschamp main phase)": 700, "L2 2,000 yr (Laschamp low)": 2000, "V1 5,000 yr (a reversal, several thousand years)": 5000, "V2 10,000 yr (Wei et al. 2014's duration)": 10000, "V3 30,000 yr (Matuyama-Brunhes dipole decay)": 30000} def sha(p): with open(p, "rb") as f: return hashlib.sha256(f.read()).hexdigest() dig = json.load(open(dig_path, encoding="utf-8")) rco = json.load(open(rco_path, encoding="utf-8")) f4 = {r["nominal"]: r["y_mean"] for r in dig["points"]["fig4"]} f5 = {r["nominal"]: r["y_mean"] for r in dig["points"]["fig5"]} blue = dig["derived"]["path_blue"] O2 = rco["o2_inventory_kg"] DECLINE = rco["decline_kg_per_yr_W1"] SLAPAK_MEAN = rco["E_annual"]["mean_rate_ions_per_s"] WEI = 6.5e29 # ---------------------------------------------------------------- Scripps check (the ring-current page's rules) def read_scripps(path): xs, ys = [], [] with open(path, encoding="utf-8", errors="replace") as f: for ln in f: p = [c.strip() for c in ln.split(",")] if len(p) < 10 or not p[0].isdigit(): continue try: yr, mn, date, o2, sa = int(p[0]), int(p[1]), float(p[3]), float(p[4]), float(p[5]) except ValueError: continue if math.isnan(o2) or math.isnan(sa): continue if 2016 <= yr <= 2025: xs.append(date) ys.append(sa) return xs, ys def ols(xs, ys): n = len(xs) mx, my = sum(xs) / n, sum(ys) / n sxx = sum((x - mx) ** 2 for x in xs) return sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / sxx, n scripps = {} for st in ("alt", "ljo", "cgo", "spo"): p = os.path.join(scripps_dir, "monthly_o2_%s.csv" % st) xs, ys = read_scripps(p) slope, n = ols(xs, ys) scripps[st] = {"slope_per_meg_per_yr": slope, "n_months": n, "sha256": sha(p)} mean_slope = sum(v["slope_per_meg_per_yr"] for v in scripps.values()) / 4 mine_kg = abs(mean_slope) * 1e-6 * 3.706e19 * 0.0319988 scripps_check = {"stations": scripps, "mean_slope_per_meg_per_yr": mean_slope, "kg_per_yr": mine_kg, "page_value_kg_per_yr": DECLINE, "relative_difference": mine_kg / DECLINE - 1, "within_1pct": abs(mine_kg / DECLINE - 1) <= 0.01} # ---------------------------------------------------------------- rates rates = { "R_now (text 5.76e21; D=1, 0 deg)": 5.76e21, "R_now digitized (Fig. 4 D=1)": f4["D=1"], "R_tilt min digitized (Fig. 5, 0 deg)": min(f5.values()), "R_tilt max digitized (Fig. 5, 150 deg)": max(f5.values()), "R_0.1 text (~1e24; D=0.1, 0 deg)": 1e24, "R_0.1 digitized (Fig. 4 D=0.1)": f4["D=0.1"], "R_rev text (~2e23; 0.1D, 90 deg)": 2e23, "R_rev digitized (Fig. 6 blue 90 deg)": blue["tilt 90"], "R_path digitized (mean of Fig. 6 blue squares)": sum(blue.values()) / 4, "R_sat text low (1e25)": 1e25, "R_sat text high (1e26)": 1e26, "R_sat digitized max (Fig. 4 D<=1e-2)": max(f4["D=1e-4"], f4["D=1e-3"], f4["D=1e-2"]), "R_10 digitized (Fig. 4 D=10)": f4["D=10"], "measured: Slapak et al. 2017, 2016-2025 Kp mean (as on /ring-current-oxygen/)": SLAPAK_MEAN, "measured: Gunell et al. 2018, 0.5 kg/s": 0.5 / M_OP, "measured: Gunell et al. 2018, 2 kg/s": 2.0 / M_OP, "measured: Seki et al. 2001 upper limit, as Wang et al. report it (~5e24)": 5e24, "measured: Seki et al. 2001 abstract, 2% of O2 over 3 Gyr (our conversion)": 0.02 * O2 / (3e9 * YR) / M_OP, "outflow: Seki et al. 2001 abstract, 18% of O2 over 3 Gyr (our conversion)": 0.18 * O2 / (3e9 * YR) / M_OP, "measured: Schillings et al. 2019, lowest dynamic pressure subset (10^24.6)": 10 ** 24.6, "measured: Schillings et al. 2019, 8.4 nPa (1.36e26)": 1.36e26, "measured: Slapak et al. 2017, most extreme storms (3e26)": 3e26, "for scale: Wei et al. 2014's required rate (6.5e29)": WEI, } rate_table = {k: {"ions_per_s": v, "kg_per_s": v * M_OP, "tonnes_per_yr": v * M_OP * YR / 1000, "ratio_to_model_now": v / 5.76e21, "ratio_to_slapak_mean": v / SLAPAK_MEAN} for k, v in rates.items()} SCEN = { "P_max text (2e23)": 2e23, "P_max digitized": blue["tilt 90"], "P_mean digitized": sum(blue.values()) / 4, "F4 text (1e24)": 1e24, "F4 digitized": f4["D=0.1"], "SAT 1e25": 1e25, "SAT 1e26": 1e26, "NOW model (5.76e21)": 5.76e21, "NOW measured (Slapak mean)": SLAPAK_MEAN, "NOW measured (Gunell 2 kg/s)": 2.0 / M_OP, "SCALED P_max (extreme reading)": SLAPAK_MEAN * blue["tilt 90"] / 5.76e21, "SCALED F4 (extreme reading)": SLAPAK_MEAN * f4["D=0.1"] / 5.76e21, "SCALED SAT 1e26 (extreme reading)": SLAPAK_MEAN * 1e26 / 5.76e21, "for scale: Wei et al. 2014 (6.5e29)": WEI, } def fmt_time(days): return ("%.2g days" % days) if days < 365 else ("%.2g years" % (days / 365.25)) table = {} for s, r in SCEN.items(): table[s] = {} for d, years in DUR.items(): kg = r * M_OP * years * YR days = kg / DECLINE * 365.25 table[s][d] = {"kg": kg, "tonnes": kg / 1000, "fraction_of_O2": kg / O2, "per_meg": kg / O2 * 1e6, "decline_days": days, "decline_time": fmt_time(days), "multiple_of_measured_escape_today": r / SLAPAK_MEAN} decisions = { "1: SAT 1e26 for 10,000 yr below 0.1% of O2": table["SAT 1e26"]["V2 10,000 yr (Wei et al. 2014's duration)"]["fraction_of_O2"] < 1e-3, "2: P_max (digitized) for 5,000 yr below one year of today's decline": table["P_max digitized"]["V1 5,000 yr (a reversal, several thousand years)"]["decline_days"] < 365.25, "2b: same with the text's 2e23": table["P_max text (2e23)"]["V1 5,000 yr (a reversal, several thousand years)"]["decline_days"] < 365.25, "3: R_now below every measured present-day rate by more than 100x": all(5.76e21 * 100 < v for k, v in rates.items() if k.startswith("measured:")), "3 detail: smallest measured / model": min(v for k, v in rates.items() if k.startswith("measured:")) / 5.76e21, } context = {"scripps_faq_industrial_era_to_2005_per_meg": 972, "scripps_faq_note": "the FAQ: 'a loss of roughly 0.1 % of the O2 content' to 2005"} res = {"inputs_sha256": {"digitize.json": sha(dig_path), "oxygen-budget.json (ring-current-oxygen)": sha(rco_path), "RULES_amounts.txt": sha(rules_path)}, "constants": {"m_O+_kg": M_OP, "year_s": YR, "O2_kg": O2, "decline_kg_per_yr": DECLINE, "slapak_mean_ions_per_s": SLAPAK_MEAN}, "scripps_check": scripps_check, "rates": rate_table, "scenarios": table, "decisions": decisions, "context": context} with open(os.path.join(out_dir, "amounts.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(res, f, indent=1) lines = ["amounts.py log", json.dumps(res["inputs_sha256"], indent=1), json.dumps(res["constants"], indent=1), "Scripps check: mean W1 slope %.3f per meg/yr -> %.4g kg/yr; page %.4g; difference %.3f%%" % ( mean_slope, mine_kg, DECLINE, 100 * (mine_kg / DECLINE - 1)), "", "RATES"] for k, v in rate_table.items(): lines.append(" %-80s %9.3g /s %9.3g kg/s %9.3g t/yr x%-9.3g of model-now x%.3g of Slapak mean" % ( k, v["ions_per_s"], v["kg_per_s"], v["tonnes_per_yr"], v["ratio_to_model_now"], v["ratio_to_slapak_mean"])) lines.append("") lines.append("SCENARIOS (rate held for the whole duration)") for s, row in table.items(): for d, v in row.items(): lines.append(" %-34s %-50s %9.2g t %9.2g of O2 (%8.2g per meg) = %-12s of today's decline" % ( s, d, v["tonnes"], v["fraction_of_O2"], v["per_meg"], v["decline_time"])) lines.append("") lines.append("DECISIONS " + json.dumps(decisions, indent=1)) with open(os.path.join(out_dir, "amounts_log.txt"), "w", encoding="utf-8", newline="\n") as f: f.write("\n".join(lines) + "\n") print("\n".join(lines))