#!/usr/bin/env python3 """Readers for the CHAOS-8 spline-coefficient file and the IGRF coefficient file. Fact-Checked Science. Standard library only. These functions are copied unchanged from the site's field_loss.py (https://factchecked.science/field-loss-projections/field_loss.py), where they were checked against NOAA's test values for the World Magnetic Model: zeros, read_igrf, igrf_at, the class Chaos (without its COV-OBS reader), span and basis_derivs (Piegl and Tiller, The NURBS Book, algorithm A2.3). Used by dipole_pace.py and dipole_context.py for /march-2023-storm/. """ NMAX = 13 # IGRF-14 runs to 13 from 2000; only degree 1 is used here def zeros(): return [[0.0] * (NMAX + 1) for _ in range(NMAX + 1)] def read_igrf(path): epochs, rows = None, {} with open(path, encoding="ascii") as f: for line in f: if line.startswith("g/h"): epochs = [float(x) for x in line.split()[3:-1]] elif line[:1] in "gh" and not line.startswith("g/h"): p = line.split() rows[(p[0], int(p[1]), int(p[2]))] = [float(x) for x in p[3:]] return epochs, {k: v[:-1] for k, v in rows.items()}, {k: v[-1] for k, v in rows.items()} def igrf_at(model, t): epochs, main, sv = model g, h = zeros(), zeros() if t >= epochs[-1]: i0 = len(epochs) - 1 for (gh, n, m), v in main.items(): (g if gh == "g" else h)[n][m] = v[i0] + (t - epochs[-1]) * sv[(gh, n, m)] else: i0 = max(i for i, e in enumerate(epochs) if e <= t) w = (t - epochs[i0]) / (epochs[i0 + 1] - epochs[i0]) for (gh, n, m), v in main.items(): (g if gh == "g" else h)[n][m] = v[i0] + w * (v[i0 + 1] - v[i0]) return g, h class Chaos: """CHAOS core field from its B-spline file: the Gauss coefficients and their time derivatives.""" def __init__(self, path, name): nums, header = [], None with open(path, encoding="ascii") as f: for line in f: s = line.strip() if not s or s.startswith("#"): continue parts = s.split() if header is None: try: vals = [int(x) for x in parts] except ValueError: continue # a text title line, e.g. "CHAOS-8.5, extracted on ..." if len(vals) == 3: header = vals continue nums.extend(float(x) for x in parts) self.name = name self.nmax, self.nspl, order = header self.p = order - 1 self.knots = nums[:self.nspl + order] vals = nums[self.nspl + order:] n = self.nmax * (self.nmax + 2) assert len(vals) == self.nspl * n, "unexpected CHAOS file layout" self.coefs = [vals[j * n:(j + 1) * n] for j in range(self.nspl)] self.t_first, self.t_last = self.knots[self.p], self.knots[self.nspl] def flat(self, t, deriv=0): i = span(self.nspl, self.p, t, self.knots) w = basis_derivs(i, t, self.p, deriv, self.knots)[deriv] out = [0.0] * len(self.coefs[0]) for j in range(self.p + 1): if w[j]: row = self.coefs[i - self.p + j] for q in range(len(out)): out[q] += w[j] * row[q] return out def gh(self, t, deriv=0, nmax=NMAX): c = self.flat(t, deriv) g, h = zeros(), zeros() for n in range(1, min(nmax, self.nmax) + 1): base = (n - 1) * (n + 1) g[n][0] = c[base] for m in range(1, n + 1): g[n][m] = c[base + 2 * m - 1] h[n][m] = c[base + 2 * m] return g, h def span(nb, p, u, U): if u >= U[nb]: return nb - 1 if u <= U[p]: return p lo, hi = p, nb mid = (lo + hi) // 2 while u < U[mid] or u >= U[mid + 1]: lo, hi = (lo, mid) if u < U[mid] else (mid, hi) mid = (lo + hi) // 2 return mid def basis_derivs(i, u, p, n, U): """Non-zero B-splines and their derivatives (Piegl and Tiller, The NURBS Book, algorithm A2.3).""" ndu = [[0.0] * (p + 1) for _ in range(p + 1)] left, right = [0.0] * (p + 1), [0.0] * (p + 1) ndu[0][0] = 1.0 for j in range(1, p + 1): left[j], right[j] = u - U[i + 1 - j], U[i + j] - u saved = 0.0 for r in range(j): ndu[j][r] = right[r + 1] + left[j - r] temp = ndu[r][j - 1] / ndu[j][r] ndu[r][j] = saved + right[r + 1] * temp saved = left[j - r] * temp ndu[j][j] = saved ders = [[0.0] * (p + 1) for _ in range(n + 1)] for j in range(p + 1): ders[0][j] = ndu[j][p] a = [[0.0] * (p + 1) for _ in range(2)] for r in range(p + 1): s1, s2 = 0, 1 a[0][0] = 1.0 for k in range(1, n + 1): d, rk, pk = 0.0, r - k, p - k if r >= k: a[s2][0] = a[s1][0] / ndu[pk + 1][rk] d = a[s2][0] * ndu[rk][pk] j1 = 1 if rk >= -1 else -rk j2 = k - 1 if (r - 1) <= pk else p - r for j in range(j1, j2 + 1): a[s2][j] = (a[s1][j] - a[s1][j - 1]) / ndu[pk + 1][rk + j] d += a[s2][j] * ndu[rk + j][pk] if r <= pk: a[s2][k] = -a[s1][k - 1] / ndu[pk + 1][r] d += a[s2][k] * ndu[r][pk] ders[k][r] = d s1, s2 = s2, s1 r = p for k in range(1, n + 1): for j in range(p + 1): ders[k][j] *= r r *= (p - k) return ders