#!/usr/bin/env python3 """North dip pole every years (RULES_independent.txt P1-P2). Seed from a 0.25-degree grid north of 55 N at the first time, then from the previous solution (checked: H < 0.001 nT at every step). Usage: py -3.12 -I pole_fine.py model: covobs | chaos | chaos_n13 | igrf14 | igrf14cubic | gufm1 Writes results/pole__.csv (t, lat, lon, H_nT, plus dipole quantities and d|g10|/dt where available).""" import csv import math import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import fieldlib as fl # noqa: E402 V, model, first, last, step = sys.argv[1], sys.argv[2], float(sys.argv[3]), float(sys.argv[4]), float(sys.argv[5]) D = os.path.join(V, "downloads") deriv = None if model == "covobs": m = fl.SplineModel(os.path.join(D, "cov-obs-x2", "COV-OBS.x2-int"), "covobs"); gh, nmax = m.gh, 14 deriv = lambda t: m.gh(t, deriv=1) elif model in ("chaos", "chaos_n13"): 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 = m.gh, nm deriv = lambda t: m.gh(t, deriv=1) elif model in ("igrf14", "igrf14cubic"): m = fl.IGRF(os.path.join(D, "igrf14", "igrf14coeffs.txt")) gh, nmax = (m.gh_cubic if model == "igrf14cubic" else m.gh), 13 if model == "igrf14": deriv = m.sv elif model == "gufm1": m = fl.SplineModel(os.path.join(D, "gufm1", "gufm1.txt"), "gufm1"); gh, nmax = m.gh, 14 deriv = lambda t: m.gh(t, deriv=1) else: raise SystemExit(model) n = int(round((last - first) / step)) seeder = fl.PoleSeeder(nmax) rows, seed = [], None for k in range(n + 1): t = round(first + k * step, 6) g, h = gh(t) if seed is None: seed = seeder.seed(g, h) p = fl.dip_pole(g, h, nmax, seed) if not p["ok"]: p = fl.dip_pole(g, h, nmax, seeder.seed(g, h)) seed = (p["lat"], p["lon"]) d = fl.dipole(g, h) r = {"t": t, "lat": p["lat"], "lon": p["lon"], "H_nT": p["H_nT"], "ok": p["ok"], "absg10": d["absg10"], "M_1e22": d["M"], "gm_lat": d["pole_lat"], "gm_lon": d["pole_lon"]} if deriv is not None: gd, hd = deriv(t) b0 = d["B0"] r["dg10_dt"] = float(gd[1][0]) r["dM_dt_1e22_per_yr"] = float(d["M"] * (g[1][0] * gd[1][0] + g[1][1] * gd[1][1] + h[1][1] * hd[1][1]) / (b0 * b0)) rows.append(r) path = os.path.join(V, "results", "pole_%s_%g.csv" % (model, step)) 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()}) bad = sum(1 for r in rows if not r["ok"]) print("wrote", path, len(rows), "rows; not converged:", bad)