#!/usr/bin/env python3 """Read numbers off the paper's vector figures (RULES_E_figures.txt). Figure reading, not a test. Usage: python3 -I e_figures.py SUPPLIED_PDF OUT_DIR Standard library only (uses pdf_objects.py and pdf_vector.py beside it). Writes OUT_DIR/E_figures.json and, for RULES_C's calibration, OUT_DIR/E_fig4_curves.json (digitised PRL and BGL curves; derived numbers, not for hosting). """ import json import os import re import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import pdf_objects as po # noqa: E402 import pdf_vector as pv # noqa: E402 sys.stdout.reconfigure(encoding="utf-8") PDF, OUT = sys.argv[1], sys.argv[2] data, objs, parsed = po.read_pdf(PDF) def load(objnum): r = pv.Renderer(parsed, objs) r.run_form(objnum, [1, 0, 0, 1, 0, 0]) return r.paths, r.texts def num(s): try: return float(s) except ValueError: return None def panels(paths): """Panel frames: black polylines with >= 5 points spanning a wide box.""" fr = [] for p in paths: if p["stroke"] == [0.0, 0.0, 0.0] and len(p["pts"]) >= 5: xs = [q[0] for q in p["pts"]] ys = [q[1] for q in p["pts"]] if max(xs) - min(xs) > 300 and max(ys) - min(ys) > 50: box = (round(min(xs), 2), round(min(ys), 2), round(max(xs), 2), round(max(ys), 2)) if box not in fr: fr.append(box) return sorted(fr, key=lambda b: b[1]) def ticks(paths, box, side): """Major tick positions on a frame side: black 2-point segments starting on that side, with the longest tick length.""" x0, y0, x1, y1 = box out = [] for p in paths: if p["stroke"] != [0.0, 0.0, 0.0] or len(p["pts"]) != 2: continue (a, b), (c, d) = p["pts"] if side in ("left", "right") and abs(b - d) < 0.01: xe = x0 if side == "left" else x1 if (abs(a - xe) < 0.05 or abs(c - xe) < 0.05) and y0 - 0.5 <= b <= y1 + 0.5 and 1 < abs(c - a) < 30: out.append((round(b, 2), round(abs(c - a), 2))) if side == "bottom" and abs(a - c) < 0.01: if (abs(b - y0) < 0.05 or abs(d - y0) < 0.05) and x0 - 0.5 <= a <= x1 + 0.5 and 1 < abs(d - b) < 30: out.append((round(a, 2), round(abs(d - b), 2))) if not out: return [] L = max(t[1] for t in out) return sorted(set(t[0] for t in out if abs(t[1] - L) < 0.2)) def labels(texts, box, side): x0, y0, x1, y1 = box res = [] for t in texts: v = num(t["s"].strip()) if v is None or t["angle"] != 0.0: continue if side == "left" and t["x"] < x0 and y0 - 15 <= t["y"] <= y1 + 5: res.append((t["y"], v)) if side == "right" and t["x"] > x1 and y0 - 15 <= t["y"] <= y1 + 5: res.append((t["y"], v)) res = sorted(set(res)) return res def fit(pairs): n = len(pairs) mx = sum(p[0] for p in pairs) / n my = sum(p[1] for p in pairs) / n sxx = sum((p[0] - mx) ** 2 for p in pairs) sxy = sum((p[0] - mx) * (p[1] - my) for p in pairs) b = sxy / sxx a = my - b * mx res = max(abs(a + b * p[0] - p[1]) for p in pairs) return a, b, res def yaxis(paths, texts, box, side): tk = ticks(paths, box, side) lb = labels(texts, box, side) if len(tk) != len(lb) or len(tk) < 2: return None, {"ticks": tk, "labels": lb} pairs = list(zip(tk, [v for _, v in lb])) a, b, r = fit(pairs) return (a, b), {"pairs": pairs, "max_residual": round(r, 4)} def xaxis(paths, texts, box, bottom_labels): tk = ticks(paths, box, "bottom") if len(tk) != len(bottom_labels): return None, {"ticks": tk, "labels": bottom_labels} pairs = list(zip(tk, bottom_labels)) a, b, r = fit(pairs) return (a, b), {"pairs": pairs, "max_residual": round(r, 4)} def curve_points(paths, color, box): x0, y0, x1, y1 = box pts = [] for p in paths: if p["stroke"] == color and len(p["pts"]) > 20: inside = [q for q in p["pts"] if x0 - 1 <= q[0] <= x1 + 1 and y0 - 200 <= q[1] <= y1 + 200] if len(inside) > 0.9 * len(p["pts"]) and sum(1 for q in p["pts"] if y0 - 1 <= q[1] <= y1 + 1) > 0.5 * len(p["pts"]): pts.extend(p["pts"]) return pts def daily_means(tv): """tv: list of (day_float, value) in drawing order. Time-weighted daily means per UT day.""" tv = sorted(tv) out = {} for i, (t, v) in enumerate(tv): tl = tv[i - 1][0] if i > 0 else t tr = tv[i + 1][0] if i + 1 < len(tv) else t w = max(0.0, (tr - tl) / 2) if w > 0.05: # gaps longer than ~2.4 h get no weight beyond their neighbours w = 0.0 d = int(t) s = out.setdefault(d, [0.0, 0.0, 99.0, 0.0]) s[0] += w * v s[1] += w s[2] = min(s[2], t) s[3] = max(s[3], t) return {d: {"mean": round(s[0] / s[1], 1) if s[1] else None, "from_UT_h": round((s[2] - d) * 24, 2), "to_UT_h": round((s[3] - d) * 24, 2)} for d, s in sorted(out.items())} result = {} # ---------------- Fig. 3 (form 147) ---------------- paths, texts = load(147) frames = panels(paths) result["fig3_frames"] = frames chf, kak = frames[0], frames[1] xa, xinfo = xaxis(paths, texts, chf, [9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5]) result["fig3_xaxis"] = xinfo for name, box, truth in (("KAK", kak, [42.5, 40.1, 55.6, 46.8]), ("CHF", chf, [106.6, 109.4, 130.9, 103.2])): ya, yinfo = yaxis(paths, texts, box, "left") yr, yrinfo = yaxis(paths, texts, box, "right") rec = {"left_axis": yinfo, "right_axis": yrinfo} pts = curve_points(paths, [1.0, 0.0, 0.0], box) tv = [(xa[0] + xa[1] * x, ya[0] + ya[1] * y) for x, y in pts] dm = daily_means(tv) rec["n_points"] = len(tv) rec["daily_means_curve"] = {str(k): v for k, v in dm.items()} # hourly maxima on 11 May hrs = {} for t, v in tv: if 11 <= t < 12: h = int((t - 11) * 24) hrs.setdefault(h, []).append(v) hm = {h: sum(v) / len(v) for h, v in hrs.items()} if hm: hmax = max(hm, key=hm.get) rec["11May_highest_hour_UT"] = [hmax, round(hm[hmax], 1)] # bars: blue closed polylines within the panel; top edge y on the right axis bars = [] for p in paths: if p["stroke"] == [0.0, 0.0, 1.0] and 4 <= len(p["pts"]) <= 6: xs = [q[0] for q in p["pts"]] ys = [q[1] for q in p["pts"]] if box[0] <= min(xs) and max(xs) <= box[2] and box[1] - 1 <= min(ys) and max(ys) <= box[3] + 1 and max(xs) - min(xs) < 40: bars.append((round(xa[0] + xa[1] * (min(xs) + max(xs)) / 2, 3), round(yr[0] + yr[1] * max(ys), 1))) rec["bars_day_value"] = sorted(bars) rec["text_values"] = truth result["fig3_" + name] = rec # ---------------- Fig. 4 (form 150) ---------------- paths, texts = load(150) frames = panels(paths) result["fig4_frames"] = frames bgl, prl, gcr = frames[0], frames[1], frames[2] xa4, xinfo4 = xaxis(paths, texts, bgl, [9, 9.5, 10, 10.5, 11, 11.5, 12, 12.5]) result["fig4_xaxis"] = xinfo4 curves = {} for name, box in (("PRL", prl), ("BGL", bgl)): ya, yinfo = yaxis(paths, texts, box, "left") pts = curve_points(paths, [1.0, 0.0, 0.0], box) tv = sorted((xa4[0] + xa4[1] * x, ya[0] + ya[1] * y) for x, y in pts) curves[name] = [[round(t, 6), round(v, 2)] for t, v in tv] result["fig4_" + name] = {"left_axis": yinfo, "n_points": len(tv), "daily_means_curve": {str(k): v for k, v in daily_means(tv).items()}} json.dump(curves, open(os.path.join(OUT, "E_fig4_curves.json"), "w", encoding="utf-8", newline="\n")) # ---------------- Fig. 7 (form 157) ---------------- paths, texts = load(157) frames = panels(paths) result["fig7_frames"] = frames cg = frames[0] tk = ticks(paths, cg, "bottom") result["fig7_bottom_ticks"] = tk # 0..24 h axis: frame spans 0 to 24 h (labels 0..24 every 2 h) xa7 = (0.0 - 24.0 * cg[0] / (cg[2] - cg[0]), 24.0 / (cg[2] - cg[0])) ya7, yinfo7 = yaxis(paths, texts, cg, "left") result["fig7_left_axis"] = yinfo7 rec = {} for cname, col in (("red_11May", [1.0, 0.0, 0.0]), ("green_14day", [0.0, 1.0, 0.0]), ("black_10May", [0.0, 0.0, 0.0]), ("blue_12May", [0.0, 0.0, 1.0])): pts = curve_points(paths, col, cg) if not pts: continue tv = sorted((xa7[0] + xa7[1] * x, ya7[0] + ya7[1] * y) for x, y in pts) tmax, vmax = max(tv, key=lambda q: q[1]) rec[cname] = {"n": len(tv), "max_value": round(vmax, 1), "max_time_UT_h": round(tmax, 3)} # vertical black line through the panels for p in paths: if p["stroke"] == [0.0, 0.0, 0.0] and len(p["pts"]) == 2: (a, b), (c, d) = p["pts"] if abs(a - c) < 0.05 and abs(d - b) > 200: rec.setdefault("vertical_lines_UT_h", []).append(round(xa7[0] + xa7[1] * a, 3)) result["fig7"] = rec # ---------------- Fig. 1 (form 141) ---------------- paths, texts = load(141) frames = panels(paths) result["fig1_frames"] = frames prot = frames[-1] bottom = frames[0] labs1 = sorted(num(t["s"]) for t in texts if num(t["s"].strip()) is not None and t["y"] < bottom[1] and t["angle"] == 0.0) tk1 = ticks(paths, bottom, "bottom") result["fig1_bottom_ticks_n"] = len(tk1) # x mapping from the first two labelled ticks: 9 and 9.5 ... use ticks spacing and the label '9' at the frame's left xa1 = None if len(tk1) >= 2: step = tk1[1] - tk1[0] xa1 = (9 - tk1[0] / (2 * step), 1 / (2 * step)) # assumes major ticks every 0.5 day starting at day 9 ya1, yinfo1 = None, None rec1 = {} if xa1: for cname, col in (("10MeV", [0.58, 0.0, 0.828]), ("50MeV", [1.0, 0.0, 1.0]), ("100MeV", [0.0, 1.0, 0.0])): pts = curve_points(paths, col, prot) on11 = [(xa1[0] + xa1[1] * x, y) for x, y in pts if 11 <= xa1[0] + xa1[1] * x < 12] if on11: t, y = max(on11, key=lambda q: q[1]) rec1[cname] = {"max_time_UT_h_on_11May": round((t - 11) * 24, 2)} vl = [] for p in paths: if p["stroke"] == [0.0, 0.0, 0.0] and len(p["pts"]) == 2: (a, b), (c, d) = p["pts"] if abs(a - c) < 0.05 and abs(d - b) > 300: vl.append(round(xa1[0] + xa1[1] * a, 4)) rec1["vertical_lines_day"] = sorted(set(vl)) rec1["x_mapping"] = {"ticks_first": tk1[:3], "assumed_step_day": 0.5} result["fig1"] = rec1 json.dump(result, open(os.path.join(OUT, "E_figures.json"), "w", encoding="utf-8", newline="\n"), indent=1) print(json.dumps(result, indent=1)[:12000])