"""EXPLORATORY, descriptive (not pre-registered): mean band pressure by period, to show where each product's full-record trend comes from. Usage: py -3.12 -I period_means.py Reads annual_bands_.csv; writes period_means.json. """ import csv import json import os import sys import numpy as np PERIODS = { "c20v3": [(1806, 1835), (1836, 1879), (1880, 1919), (1920, 1949), (1950, 1956), (1957, 1978), (1979, 1999), (2000, 2015)], "r1g": [(1948, 1957), (1958, 1967), (1968, 1978), (1979, 1989), (1990, 1999), (2000, 2009), (2010, 2025)], "r1": [(1948, 1957), (1958, 1967), (1968, 1978), (1979, 1989), (1990, 1999), (2000, 2009), (2010, 2025)], "jra55": [(1958, 1967), (1968, 1978), (1979, 1989), (1990, 1999), (2000, 2009), (2010, 2023)], } out = sys.argv[1] res = {} for p, periods in PERIODS.items(): rows = list(csv.DictReader(open(os.path.join(out, f"annual_bands_{p}.csv"), encoding="utf-8"))) y = np.array([int(r["year"]) for r in rows]) res[p] = {} for band in ("arctic", "antarctic", "polar", "tropical", "global"): v = np.array([float(r[band]) for r in rows]) res[p][band] = {f"{a}-{b}": float(v[(y >= a) & (y <= b)].mean()) for a, b in periods} print(p, band, " ".join(f"{k}:{x:.2f}" for k, x in res[p][band].items())) with open(os.path.join(out, "period_means.json"), "w", newline="\n") as fh: json.dump(res, fh, indent=1)