"""Thermal labels: hourly median of each target's corr minus its level over the hours before the label (night part), in the target's own noise (median |step| over the batch, as ranges.py). Hours inside a Visibility label are also reported apart (clean = outside every non-thermal long label). usage: requal_thermal.py """ import sys, json, statistics from datetime import timedelta sys.path.insert(0, 'tools/scripts') from data import load, parse_time, is_reference from common import own_noise labs = [json.loads(l) for l in open(sys.argv[1])] B0, B1 = parse_time(sys.argv[2]), parse_time(sys.argv[3]) by = load('data/LENZSPITZE') tg = {s: ms for s, ms in by.items() if not is_reference(ms)} vis = [(parse_time(d['start']), parse_time(d['end'])) for d in labs if d['family'] == 'Visibility'] for d in labs: if d['family'] != 'Thermal displacement': continue s, e = parse_time(d['start']), parse_time(d['end']) out = [] for name, ms in tg.items(): for c in ('corr_x', 'corr_y'): batch = [m[c] for m in ms if B0 <= m['t'] <= B1 and m[c] is not None] noise = own_noise(batch) if not noise: continue night = [m[c] for m in ms if s - timedelta(hours=6) <= m['t'] < s and m[c] is not None] if len(night) < 12: continue lvl = statistics.median(night) h = s best = (0, None, False) while h < e: v = [m[c] for m in ms if h <= m['t'] < h + timedelta(hours=1) and m[c] is not None] if len(v) >= 6: dev = (statistics.median(v) - lvl) / noise clean = not any(a < h + timedelta(hours=1) and b > h for a, b in vis) if abs(dev) > abs(best[0]): best = (dev, h, clean) h += timedelta(hours=1) # best clean h = s; bc = (0, None) while h < e: v = [m[c] for m in ms if h <= m['t'] < h + timedelta(hours=1) and m[c] is not None] if len(v) >= 6 and not any(a < h + timedelta(hours=1) and b > h for a, b in vis): dev = (statistics.median(v) - lvl) / noise if abs(dev) > abs(bc[0]): bc = (dev, h) h += timedelta(hours=1) out.append((abs(best[0]), name, c, best, bc)) out.sort(reverse=True) print(f"== {d['id']} {d['start']} -> {d['end']}") for _, name, c, b, bc in out[:6]: print(f" {name:6s} {c}: {b[0]:+7.1f} at {b[1]:%m-%d %H}h{'' if b[2] else ' (in Visibility)'} | outside Visibility: {bc[0]:+7.1f}" + (f" at {bc[1]:%m-%d %H}h" if bc[1] else ''))