# Hosted copy (Fact-Checked Science, /amundsen-low-solar-cycle/): reads its inputs from the folder given as the # first argument, filled by fetch_inputs.py, and writes its outputs to /out/. Run it with Python 3.10+, # numpy and scipy, from the folder that holds this script, asl_lib.py, surr_lib.py and the RULES files. """05_sam.py — the note's SAM numbers and the SAM-independent part of the ASL. Rules: RULES_sam.txt (hash logged before the first run). Fact-Checked Science, 2026-11-15. Usage: py -3.12 -I 05_sam.py """ import os, sys, json, hashlib here = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, here) import numpy as np import asl_lib as A import surr_lib as S root = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else os.getcwd() dl = root outdir = os.path.join(root, "out") os.makedirs(outdir, exist_ok=True) lines, res = [], {} def say(*a): s = " ".join(str(x) for x in a) print(s, flush=True) lines.append(s) def sha(p): return hashlib.sha256(open(p, "rb").read()).hexdigest() P_V3 = os.path.join(dl, "asli_calculation_latest.csv") P_SN = os.path.join(dl, "SN_m_tot_V2.0.csv") P_AAO = os.path.join(dl, "monthly.aao.index.b79.current.ascii") P_SAM = os.path.join(dl, "newsam.1957.2007.txt") for p in [os.path.join(here, "RULES_sam.txt"), P_V3, P_SN, P_AAO, P_SAM]: say("input", os.path.basename(p), sha(p)) v3, _ = A.load_v3(P_V3) rel = A.load_v3_column(P_V3, "relative_central_pressure (hPa) [b]") act = A.load_v3_column(P_V3, "actual_central_pressure (hPa)") sn, _ = A.load_silso(P_SN) END = A.ym("2026-07") v3, rel, act = (s.restrict(s.start, END) for s in (v3, rel, act)) sn = sn.restrict(sn.start, A.ym("2026-10")) def load_aao(path): d = {} with open(path, encoding="utf-8") as f: for line in f: p = line.split() if len(p) == 3 and p[0].isdigit(): d[int(p[0]) * 12 + int(p[1]) - 1] = float(p[2]) return A._from_dict(d) def load_marshall(path): d = {} with open(path, encoding="utf-8") as f: for line in f: p = line.split() if not p or not p[0].isdigit() or len(p[0]) != 4: continue y = int(p[0]) for k, val in enumerate(p[1:13]): d[y * 12 + k] = float(val) return A._from_dict(d) idx = {"AAO (CPC)": load_aao(P_AAO), "SAM (Marshall)": load_marshall(P_SAM)} for k, s in idx.items(): say(f"{k}: {A.ym_str(s.start)}..{A.ym_str(s.end)}") W0 = A.ym("1979-01") asl = v3.restrict(W0, END) ssn = sn.restrict(W0, A.ym("2026-10")) for name, ix in idx.items(): out = {} ixw = ix.restrict(W0, END) # S1 xa, yi, _ = A.pair(A.cma(asl, 60), A.cma(ixw, 60), 0) out["S1_r"], out["S1_n"] = A.pearson(xa, yi), len(xa) say(f"\n[{name}] S1 60-month CMAs, ASL vs index, lag 0: r = {out['S1_r']:.4f} (n = {len(xa)}); note: -0.883") # S2 for var, ixv in (("to 2026-07", ixw), ("to own end", ix.restrict(W0, ix.end))): xs, ys, _ = A.pair(A.cma(ssn, 60), A.cma(ixv, 60), 33) r_, ne = A.pearson(xs, ys), A.neff_bretherton(xs, ys) out[f"S2_{var}"] = dict(r=r_, n=len(xs), neff=ne, p=A.p_two_sided(r_, ne)) say(f" S2 SSN vs index at 5y/33m ({var}): r = {r_:.4f}, n = {len(xs)}, n_eff = {ne:.2f}, p_eff = {A.p_two_sided(r_, ne):.4f}; note: -0.667") # S3a residual of ASL on index ok = np.isfinite(asl.v) & np.isfinite(ixw.v) b, a = np.polyfit(ixw.v[ok], asl.v[ok], 1) resid = A.Monthly(asl.start, asl.v - (a + b * ixw.v)) r_raw = A.pearson(asl.v[ok], ixw.v[ok]) xs, ys, _ = A.pair(A.cma(ssn, 60), A.cma(resid, 60), 33) r_, ne = A.pearson(xs, ys), A.neff_bretherton(xs, ys) out["S3a"] = dict(slope=b, r_raw_monthly=r_raw, r=r_, n=len(xs), neff=ne, p=A.p_two_sided(r_, ne)) say(f" S3a raw monthly ASL vs index r = {r_raw:.3f} (slope {b:.2f} hPa per unit); SSN vs ASL residual at 5y/33m: r = {r_:.4f}, n = {len(xs)}, n_eff = {ne:.2f}, p_eff = {A.p_two_sided(r_, ne):.4f}") # S4 detrended def detr(s): t = np.arange(len(s.v), dtype=float) bb, aa = np.polyfit(t, s.v, 1) return A.Monthly(s.start, s.v - (aa + bb * t)) xs, ys, _ = A.pair(A.cma(detr(ssn), 60), A.cma(detr(ixw), 60), 33) out["S4_detrended_r"] = A.pearson(xs, ys) tr = np.polyfit(np.arange(len(ixw.v)) / 120.0, ixw.v, 1)[0] out["index_trend_per_decade"] = float(tr) say(f" S4 detrended SSN and index, 5y/33m: r = {out['S4_detrended_r']:.4f} (index trend {tr:+.3f} per decade removed)") res[name] = out # S3b BAS's relative and actual central pressure for nm, s in (("relative central pressure", rel), ("actual central pressure", act), ("sector pressure", v3)): xs, ys, _ = A.pair(A.cma(ssn, 60), A.cma(s.restrict(W0, END), 60), 33) r_, ne = A.pearson(xs, ys), A.neff_bretherton(xs, ys) res["S3b_" + nm] = dict(r=r_, n=len(xs), neff=ne, p=A.p_two_sided(r_, ne)) say(f"\n[S3b {nm}] SSN vs ASL {nm} at 5y/33m: r = {r_:.4f}, n = {len(xs)}, n_eff = {ne:.2f}, p_eff = {A.p_two_sided(r_, ne):.4f}") # S5 surrogates for SSN vs AAO (to 2026-07) aao = idx["AAO (CPC)"].restrict(W0, END) xs, ys, _ = A.pair(A.cma(ssn, 60), A.cma(aao, 60), 33) r_obs = A.pearson(xs, ys) s5 = dict(r=r_obs) for kind, seed in (("B_ssn", 2026111561), ("A_aao", 2026111562)): rng = np.random.default_rng(seed) if kind == "B_ssn": sur = A.phase_surrogates(ssn.v, 10000, rng) R = S.grid_matrix_x_fixed_y(ssn.start, sur, aao, [60], [33])[:, 0, 0] else: sur = A.phase_surrogates(aao.v, 10000, rng) R = S.grid_fixed_x_matrix_y(ssn, aao.start, sur, [60], [33])[:, 0, 0] p = (np.sum(np.abs(R) >= abs(r_obs) - 1e-12) + 1) / 10001 s5[kind] = dict(p_two_sided=float(p), median_abs=float(np.median(np.abs(R))), p95_abs=float(np.percentile(np.abs(R), 95))) say(f"\n[S5 SSN vs AAO r = {r_obs:.4f}] Null {kind} (10,000, seed {seed}): two-sided p = {p:.4f}; |r| median {np.median(np.abs(R)):.3f}, 95th pct {np.percentile(np.abs(R), 95):.3f}") res["S5"] = s5 open(os.path.join(outdir, "05_sam.txt"), "w", encoding="utf-8", newline="\n").write("\n".join(lines) + "\n") json.dump(res, open(os.path.join(outdir, "05_sam.json"), "w", encoding="utf-8", newline="\n"), indent=1)