"""Run tools/scripts/ranges.py --k 5 for each label and keep the ranges inside it (targets only, PR04 excluded). Non-thermal: window label +-6h, --rolling 4 (local level). Slow ones (ids in SLOW): --outside the label. Thermal: window from 6h before the label to its end, --outside the label (level and noise = the night before), ranges of at least 3 measurements, in daytime (06-20 UTC), outside the non-thermal labels. Usage: ranges_per_label.py labels.jsonl""" import sys, json, subprocess, re from datetime import timedelta sys.path.insert(0, 'tools/scripts'); import data SLOW = {'NORDEND-257', 'NORDEND-262', 'NORDEND-91'} L = [json.loads(x) for x in open(sys.argv[1])] OTHER = [(data.parse_time(x['start']), data.parse_time(x['end'])) for x in L if x['family'] != 'Thermal displacement'] by = data.load('data/NORDEND') sensors = [s for s in by if s != 'PR04'] refs = {s for s in by if data.is_reference(by[s])} f = lambda t: data.iso(t)[:16] + 'Z' for l in L: a, b = data.parse_time(l['start']), data.parse_time(l['end']) th = l['family'] == 'Thermal displacement' if th: args = ['--start', f(a - timedelta(hours=6)), '--end', f(b), '--outside', f(a) + '/' + f(b), '--min-measurements', '3'] elif l.get('id') in SLOW: args = ['--start', f(a - timedelta(hours=6)), '--end', f(b + timedelta(hours=6)), '--outside', f(a) + '/' + f(b)] else: args = ['--start', f(a - timedelta(hours=6)), '--end', f(b + timedelta(hours=6)), '--rolling', '4'] cmd = ['python3', 'tools/scripts/ranges.py', 'data/NORDEND', '--columns', 'corr_x,corr_y', '--k', '5', '--sensors', ','.join(sensors)] + args out = subprocess.run(cmd, capture_output=True, text=True).stdout col, hits = None, [] for line in out.splitlines(): m = re.match(r'(corr_[xy]): ranges', line) if m: col = m.group(1); continue p = line.split() if col and len(p) >= 8 and re.match(r'(PR|CTR)\d+', p[0]) and p[1].startswith('20'): s, t0, t1, n, ext, k, at = p[0], data.parse_time(p[1]), data.parse_time(p[2]), int(p[3]), p[4], float(p[5]), data.parse_time(p[6]) if s in refs or not (a < at < b): continue if th and (not 6 <= at.hour < 20 or any(x < at < y for x, y in OTHER)): continue hits.append((abs(k), f"{s}:{col[-1]}{k:+.1f}@{f(at)[11:16]}({n})")) hits.sort(reverse=True) print(f"{l.get('id')}\t{l['start']}→{l['end']}\t{l['family']}\tmax={hits[0][0] if hits else 0:.1f}\t{' '.join(h for _, h in hits[:6])}") print(' cmd:', ' '.join(cmd[:2] + cmd[2:4] + ['--columns corr_x,corr_y --k 5 --sensors '] + args))