"""Thermal labels: deviation from the preceding night level (01:00-11:00 UTC) of the median of 3 consecutive accepted measurements (removes single-measurement spikes: trains, plant), in own noises (noise of ranges_k5.txt). usage: python3 thermal3.py """ import json, sys, statistics sys.path.insert(0, 'tools/scripts') import data D = sys.argv[1] noise, col = {}, None for ln in open(D + '/work/requal/ranges_k5.txt'): if ln.startswith('== '): col = ln.split()[1].rstrip(':'); continue if 'ranges more' in ln: col = None if col: p = ln.split() if len(p) >= 5 and p[1].isdigit(): try: noise[(p[0], col)] = float(p[3]) except ValueError: pass M = data.load('data/NIEDERHORN') for lab in map(json.loads, open(D + '/work/requalify_input.jsonl')): if lab['family'] != 'Thermal displacement': continue day = lab['start'][:10] n0, n1 = data.parse_time(day + 'T01:00Z'), data.parse_time(day + 'T11:00Z') a, b = data.parse_time(lab['start']), data.parse_time(lab['end']) res = [] for s, ms in M.items(): if data.is_reference(ms): continue for c in ('corr_x', 'corr_y'): nz = noise.get((s, c)) if not nz: continue night = [m[c] for m in ms if n0 <= m['t'] <= n1 and m[c] is not None] if len(night) < 4: continue lvl = statistics.median(night) pts = [m for m in ms if a < m['t'] < b and m[c] is not None] for i in range(1, len(pts) - 1): v = statistics.median(p[c] for p in pts[i-1:i+2]) res.append(((v - lvl) / nz, s, c, data.iso(pts[i]['t'])[5:16])) res.sort(key=lambda r: -abs(r[0])) seen, top = set(), [] for r in res: if r[1] not in seen: seen.add(r[1]); top.append(f'{r[1]} {r[2]} {r[0]:+.1f} at {r[3]}') if len(top) == 4: break print(lab['id'], lab['start'][5:16], '; '.join(top))