"""Per-instant scan of a window: rejections, references retained, pose, flux of the targets, and the sensors whose corr_x / corr_y leave their local level (rolling median of the neighbours, +-N measurements) by more than K own noises. Usage: python3 scan.py START END [--k 6] [--n 6] [--sensors A,B] [--flux]""" import sys, os, pickle, statistics as st, argparse sys.path.insert(0, 'tools/scripts') import data from datetime import timedelta CACHE = os.path.join(os.path.dirname(__file__), '..', 'work', 'cache.pkl') W0, W1 = data.parse_time('2026-10-23T12:00Z'), data.parse_time('2026-11-01T12:00Z') def load(): if os.path.exists(CACHE): return pickle.load(open(CACHE, 'rb')) by = data.load('data/LENZSPITZE') by = {s: [m for m in ms if W0 <= m['t'] <= W1] for s, ms in by.items()} refs = {s for s, ms in by.items() if data.is_reference(ms)} cam = {p['t']: p for p in data.load_camera('data/LENZSPITZE') if W0 <= p['t'] <= W1} out = (by, refs, cam) pickle.dump(out, open(CACHE, 'wb')) return out def noise(vals): d = [abs(b - a) for a, b in zip(vals, vals[1:])] return st.median(d) if d else 0 if __name__ == '__main__': ap = argparse.ArgumentParser() ap.add_argument('start'); ap.add_argument('end') ap.add_argument('--k', type=float, default=6); ap.add_argument('--n', type=int, default=6) ap.add_argument('--sensors'); ap.add_argument('--flux', action='store_true') a = ap.parse_args() by, refs, cam = load() t0, t1 = data.parse_time(a.start), data.parse_time(a.end) sens = a.sensors.split(',') if a.sensors else sorted(by) # flux reference: median flux per sensor over the whole cached window fref = {s: st.median([m['flux'] for m in by[s] if m['flux'] is not None] or [1]) for s in by} dev = {} # t -> list of (sensor, col, dev in noises) for s in sens: ms = by[s] for col in ('corr_x', 'corr_y'): pts = [(m['t'], m[col]) for m in ms if m[col] is not None] vals = [v for _, v in pts] nz = noise(vals) or 1e-9 for i, (t, v) in enumerate(pts): if not (t0 <= t <= t1): continue nb = vals[max(0, i - a.n):i] + vals[i + 1:i + 1 + a.n] if len(nb) < 3: continue d = (v - st.median(nb)) / nz if abs(d) >= a.k: dev.setdefault(t, []).append((abs(d), f"{s}{'x' if col=='corr_x' else 'y'}{d:+.0f}")) times = sorted({m['t'] for s in by for m in by[s] if t0 <= m['t'] <= t1}) idx = {s: {m['t']: m for m in by[s]} for s in by} for t in times: rej = [s for s in sens if t in idx[s] and data.rejected(idx[s][t])] rr = [s for s in rej if s in refs]; rt = [s for s in rej if s not in refs] nin = sum(1 for s in refs if t in idx[s] and idx[s][t]['inlier'] == 1) p = cam.get(t) ps = f"pan{p['pan']:.4f} tilt{p['tilt']:.4f} rot{p['rotation']:.4f} sc{p['scale']:.6f}" if p else 'no pose' line = f"{data.iso(t)[5:]} in{nin} {ps}" if rr: line += ' Rref:' + ','.join(rr) if rt: line += ' Rtgt:' + ','.join(rt) if a.flux: low = sorted((idx[s][t]['flux'] / fref[s], s) for s in sens if t in idx[s] and idx[s][t]['flux'] is not None and fref[s]) fl = [f"{s}:{r:.2f}" for r, s in low if r < 0.5] if fl: line += ' F<0.5:' + ','.join(fl) d = sorted(dev.get(t, []), reverse=True) if d: line += f" | {len(d)}: " + ' '.join(x for _, x in d[:14]) print(line)