"""Visibility labels: ranges.py --k 5 --rolling R on label +-12 h, noise outside the label; keep the ranges overlapping the label (targets only).""" import json, subprocess, sys, re from datetime import datetime, timedelta D = sys.argv[1]; R = sys.argv[2] if len(sys.argv) > 2 else "4" f = lambda s: datetime.strptime(s, "%Y-%m-%dT%H:%MZ") g = lambda d: d.strftime("%Y-%m-%dT%H:%MZ") for line in open(f"{D}/work/requalify_input.jsonl"): L = json.loads(line) if L["family"] != "Visibility": continue s, e = f(L["start"]), f(L["end"]) for name, extra in [("roll" + R, ["--rolling", R]), ("plain", [])]: cmd = ["python3", "tools/scripts/ranges.py", "data/JUNGFRAU", "--columns", "corr_x,corr_y", "--k", "5", "--start", g(s - timedelta(hours=12)), "--end", g(e + timedelta(hours=12)), "--outside", f"{L['start']}/{L['end']}"] + extra txt = subprocess.run(cmd, capture_output=True, text=True).stdout open(f"{D}/work/rq/{L['id']}_wide_{name}.txt", "w").write(" ".join(cmd) + "\n" + txt) best = {}; col = None for l in txt.splitlines(): m = re.match(r"(corr_[xy]): ranges", l) if m: col = m.group(1); continue if l.startswith("=="): col = None; continue p = l.split() if col and len(p) >= 8 and re.match(r"\d{4}-", p[1]) and not p[0].startswith("REF"): try: z = float(p[5]); t = f(p[6]) except ValueError: continue if not (s <= t <= e): continue k = (p[0], col) if abs(z) > abs(best.get(k, (0,))[0]): best[k] = (z, p[6], int(p[3])) top = sorted(best.items(), key=lambda kv: -abs(kv[1][0]))[:5] print(L["id"], L["start"][5:], L["end"][5:], name, len(best), "|", "; ".join(f"{a} {c[-1]} {z:+.1f} @{t[11:16]} n={n}" for (a, c), (z, t, n) in top) or "none")