"""Per instant of the given windows: sensors whose corr deviates >= K noises from the rolling median (+-4, without the point), rejections, error_value. Usage: python3 look.py START END [K] ...""" import sys, statistics, pickle, os sys.path.insert(0, 'tools/scripts') import data from datetime import timedelta CACHE = os.path.join(os.path.dirname(__file__), '..', 'work', 'cache.pkl') S, E = data.parse_time('2026-09-16T00:00Z'), data.parse_time('2026-10-01T00:00Z') if os.path.exists(CACHE): by = pickle.load(open(CACHE, 'rb')) else: by = {s: data.filter_window(m, S, E) for s, m in data.load('data/NORDEND').items()} pickle.dump(by, open(CACHE, 'wb')) args = sys.argv[1:] K = float(args[2]) if len(args) > 2 else 4 a, b = data.parse_time(args[0]), data.parse_time(args[1]) noise = {} for s, ms in by.items(): for c in ('corr_x', 'corr_y'): v = [m[c] for m in ms if m[c] is not None and not data.rejected(m)] d = [abs(v[i+1]-v[i]) for i in range(len(v)-1)] noise[s, c] = statistics.median(d) if d else 1 times = sorted({m['t'] for ms in by.values() for m in ms if a <= m['t'] <= b}) idx = {s: {m['t']: i for i, m in enumerate(ms)} for s, ms in by.items()} for t in times: out = [] for s, ms in by.items(): i = idx[s].get(t) if i is None: continue m = ms[i] if data.rejected(m): out.append(f"{s}:REJ({m['error_value']:.2f})"); continue for c in ('corr_x', 'corr_y'): nb = [ms[j][c] for j in range(max(0, i-4), min(len(ms), i+5)) if j != i and ms[j][c] is not None and not data.rejected(ms[j])] if m[c] is None or len(nb) < 3: continue dv = (m[c] - statistics.median(nb)) / noise[s, c] if abs(dv) >= K: out.append(f"{s}.{c[-1]}{dv:+.0f}(ev{m['error_value']:.2f})") print(data.iso(t), ' '.join(out))