"""Thermal labels: does the corrected curve keep the cycle beyond 5 noises of the night level? For each Thermal displacement label: night level = median of corr over the night before (20:00-03:00 UTC of the previous evening; on the first day, the measurements before the label), own noise = median |difference between two measurements| over that night (as ranges.py); then the hourly median of corr within the label minus the night level, in noises (the hourly median removes the agitation of the visibility labels that overlap the thermal days). Prints the targets whose hourly deviation exceeds 5 noises, largest first, in px and mm. Usage: .venv/bin/python thermal.py """ import json, sys, statistics from datetime import datetime, timedelta, timezone sys.path.insert(0, "tools/scripts") import data REFS = {"8002", "8004", "8011", "8012", "8013", "8014", "8017", "8019", "92H"} def T(s): return datetime.strptime(s, "%Y-%m-%dT%H:%MZ").replace(tzinfo=timezone.utc) def main(): labs = [json.loads(l) for l in open(sys.argv[1]) if l.strip()] labs = [l for l in labs if l["family"] == "Thermal displacement"] d = data.load("data/LENZSPITZE") for lab in labs: s, e = T(lab["start"]), T(lab["end"]) day = s.replace(hour=0, minute=0) n0, n1 = day - timedelta(hours=4), day + timedelta(hours=3) if s.day == 16: n0, n1 = day, s out = [] for name, ms in d.items(): if name in REFS: continue for c in ("corr_x", "corr_y"): night = [m[c] for m in ms if n0 <= m["t"] < n1 and m[c] is not None] if len(night) < 6: continue lvl = statistics.median(night) noise = statistics.median(abs(a - b) for a, b in zip(night[1:], night[:-1])) if noise <= 0: continue hours = {} for m in ms: if s < m["t"] < e and m[c] is not None: hours.setdefault(m["t"].replace(minute=0), []).append(m[c]) best = None for h, v in hours.items(): if len(v) < 6: continue dev = statistics.median(v) - lvl if best is None or abs(dev) > abs(best[0]): best = (dev, h) if best and abs(best[0]) / noise > 5: k = (ms[0].get("corr_x_mm") or 0) / ms[0]["corr_x"] if ms[0].get("corr_x_mm") and ms[0]["corr_x"] else None mm = f" ({best[0]*k:+.2f} mm)" if k else "" out.append((abs(best[0]) / noise, f"{name} {c} {best[0]:+.3f} px{mm} = {best[0]/noise:+.1f} noises, hour {best[1]:%H}")) out.sort(reverse=True) print(f"{lab['id']} {lab['start']} -> {lab['end']}: {len(out)} target-columns > 5 noises (hourly median vs night {n0:%d %H}-{n1:%d %H})") for _, txt in [o for o in out if (len(sys.argv) < 3 or o[1].split()[0] == sys.argv[2])][:8]: print(" ", txt) if __name__ == "__main__": main()