# Prints, per target and axis, the slow-drift figures of displacement.py (whole-window Theil-Sen drift, median daily amplitude, max day step, ratio) on a window. Args: detectors_dir station_dir FROM TO [--levels] import statistics, sys from datetime import datetime, timezone from pathlib import Path det = Path(sys.argv[1]).resolve() sys.path[:0] = [str(det / "scripts"), str(det.parents[0] / "aimsight-tools" / "scripts"), "/home/maxime/aimsight-tools/scripts"] import displacement as D from data import load, is_reference day = lambda d: datetime.strptime(d, "%Y-%m-%d").replace(tzinfo=timezone.utc) ms_all = load(sys.argv[2]) a, b = day(sys.argv[3]), day(sys.argv[4]) for s, ms in sorted(ms_all.items()): ms = [m for m in ms if a <= m["t"] < b] if not ms or is_reference(ms): continue for col in ("corr_x", "corr_y"): ds, lv, ampl, _ = D._days(ms, col) if len(ds) < 3: continue pairs = [(i, j) for i in range(len(ds)) for j in range(i + 1, len(ds))] slope = statistics.median((lv[j] - lv[i]) / (ds[j] - ds[i]).days for i, j in pairs) d = slope * (ds[-1] - ds[0]).days step = max(abs(y - x) for x, y in zip(lv, lv[1:])) flag = "WHOLE" if abs(d) >= D.SIZE * ampl and step <= abs(d) / 2 else "" sg = 1 if d >= 0 else -1 # retreat: largest move back against the drift's direction top, back = sg * lv[0], 0 for v in lv: top = max(top, sg * v) back = max(back, top - sg * v) if "--flagged" in sys.argv and not flag: continue x = [(e - ds[0]).days for e in ds] # residuals of the levels around the Theil-Sen line icpt = statistics.median(v - slope * xi for v, xi in zip(lv, x)) res = [v - icpt - slope * xi for v, xi in zip(lv, x)] mad = statistics.median(abs(r) for r in res) print(f"{s:<8}{col:<7} days={len(ds):>3} drift={d:+8.3f} ampl={ampl:7.3f} ratio={abs(d)/ampl if ampl else 0:5.2f} step={step:6.3f} back/drift={back/abs(d) if d else 0:5.2f} resMAD/drift={mad/abs(d) if d else 0:5.2f} resMAX/drift={max(map(abs,res))/abs(d) if d else 0:5.2f} resMAD/ampl={mad/ampl if ampl else 0:5.2f} {flag}") if "--levels" in sys.argv and flag: print(" ", " ".join(f"{v:+.2f}" for v in lv))