Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

Add exact-interval motion audit and frozen raw-comparison protocol for #64

Cinna Davis9f5bd54bbd7a
diff --git a/research/trajectory-motion/PROTOCOL.md b/research/trajectory-motion/PROTOCOL.md
new file mode 100644
index 0000000..9ea2160
--- /dev/null
+++ b/research/trajectory-motion/PROTOCOL.md
@@ -0,0 +1,43 @@
+# Targeted trajectory-motion audit protocol
+
+Issue64 identifies four source identities: a6cc10, ae8b78, ae05a8 and c01125.
+Their selection comes from the prior failed national scale tiles, independently
+of any recorder position. Freeze the existing v2 index and canonical15-minute
+Parquet, time1788899400 through1788900300 (2026-09-08 20:30–20:45UTC), and the
+two original daily archive parts before comparison. Compute source/member/code
+hashes and retain missing/failed cases. This is a targeted explanation study,
+not an estimate of whole-archive defect prevalence or localization accuracy.
+
+Enumerate endpoints and all real interior stationary points of squared cubic
+Hermite speed in normalized interval time. Its derivative is at most cubic.
+Verify against constant-velocity and closed-form zero-tangent/noncollinear
+controls, plus independent SciPy spline samples. Floating-point polynomial
+roots are not a certified mathematical enclosure. Check every observed interval
+against65 samples, preserve every interval, and flag speeds >=343m/s as outside
+the declared globally subsonic model assumption. This does not establish that
+every observer has a nonunique acoustic solution or that the aircraft actually
+flew at that speed.
+
+Compare these unchanged inputs:
+
+1. Canonical observations with the current gap/dedup/velocity rules.
+2. Exact stored index payloads, including their payload checksums.
+3. Raw traces normalized in full before applying the same15-minute window and
+ segment-local velocity rules.
+4. Fresh full-trace segmentation/derivative support, reporting only intervals
+ whose endpoint times both lie inside the comparison window. This distinguishes
+ window-edge derivative inference from persistent internal source problems.
+
+Join native observation timestamps exactly and report unmatched samples,
+position/altitude differences, missing motion fields and historical zero fills.
+Report chord speed, endpoint derivatives, continuous maximum, its time and all
+unsupported intervals. A chord already exceeding343m/s is a position/time
+problem under the model regardless of Hermite interpolation. A subsonic chord
+with an interior peak above that threshold needs separate derivative/interpolant
+examination. Do not tune thresholds or select an interpolator by recorder error.
+
+Keep production code, source data, acoustic scores and coverage decisions fixed.
+Fresh normalization alone does not repair an observation jump. Hand measured
+handling requirements to W2/W3; leave issue64 open until production handling and
+explicit lost-coverage semantics pass verification. The audit uses no audio,
+recording coordinates, flight-match truth or new worker deployment.
diff --git a/scripts/audit_trajectory_motion.py b/scripts/audit_trajectory_motion.py
new file mode 100644
index 0000000..5e9d28e
--- /dev/null
+++ b/scripts/audit_trajectory_motion.py
@@ -0,0 +1,200 @@
+#!/usr/bin/env python3
+"""Audit frozen indexed motion against freshly normalized raw ADS-B observations.
+
+This diagnostic never changes trajectories, acoustic scores or coverage status.
+Select aircraft identified by a prior data-quality audit, not recording truth.
+"""
+import argparse
+import gzip
+import hashlib
+import io
+import json
+from pathlib import Path
+import re
+import resource
+import sqlite3
+import tarfile
+import time
+
+import numpy as np
+import pandas as pd
+
+from aircraft_geoloc import geometry, tracks
+from aircraft_geoloc.data_access import SplitReader, _frame_track
+
+
+def digest(path):
+ with Path(path).open("rb") as stream:
+ return hashlib.file_digest(stream, "sha256").hexdigest()
+
+
+def write(path, value):
+ Path(path).write_text(json.dumps(value, indent=2, allow_nan=False) + "\n")
+
+
+def segment_peak(dt, p0, p1, v0, v1):
+ """Enumerate endpoints and stationary points of squared Hermite speed.
+
+ Use normalized time u in [0,1]. Velocity is A*u**2+B*u+C, so its squared
+ norm is quartic and its stationary polynomial has degree at most three.
+ Root solving uses double precision, not certified interval arithmetic.
+ """
+ vectors = np.asarray([p0, p1, v0, v1], dtype=float)
+ if dt <= 0 or not np.isfinite(dt) or vectors.shape != (4, 3) or not np.isfinite(vectors).all():
+ raise ValueError("Need positive finite duration and four finite 3D vectors")
+ p0, p1, v0, v1 = vectors
+ mean = (p1 - p0) / dt
+ a, b, c = 3 * (-2 * mean + v0 + v1), 2 * (3 * mean - 2 * v0 - v1), v0
+ derivative = [2 * np.dot(b, c), 2 * (np.dot(b, b) + 2 * np.dot(a, c)),
+ 6 * np.dot(a, b), 4 * np.dot(a, a)]
+ roots = np.polynomial.polynomial.polyroots(derivative)
+ candidates = [0.0, 1.0] + [float(root.real) for root in roots
+ if abs(root.imag) <= 1e-8 and 0 < root.real < 1]
+ speeds = [float(np.linalg.norm((a * u + b) * u + c)) for u in candidates]
+ best = int(np.argmax(speeds))
+ return {"speed_mps": speeds[best], "fraction": candidates[best],
+ "mean_displacement_speed_mps": float(np.linalg.norm(mean)),
+ "endpoint_speeds_mps": [float(np.linalg.norm(v0)), float(np.linalg.norm(v1))]}
+
+
+def audit_frame(frame, sound_speed, gap=30.0, interval_window=None):
+ """Use production gap/dedup/velocity rules without modifying any input rows."""
+ required = ["time_s", "lat", "lon", "alt_m"]
+ valid = np.isfinite(frame[required].to_numpy(dtype=float)).all(axis=1)
+ clean = frame.loc[valid].sort_values("time_s", kind="stable").drop_duplicates("time_s")
+ report = {"rows": len(frame), "nonfinite_geometry_rows": int((~valid).sum()),
+ "duplicate_times": int(frame.time_s.duplicated().sum()),
+ "altitude_basis_counts": frame.altitude_basis.value_counts().to_dict(),
+ "missing_velocity_components": {name: int(frame[name].isna().sum())
+ for name in ("east_mps", "north_mps", "up_mps")},
+ "segments": [], "short_segments": []}
+ groups = (clean.time_s.diff() > gap).cumsum()
+ for number, (_, chunk) in enumerate(clean.groupby(groups)):
+ if interval_window and not chunk.time_s.between(*interval_window).any():
+ continue
+ identity = str(chunk.flight_id.iloc[0]) + (f":{number}" if groups.max() else "")
+ if len(chunk) < 3:
+ report["short_segments"].append({"id": identity, "rows": len(chunk),
+ "reason": "Production requires at least three knots"})
+ continue
+ track = _frame_track(identity, chunk)
+ velocity = track.speed_vector(track.time)
+ intervals = []
+ for i, dt in enumerate(np.diff(track.time)):
+ if interval_window and not (interval_window[0] <= track.time[i] < track.time[i+1] <= interval_window[1]):
+ continue
+ peak = segment_peak(dt, track.xyz[i], track.xyz[i+1], velocity[i], velocity[i+1])
+ times = np.linspace(track.time[i], track.time[i+1], 65)
+ sampled = float(np.linalg.norm(track.speed_vector(times), axis=1).max())
+ intervals.append(dict(peak, start_s=float(track.time[i]), end_s=float(track.time[i+1]),
+ peak_time_s=float(track.time[i] + peak["fraction"] * dt),
+ sampled_max_mps=sampled, exceeds_subsonic_speed=peak["speed_mps"] >= sound_speed))
+ if not intervals:
+ continue
+ report["segments"].append({"id": identity, "typecode": track.typecode,
+ "velocity_inferred_components": track.velocity_inferred_components,
+ "max_knot_speed_mps": max(max(p["endpoint_speeds_mps"]) for p in intervals),
+ "max_continuous_speed_mps": max(p["speed_mps"] for p in intervals),
+ "unsupported_intervals": sum(p["exceeds_subsonic_speed"] for p in intervals),
+ "intervals": intervals})
+ return report
+
+
+def compare_observations(canonical, fresh):
+ common = canonical.merge(fresh, on="time_s", suffixes=("_canonical", "_fresh"), how="outer", indicator=True)
+ paired = common[common._merge == "both"]
+ xyz = [geometry.ecef(paired[f"lat_{tag}"], paired[f"lon_{tag}"], paired[f"alt_m_{tag}"])
+ for tag in ("canonical", "fresh")]
+ return {"canonical_only_times": int((common._merge == "left_only").sum()),
+ "fresh_only_times": int((common._merge == "right_only").sum()), "paired_times": len(paired),
+ "max_ecef_observation_difference_m": float(np.linalg.norm(xyz[0] - xyz[1], axis=1).max()) if len(paired) else None,
+ "velocity_components": {col: {
+ "canonical_zero_fresh_missing": int(((paired[col+"_canonical"] == 0) & paired[col+"_fresh"].isna()).sum()),
+ "both_finite_differing": int((paired[col+"_canonical"].notna() & paired[col+"_fresh"].notna() &
+ (abs(paired[col+"_canonical"] - paired[col+"_fresh"]) > 1e-9)).sum())}
+ for col in ("east_mps", "north_mps", "up_mps")}}
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--canonical", type=Path, required=True)
+ parser.add_argument("--index", type=Path, required=True)
+ parser.add_argument("--parts", type=Path, nargs="+", required=True)
+ parser.add_argument("--ids", nargs="+", required=True)
+ parser.add_argument("--window", type=float, nargs=2, required=True)
+ parser.add_argument("--output", type=Path, required=True)
+ args = parser.parse_args()
+ if not all(re.fullmatch("[0-9a-f]{6}", identity) for identity in args.ids) or args.window[0] >= args.window[1]:
+ parser.error("Need lowercase six-hex aircraft IDs and increasing time bounds")
+ args.output.mkdir(parents=True, exist_ok=False)
+ began = time.monotonic()
+ sources = [{"path": str(p.resolve()), "bytes": p.stat().st_size, "sha256": digest(p)}
+ for p in [args.canonical, args.index, *args.parts]]
+ declaration = {"ids": args.ids, "window_s": args.window, "gap_s": 30.0, "sound_speed_mps": 343.0,
+ "scope": "Targeted data-quality audit, not an acoustic localization benchmark",
+ "sources": sources, "code": {str(p): digest(p) for p in [Path(__file__), Path(tracks.__file__), Path(geometry.__file__)]}}
+ write(args.output / "declaration.json", declaration)
+ canonical = pd.read_parquet(args.canonical, filters=[("flight_id", "in", args.ids)])
+ canonical.to_json(args.output / "canonical-observations.json", orient="records", double_precision=15)
+ fresh, fresh_full, raw_members, scanned = {}, {}, [], 0
+ with io.BufferedReader(SplitReader(args.parts), buffer_size=1024*1024) as stream, tarfile.open(fileobj=stream, mode="r|*") as archive:
+ while (member := archive.next()) is not None:
+ archive.members.clear()
+ scanned += 1
+ match = re.search(r"trace_full_([0-9a-f]{6})\.json$", member.name)
+ if not member.isfile() or not match or match[1] not in args.ids:
+ continue
+ if member.size > 64*1024*1024:
+ raise ValueError(f"Trace too large: {member.name}")
+ raw = archive.extractfile(member).read()
+ if raw[:2] == b"\x1f\x8b":
+ with gzip.GzipFile(fileobj=io.BytesIO(raw)) as compressed:
+ decoded = compressed.read(64*1024*1024+1)
+ else:
+ decoded = raw
+ if len(decoded) > 64*1024*1024:
+ raise ValueError(f"Expanded trace too large: {member.name}")
+ document = json.loads(decoded)
+ identity = document["icao"]
+ if identity != match[1] or identity in fresh:
+ raise ValueError(f"Unexpected or duplicate identity: {member.name}")
+ frame = tracks.adsblol_frame(document) # Normalize full trace before restricting time.
+ fresh_full[identity] = frame
+ fresh[identity] = frame[frame.time_s.between(*args.window)].copy()
+ raw_members.append({"member": member.name, "bytes": len(raw), "sha256": hashlib.sha256(raw).hexdigest(),
+ "raw_observations": len(document["trace"]), "normalized_full_rows": len(frame)})
+ print(json.dumps({"found": identity, "window_rows": len(fresh[identity])}), flush=True)
+ failures, audits = [], []
+ with sqlite3.connect(f"file:{args.index.resolve()}?mode=ro", uri=True) as db:
+ metadata = json.loads(db.execute("SELECT value FROM metadata").fetchone()[0])
+ for identity in args.ids:
+ try:
+ old = canonical[canonical.flight_id == identity]
+ current = fresh[identity]
+ if old.empty or current.empty:
+ raise ValueError("No observations in declared window")
+ current.to_json(args.output / f"fresh-{identity}.json", orient="records", double_precision=15)
+ indexed = []
+ for name, payload, expected in db.execute("SELECT name,payload,payload_hash FROM segments WHERE name=? OR name LIKE ?", (identity, identity+":%")):
+ if hashlib.sha256(payload).hexdigest() != expected:
+ raise ValueError(f"Index payload hash mismatch: {name}")
+ frame = pd.DataFrame(json.loads(gzip.decompress(payload)))
+ indexed.append({"id": name, "audit": audit_frame(frame, 343.0)})
+ audits.append({"identity": identity, "observation_comparison": compare_observations(old, current),
+ "canonical": audit_frame(old, 343.0), "fresh": audit_frame(current, 343.0),
+ "fresh_full_support": audit_frame(fresh_full[identity], 343.0, interval_window=args.window),
+ "indexed": indexed})
+ except (KeyError, ValueError) as exc:
+ failures.append({"identity": identity, "reason": str(exc)})
+ report = {"declaration": declaration, "index_input_hash": metadata["input_hash"], "raw_members": raw_members,
+ "archive_members_scanned": scanned, "aircraft": audits, "failures": failures,
+ "elapsed_s": time.monotonic()-began, "process_peak_rss_kib": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
+ "limitations": ["No production trajectory, score, threshold or coverage decision changed.",
+ "Source inconsistencies and speed >= sound speed do not establish real supersonic motion.",
+ "Stationary-point roots use floating-point arithmetic, checked against dense segment samples."]}
+ write(args.output / "report.json", report)
+ print(json.dumps({"completed_aircraft": len(audits), "failures": failures, "elapsed_s": report["elapsed_s"]}), flush=True)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tests/test_trajectory_motion_audit.py b/tests/test_trajectory_motion_audit.py
new file mode 100644
index 0000000..317818f
--- /dev/null
+++ b/tests/test_trajectory_motion_audit.py
@@ -0,0 +1,73 @@
+"""Analytic and independent spline-oracle controls for the diagnostic audit."""
+import importlib.util
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+import pytest
+from scipy.interpolate import CubicHermiteSpline
+
+spec = importlib.util.spec_from_file_location("motion_audit", Path(__file__).parents[1] / "scripts/audit_trajectory_motion.py")
+audit = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(audit)
+
+
+def test_constant_vector_velocity_and_stationary_segment():
+ velocity = np.array([30., -40., 12.])
+ peak = audit.segment_peak(7., np.zeros(3), 7*velocity, velocity, velocity)
+ assert peak["speed_mps"] == pytest.approx(np.linalg.norm(velocity))
+ assert audit.segment_peak(7., np.zeros(3), np.zeros(3), np.zeros(3), np.zeros(3))["speed_mps"] == 0
+
+
+def test_zero_measured_knot_speeds_do_not_bound_interior_motion():
+ peak = audit.segment_peak(2., [0, 0, 0], [1000, 0, 0], [0, 0, 0], [0, 0, 0])
+ assert peak["endpoint_speeds_mps"] == [0, 0]
+ assert peak["mean_displacement_speed_mps"] == 500
+ assert peak["fraction"] == pytest.approx(.5)
+ assert peak["speed_mps"] == pytest.approx(750)
+
+
+def test_noncollinear_interior_peak_with_independent_closed_form():
+ # v(u)=[4u(1-u),0.6,0]; its integral is [2/3,0.6,0].
+ peak = audit.segment_peak(1., [0, 0, 0], [2/3, .6, 0], [0, .6, 0], [0, .6, 0])
+ assert peak["fraction"] == pytest.approx(.5)
+ assert peak["speed_mps"] == pytest.approx(np.sqrt(1+.6**2))
+
+
+def test_stationary_point_enumeration_covers_independent_spline_samples():
+ rng = np.random.default_rng(640031)
+ for _ in range(30):
+ dt = rng.uniform(.1, 30)
+ p0, displacement, v0, v1 = rng.normal(size=(4, 3))*100
+ p1 = p0+displacement
+ peak = audit.segment_peak(dt, p0, p1, v0, v1)
+ spline = CubicHermiteSpline([0, dt], [p0, p1], [v0, v1])
+ sampled = np.linalg.norm(spline.derivative()(np.linspace(0, dt, 8193)), axis=1).max()
+ assert peak["speed_mps"] >= sampled-1e-8
+ assert peak["speed_mps"]-sampled < 1e-4*max(1, sampled)
+ actual = np.linalg.norm(spline.derivative()(peak["fraction"]*dt))
+ assert peak["speed_mps"] == pytest.approx(actual, rel=1e-10)
+
+
+def test_audit_keeps_measured_zeros_and_does_not_bridge_a_gap():
+ frame = pd.DataFrame({"flight_id": ["abcdef"]*6, "time_s": [0., 1., 2., 100., 101., 102.],
+ "lat": [40.]*6, "lon": [-100., -99.9, -99.8, -95., -94.9, -94.8], "alt_m": [1000.]*6,
+ "east_mps": [0.]*6, "north_mps": [0.]*6, "up_mps": [0.]*6,
+ "typecode": ["C172"]*6, "altitude_basis": ["ellipsoidal"]*6})
+ report = audit.audit_frame(frame, 343)
+ assert len(report["segments"]) == 2
+ assert all(row["max_knot_speed_mps"] == 0 for row in report["segments"])
+ assert sum(row["unsupported_intervals"] for row in report["segments"]) == 4
+ assert all(p["end_s"]-p["start_s"] == 1 for row in report["segments"] for p in row["intervals"])
+ assert all(not any(row["velocity_inferred_components"].values()) for row in report["segments"])
+ fresh = frame.copy()
+ fresh.loc[0, "up_mps"] = np.nan
+ compared = audit.compare_observations(frame, fresh)
+ assert compared["max_ecef_observation_difference_m"] == 0
+ assert compared["velocity_components"]["up_mps"]["canonical_zero_fresh_missing"] == 1
+
+
[email protected]("duration", [0., -1., np.inf])
+def test_invalid_duration_is_explicit(duration):
+ with pytest.raises(ValueError, match="duration"):
+ audit.segment_peak(duration, *([np.zeros(3)]*4))