"""EXPLORATORY (added after the registered runs): where does the polar caps' lost air go? Not pre-registered; arithmetic on the same band rules. Usage: py -3.12 -I band_budget.py [products...] For each product and window, the trends of four complementary bands (polar |lat| > 60, middle latitudes 23.5 < |lat| <= 60, tropics |lat| <= 23.5) times each band's cos-weight share of the globe; their sum is the global trend. Shares use the grid's own weights. """ 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 WIN = {"r1": [(1948, 2025), (1979, 2025)], "r1g": [(1948, 2025), (1979, 2025)], "jra55": [(1958, 2023), (1979, 2023)], "c20v3": [(1806, 2015), (1979, 2015)]} BANDS = {"polar": lambda la: np.abs(la) > 60, "mid": lambda la: (np.abs(la) > 23.5) & (np.abs(la) <= 60), "tropical": lambda la: np.abs(la) <= 23.5} out = sys.argv[1] res = {} for p in sys.argv[2:]: c = dict(np.load(os.path.join(out, "cache", p + ".npz"))) lat = c["lat"] w = L.cos_weights(lat) share = {b: float(w[f(lat)].sum() / w.sum()) for b, f in BANDS.items()} series = np.column_stack([L.band_series(c["zonal"], lat, f) for f in BANDS.values()] + [L.band_series(c["zonal"], lat, "global")]) yrs, ann = L.annual_means(c["years"], c["months"], series) res[p] = {"area_share": share} for a, b in WIN[p]: tr = {name: L.trend_window(yrs, ann[:, i], a, b)["slope"] for i, name in enumerate(list(BANDS) + ["global"])} contrib = {name: tr[name] * share[name] for name in BANDS} res[p][f"{a}-{b}"] = {"trend": tr, "contribution_to_global": contrib, "sum_of_contributions": sum(contrib.values())} print(p, f"{a}-{b}", {k: round(v, 4) for k, v in tr.items()}, "contrib", {k: round(v, 4) for k, v in contrib.items()}, "sum", round(sum(contrib.values()), 4)) with open(os.path.join(out, "band_budget.json"), "w", newline="\n") as fh: json.dump(res, fh, indent=1)