#!/usr/bin/env python3 """Digitize Figure 7 of Tacza et al. (2026), J. Space Weather Space Clim. 16, 36 (CC BY 4.0), as fixed in RULES_fig7.txt, RULES_fig7_amendment1.txt and the post-review corrections RULES_fig7_amendment2.txt to RULES_fig7_amendment4.txt: markers span h:00-h+1:00, the curve is traced over the whole plot, one traced column is enough for an hour's value, and the bottom lanes' semi-transparent markers are found by their tint (amendment 4 replaces amendment 3's test). Usage: python3 -I fig7_digitize.py PAPER_PDF OUT_FOLDER PAPER_PDF is the journal's swsc250080.pdf, which you download yourself (see fetch_inputs.py); fig7_image.py, beside this script, takes Figure 7's embedded image out of it and checks it is the one we digitized. Needs numpy and Pillow. Writes OUT_FOLDER/fig7_digitized_hourly.csv (one row per station and UT hour, 7-16 May 2024: our reading of the curve and of the three marker lanes) and OUT_FOLDER/fig7_sigma_by_hour.csv (the half-width of the figure's +-1 sigma band by UT hour, read on the six non-storm days). These are our readings of a published figure, approximate: about 0.2-2 V/m and 15 minutes per pixel, depending on the panel. The figure itself is not redistributed. """ import csv import datetime as dt import os import sys import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from fig7_image import figure7 # noqa: E402 SRC, OUT = sys.argv[1], sys.argv[2] im = np.asarray(figure7(SRC)).astype(int) R, G, B = im[:, :, 0], im[:, :, 1], im[:, :, 2] DARK = im.max(axis=2) < 100 RED = (R > 200) & (G < 110) & (B < 110) YEL = (R > 215) & (G > 195) & (B < 195) & (R - B > 45) GRN = (G > 150) & (G - R > 25) & (G - B > 25) # amendment 4: the bottom lanes' markers are semi-transparent, so over the pale red (May 10) and pale blue (May 11-13) # bands they change colour; they are found by tint, which no background, band edge, sigma band, dashed line or grid has NOT_DARK = im.max(axis=2) >= 100 GRN_TINT = (G - R >= 15) & (G - B >= 20) & NOT_DARK YEL_TINT = (R >= 200) & (R - B >= 30) & (G - B >= 20) & NOT_DARK # the +-1 sigma band: light cyan on white, mauve on the pale red band, mid blue on the pale blue band ENV = ((B > 240) & (G > 225) & (R < 235) & (B - R > 15)) | ((R > 205) & (R < 230) & (G > 180) & (G < 200) & (B > 215)) | \ ((R > 180) & (R < 205) & (G > 200) & (G < 225) & (B > 245)) # Frames (x0, x1, y0, y1) found from the long dark lines; tick values typed from the printed labels. LEFT, RIGHT = (196, 1275), (1516, 2595) ROWS = [(154, 500), (671, 1018), (1189, 1535), (1706, 2053), (2224, 2571), (2742, 3087), (3258, 3605)] PANELS = { # name: (column, row index, [(tick row, tick value), ...]) "CAS1": (LEFT, 0, [(191.0, 50), (326.0, 0), (460.5, -50)]), "BAT": (LEFT, 1, [(755.5, 50), (894.5, 0)]), "CRS": (LEFT, 2, [(1282.5, 50), (1436.0, 0)]), "LM": (LEFT, 3, [(1800.0, 50), (1907.0, 0), (2014.0, -50)]), "SWI_PG": (LEFT, 4, [(2329.0, 200), (2437.0, 0), (2546.0, -200)]), "NCK": (LEFT, 5, [(2746.5, 400), (2842.0, 200), (2938.0, 0), (3032.0, -200)]), "EVO": (LEFT, 6, [(3316.0, 20), (3415.0, 0), (3514.0, -20)]), "CAS2": (RIGHT, 0, [(240.0, 50), (376.5, 0)]), "CLF": (RIGHT, 1, [(673.5, 50), (761.0, 25), (849.5, 0), (937.5, -25)]), "LA": (RIGHT, 2, [(1231.0, 50), (1324.5, 25), (1418.0, 0), (1512.0, -25)]), "HYO": (RIGHT, 3, [(1791.5, 100), (1939.0, 0)]), "SWI_Jz": (RIGHT, 4, [(2304.0, 0.5), (2429.0, 0.0), (2554.0, -0.5)]), "RDG": (RIGHT, 5, [(2814.0, 50), (2937.0, 0), (3059.0, -50)]), } X_TICK0 = {LEFT: 294.5, RIGHT: 1614.5} # 7 May 2024 00:00 UT; ticks every 3 days at 294 px (98.0 px a day) PX_DAY = (1177.0 - 294.5) / 9.0 T0 = dt.datetime(2024, 5, 7) def runs(mask_col): idx = np.where(mask_col)[0] if not len(idx): return [] out, s = [], idx[0] for a, b in zip(idx[:-1], idx[1:]): if b - a > 1: out.append((s, a)); s = b out.append((s, idx[-1])) return out def main(): os.makedirs(OUT, exist_ok=True) rows_out, sig_out = [], [] for name, (col, ri, ticks) in PANELS.items(): x0, x1 = col y0, y1 = ROWS[ri] ys = np.array([t[0] for t in ticks]); vs = np.array([t[1] for t in ticks], float) a, b = np.polyfit(ys, vs, 1) # value = a*row + b yz = (0 - b) / a # row of the zero line val = lambda r: a * r + b xt = X_TICK0[col] top, bot = y0 + 3, y1 - 3 # amendment 2: the curve over the whole plot (markers are coloured, not dark) env_top, env_bot = y0 + 30, y1 - 56 # the sigma band is read where it always was # trace the curve column by column centre = {} prev = None for x in range(x0 + 3, x1 - 2): rr = [] for s, e in runs(DARK[top:bot, x]): s += top; e += top if e - s <= 3 and abs((s + e) / 2 - yz) <= 3: continue # a dot of the dotted zero line rr.append((s, e)) if not rr: prev = None continue if prev is None or len(rr) == 1: s, e = max(rr, key=lambda t: t[1] - t[0]) if prev is None else rr[0] else: s, e = min(rr, key=lambda t: abs((t[0] + t[1]) / 2 - prev)) centre[x] = (s + e) / 2 prev = centre[x] # hourly values and markers for h in range(10 * 24): t = T0 + dt.timedelta(hours=h) xf = xt + PX_DAY * h / 24.0 xc = int(round(xf)) cands = [centre[x] for x in (xc - 1, xc, xc + 1) if x in centre] v = float(val(np.median(cands))) if len(cands) >= 1 else None # amendment 2: one column is enough # amendment 2: each marker spans its hour, h:00 to h+1:00 c0, c1 = int(round(xf)), int(round(xt + PX_DAY * (h + 1) / 24.0)) # columns c0 .. c1-1 xdash = int(round(xt + PX_DAY * (3 + 17 / 24.0))) # the dashed 17 UT line of May 10 red_cols = [c for c in range(c0, c1) if abs(c - xdash) > 2] def marked(mask, ra, rb, cols): if not cols: return False block = mask[ra:rb, cols] return bool(block.mean() >= 0.5) cloud = marked(RED, y0 + 15, y0 + 28, red_cols) era5 = marked(GRN_TINT, y1 - 54, y1 - 41, list(range(c0, c1))) # amendment 4 inst = marked(YEL_TINT, y1 - 30, y1 - 17, list(range(c0, c1))) rows_out.append([name, t.strftime("%Y-%m-%d %H:%M"), "" if v is None else round(v, 3), int(cloud), int(inst), int(era5), int(v is not None and not (cloud or inst or era5))]) # sigma band half-width, by UT hour, on the six non-storm days for hod in range(24): hw = [] for day in (0, 1, 2, 7, 8, 9): xc = int(round(xt + PX_DAY * (day + hod / 24.0))) env = np.where(ENV[env_top:env_bot, xc])[0] if len(env) < 4: continue r_top, r_bot = env_top + env.min(), env_top + env.max() if r_top < yz < r_bot: hw.append(abs(a) * (r_bot - r_top) / 2.0) sig_out.append([name, hod, round(float(np.median(hw)), 3) if hw else "", len(hw)]) print(name, "zero row %.1f" % yz, "units/px %.3f" % abs(a), "traced columns", len(centre)) with open(os.path.join(OUT, "fig7_digitized_hourly.csv"), "w", newline="\n", encoding="utf-8") as fh: w = csv.writer(fh, lineterminator="\n") w.writerow(["station", "time_utc", "departure", "cloud_gt90", "instrument_fail", "era5_fail", "retained"]) w.writerows(rows_out) with open(os.path.join(OUT, "fig7_sigma_by_hour.csv"), "w", newline="\n", encoding="utf-8") as fh: w = csv.writer(fh, lineterminator="\n") w.writerow(["station", "ut_hour", "sigma_halfwidth", "n_days_read"]) w.writerows(sig_out) if __name__ == "__main__": main()