"""Is the note's Antarctic fall the Southern Annular Mode's trend? (RULES_sam.txt) Usage: py -3.12 -I sam.py [products...] """ import json import os import sys import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import ppt_lib as L # noqa: E402 def read_seas(path): out = {"ANN": {}, "AUT": {}, "WIN": {}, "SPR": {}, "SUM": {}} for line in open(path, encoding="utf-8"): if not line[:4].isdigit(): continue y = int(line[:4]) for k, name in enumerate(["ANN", "AUT", "WIN", "SPR", "SUM"]): tok = line[4 + 6 * k: 10 + 6 * k].strip() if tok: out[name][y] = float(tok) return out def bands(out, p): c = dict(np.load(os.path.join(out, "cache", p + ".npz"))) lat = c["lat"] ant = L.band_series(c["zonal"], lat, "antarctic") s40 = L.band_series(c["zonal"], lat, lambda la: np.abs(la + 40.0) <= 5.0) yrs, ann = L.annual_means(c["years"], c["months"], np.column_stack([ant, s40])) return yrs, ann[:, 0], ann[:, 1] def detrended_corr(x, a, b): ra = a - np.polyval(np.polyfit(x, a, 1), x) rb = b - np.polyval(np.polyfit(x, b, 1), x) return float(np.corrcoef(ra, rb)[0, 1]) def analyse(yrs, band, sam, start): ys = np.array(sorted(y for y in sam if y >= start and y in set(yrs.tolist()))) v = np.array([band[list(yrs).index(y)] for y in ys]) s = np.array([sam[y] for y in ys]) beta, alpha = np.polyfit(s, v, 1) resid = v - (alpha + beta * s) tb = L.trend(ys, v) ts = L.trend(ys, s) return {"years": [int(ys[0]), int(ys[-1])], "n": int(len(ys)), "r": float(np.corrcoef(v, s)[0, 1]), "r_detrended": detrended_corr(ys, v, s), "beta_hPa_per_SAM_unit": float(beta), "band_trend": tb, "sam_trend": ts, "residual_trend": L.trend(ys, resid), "share_explained": float(beta * ts["slope"] / tb["slope"]) if tb["slope"] else None} def main(downloads, out, products): seas = read_seas(os.path.join(downloads, "bas-sam", "newsam.1957.2007.seas.txt")) res = {"rules": "RULES_sam.txt", "S1": {}, "S2": {}, "S3": {}} for k in ("ANN", "SUM"): y = np.array(sorted(seas[k])) v = np.array([seas[k][q] for q in y]) last = 2025 res["S1"][k] = {f"1957-{last}": L.trend_window(y, v, 1957, last), f"1979-{last}": L.trend_window(y, v, 1979, last)} for p in products: yrs, ant, s40 = bands(out, p) res["S2"][p] = {"from1957": analyse(yrs, ant, seas["ANN"], 1957), "from1979": analyse(yrs, ant, seas["ANN"], 1979)} res["S3"][p] = {"from1957": analyse(yrs, s40, seas["ANN"], 1957), "from1979": analyse(yrs, s40, seas["ANN"], 1979)} for key in ("S2", "S3"): for span, a in res[key][p].items(): rd = a["r_detrended"] rt = a["residual_trend"] if key == "S2": a["reading"] = ("the SAM's trend" if rd <= -0.7 and rt["lo"] <= 0 <= rt["hi"] else "part beyond the SAM index" if not (rt["lo"] <= 0 <= rt["hi"]) else "correlation below the threshold") with open(os.path.join(out, "sam.json"), "w", newline="\n") as fh: json.dump(res, fh, indent=1) for k, v in res["S1"].items(): print("SAM", k, {s: (round(t["slope"], 3), round(t["lo"], 3), round(t["hi"], 3), t["n"]) for s, t in v.items()}) for key in ("S2", "S3"): for p, d in res[key].items(): for span, a in d.items(): print(key, p, span, a["years"], "r", round(a["r"], 3), "r_dt", round(a["r_detrended"], 3), "beta", round(a["beta_hPa_per_SAM_unit"], 3), "band", round(a["band_trend"]["slope"], 3), "resid", round(a["residual_trend"]["slope"], 3), [round(a["residual_trend"]["lo"], 3), round(a["residual_trend"]["hi"], 3)], "share", None if a["share_explained"] is None else round(a["share_explained"], 2), a.get("reading", "")) if __name__ == "__main__": main(sys.argv[1], sys.argv[2], sys.argv[3:])