"""Field models for the florida-aurora-cycle-25 checks (Fact-Checked Science). Standard library only. Readers for IGRF-14, gufm1 and COV-OBS.x2 (parsed as data); the dipole; centred-dipole and apex latitude. The IGRF reader, the B-spline evaluation, the field synthesis and the field-line tracer are those of the site's aurora_numbers.py (the 1 November page), which reproduce Love et al.'s (2025) geomagnetic latitudes within about 0.1 degree; the gufm1 reader follows the layout the site's saa_pole.py reads (title; nmax, nspl, knots; coefficients; cubic splines). """ import math A_KM = 6371.2 MU0 = 4e-7 * math.pi def read_igrf(path): header, rows = None, [] for line in open(path, encoding="utf-8", errors="replace"): p = line.split() if not p or line.startswith("#"): continue if p[0] == "g/h": header = p elif p[0] in ("g", "h") and header is not None: rows.append(p) cols = header[3:] epochs = {float(c): {} for c in cols if "-" not in c} sv = {} for p in rows: key = (p[0], int(p[1]), int(p[2])) for c, v in zip(cols, p[3:]): if "-" in c: sv[key] = float(v) else: epochs[float(c)][key] = float(v) last = max(epochs) epochs[last + 5.0] = {k: v + 5.0 * sv.get(k, 0.0) for k, v in epochs[last].items()} # IGRF's secular variation, 2025-2030 return epochs def igrf_at(epochs, t): ks = sorted(epochs) for a, b in zip(ks, ks[1:]): if a <= t <= b: w = (t - a) / (b - a) keys = set(epochs[a]) | set(epochs[b]) return {k: (1 - w) * epochs[a].get(k, 0.0) + w * epochs[b].get(k, 0.0) for k in keys} raise ValueError(t) class SplineModel: """gufm1 (kind='gufm1') or COV-OBS.x2 (kind='covobs'): B-spline coefficients in the order g10, g11, h11, g20, ...""" def __init__(self, path, kind): lines = [s for s in open(path, encoding="ascii").read().split("\n")] nums = " ".join(lines[1:]).split() if kind == "gufm1": self.nmax, self.nspl, order = int(nums[0]), int(nums[1]), 4 rest = nums[2:] elif kind == "covobs": self.nmax, self.nspl, order = int(nums[0]), int(nums[1]), int(nums[2]) rest = nums[3:] else: raise ValueError(kind) nk = self.nspl + order self.knots = [float(x) for x in rest[:nk]] vals = [float(x) for x in rest[nk:]] n = self.nmax * (self.nmax + 2) if len(vals) != self.nspl * n: raise ValueError(f"unexpected layout in {path}: {len(vals)} values, expected {self.nspl * n}") self.coefs = [vals[j * n:(j + 1) * n] for j in range(self.nspl)] self.p = order - 1 self.t_first, self.t_last = self.knots[self.p], self.knots[self.nspl] def at(self, t): U, p = self.knots, self.p if not (self.t_first <= t < self.t_last): raise ValueError(f"t = {t} outside {self.t_first}-{self.t_last}") i = p while not (U[i] <= t < U[i + 1]): i += 1 N = [1.0] + [0.0] * p left, right = [0.0] * (p + 1), [0.0] * (p + 1) for j in range(1, p + 1): left[j], right[j] = t - U[i + 1 - j], U[i + j] - t saved = 0.0 for r in range(j): temp = N[r] / (right[r + 1] + left[j - r]) N[r] = saved + right[r + 1] * temp saved = left[j - r] * temp N[j] = saved out = [0.0] * len(self.coefs[0]) for j in range(p + 1): row = self.coefs[i - p + j] for q in range(len(out)): out[q] += N[j] * row[q] c = {} for n in range(1, self.nmax + 1): base = (n - 1) * (n + 1) c[("g", n, 0)] = out[base] for m in range(1, n + 1): c[("g", n, m)] = out[base + 2 * m - 1] c[("h", n, m)] = out[base + 2 * m] return c def dipole(c): g10, g11, h11 = c[("g", 1, 0)], c[("g", 1, 1)], c[("h", 1, 1)] b0 = math.sqrt(g10 ** 2 + g11 ** 2 + h11 ** 2) colat = math.degrees(math.acos(-g10 / b0)) return {"M": 4 * math.pi * (A_KM * 1e3) ** 3 / MU0 * b0 * 1e-9 / 1e22, "plat": 90.0 - colat, "plon": math.degrees(math.atan2(-h11, -g11)), "B0": b0, "g10": g10} def cd_latitude(lat, lon, plat, plon): la, lo, pa, po = map(math.radians, (lat, lon, plat, plon)) return math.degrees(math.asin(max(-1.0, min(1.0, math.sin(la) * math.sin(pa) + math.cos(la) * math.cos(pa) * math.cos(lo - po))))) def geocentric(lat_deg): a, f = 6378.137, 1 / 298.257223563 la = math.radians(lat_deg) n = a / math.sqrt(1 - (2 * f - f * f) * math.sin(la) ** 2) return math.degrees(math.atan2(n * (1 - f) ** 2 * math.sin(la), n * math.cos(la))) def legendre(nmax, theta): ct, st = math.cos(theta), math.sin(theta) P = [[0.0] * (nmax + 2) for _ in range(nmax + 2)] dP = [[0.0] * (nmax + 2) for _ in range(nmax + 2)] P[0][0] = 1.0 for n in range(1, nmax + 1): if n == 1: P[1][1], dP[1][1] = st, ct else: k = math.sqrt((2 * n - 1) / (2 * n)) P[n][n] = k * st * P[n - 1][n - 1] dP[n][n] = k * (ct * P[n - 1][n - 1] + st * dP[n - 1][n - 1]) for m in range(0, n): a, b, d = 2 * n - 1, math.sqrt(max(0.0, (n - 1) ** 2 - m ** 2)), math.sqrt(n ** 2 - m ** 2) p2 = P[n - 2][m] if n >= 2 else 0.0 dp2 = dP[n - 2][m] if n >= 2 else 0.0 P[n][m] = (a * ct * P[n - 1][m] - b * p2) / d dP[n][m] = (a * (ct * dP[n - 1][m] - st * P[n - 1][m]) - b * dp2) / d return P, dP def field(c, r_km, colat_deg, lon_deg, nmax): theta, phi = math.radians(colat_deg), math.radians(lon_deg) P, dP = legendre(nmax, theta) st = math.sin(theta) or 1e-10 br = bt = bp = 0.0 ar = A_KM / r_km for n in range(1, nmax + 1): f = ar ** (n + 2) for m in range(0, n + 1): g = c.get(("g", n, m), 0.0) h = c.get(("h", n, m), 0.0) if m > 0 else 0.0 cm, sm = math.cos(m * phi), math.sin(m * phi) gh = g * cm + h * sm br += (n + 1) * f * gh * P[n][m] bt += -f * gh * dP[n][m] bp += f * m * (g * sm - h * cm) * P[n][m] / st return br, bt, bp def bdir(c, x, y, z, nmax): r = math.sqrt(x * x + y * y + z * z) th = math.acos(max(-1.0, min(1.0, z / r))) ph = math.atan2(y, x) br, bt, bp = field(c, r, math.degrees(th), math.degrees(ph), nmax) st, ct, sp, cp = math.sin(th), math.cos(th), math.sin(ph), math.cos(ph) bx = br * st * cp + bt * ct * cp - bp * sp by = br * st * sp + bt * ct * sp + bp * cp bz = br * ct - bt * st b = math.sqrt(bx * bx + by * by + bz * bz) return bx / b, by / b, bz / b, br def apex_latitude(c, lat_geocentric, lon, step_km=20.0, max_steps=200000): """Apex latitude of the point at the reference sphere: trace the full model's field line to its apex.""" nmax = max(n for (_, n, _) in c) th, ph = math.radians(90.0 - lat_geocentric), math.radians(lon) x, y, z = A_KM * math.sin(th) * math.cos(ph), A_KM * math.sin(th) * math.sin(ph), A_KM * math.cos(th) sgn = 1.0 if bdir(c, x, y, z, nmax)[3] > 0 else -1.0 rmax = prev_r = A_KM h = step_km for _ in range(max_steps): k1 = bdir(c, x, y, z, nmax)[:3] k2 = bdir(c, x + 0.5 * h * sgn * k1[0], y + 0.5 * h * sgn * k1[1], z + 0.5 * h * sgn * k1[2], nmax)[:3] k3 = bdir(c, x + 0.5 * h * sgn * k2[0], y + 0.5 * h * sgn * k2[1], z + 0.5 * h * sgn * k2[2], nmax)[:3] k4 = bdir(c, x + h * sgn * k3[0], y + h * sgn * k3[1], z + h * sgn * k3[2], nmax)[:3] dx = [h * sgn * (k1[i] + 2 * k2[i] + 2 * k3[i] + k4[i]) / 6.0 for i in range(3)] x, y, z = x + dx[0], y + dx[1], z + dx[2] r = math.sqrt(x * x + y * y + z * z) rmax = max(rmax, r) if r < prev_r: if h > 0.5: x, y, z = x - dx[0], y - dx[1], z - dx[2] h /= 4.0 prev_r = math.sqrt(x * x + y * y + z * z) continue break prev_r = r h = max(step_km, 0.01 * r) if h >= step_km else h if r > 60 * A_KM: break lam = math.degrees(math.acos(math.sqrt(min(1.0, A_KM / rmax)))) return lam if lat_geocentric >= 0 else -lam