# 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. """01_reproduce.py — recompute S0 News research note 008's numbers from the public files. Rules: RULES_reproduce.txt (hash logged before the first run). Fact-Checked Science, 2026-11-15. Usage: py -3.12 -I 01_reproduce.py (reads ../downloads, writes ../out/01_reproduce.txt and .json) """ 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 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) os.makedirs(outdir, exist_ok=True) lines = [] res = {} def say(*a): s = " ".join(str(x) for x in a) print(s) 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_V2 = os.path.join(dl, "ASL-index-Version2-Monthly-ERA-Interim_Hosking2016.txt") P_SN = os.path.join(dl, "SN_m_tot_V2.0.csv") P_WB = os.path.join(dl, "workbook.csv") P_T3 = os.path.join(root, "ms2017_table3_transcribed.csv") # optional: our transcription is not hosted P_RULES = os.path.join(here, "RULES_reproduce.txt") for p in [P_RULES, P_V3, P_V2, P_SN, P_WB] + ([P_T3] if os.path.isfile(P_T3) else []): say("input", os.path.relpath(p, root).replace("\\", "/"), sha(p)) v3, meta = A.load_v3(P_V3) v2 = A.load_v2(P_V2) sn, sn_def = A.load_silso(P_SN) wb_asl, wb_ssn = A.load_workbook(P_WB) say("v3 id:", meta.get("id"), "| product:", meta.get("product_version"), "| coverage:", A.ym_str(v3.start), "to", A.ym_str(v3.end)) say("v2 coverage:", A.ym_str(v2.start), "to", A.ym_str(v2.end), "| SILSO coverage:", A.ym_str(sn.start), "to", A.ym_str(sn.end)) prov = [A.ym_str(m) for m in sorted(sn_def) if sn_def[m] == 0] say("SILSO provisional months:", prov[0] if prov else None, "to", prov[-1] if prov else None, f"({len(prov)})") say("workbook coverage:", A.ym_str(wb_asl.start), "to", A.ym_str(wb_asl.end), "n =", len(wb_asl)) FREEZE_V3 = A.ym("2026-07") FREEZE_SN = A.ym("2026-10") v3f = v3.restrict(v3.start, FREEZE_V3) snf = sn.restrict(sn.start, FREEZE_SN) def stat(ssn_raw, asl_raw, w=60, lag=33, odd=False): sc, ac = A.cma(ssn_raw, w, odd), A.cma(asl_raw, w, odd) x, y, t = A.pair(sc, ac, lag) r = A.pearson(x, y) ne = A.neff_bretherton(x, y) p = A.p_two_sided(r, ne) return dict(r=r, n=len(x), neff=ne, p=p, t0=A.ym_str(int(t[0])), t1=A.ym_str(int(t[-1])), asl0=A.ym_str(int(t[0]) + lag), asl1=A.ym_str(int(t[-1]) + lag)) def peaks(ssn_raw, asl_raw): R, N = A.grid(ssn_raw, asl_raw) i5 = A.WIDTHS.index(60) jb = int(np.nanargmax(R[i5])) gi, gj = np.unravel_index(int(np.nanargmax(R)), R.shape) ai, aj = np.unravel_index(int(np.nanargmax(np.abs(R))), R.shape) return R, N, dict(band_lag=A.LAGS[jb], band_r=float(R[i5, jb]), band_n=int(N[i5, jb]), glob_w=A.WIDTHS[gi] // 12, glob_lag=A.LAGS[gj], glob_r=float(R[gi, gj]), glob_n=int(N[gi, gj]), abs_w=A.WIDTHS[ai] // 12, abs_lag=A.LAGS[aj], abs_r=float(R[ai, aj])) windows = { "satellite": (A.ym("1979-01"), FREEZE_V3, A.ym("1979-01"), FREEZE_SN), "full": (A.ym("1959-01"), FREEZE_V3, A.ym("1959-01"), FREEZE_SN), "pre1979": (A.ym("1959-01"), A.ym("1978-12"), A.ym("1959-01"), A.ym("1978-12")), } for name, (a0, a1, s0, s1) in windows.items(): asl_raw = v3f.restrict(a0, a1) ssn_raw = snf.restrict(s0, s1) st = stat(ssn_raw, asl_raw) R, N, pk = peaks(ssn_raw, asl_raw) st.update(pk) res[name] = st say(f"\n[{name}] ASL {A.ym_str(asl_raw.start)}..{A.ym_str(asl_raw.end)}, SSN {A.ym_str(ssn_raw.start)}..{A.ym_str(ssn_raw.end)}") say(f" 5y/33m: r = {st['r']:.4f}, n = {st['n']}, n_eff = {st['neff']:.3f}, p_eff = {st['p']:.5f}; SSN t {st['t0']}..{st['t1']}, ASL {st['asl0']}..{st['asl1']}") say(f" 5-year lag sweep peak: lag {pk['band_lag']} m, r = {pk['band_r']:.4f}, n = {pk['band_n']}") say(f" grid (8x49) peak: {pk['glob_w']}-year CMA, lag {pk['glob_lag']} m, r = {pk['glob_r']:.4f}, n = {pk['glob_n']}; max |r|: {pk['abs_w']}y/{pk['abs_lag']}m r = {pk['abs_r']:.4f}") if name == "satellite": res["satellite_grid_r"] = R.round(4).tolist() say(" 5-year column, lags 30..36:", " ".join(f"{L}:{R[4, L]:.4f}" for L in range(30, 37))) # 1996 mask asl_m = v3f.restrict(A.ym("1979-01"), FREEZE_V3) for mm in range(A.ym("1996-05"), A.ym("1996-10") + 1): asl_m.v[mm - asl_m.start] = np.nan st = stat(snf.restrict(A.ym("1979-01"), FREEZE_SN), asl_m) res["mask1996"] = st say(f"\n[1996 mask] 5y/33m: r = {st['r']:.4f}, n = {st['n']}, n_eff = {st['neff']:.3f}, p_eff = {st['p']:.5f}") # rolling 11-year asl_raw = v3f.restrict(A.ym("1959-01"), FREEZE_V3) ssn_raw = snf.restrict(A.ym("1959-01"), FREEZE_SN) x, y, t = A.pair(A.cma(ssn_raw, 60), A.cma(asl_raw, 60), 33) B = 132 roll = [] for k in range(0, len(x) - B + 1): r = A.pearson(x[k:k + B], y[k:k + B]) lab = int(t[k + 66]) + 33 roll.append((lab, r)) labs = np.array([a for a, _ in roll]) rr = np.array([b for _, b in roll]) dec = np.array([A.ym_dec(a) for a in labs]) res["rolling"] = [(A.ym_str(int(a)), round(float(b), 4)) for a, b in roll] neg_before_1990 = [A.ym_str(int(a)) for a, b in roll if b < 0 and A.ym_dec(a) < 1990] say(f"\n[rolling 132-pair r] labels {A.ym_str(int(labs[0]))}..{A.ym_str(int(labs[-1]))} ({len(roll)} windows)") say(" first label:", A.ym_str(int(labs[0])), f"r = {rr[0]:.3f}") say(" last label with r < 0 before 1990:", neg_before_1990[-1] if neg_before_1990 else None) imax = int(np.argmax(rr)) say(f" max r = {rr[imax]:.3f} at {A.ym_str(int(labs[imax]))}") sel = (dec >= 2005) & (dec < 2020) imin = np.where(sel)[0][np.argmin(rr[sel])] say(f" min r in 2005-2020 labels = {rr[imin]:.3f} at {A.ym_str(int(labs[imin]))}") low = [A.ym_str(int(a)) for a, b in zip(labs, rr) if b <= -0.40 and A.ym_dec(a) >= 2005] say(" labels with r <= -0.40 (2005 on):", (low[0], low[-1], len(low)) if low else None) say(f" last r = {rr[-1]:.3f} at {A.ym_str(int(labs[-1]))}") say(" r at selected labels:", "; ".join(f"{A.ym_str(int(a))} {b:.3f}" for a, b in roll if A.ym_str(int(a)).endswith("-01") or a == labs[-1])) res["rolling_summary"] = dict(first=(A.ym_str(int(labs[0])), float(rr[0])), last_neg_before_1990=neg_before_1990[-1] if neg_before_1990 else None, max=(A.ym_str(int(labs[imax])), float(rr[imax])), min_2005_2020=(A.ym_str(int(labs[imin])), float(rr[imin])), last=(A.ym_str(int(labs[-1])), float(rr[-1]))) # v2 replication and the 2017 table w0, w1 = A.ym("1979-01"), A.ym("2016-11") v2w = v2.restrict(w0, w1) snw = snf.restrict(w0, w1) say(f"\n[v2 replication] ASL v2 {A.ym_str(v2w.start)}..{A.ym_str(v2w.end)} ({len(v2w)} months)") for odd in (False, True): st = stat(snw, v2w, odd=odd) tag = "v2_odd" if odd else "v2" res[tag] = st say(f" {'odd 61-month' if odd else '60-month'} window: r = {st['r']:.4f}, n = {st['n']}, n_eff = {st['neff']:.3f}, p_eff = {st['p']:.5f}") c5 = A.cma(v2w, 60) c5o = A.cma(v2w, 60, odd=True) say(" number of 5-year CMA values: 60-month form", int(np.isfinite(c5.v).sum()), "| odd form", int(np.isfinite(c5o.v).sum())) # data checks d_asl = [abs(wb_asl.get(m) - v2.get(m)) for m in range(wb_asl.start, wb_asl.end + 1)] d_ssn = [abs(wb_ssn.get(m) - sn.get(m)) for m in range(wb_ssn.start, wb_ssn.end + 1)] n_ssn_diff = sum(1 for d in d_ssn if d > 0.05) say(f"\n[data checks] workbook ASL vs v2: max |diff| = {max(d_asl):.4f} hPa over {len(d_asl)} months") say(f" workbook SSN vs current SILSO: max |diff| = {max(d_ssn):.2f}; months differing by > 0.05: {n_ssn_diff}") ov0, ov1 = A.ym("1979-01"), A.ym("2018-03") a2 = v2.restrict(ov0, ov1).v a3 = v3.restrict(ov0, ov1).v say(f" v2 vs v3 sector pressure 1979-01..2018-03: r(raw) = {A.pearson(a2, a3):.4f}, mean(v3 - v2) = {np.mean(a3 - a2):+.3f} hPa, sd diff = {np.std(a3 - a2):.3f}") c2 = A.cma(v2.restrict(ov0, ov1), 60).v c3 = A.cma(v3.restrict(ov0, ov1), 60).v ok = np.isfinite(c2) & np.isfinite(c3) say(f" 5-year CMAs: r = {A.pearson(c2[ok], c3[ok]):.4f}, max |diff - mean diff| = {np.max(np.abs((c3 - c2)[ok] - np.mean((c3 - c2)[ok]))):.3f} hPa") res["v2v3"] = dict(r_raw=A.pearson(a2, a3), mean_diff=float(np.mean(a3 - a2)), r_cma=A.pearson(c2[ok], c3[ok])) # 2017 table HAVE_T3 = os.path.isfile(P_T3) if not HAVE_T3: say(" no transcription of the 2017 table in the data folder (ms2017_table3_transcribed.csv): the comparison columns print nan") T = np.loadtxt(P_T3, delimiter=",", comments="#", skiprows=3) if HAVE_T3 else np.column_stack([np.arange(49)] + [np.full(49, np.nan)] * 8) lag_idx = T[:, 0].astype(int) tab = np.full((8, 49), np.nan) for row in T: tab[:, int(row[0])] = row[1:] for label, (sraw, araw) in {"workbook": (wb_ssn, wb_asl), "v2+SILSO now": (snw, v2w)}.items(): for odd in (False, True): R, N = A.grid(sraw, araw, odd=odd) diff = np.abs(np.round(R, 3) - tab) i5 = 4 jb = int(np.nanargmax(R[i5])) say(f" 2017 table vs {label}, {'odd' if odd else '60-month-type'} windows: max |diff| = {(np.nanmax(diff) if HAVE_T3 else float("nan")):.3f}, " f"cells within 0.001: {int((diff <= 0.0011).sum())}/392; 5y peak lag {jb} r = {R[i5, jb]:.4f}; 5y/33 = {R[i5, 33]:.4f} n = {N[i5, 33]}") res[f"table_{label}_{'odd' if odd else 'even'}"] = dict(maxdiff=float(np.nanmax(diff)) if HAVE_T3 else None, within=int((diff <= 0.0011).sum()), r5_33=float(R[i5, 33]), n5_33=int(N[i5, 33])) # reproduction verdicts say("\n[comparison with the note]") note = [ ("satellite r", res["satellite"]["r"], 0.803, 0.001), ("satellite n", res["satellite"]["n"], 479, 0), ("satellite n_eff", res["satellite"]["neff"], 10.33, 0.01), ("satellite p_eff", res["satellite"]["p"], 0.00425, 0.00001), ("satellite band peak lag", res["satellite"]["band_lag"], 34, 0), ("satellite band peak r", res["satellite"]["band_r"], 0.803, 0.001), ("satellite global peak width (y)", res["satellite"]["glob_w"], 7, 0), ("satellite global peak lag", res["satellite"]["glob_lag"], 30, 0), ("satellite global peak r", res["satellite"]["glob_r"], 0.812, 0.001), ("full r", res["full"]["r"], 0.391, 0.001), ("full n", res["full"]["n"], 719, 0), ("full n_eff", res["full"]["neff"], 15.7, 0.1), ("full p_eff", res["full"]["p"], 0.138, 0.001), ("full band peak lag", res["full"]["band_lag"], 29, 0), ("full band peak r", res["full"]["band_r"], 0.394, 0.001), ("full band peak n", res["full"]["band_n"], 723, 0), ("full global peak width (y)", res["full"]["glob_w"], 8, 0), ("full global peak lag", res["full"]["glob_lag"], 19, 0), ("full global peak r", res["full"]["glob_r"], 0.515, 0.001), ("full global peak n", res["full"]["glob_n"], 697, 0), ("pre-1979 r", res["pre1979"]["r"], -0.815, 0.001), ("pre-1979 n", res["pre1979"]["n"], 148, 0), ("pre-1979 n_eff", res["pre1979"]["neff"], 4.6, 0.1), ("pre-1979 p_eff", res["pre1979"]["p"], 0.12, 0.01), ("1996 mask r", res["mask1996"]["r"], 0.789, 0.001), ("1996 mask n", res["mask1996"]["n"], 414, 0), ("v2 r", res["v2"]["r"], 0.903, 0.001), ("v2 n", res["v2"]["n"], 363, 0), ("v2 n_eff", res["v2"]["neff"], 7.79, 0.01), ("v2 p", res["v2"]["p"], 0.00258, 0.00001), ] tab_out = [] for name, ours, theirs, tol in note: okk = abs(ours - theirs) <= tol + 1e-12 tab_out.append(dict(item=name, ours=ours, note=theirs, reproduces=bool(okk))) say(f" {name:34s} ours {ours:>12.5f} note {theirs:>10} {'REPRODUCES' if okk else 'DIFFERS'}") res["comparison"] = tab_out open(os.path.join(outdir, "01_reproduce.txt"), "w", encoding="utf-8", newline="\n").write("\n".join(lines) + "\n") json.dump(res, open(os.path.join(outdir, "01_reproduce.json"), "w", encoding="utf-8", newline="\n"), indent=1, default=float)