"""Thermal labels: per target, deviation of corr_x / corr_y from the night level (median of the accepted measurements of the N hours before the label start), in the target's own noise (ranges.py: median of |consecutive differences| over the batch). Instants under another Visibility or Obstruction label (not the plant labels, which touch one target) are skipped, so a passage or the sun does not count for the thermal. Prints, per label, the targets beyond 5 noises on at least two measurements, with the largest deviation, and the largest deviation overall. Usage: python3 thermal_night.py [hours before, default 6]""" import json, sys, statistics from datetime import timedelta sys.path.insert(0, "tools/scripts") import data from common import own_noise D = sys.argv[1] H = int(sys.argv[2]) if len(sys.argv) > 2 else 6 by = data.load("data/NIEDERHORN") B0, B1 = data.parse_time("2026-10-10T17:00Z"), data.parse_time("2026-10-21T12:00Z") labels = [json.loads(l) for l in open(f"{D}/work/requalify_input.jsonl")] others = [(data.parse_time(l["start"]), data.parse_time(l["end"])) for l in labels if l["family"] in ("Obstruction", "Visibility") and "PWD_IMP_B07_016_C09 alone" not in l["description"]] covered = lambda t: any(a < t < b for a, b in others) # the plant labels concern PWD_IMP_B07_016_C09 alone: that target is left out of the thermal test instead targets = [s for s in by if "REF" not in s and s != "PWD_IMP_B07_016_C09"] noise = {} for s in targets: for c in ("corr_x", "corr_y"): v = [m[c] for m in data.filter_window(by[s], B0, B1) if not data.rejected(m) and m[c] is not None] noise[(s, c)] = own_noise(v) if len(v) > 2 else None for lab in labels: if lab["family"] != "Thermal displacement": continue a, b = data.parse_time(lab["start"]), data.parse_time(lab["end"]) rows = [] for s in targets: ms = [m for m in data.filter_window(by[s], a - timedelta(hours=H), b) if not data.rejected(m)] for c in ("corr_x", "corr_y"): n = noise[(s, c)] night = [m[c] for m in ms if m["t"] <= a and m[c] is not None] inside = [(m["t"], m[c]) for m in ms if a < m["t"] < b and m[c] is not None and not covered(m["t"])] if not n or len(night) < 3 or not inside: continue lvl = statistics.median(night) dev = [((v - lvl) / n, t) for t, v in inside] big = [d for d in dev if abs(d[0]) > 5] ext = max(dev, key=lambda d: abs(d[0])) rows.append((abs(ext[0]), s, c, ext, len(big), len(inside))) rows.sort(reverse=True) many = [r for r in rows if r[4] >= 2] print(f"\n{lab['id']} {lab['start']} -> {lab['end']}: {len(many)} target-columns beyond 5 noises on >= 2 measurements") for r in (many or rows)[:6]: _, s, c, (d, t), k, n = r print(f" {s.replace('PWD_', '').replace('_C09', ''):12} {c[-1]} {d:+6.1f} at {t:%dT%H} ({k}/{n} measurements beyond 5)")