"""NIEDERHORN : les traits de chaque cycle thermique du jour, en JSON (une ligne par jour), pour écrire les descriptions. Lancé depuis la racine du dépôt : .venv/bin/python labeling/auto/revue-croisee/2026-11-04/finition/niederhorn/outils/traits.py > /figures/traits.jsonl Mêmes conventions que jour_par_jour.py (nuit 03-10 UTC du jour, ou la plus proche ; bruit : pas médian de nuit). Pose en degrés (échelle : écart à 1), corr en unités des courbes, tout en écart au niveau de la nuit. """ import json import math import statistics import sys from datetime import datetime, timedelta sys.path.insert(0, "labeling/auto/revue-croisee/2026-11-04/finition/niederhorn/outils") from jour_par_jour import similitude, UTC, K, acc, agit, bp, bx, by, cibles, cx, cy, nuit_de, pose, temp, TOUJOURS # noqa: E402 h = timedelta(hours=1) ev = {s: {t: m["error_value"] for t, m in acc[s].items()} for s in cibles} def traits(J, h1=10, h2=26): fen = [J + timedelta(hours=k) for k in range(h1, h2 + 1)] o = {"jour": J.strftime("%d/%m")} tn = [temp[t] for t in (J + timedelta(hours=k) for k in range(3, 10)) if t in temp] tw = [t for t in fen if t in temp] if tw: s = max((t for t in tw if t - h in temp and t.hour >= 12 and t < J + timedelta(hours=20)), key=lambda t: temp[t] - temp[t - h]) m = max(tw, key=lambda t: temp[t]) o["T"] = {"nuit": statistics.median(tn) if tn else None, "saut_h": s.hour, "avant": temp[s - h], "apres": temp[s], "max": temp[m], "max_h": m.strftime("%H"), "min_jour": min(temp[t] for t in tw if t < J + timedelta(hours=20))} for c in ("pan", "tilt", "rotation", "scale"): s = pose[c] n = nuit_de(s, J, []) if not n: continue n0 = statistics.median(n) d = {t: s[t] - n0 for t in fen if t in s} if not d: continue pk = max((t for t in d if t < J + timedelta(hours=23)), key=lambda t: abs(d[t])) sg = math.copysign(1, d[pk]) apres = [t for t in sorted(d) if t > pk and t - h in s] r = min(apres, key=lambda t: sg * (s[t] - s[t - h])) if apres else None tiers = next((t for t in sorted(d) if t > pk and abs(d[t]) < abs(d[pk]) / 3), None) a_n = agit(s, 3, 9, J) o[c] = {"pic": d[pk], "pic_br": d[pk] / bp[c], "pic_h": pk.strftime("%H"), "debut_h": next((t.strftime("%H") for t in sorted(d) if t.hour >= 11 and abs(d[t]) > K * bp[c] and d[t] * sg > 0), None), "retour_h": r.strftime("%H") if r else None, "retour_part": (-(s[r] - s[r - h]) / d[pk]) if r else None, "tiers_h": tiers.strftime("%d %H") if tiers else None, "a_02h": d.get(J + timedelta(hours=26), math.nan) / d[pk], "agit": agit(s, 13, 22, J) / a_n if a_n else None} cib = {} for s in cibles: if s in TOUJOURS: continue for nom, ser, b in (("cx", cx[s], bx[s]), ("cy", cy[s], by[s])): n = nuit_de(ser, J, []) if len(n) < 3: continue n0 = statistics.median(n) w = {t: ser[t] - n0 for t in fen if t in ser and t < J + timedelta(hours=24)} for sg in (1, -1): hh = sorted(t for t in w if sg * w[t] > K * b) if len(hh) >= 3: e = max(hh, key=lambda t: abs(w[t])) cib.setdefault(nom + ("+" if sg > 0 else "-"), []).append( {"s": s, "ext": w[e], "br": w[e] / b, "h": e.strftime("%H"), "de": hh[0].strftime("%H"), "a": hh[-1].strftime("%H"), "n": len(hh)}) for k in cib: cib[k].sort(key=lambda x: -abs(x["br"])) o["cibles"] = cib # retour des courbes : dernière heure (12h -> 02h) où au moins 3 des cibles qui bougent sont encore au-delà de 5 bruits hors = {} for k, lst in cib.items(): for x in lst: ser = cx[x["s"]] if k[:2] == "cx" else cy[x["s"]] b = bx[x["s"]] if k[:2] == "cx" else by[x["s"]] n0 = statistics.median(nuit_de(ser, J, [])) for t in fen: if t in ser and (ser[t] - n0) * (1 if k[2] == "+" else -1) > K * b: hors.setdefault(t, set()).add(x["s"]) tt = sorted(t for t, v in hors.items() if len(v) >= 3 and t.hour >= 12 or t >= J + timedelta(days=1)) tt = [t for t in tt if len(hors[t]) >= 3] o["courbes"] = {"de": tt[0].strftime("%H") if tt else None, "a": tt[-1].strftime("%d %H") if tt else None} ra = [agit(cx[s], 13, 22, J) / agit(cx[s], 3, 9, J) for s in cibles if s not in TOUJOURS and agit(cx[s], 3, 9, J)] ra = [v for v in ra if not math.isnan(v)] o["agit_corr_x"] = statistics.median(ra) if ra else None de = [] for s in cibles: n = nuit_de(ev[s], J, []) j = [ev[s][t] for t in (J + timedelta(hours=k) for k in range(13, 23)) if t in ev[s]] if len(n) >= 3 and len(j) >= 3: de.append(statistics.median(j) - statistics.median(n)) o["ev_jour_moins_nuit"] = statistics.median(de) if de else None # résidu face au mouvement d'ensemble des cibles (similitude depuis J 06:00, comme ensemble.py --rigide) : 90e centile tb = J + timedelta(hours=6) ok0 = [s for s in cibles if tb in acc[s]] p90 = {} for t in fen: ok = [s for s in ok0 if t in acc[s]] if len(ok) < 5: continue a, b, tx, ty = similitude([(acc[s][tb]["x"], acc[s][tb]["y"]) for s in ok], [(acc[s][t]["x"], acc[s][t]["y"]) for s in ok]) r = sorted(math.hypot(acc[s][t]["x"] - (a * acc[s][tb]["x"] - b * acc[s][tb]["y"] + tx), acc[s][t]["y"] - (b * acc[s][tb]["x"] + a * acc[s][tb]["y"] + ty)) for s in ok) p90[t.hour] = r[int(0.9 * (len(r) - 1))] o["rigide_p90"] = {"matin": statistics.median([p90[k] for k in (10, 11) if k in p90]) if any(k in p90 for k in (10, 11)) else None, "jour_max": max([p90[k] for k in range(13, 23) if k in p90], default=None), "jour_med": statistics.median([p90[k] for k in range(13, 23) if k in p90]) if any(k in p90 for k in range(13, 23)) else None} return o if __name__ == "__main__": for d in range(1, 32): print(json.dumps(traits(datetime(2026, 10, d, tzinfo=UTC)), ensure_ascii=False))