"""The note's Amundsen Sea Low trend (RULES_asl.txt). Usage: py -3.12 -I asl.py """ import csv 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 COLS = {"actual_central_pressure": 3, "sector_pressure": 4, "relative_central_pressure": 5} rows = [r for r in csv.reader(open(sys.argv[1], encoding="utf-8")) if r and not r[0].startswith("#") and r[0][:1].isdigit()] years = np.array([int(r[0][:4]) for r in rows]) months = np.array([int(r[0][5:7]) for r in rows]) res = {"rules": "RULES_asl.txt", "n_months": len(rows), "first": rows[0][0], "last": rows[-1][0], "columns": {}} flag = (years == 1996) & (months >= 5) & (months <= 10) for name, j in COLS.items(): v = np.array([float(r[j]) for r in rows]) out = {} for wt in ("days", "equal"): yrs, ann = L.annual_means(years, months, v, weighting=wt) out[wt] = {m: L.trend_window(yrs, ann, 1959, 2025, m) for m in ("M1", "M5")} out[wt]["since1979_M1"] = L.trend_window(yrs, ann, 1979, 2025, "M1") # 1996 flagged months left out; 1996 from its other six months yrs, ann = L.annual_means(years[~flag], months[~flag], v[~flag]) m96 = (years == 1996) & ~flag d = np.array([L.days_in_month(1996, m) for m in months[m96]], dtype=float) a96 = (v[m96] * d).sum() / d.sum() yrs2 = np.sort(np.append(yrs, 1996)) ann2 = np.array([a96 if y == 1996 else ann[list(yrs).index(y)] for y in yrs2]) out["flag1996_out"] = L.trend_window(yrs2, ann2, 1959, 2025, "M1") res["columns"][name] = out t = out["days"]["M1"] print(f"{name}: {t['slope']:.3f} [{t['lo']:.3f}, {t['hi']:.3f}] n={t['n']} " f"(M5 [{out['days']['M5']['lo']:.3f}, {out['days']['M5']['hi']:.3f}]); " f"equal-weight {out['equal']['M1']['slope']:.3f}; 1996 flag out " f"{out['flag1996_out']['slope']:.3f}; since 1979 " f"{out['days']['since1979_M1']['slope']:.3f} " f"[{out['days']['since1979_M1']['lo']:.3f}, {out['days']['since1979_M1']['hi']:.3f}]") with open(os.path.join(sys.argv[2], "asl.json"), "w", newline="\n") as fh: json.dump(res, fh, indent=1)