#!/usr/bin/env python3 """Draw the three charts of /south-atlantic-anomaly-growth/ from saa_pole.py's outputs, as inline SVG. Usage: python3 -I -B charts.py Writes chart_saa_min.svg.html, chart_saa_area.svg.html, chart_speed.svg.html and chart_rates.svg.html. Standard library only. Colours are classes styled by the page's page.css with the site's --chart-* tokens: blue for gufm1 and IGRF-14, orange for the other models and the alternative assumption, ink for the ground surveys, the band for spans the caption names. """ import csv import json import os import sys src, out = sys.argv[1], sys.argv[2] J = json.load(open(os.path.join(src, "saa-pole-numbers.json"), encoding="utf-8")) rows = list(csv.DictReader(open(os.path.join(src, "saa_yearly.csv"), encoding="utf-8"))) spl = {} for r in rows: y = int(r["year"]) if (r["model"] == "gufm1" and y <= 1899) or r["model"].startswith("IGRF-14"): spl[y] = r fine = {} for r in csv.DictReader(open(os.path.join(src, "pole_speeds_fine.csv"), encoding="utf-8")): fine.setdefault(r["model"], []).append((float(r["mid_year"]), float(r["km_per_yr"]))) rates = list(csv.DictReader(open(os.path.join(src, "axial_dipole_rates.csv"), encoding="utf-8"))) EARTH = 510.0656217 def f1(x): return "%.1f" % x def grid_y(vals, x0, x1, y_of, fmt): return "".join('%s' % (f1(x0), f1(y_of(v)), f1(x1), f1(y_of(v)), f1(x0 - 8), f1(y_of(v) + 4.5), fmt(v)) for v in vals) def axis_x(ticks, y, x_of, x0, x1): s = ['' % (f1(x0), f1(y), f1(x1), f1(y))] for t in ticks: x = x_of(t) s.append('%d' % (f1(x), f1(y), f1(x), f1(y + 6), f1(x), f1(y + 22), t)) return "".join(s) def path(pts, x_of, y_of): return "M" + " L".join("%s %s" % (f1(x_of(a)), f1(y_of(b))) for a, b in pts) def nt(v): return "{:,.0f}".format(v) def write(name, svg): with open(os.path.join(out, name), "w", encoding="utf-8", newline="\n") as f: f.write(svg + "\n") alt = J["alternatives_1800"] c, lo, hi = alt["whole field rescaled, 2.28 nT/yr"], alt["whole field rescaled, -1.70 nT/yr"], alt["whole field rescaled, 18.00 nT/yr"] other = J["anomaly_other_models"] # ---------- 1a: the weakest field, 1800-2030 ---------- W, H = 1000, 300 x0, x1, t0, t1 = 66.0, 986.0, 1800.0, 2030.0 yt, yb, vt, vb = 18.0, 252.0, 30000.0, 21000.0 x_of = lambda t: x0 + (t - t0) * (x1 - x0) / (t1 - t0) y_of = lambda v: yt + (vt - v) * (yb - yt) / (vt - vb) meas = [(y, float(spl[y]["weakest_F_nT"])) for y in range(1800, 2026)] fc = [(y, float(spl[y]["weakest_F_nT"])) for y in range(2025, 2031)] s = ['' % (W, H, nt(meas[0][1]), nt(float(spl[1840]["weakest_F_nT"])), nt(float(spl[1900]["weakest_F_nT"])), nt(float(spl[2025]["weakest_F_nT"])), nt(fc[-1][1]), nt(c["min_F"]), nt(lo["min_F"]), nt(hi["min_F"]))] s.append('' % (f1(x_of(1800)), f1(yt), f1(x_of(1840) - x_of(1800)), f1(yb - yt))) s.append(grid_y([22000, 24000, 26000, 28000, 30000], x0, x1, y_of, nt)) s.append('Before 1840: strengthassumed, not measured' % (f1(x_of(1802)), f1(yb - 26), f1(x_of(1802)), f1(yb - 9))) s.append('1900: gufm1 → IGRF-14' % (f1(x_of(1900)), f1(yt), f1(x_of(1900)), f1(yb), f1(x_of(1902)), f1(yt + 14))) s.append('' % (path(meas, x_of, y_of), path(fc, x_of, y_of))) for t, v in ((1840, None), (1900, None), (2025, None)): v = float(spl[t]["weakest_F_nT"]) s.append('%d: %s nT' % (t, nt(v), f1(x_of(t)), f1(y_of(v)))) s.append('%s nT in 1800' % (f1(x_of(1800) + 10), f1(y_of(meas[0][1]) - 8), nt(meas[0][1]))) s.append('%s in 1840' % (f1(x_of(1840) + 6), f1(y_of(float(spl[1840]["weakest_F_nT"])) + 24), nt(float(spl[1840]["weakest_F_nT"])))) s.append('%s in 1900' % (f1(x_of(1900) + 9), f1(y_of(float(spl[1900]["weakest_F_nT"])) + 20), nt(float(spl[1900]["weakest_F_nT"])))) s.append('%s in 2025' % (f1(x_of(2025) - 10), f1(y_of(float(spl[2025]["weakest_F_nT"])) + 22), nt(float(spl[2025]["weakest_F_nT"])))) # the alternative for 1800 xa = x_of(1800) + 3 s.append('1800 with Gubbins and colleagues’ slower dipole trend: %s nT; with the published pre-1840 rates, %s to %s' '' % (nt(c["min_F"]), nt(lo["min_F"]), nt(hi["min_F"]), f1(xa), f1(y_of(hi["min_F"])), f1(xa), f1(y_of(lo["min_F"])), f1(xa), f1(y_of(c["min_F"])))) s.append('%s with a slower trend' % (f1(xa + 10), f1(y_of(c["min_F"]) + 16), nt(c["min_F"]))) for o in other: s.append('%s, %d: %s nT' % (o["model"], o["year"], nt(o["min_F"]), f1(x_of(o["year"])), f1(y_of(o["min_F"])))) s.append(axis_x(list(range(1800, 2031, 25)), yb, x_of, x0, x1)) s.append("") write("chart_saa_min.svg.html", "".join(s)) # ---------- 1b: the area below 32,000 nT, as a share of Earth's surface ---------- yt, yb, vt, vb = 18.0, 252.0, 24.0, 0.0 y_of = lambda v: yt + (vt - v) * (yb - yt) / (vt - vb) meas = [(y, float(spl[y]["area_pct_of_earth"])) for y in range(1800, 2026)] fc = [(y, float(spl[y]["area_pct_of_earth"])) for y in range(2025, 2031)] pc = lambda mk: 100 * mk / EARTH s = ['' % (W, H, meas[0][1], float(spl[1840]["area_pct_of_earth"]), float(spl[1900]["area_pct_of_earth"]), float(spl[2025]["area_pct_of_earth"]), fc[-1][1], pc(c["area_Mkm2"]), pc(hi["area_Mkm2"]), pc(lo["area_Mkm2"]))] s.append('' % (f1(x_of(1800)), f1(yt), f1(x_of(1840) - x_of(1800)), f1(yb - yt))) s.append(grid_y([0, 5, 10, 15, 20], x0, x1, y_of, lambda v: "%d%%" % v)) s.append('' % (f1(x_of(1900)), f1(yt), f1(x_of(1900)), f1(yb))) s.append('' % (path(meas, x_of, y_of), path(fc, x_of, y_of))) for t in (1840, 1900, 2025): v = float(spl[t]["area_pct_of_earth"]) s.append('%d: %.1f%% of Earth’s surface, %.1f million km²' % (t, v, float(spl[t]["area_below_32000_nT_million_km2"]), f1(x_of(t)), f1(y_of(v)))) s.append('%.1f%% in 1800' % (f1(x_of(1800) + 12), f1(y_of(meas[0][1]) + 20), meas[0][1])) s.append('%.1f%% in 1840' % (f1(x_of(1840) + 8), f1(y_of(float(spl[1840]["area_pct_of_earth"])) + 24), float(spl[1840]["area_pct_of_earth"]))) s.append('%.1f%% in 1900' % (f1(x_of(1900) + 9), f1(y_of(float(spl[1900]["area_pct_of_earth"])) + 22), float(spl[1900]["area_pct_of_earth"]))) s.append('%.1f%% in 2025' % (f1(x_of(2025) - 10), f1(y_of(float(spl[2025]["area_pct_of_earth"])) - 10), float(spl[2025]["area_pct_of_earth"]))) s.append('1800 with Gubbins and colleagues’ slower dipole trend: %.2f%%; with the published pre-1840 rates, %.2f%% to %.2f%%' '' % (pc(c["area_Mkm2"]), pc(hi["area_Mkm2"]), pc(lo["area_Mkm2"]), f1(xa), f1(y_of(pc(lo["area_Mkm2"]))), f1(xa), f1(y_of(pc(hi["area_Mkm2"]))), f1(xa), f1(y_of(pc(c["area_Mkm2"]))))) s.append('%.1f%% with a slower trend' % (f1(xa + 10), f1(y_of(pc(c["area_Mkm2"])) - 16), pc(c["area_Mkm2"]))) for o in other: s.append('%s, %d: %.1f%%' % (o["model"], o["year"], o["area_pct"], f1(x_of(o["year"])), f1(y_of(o["area_pct"])))) s.append(axis_x(list(range(1800, 2031, 25)), yb, x_of, x0, x1)) s.append("") write("chart_saa_area.svg.html", "".join(s)) # ---------- 2: the north dip pole's speed, 1960-2030 ---------- W, H = 1000, 360 x0, x1, t0, t1 = 58.0, 986.0, 1960.0, 2030.0 yt, yb, vt, vb = 18.0, 316.0, 65.0, 0.0 x_of = lambda t: x0 + (t - t0) * (x1 - x0) / (t1 - t0) y_of = lambda v: yt + (vt - v) * (yb - yt) / (vt - vb) ig = [p for p in fine["IGRF-14"] if p[0] >= 1960.0] cu = [p for p in fine["IGRF-14, cubic control"] if p[0] >= 1960.0] co = [p for p in fine["COV-OBS.x2"] if p[0] >= 1960.0] ch = fine["CHAOS-8.7"] st = J["step_test"]["igrf14_1900_2029"] mx = J["fine_maxima"] s = ['' % (W, H)] s.append('' % (f1(x_of(2025)), f1(yt), f1(x_of(2030) - x_of(2025)), f1(yb - yt))) for e in range(1965, 2030, 5): s.append('' % (f1(x_of(e)), f1(yt), f1(x_of(e)), f1(yb))) s.append(grid_y([0, 10, 20, 30, 40, 50, 60], x0, x1, y_of, lambda v: "%d" % v)) s.append('forecast' % (f1(x_of(2030) - 4), f1(yb - 8))) for a, b, v in ((1962.5, 1973.5, 9.1), (1973.5, 1983.9, 11.3), (1983.9, 1994.0, 14.9), (1994.0, 2001.0, 51.5), (2001.0, 2007.3, 52.0)): s.append('Ground surveys, %.1f to %.1f: %.1f km a year' % (a, b, v, f1(x_of(a)), f1(y_of(v)), f1(x_of(b)), f1(y_of(v)))) s.append('' % path(co, x_of, y_of)) s.append('' % path(ch, x_of, y_of)) s.append('' % path(cu, x_of, y_of)) s.append('' % path(ig, x_of, y_of)) pk = max((p for p in ig if p[0] < 2025), key=lambda p: p[1]) s.append('IGRF-14’s step: 58.5 in 2010–11' % (f1(x_of(2010.4)), f1(y_of(62.6)))) s.append('continuous models: about 58 in 2002' % (f1(x_of(2001.2)), f1(y_of(62.6)))) s.append('1995: +22' % (f1(x_of(1995) + 6), f1(y_of(40)))) s.append('ground surveys' % (f1(x_of(1963)), f1(y_of(9.1) - 8))) s.append('36.6 in 2024–25' % (f1(x_of(2024.5) - 6), f1(y_of(36.6) + 22))) s.append(axis_x(list(range(1960, 2031, 10)), yb, x_of, x0, x1)) s.append("") write("chart_speed.svg.html", "".join(s)) # ---------- 3: how fast the axial dipole weakened ---------- W, H = 1000, 330 x0, x1, t0, t1 = 58.0, 986.0, 1840.0, 2030.0 yt, yb, vt, vb = 18.0, 290.0, 32.0, -6.0 x_of = lambda t: x0 + (t - t0) * (x1 - x0) / (t1 - t0) y_of = lambda v: yt + (vt - v) * (yb - yt) / (vt - vb) cr = J["chaos_yearly_absg10_loss_nT"] s = ['' % (W, H)] s.append(grid_y([-5, 0, 5, 10, 15, 20, 25, 30], x0, x1, y_of, lambda v: ("−%d" % -v) if v < 0 else "%d" % v)) s.append('' % (f1(x0), f1(y_of(0)), f1(x1), f1(y_of(0)))) bw = (x_of(1905) - x_of(1900)) * 0.78 for r in rates: a = int(r["span"][:4]) v = float(r["igrf14_absg10_loss_nT_per_yr"]) xm = x_of(a + 2.5) cls = "r-fc" if r["forecast"] == "True" else "r-bar" s.append('IGRF-14, %s: %.2f nT a year%s' % (r["span"].replace("-", "–"), v, " (forecast)" if r["forecast"] == "True" else "", cls, f1(xm - bw / 2), f1(y_of(v)), f1(bw), f1(y_of(0) - y_of(v)))) cov = J.get("covobs_five_year_absg10_loss") pts = [] for e in range(1840, 2020, 5): v = J["covobs_five_year"][str(e)] if "covobs_five_year" in J else None if v is not None: pts.append((e + 2.5, v)) s.append('' % path(pts, x_of, y_of)) for t, v in pts: s.append('COV-OBS.x2, %d–%d: %s nT a year' % (t - 2.5, t + 2.5, ("%.1f" % v).replace("-", "−"), f1(x_of(t)), f1(y_of(v)))) for k, v in cr.items(): a = int(k[:4]) s.append('CHAOS-8.7, %s: %.1f nT' % (k.replace("-", "–"), v, f1(x_of(a + 0.5)), f1(y_of(v)))) s.append('30.4' % (f1(x_of(1917.5)), f1(y_of(30.4) - 7))) s.append('23.8' % (f1(x_of(1982.5)), f1(y_of(23.8) - 7))) s.append('forecast 12.6' % (f1(x_of(2030)), f1(y_of(12.6) - 22))) s.append('COV-OBS.x2: the dipole strengthened' % (f1(x_of(1856)), f1(y_of(-3.4)))) s.append(axis_x(list(range(1850, 2031, 25)), yb, x_of, x0, x1)) s.append("") write("chart_rates.svg.html", "".join(s)) print("wrote 4 charts to", out)