#!/usr/bin/env python3 """Code checks C1 and C2 of RULES_reproduce.txt. Run: py -3.12 -I validate.py """ import json import math import os import random import sys import zipfile 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 = sys.argv[1] D = os.path.join(V, "downloads") PAGES = os.path.join(os.path.dirname(os.path.dirname(V)), "pages") out = {} # C1: NOAA WMM2025 test values z = zipfile.ZipFile(os.path.join(D, "wmm2025", "WMM2025COF.zip")) names = {os.path.basename(n): n for n in z.namelist()} cof = z.read(names["WMM.COF"]).decode("ascii").splitlines() tests = z.read(names["WMM2025_TEST_VALUES.txt"]).decode("ascii").splitlines() N = 12 g = np.zeros((N + 1, N + 1)); h = np.zeros_like(g); gd = np.zeros_like(g); hd = np.zeros_like(g) epoch = float(cof[0].split()[0]) for line in cof[1:]: p = line.split() if len(p) < 6 or p[0].startswith("9999"): continue n, m = int(p[0]), int(p[1]) g[n, m], h[n, m], gd[n, m], hd[n, m] = map(float, p[2:6]) worst_np = worst_py = 0.0 npts = 0 for line in tests: if line.startswith("#") or not line.strip(): continue p = [float(x) for x in line.split()] t, hkm, lat, lon = p[0], p[1], p[2], p[3] gg, hh = g + (t - epoch) * gd, h + (t - epoch) * hd rows = fl.Rows([lat], [lon], N, h_km=hkm) X, Y, Zc = (float(a[0, 0]) for a in rows.xyz(gg, hh)) F = math.sqrt(X * X + Y * Y + Zc * Zc) Xp, Yp, Zp = fl.point_xyz(gg, hh, lat, lon, N, h_km=hkm) Fp = math.sqrt(Xp * Xp + Yp * Yp + Zp * Zp) for mine, ref in ((X, p[4]), (Y, p[5]), (Zc, p[6]), (F, p[8])): worst_np = max(worst_np, abs(mine - ref)) for mine, ref in ((Xp, p[4]), (Yp, p[5]), (Zp, p[6]), (Fp, p[8])): worst_py = max(worst_py, abs(mine - ref)) npts += 1 out["C1_wmm2025_test_points"] = npts out["C1_largest_abs_diff_numpy_nT"] = worst_np out["C1_largest_abs_diff_pointpy_nT"] = worst_py out["C1_pass"] = worst_np <= 0.1 and worst_py <= 0.1 # C2a: against the site's checked pure-Python code (field_decline.py), IGRF-14, 200 random points and times sys.path.insert(0, os.path.join(PAGES, "magnetic-field-guide")) import field_decline as fd # noqa: E402 ig = fl.IGRF(os.path.join(D, "igrf14", "igrf14coeffs.txt")) ep, main, sv = fd.read_igrf(os.path.join(D, "igrf14", "igrf14coeffs.txt")) rng = random.Random(20261109) worst = 0.0 for _ in range(200): t = rng.uniform(1900.0, 2030.0) lat = math.degrees(math.asin(rng.uniform(-0.999, 0.999))) lon = rng.uniform(-180, 180) G1, H1 = fd.igrf_at(ep, main, sv, t) ref = fd.field(G1, H1, lat, lon) G2, H2 = ig.gh(t) rows = fl.Rows([lat], [lon], 13) mine = [float(a[0, 0]) for a in rows.xyz(G2, H2)] pt = fl.point_xyz(G2, H2, lat, lon, 13) worst = max(worst, max(abs(a - b) for a, b in zip(mine, ref)), max(abs(a - b) for a, b in zip(pt, ref))) out["C2a_vs_field_decline_200pts_largest_abs_diff_nT"] = worst out["C2a_pass"] = worst <= 1e-3 # C2b: gufm1 B-splines (scipy) against the site's pure-Python de Boor (field_loss.py span/basis_derivs) sys.path.insert(0, os.path.join(PAGES, "field-loss-projections")) import field_loss as fls # noqa: E402 gu = fl.SplineModel(os.path.join(D, "gufm1", "gufm1.txt"), "gufm1") worst = 0.0 for _ in range(50): t = rng.uniform(1590.0, 1990.0) a = gu.flat(t) i = fls.span(gu.nspl, gu.k, t, list(gu.knots)) w = fls.basis_derivs(i, t, gu.k, 0, list(gu.knots))[0] b = sum(w[j] * gu.coefs[i - gu.k + j] for j in range(gu.k + 1)) worst = max(worst, float(np.max(np.abs(a - b)))) out["C2b_gufm1_bspline_vs_deboor_largest_abs_diff_nT"] = worst out["C2b_pass"] = worst <= 1e-6 out["gufm1_span"] = [gu.t_first, gu.t_last] out["gufm1_nmax_nspl_order"] = [gu.nmax_file, gu.nspl, gu.order] co = fl.SplineModel(os.path.join(D, "cov-obs-x2", "COV-OBS.x2-int"), "covobs") ch = fl.SplineModel(os.path.join(D, "chaos-8.7", "CHAOS-8.7_spline-coefficients.dat"), "chaos") out["covobs_span_nmax_nspl_order"] = [co.t_first, co.t_last, co.nmax_file, co.nspl, co.order] out["chaos_span_nmax_nspl_order"] = [ch.t_first, ch.t_last, ch.nmax_file, ch.nspl, ch.order] out["earth_area_km2"] = fl.EARTH_AREA json.dump(out, open(os.path.join(V, "results", "validate.json"), "w", newline="\n"), indent=1) print(json.dumps(out, indent=1))