# 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. """04_signflip.py — the pre-1979 "sign flip", rolling 11-year r under the null, a station-based check, the field. Rules: RULES_signflip.txt (hash logged before the first run). Fact-Checked Science, 2026-11-15. Usage: py -3.12 -I 04_signflip.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_SAM = os.path.join(dl, "newsam.1957.2007.txt") P_FG = os.path.join(root, "magnetic-field-guide-numbers.json") for p in [os.path.join(here, "RULES_signflip.txt"), P_V3, P_SN, P_SAM, P_FG]: say("input", os.path.basename(p), sha(p)) v3, _ = A.load_v3(P_V3) sn, _ = A.load_silso(P_SN) v3 = v3.restrict(v3.start, A.ym("2026-07")) sn = sn.restrict(sn.start, A.ym("2026-10")) 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) sam = load_marshall(P_SAM) say("Marshall SAM coverage:", A.ym_str(sam.start), "to", A.ym_str(sam.end), "missing:", int(np.isnan(sam.v).sum())) # F1 pre-1979 significance asl_pre = v3.restrict(A.ym("1959-01"), A.ym("1978-12")) ssn_pre = sn.restrict(A.ym("1959-01"), A.ym("1978-12")) x, y, _ = A.pair(A.cma(ssn_pre, 60), A.cma(asl_pre, 60), 33) r_obs = A.pearson(x, y) Robs = S.grid_fixed_x_matrix_y(ssn_pre, asl_pre.start, asl_pre.v[None, :], [60], A.LAGS)[0, 0] s2abs_obs = float(np.nanmax(np.abs(Robs))) say(f"\n[F1 pre-1979] r(5y/33m) = {r_obs:.4f}; max |r| over lags 0..48 = {s2abs_obs:.4f} at lag {int(np.nanargmax(np.abs(Robs)))}") f1 = dict(r=r_obs, s2abs=s2abs_obs) for kind, seed in (("A", 2026111551), ("B", 2026111552)): rng = np.random.default_rng(seed) if kind == "A": sur = A.phase_surrogates(asl_pre.v, 10000, rng) R = S.grid_fixed_x_matrix_y(ssn_pre, asl_pre.start, sur, [60], A.LAGS)[:, 0, :] else: sur = A.phase_surrogates(ssn_pre.v, 10000, rng) R = S.grid_matrix_x_fixed_y(ssn_pre.start, sur, asl_pre, [60], A.LAGS)[:, 0, :] p1 = (np.sum(np.abs(R[:, 33]) >= abs(r_obs) - 1e-12) + 1) / 10001 p2 = (np.sum(np.nanmax(np.abs(R), axis=1) >= s2abs_obs - 1e-12) + 1) / 10001 f1[f"null{kind}"] = dict(p_fixed_two_sided=float(p1), p_49lag_abs=float(p2), median_abs_r33=float(np.median(np.abs(R[:, 33]))), p95_abs_r33=float(np.percentile(np.abs(R[:, 33]), 95))) say(f" Null {kind} (10,000, seed {seed}): two-sided p(fixed 5y/33m) = {p1:.4f}; p(49-lag max |r|) = {p2:.4f}; " f"|r| at 5y/33m: median {np.median(np.abs(R[:, 33])):.3f}, 95th pct {np.percentile(np.abs(R[:, 33]), 95):.3f}") res["F1"] = f1 # F2 rolling r under the null asl_f = v3.restrict(A.ym("1959-01"), A.ym("2026-07")) ssn_f = sn.restrict(A.ym("1959-01"), A.ym("2026-10")) xo, yo, to = A.pair(A.cma(ssn_f, 60), A.cma(asl_f, 60), 33) ro = S.rolling_r((xo - xo.mean())[None, :], (yo - yo.mean())[None, :], 132)[0] obs_range = float(ro.max() - ro.min()) obs_signch = int(np.sum(np.diff(np.sign(ro)) != 0)) say(f"\n[F2 rolling 132-pair r, full record] observed: min {ro.min():.3f}, max {ro.max():.3f}, range {obs_range:.3f}, sign changes {obs_signch}, windows {len(ro)}") f2 = dict(obs_min=float(ro.min()), obs_max=float(ro.max()), obs_range=obs_range, obs_sign_changes=obs_signch) for kind, seed in (("A", 2026111553), ("B", 2026111554)): rng = np.random.default_rng(seed) ranges, both, sch = [], 0, [] for b0 in range(0, 10000, 1000): if kind == "A": sur = A.phase_surrogates(asl_f.v, 1000, rng) xp, Yp, tp = S.paired_matrix_y(ssn_f, asl_f.start, sur, 60, 33) Xp = np.repeat(xp[None, :], len(Yp), axis=0) else: sur = A.phase_surrogates(ssn_f.v, 1000, rng) Xp, yp, tp = S.paired_matrix_x(ssn_f.start, sur, asl_f, 60, 33) Yp = np.repeat(yp[None, :], len(Xp), axis=0) assert len(tp) == len(to) Rr = S.rolling_r(Xp - Xp.mean(axis=1, keepdims=True), Yp - Yp.mean(axis=1, keepdims=True), 132) ranges.append(Rr.max(axis=1) - Rr.min(axis=1)) both += int(np.sum((Rr.min(axis=1) <= -0.80) & (Rr.max(axis=1) >= 0.95))) sch.append(np.sum(np.diff(np.sign(Rr), axis=1) != 0, axis=1)) ranges = np.concatenate(ranges) sch = np.concatenate(sch) f2[f"null{kind}"] = dict(frac_range_ge=float(np.mean(ranges >= obs_range - 1e-12)), frac_both_extremes=both / 10000, median_range=float(np.median(ranges)), median_sign_changes=float(np.median(sch)), frac_sign_changes_le_obs=float(np.mean(sch <= obs_signch))) say(f" Null {kind} (10,000, seed {seed}): fraction with range >= observed {np.mean(ranges >= obs_range - 1e-12):.4f}; " f"with both <= -0.80 and >= +0.95: {both / 10000:.4f}; median range {np.median(ranges):.3f}; " f"median sign changes {np.median(sch):.0f} (fraction <= observed {np.mean(sch <= obs_signch):.3f})") res["F2"] = f2 # F3 station-based SAM def era(a0, a1, s0=None, s1=None): s0 = a0 if s0 is None else s0 s1 = a1 if s1 is None else s1 return a0, a1, s0, s1 f3 = {} for name, (a0, a1) in {"1959-1978": (A.ym("1959-01"), A.ym("1978-12")), "1979-2026": (A.ym("1979-01"), A.ym("2026-07"))}.items(): asl_e = v3.restrict(a0, a1) sam_e = sam.restrict(a0, a1) xa, ys, _ = A.pair(A.cma(asl_e, 60), A.cma(sam_e, 60), 0) r_c = A.pearson(xa, ys) raw_ok = np.isfinite(sam_e.v) & np.isfinite(asl_e.v[:len(sam_e.v)]) r_raw = A.pearson(asl_e.v[:len(sam_e.v)][raw_ok], sam_e.v[raw_ok]) f3[name] = dict(r_asl_sam_cma=r_c, n_cma=len(xa), r_asl_sam_raw=r_raw, n_raw=int(raw_ok.sum())) say(f"\n[F3a {name}] ERA5 ASL sector pressure vs station SAM: 5-year CMAs r = {r_c:.3f} (n = {len(xa)}); raw monthly r = {r_raw:.3f} (n = {int(raw_ok.sum())})") for name, (s0, s1, m0, m1) in {"1959-1978": (A.ym("1959-01"), A.ym("1978-12"), A.ym("1959-01"), A.ym("1978-12")), "1979-2026": (A.ym("1979-01"), A.ym("2026-10"), A.ym("1979-01"), A.ym("2026-09"))}.items(): xs, ym_, _ = A.pair(A.cma(sn.restrict(s0, s1), 60), A.cma(sam.restrict(m0, m1), 60), 33) r_ = A.pearson(xs, ym_) ne = A.neff_bretherton(xs, ym_) p_ = A.p_two_sided(r_, ne) f3[name].update(r_ssn_sam=r_, n_ssn_sam=len(xs), neff=ne, p=p_) # ASL in the same era for comparison a1_ = A.ym("1978-12") if name == "1959-1978" else A.ym("2026-07") xa2, ya2, _ = A.pair(A.cma(sn.restrict(s0, s1), 60), A.cma(v3.restrict(s0, a1_), 60), 33) f3[name]["r_ssn_asl"] = A.pearson(xa2, ya2) say(f"[F3b {name}] SSN vs station SAM at 5y/33m: r = {r_:.3f}, n = {len(xs)}, n_eff = {ne:.2f}, p_eff = {p_:.3f} (SSN vs ERA5 ASL, same era: r = {f3[name]['r_ssn_asl']:.3f})") res["F3"] = f3 # F4 the field fg = json.load(open(P_FG, encoding="utf-8")) ser = {r["epoch"]: r["M_1e22_Am2"] for r in fg["field_decline"]["dipole_series"] if not r["forecast"]} def pct(a, b): return 100.0 * (ser[b] - ser[a]) / ser[a] f4 = dict(M1960=ser[1960.0], M1980=ser[1980.0], M1990=ser[1990.0], M2025=ser[2025.0], pct_1960_1980=pct(1960.0, 1980.0), pct_1980_1990=pct(1980.0, 1990.0), pct_1960_2025=pct(1960.0, 2025.0)) res["F4"] = f4 say(f"\n[F4 IGRF-14 dipole moment, 10^22 A m^2] 1960 {f4['M1960']:.3f}; 1980 {f4['M1980']:.3f}; 1990 {f4['M1990']:.3f}; 2025 {f4['M2025']:.3f}") say(f" change 1960-1980 {f4['pct_1960_1980']:+.2f}%; 1980-1990 {f4['pct_1980_1990']:+.2f}%; 1960-2025 {f4['pct_1960_2025']:+.2f}%") open(os.path.join(outdir, "04_signflip.txt"), "w", encoding="utf-8", newline="\n").write("\n".join(lines) + "\n") json.dump(res, open(os.path.join(outdir, "04_signflip.json"), "w", encoding="utf-8", newline="\n"), indent=1)