#!/usr/bin/env python3 """Read the hourly background means from Figure 4 of Tacza et al. (2026), J. Space Weather Space Clim. 16, 36 (CC BY 4.0), for the post hoc check in RULES_raw_check.txt. Needs numpy and Pillow. Usage: python3 -I fig4_digitize.py PAPER_PDF OUT_CSV Figure 4 is embedded in the paper's PDF as one CMYK JPEG, 2640 x 3998 pixels; fig7_image.py's reader takes it out the same way as Figure 7 (exact stream, checked by SHA-256, Adobe inversion undone). For each of the 13 panels, the dark blue mean line is read at each UT hour 0-23 on the panel's own tick marks; tick values are typed from the printed labels, top to bottom, and the script stops if the number of tick marks it finds differs. The printed Mean and AvgSD are typed too. """ import csv import os import sys import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from fig7_image import embedded_image # noqa: E402 FIG4_JPEG_SHA256 = "cb8c81bcb69e72741e6d20db8b4526b1e8e986e1611e799606814cc1821e45fb" LEFT, RIGHT = (131, 1278), (1470, 2617) ROWS = [(1, 431), (580, 1009), (1158, 1588), (1737, 2166), (2315, 2745), (2893, 3324), (3472, 3902)] # station: (column, row index, tick labels top to bottom, printed Mean, printed AvgSD) PANELS = { "CAS1": (LEFT, 0, [120, 100, 80, 60], 79.96, 13.02), "CAS2": (RIGHT, 0, [80, 60, 40, 20], 41.46, 12.31), "BAT": (LEFT, 1, [80, 60, 40, 20], 52.58, 17.82), "CLF": (RIGHT, 1, [80, 60, 40, 20], 42.17, 15.74), "CRS": (LEFT, 2, [50, 40, 30, 20], 27.48, 5.48), "LA": (RIGHT, 2, [60, 40, 20], 39.97, 12.49), "LM": (LEFT, 3, [80, 60, 40, 20], 52.43, 15.14), "HYO": (RIGHT, 3, [150, 100, 50], 69.54, 18.99), "SWI_PG": (LEFT, 4, [500, 400, 300, 200, 100], 280.28, 71.41), "SWI_Jz": (RIGHT, 4, [1.0, 0.8, 0.6, 0.4, 0.2], 0.70, 0.14), "NCK": (LEFT, 5, [500, 400, 300, 200], 334.39, 96.51), "RDG": (RIGHT, 5, [80, 60, 40], 71.98, 13.70), "EVO": (LEFT, 6, [40, 20, 0], 28.42, 15.06), } def groups(idx): out, cur = [], [] for i in idx: if cur and i - cur[-1] > 1: out.append(cur); cur = [] cur.append(i) if cur: out.append(cur) return [sum(c) / len(c) for c in out] def main(): pdf, out_csv = sys.argv[1], sys.argv[2] im = np.asarray(embedded_image(pdf, 2640, 3998, FIG4_JPEG_SHA256)).astype(int) R, G, B = im[:, :, 0], im[:, :, 1], im[:, :, 2] dark = im.max(axis=2) < 100 blue = (B > 200) & (R < 120) & (G > 95) & (G < 190) rows = [] for st, ((x0, x1), ri, labels, mean_p, sd_p) in PANELS.items(): y0, y1 = ROWS[ri] yt = groups([y for y in range(y0 + 2, y1 - 1) if dark[y, x0 - 11:x0 - 2].sum() >= 6]) if len(yt) != len(labels): raise SystemExit("%s: %d y ticks found, %d labels" % (st, len(yt), len(labels))) a, b = np.polyfit(yt, labels, 1) xt = groups([x for x in range(x0, x1 + 1) if dark[y1 + 3:y1 + 13, x].sum() >= 6]) hours = [0, 6, 12, 18, 23] if len(xt) != len(hours): raise SystemExit("%s: %d x ticks found" % (st, len(xt))) c, d = np.polyfit(hours, xt, 1) for h in range(24): xc = int(round(c * h + d)) ys = [y for x in (xc - 1, xc, xc + 1) for y in np.where(blue[y0 + 2:y1 - 1, x])[0] + y0 + 2] v = float(a * np.median(ys) + b) if ys else None rows.append([st, h, "" if v is None else round(v, 3), mean_p, sd_p]) print("%-7s y ticks %s x ticks %s" % (st, [round(v) for v in yt], [round(v) for v in xt])) with open(out_csv, "w", newline="\n", encoding="utf-8") as fh: w = csv.writer(fh, lineterminator="\n") w.writerow(["station", "ut_hour", "background_mean", "printed_mean", "printed_avgsd"]) w.writerows(rows) if __name__ == "__main__": main()