# 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. """07_chart_data.py — the numbers behind the page's three charts (RULES_chartdata.txt; post hoc, descriptive). Fact-Checked Science, 2026-11-15. Needs numpy (and scipy through asl_lib). Usage: py -3.12 -I 07_chart_data.py (reads ../downloads and ../out/03_oos_targets.csv; writes ../out/chart_*.csv)""" import os, sys, csv, 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) P_V3 = os.path.join(dl, "asli_calculation_latest.csv") P_SN = os.path.join(dl, "SN_m_tot_V2.0.csv") for p in [os.path.join(here, "RULES_chartdata.txt"), P_V3, P_SN]: print("input", os.path.basename(p), hashlib.sha256(open(p, "rb").read()).hexdigest()) v3, _ = A.load_v3(P_V3) sn, _ = A.load_silso(P_SN) asl = v3.restrict(A.ym("1959-01"), A.ym("2026-07")) ssn = sn.restrict(A.ym("1959-01"), A.ym("2026-10")) ac, sc = A.cma(asl, 60), A.cma(ssn, 60) # C1 m0, m1 = A.ym("1961-01"), A.ym("2027-06") with open(os.path.join(outdir, "chart_series.csv"), "w", encoding="utf-8", newline="\n") as f: f.write("month,asl_5y_mean_hPa,ssn_5y_mean_33_months_earlier\n") for m in range(m0, m1 + 1): a = ac.get(m) s = sc.get(m - 33) f.write(f"{A.ym_str(m)},{'' if not np.isfinite(a) else f'{a:.3f}'},{'' if not np.isfinite(s) else f'{s:.2f}'}\n") # C2 xo, yo, to = A.pair(sc, ac, 33) ro = S.rolling_r((xo - xo.mean())[None, :], (yo - yo.mean())[None, :], 132)[0] labels = [int(to[k + 66]) + 33 for k in range(len(ro))] rng = np.random.default_rng(2026111571) allr = [] for b0 in range(0, 10000, 1000): sur = A.phase_surrogates(asl.v, 1000, rng) xp, Yp, tp = S.paired_matrix_y(ssn, asl.start, sur, 60, 33) Xp = np.repeat(xp[None, :], len(Yp), axis=0) allr.append(S.rolling_r(Xp - Xp.mean(axis=1, keepdims=True), Yp - Yp.mean(axis=1, keepdims=True), 132)) allr = np.concatenate(allr) p5, p50, p95 = (np.percentile(allr, q, axis=0) for q in (5, 50, 95)) with open(os.path.join(outdir, "chart_rolling.csv"), "w", encoding="utf-8", newline="\n") as f: f.write("window_label_asl_month,r_observed,null_A_p5,null_A_p50,null_A_p95\n") for i, lab in enumerate(labels): f.write(f"{A.ym_str(lab)},{ro[i]:.4f},{p5[i]:.4f},{p50[i]:.4f},{p95[i]:.4f}\n") print("C2 band: median 5th pct %.3f, median 95th pct %.3f; share of windows with observed outside the band %.3f" % (np.median(p5), np.median(p95), np.mean((ro < p5) | (ro > p95)))) # C3 rows = list(csv.DictReader(open(os.path.join(outdir, "03_oos_targets.csv"), encoding="utf-8"))) with open(os.path.join(outdir, "chart_oos.csv"), "w", encoding="utf-8", newline="\n") as f: f.write("centre,observed_asl_5y_mean_hPa,wholly_new,M_recipe,C_climatology,P_persistence,T_trend\n") for m in range(A.ym("2014-07"), A.ym("2019-05") + 1): f.write(f"{A.ym_str(m)},{ac.get(m):.4f},0,,,,\n") for r in rows: f.write(f"{r['centre']},{float(r['observed_asl_cma_hPa']):.4f},1,{r['M_recipe']},{r['C_climatology']},{r['P_persistence']},{r['T_trend']}\n") print("wrote chart_series.csv, chart_rolling.csv, chart_oos.csv")