#!/usr/bin/env python3 """Yearly (1 January) anomaly, dip pole and dipole on a 0.25-degree WGS84 grid (RULES_reproduce.txt M1-M9, M11). Usage: py -3.12 -I run_yearly.py [first last] model: gufm1 | igrf14 | covobs | chaos | igrf14cubic | gufm1_gubbins[_lo|_hi] | gufm1_sphere | igrf14_sphere Writes results/yearly_.csv """ import csv import math import os import sys import time HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import numpy as np # noqa: E402 import fieldlib as fl # noqa: E402 V, model = sys.argv[1], sys.argv[2] D = os.path.join(V, "downloads") first = last = None if len(sys.argv) > 4: first, last = float(sys.argv[3]), float(sys.argv[4]) step_years = float(sys.argv[5]) if len(sys.argv) > 5 else 1.0 base = model.split("_")[0] sphere = model.endswith("_sphere") if base in ("igrf14", "igrf14cubic"): m = fl.IGRF(os.path.join(D, "igrf14", "igrf14coeffs.txt")) gh = m.gh_cubic if base == "igrf14cubic" else m.gh nmax, span = 13, (1900.0, 2030.0) elif base == "igrf13": m = fl.IGRF(os.path.join(D, "igrf13", "igrf13coeffs.txt")) gh, nmax, span = m.gh, 13, (1900.0, 2025.0) elif base == "gufm1": m = fl.SplineModel(os.path.join(D, "gufm1", "gufm1.txt"), "gufm1") gh, nmax, span = m.gh, 14, (1590.0, 1990.0) if "gubbins" in model: # M10: |g10| grows 2.28 (+/- 2.72) nT/yr into the past from 1840 rate = 2.28 + (2.72 if model.endswith("_hi") else -2.72 if model.endswith("_lo") else 0.0) g1840 = float(m.flat(1840.0)[0]) m.g10_override = lambda t, r=rate, g0=g1840: (g0 - r * (1840.0 - t)) if t < 1840.0 else None span = (1590.0, 1840.0) elif base == "covobs": m = fl.SplineModel(os.path.join(D, "cov-obs-x2", "COV-OBS.x2-int"), "covobs") gh, nmax, span = m.gh, 14, (1840.0, 2020.0) elif base == "chaos": nm = 13 if model.endswith("_n13") else 20 m = fl.SplineModel(os.path.join(D, "chaos-8.7", "CHAOS-8.7_spline-coefficients.dat"), "chaos", nmax_use=nm) gh, nmax, span = m.gh, nm, (1999.0, 2026.0) else: raise SystemExit("unknown model " + model) if first is None: first, last = span t0 = time.time() grid = fl.Grid(step=0.25, nmax=nmax, sphere=sphere) north = grid.lat > 55.0 rows = [] seed = None nsteps = int(round((last - first) / step_years)) for k in range(nsteps + 1): t = round(first + k * step_years, 6) g, h = gh(t) F, H = grid.fields(g, h) a = fl.anomaly(grid, g, h, nmax, F=F) Hn = H[north] i, j = np.unravel_index(np.argmin(Hn), Hn.shape) seed = (float(grid.lat[north][i]), float(grid.lon[j])) p = fl.dip_pole(g, h, nmax, seed) if not sphere else {"lat": float("nan"), "lon": float("nan"), "H_nT": float("nan"), "ok": True} d = fl.dipole(g, h) rows.append({"t": t, "min_F": a["min_F"], "min_lat": a["min_lat"], "min_lon": a["min_lon"], "grid_min_F": a["grid_min_F"], "grid_min_lat": a["grid_min_lat"], "grid_min_lon": a["grid_min_lon"], "area8_km2": a["area_km2_8"], "area8_pct": a["area_pct_8"], "area4_km2": a["area_km2_4"], "area4_pct": a["area_pct_4"], "all_below_km2": a["area_km2_all_below"], "centre_lon_geodetic": a["centre_lon_geodetic"], "centre_lat_geodetic": a["centre_lat_geodetic"], "centre_lon_geocentric": a["centre_lon_geocentric"], "centre_lat_geocentric": a["centre_lat_geocentric"], "pole_lat": p["lat"], "pole_lon": p["lon"], "pole_H_nT": p["H_nT"], "pole_ok": p["ok"], "M_1e22": d["M"], "absg10": d["absg10"], "B0": d["B0"], "gm_pole_lat_geocentric": d["pole_lat"], "gm_pole_lon": d["pole_lon"]}) if k % 20 == 0: print("%s %.1f min %.1f at %.2f %.2f area %.2f%% pole %.3f %.3f H %.2e (%.0fs)" % ( model, t, a["min_F"], a["min_lat"], a["min_lon"], a["area_pct_8"], p["lat"], p["lon"], p["H_nT"], time.time() - t0), flush=True) path = os.path.join(V, "results", "yearly_%s%s.csv" % (model, "" if step_years == 1.0 else "_step%g" % step_years)) with open(path, "w", newline="\n", encoding="utf-8") as f: w = csv.DictWriter(f, fieldnames=list(rows[0].keys()), lineterminator="\n") w.writeheader() for r in rows: w.writerow({k: (repr(float(v)) if isinstance(v, float) else v) for k, v in r.items()}) print("wrote", path, "in %.0fs" % (time.time() - t0))