"""Write the derived series that the standard-library checker and the charts use. Usage: py -3.12 -I export_derived.py Needs numpy, netCDF4 and cftime (and extract_fields.py's caches in /cache). Writes, in : station_points_annual_.csv annual means (hPa) of each reanalysis at each station r1_slp_zonal_annual.csv R1's zonal-mean sea-level pressure on the 65 S and 40 S rows eureka_isd_annual.csv Eureka's ISD station and sea-level pressure, annual means These are our derived numbers; no input file is copied. """ import glob import os import sys import cftime import netCDF4 import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import ppt_lib as L # noqa: E402 import stations as S # noqa: E402 downloads, out = sys.argv[1], sys.argv[2] for p in ("r1", "r1g", "jra55", "c20v3"): c = dict(np.load(os.path.join(out, "cache", p + ".npz"))) names = [str(x) for x in c["stations"]] yrs, ann = L.annual_means(c["years"], c["months"], c["points"]) with open(os.path.join(out, f"station_points_annual_{p}.csv"), "w", newline="\n") as fh: fh.write("year," + ",".join(names) + "\n") for i, y in enumerate(yrs): fh.write(f"{y}," + ",".join(f"{v:.4f}" for v in ann[i]) + "\n") with netCDF4.Dataset(os.path.join(downloads, "psl-r1", "slp.mon.mean.nc")) as ds: ds.set_auto_mask(False) t = ds.variables["time"] dd = cftime.num2date(t[:], t.units, calendar=getattr(t, "calendar", "standard")) yy = np.array([d.year for d in dd]) mm = np.array([d.month for d in dd]) lat = ds.variables["lat"][:] slp = np.asarray(ds.variables["slp"][:], dtype=float) cols = [] for band in (-65.0, -40.0): j = int(np.argmin(np.abs(lat - band))) cols.append(slp[:, j, :].mean(axis=1)) yrs, ann = L.annual_means(yy, mm, np.column_stack(cols)) with open(os.path.join(out, "r1_slp_zonal_annual.csv"), "w", newline="\n") as fh: fh.write("year,r1_slp_65S,r1_slp_40S\n") for i, y in enumerate(yrs): fh.write(f"{y},{ann[i, 0]:.4f},{ann[i, 1]:.4f}\n") files = sorted(glob.glob(os.path.join(downloads, "noaa-isd", "eureka", "*.gz"))) isd, _ = S.read_isd(files, 4) stp = dict(zip(*S.annual(*isd["stp"]))) slp_e = dict(zip(*S.annual(*isd["slp"]))) with open(os.path.join(out, "eureka_isd_annual.csv"), "w", newline="\n") as fh: fh.write("year,station_pressure_hPa,sea_level_pressure_hPa\n") for y in sorted(set(stp) | set(slp_e)): a = f"{stp[y]:.3f}" if y in stp else "" b = f"{slp_e[y]:.3f}" if y in slp_e else "" fh.write(f"{int(y)},{a},{b}\n") print("written")