Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)
Python acoustic-geolocationads-baircraft-trackingairplaneaudio-forensicsdoppler-effectdoppler-shiftgeolocationosintsignal-processing
fix(#10): report live acquisition and index progress (#366)
Cinna Davisd2c3f65c68ed
diff --git a/src/dopplergeist/automatic.py b/src/dopplergeist/automatic.pyindex 668a68e..e4ce989 100644--- a/src/dopplergeist/automatic.py+++ b/src/dopplergeist/automatic.py@@ -146,16 +146,51 @@ def analyze(request, output_dir, config=None, progress=lambda *args, **kwargs: N# Keep the initial state for explicit acquisition failure results.running_state = {key: value for key, value in state.items() if key != "coverage"}progress("acquisition", 0.35, **running_state)+ last_acquisition_report = 0.0+ last_acquisition_phase = None++ def report_acquisition_progress(**details):+ nonlocal last_acquisition_report, last_acquisition_phase+ now = time.monotonic()+ phase = str(details.get("phase", "working")).replace("_", " ")+ if phase == last_acquisition_phase and now - last_acquisition_report < 2.0:+ return+ label = details.get("label")+ message = f"{label}: {phase}" if label else phase[:1].upper() + phase[1:]+ done, total = details.get("bytes_done"), details.get("bytes_total")+ if isinstance(done, (int, float)) and math.isfinite(done):+ if isinstance(total, (int, float)) and math.isfinite(total) and total > 0:+ message += f" ({done / (1024 * 1024):.0f}/{total / (1024 * 1024):.0f} MiB)"+ else:+ message += f" ({done / (1024 * 1024):.0f} MiB)"+ metrics = []+ for key, metric_label in (("rows", "sample rows"), ("batches", "archive batches"),+ ("segments", "flight segments"), ("flights", "flights"),+ ("tracks", "prepared tracks")):+ value = details.get(key)+ if type(value) is int:+ metrics.append(f"{value:,} {metric_label}")+ if metrics:+ message += f" ({', '.join(metrics)})"+ progress("acquisition", 0.35, message=message, **running_state)+ last_acquisition_report, last_acquisition_phase = now, phasedef report_search_progress(fraction=0.0):fraction = min(1.0, max(0.0, float(fraction)))+ if fraction <= 0.8:+ message = f"Coarse search area: {fraction / 0.8:.1%} processed; coverage not assessed."+ elif fraction < 1.0:+ message = f"Finalist refinement: {(fraction - 0.8) / 0.2:.1%} complete; coverage not assessed."+ else:+ message = "Search scoring complete; coverage not assessed."progress("search", 0.55 + 0.29 * fraction,- message="Data coverage is not yet assessed.", **running_state)+ message=message, **running_state)try:from .data_access import DataUnavailablesearch, prepared_events, measurements, indexes, terrain = _search(- request, recording, events, output, config, report_search_progress)+ request, recording, events, output, config, report_search_progress,+ report_acquisition_progress)except ImportError as exc:return finish("failed", "component_unavailable", f"Automatic matching/data components are not installed: {exc}")except EmptySearchArea as exc:@@ -248,7 +283,7 @@ def _latest_emission(hi, duration, events, filters):return max(original, refinement, candidate)-def _search(request, recording, events, output, config, search_progress):+def _search(request, recording, events, output, config, search_progress, acquisition_progress=None):from .data_access import FlightIndex, acquire_inputsfrom .geography import profile_for_search, receiver_mask_for_searchfrom .matching import prepare_events@@ -308,7 +343,8 @@ def _search(request, recording, events, output, config, search_progress):bundle = acquire_inputs(request, recording.metadata, cache_root=config.get("cache_root", output / "cache"),index_paths=config.get("flight_indexes"), offline=config.get("offline", True),terrain_options=ground, max_sound_range_m=max_distance,- earliest_emission_s=earliest, latest_emission_s=latest)+ earliest_emission_s=earliest, latest_emission_s=latest,+ progress_callback=acquisition_progress)indexes, terrain, time_range, bounds = bundle.indexes, bundle.terrain, bundle.time_range_s, bundle.boundsif ground.get("geoid_raster"):hashes["geoid_sha256"] = _digest(ground["geoid_raster"])diff --git a/src/dopplergeist/cli.py b/src/dopplergeist/cli.pyindex 7b92a22..886201c 100644--- a/src/dopplergeist/cli.py+++ b/src/dopplergeist/cli.py@@ -283,10 +283,10 @@ def main():progress_stale=" (stale)" if result_age>stale_after else ""progress_text=(f", overall progress {progress:.1%}{progress_stale}"if isinstance(progress,(int,float)) and math.isfinite(progress) else "")- refinement_message=value.get("message")- activity_text=(f", {refinement_message}"- if value.get("stage") in {"refinement", "decision"}- and isinstance(refinement_message,str) else "")+ stage_message=value.get("message")+ activity_text=(f", {stage_message}"+ if value.get("stage") in {"acquisition", "search", "refinement", "decision"}+ and isinstance(stage_message,str) else "")now=time.monotonic()rate=""if previous_count is not None and count >= previous_count and now > previous_time:diff --git a/src/dopplergeist/data_access.py b/src/dopplergeist/data_access.pyindex 2e2530d..4364b62 100644--- a/src/dopplergeist/data_access.py+++ b/src/dopplergeist/data_access.py@@ -20,6 +20,7 @@ import pickleimport sqlite3import sysimport tarfile+import timefrom threading import Lockimport numpy as np@@ -318,23 +319,32 @@ def cached_sha256(path):return digest-def download(url, path, *, size=None, digest=None, offline=False, session=None):+def download(url, path, *, size=None, digest=None, offline=False, session=None,+ progress_callback=None, progress_label=None):"""Resume only verified byte ranges; atomically publish validated files.A provider SHA-256 is preferred. Otherwise a locally computed hash receiptdetects subsequent cache corruption, but does not authenticate the provider.+ The optional callback reports transfer/validation activity, not source trust."""path = Path(path)receipt = path.with_name(path.name + ".sha256.json")path.parent.mkdir(parents=True, exist_ok=True)with cache_lock(path.with_name(path.name + ".lock")):if path.exists():+ if progress_callback:+ progress_callback(phase="validating_cached_input", label=progress_label,+ bytes_done=0, bytes_total=path.stat().st_size)actual = sha256(path)expected = digest or (json.loads(receipt.read_text())["sha256"] if receipt.exists() else None)if size is not None and path.stat().st_size != size:raise DataUnavailable(f"Truncated cache: {path}")if expected is None or actual != expected:raise DataUnavailable(f"Unvalidated or corrupt cache: {path}")+ if progress_callback:+ cached_size = path.stat().st_size+ progress_callback(phase="cached_input_ready", label=progress_label,+ bytes_done=cached_size, bytes_total=cached_size)return {"url": url, "path": str(path), "sha256": actual, "bytes": path.stat().st_size}if offline:raise DataUnavailable(f"Offline cache miss: {path}")@@ -342,16 +352,25 @@ def download(url, path, *, size=None, digest=None, offline=False, session=None):offset = temporary.stat().st_size if temporary.exists() else 0headers = {"Range": f"bytes={offset}-", "Accept-Encoding": "identity"} if offset else {"Accept-Encoding": "identity"}try:+ if progress_callback:+ progress_callback(phase="starting_download", label=progress_label,+ bytes_done=offset, bytes_total=size)with (session or requests).get(url, headers=headers, stream=True, timeout=(30, 120)) as response:response.raise_for_status()if offset and (response.status_code != 206 or not response.headers.get("Content-Range", "").startswith(f"bytes {offset}-")):raise DataUnavailable("Provider did not honor requested byte range; partial preserved")- with temporary.open("ab" if offset else "wb") as stream:- for chunk in response.iter_content(1024 * 1024):- stream.write(chunk)expected_size = sizeif expected_size is None and response.headers.get("Content-Length"):expected_size = offset + int(response.headers["Content-Length"])+ downloaded = offset+ with temporary.open("ab" if offset else "wb") as stream:+ for chunk in response.iter_content(1024 * 1024):+ if chunk:+ stream.write(chunk)+ downloaded += len(chunk)+ if progress_callback:+ progress_callback(phase="downloading", label=progress_label,+ bytes_done=downloaded, bytes_total=expected_size)if expected_size is not None and temporary.stat().st_size != expected_size:raise DataUnavailable(f"Truncated download, rerun to resume: {temporary}")actual = sha256(temporary)@@ -360,6 +379,9 @@ def download(url, path, *, size=None, digest=None, offline=False, session=None):temporary.replace(path)result = {"url": url, "path": str(path), "sha256": actual, "bytes": path.stat().st_size}write_json(receipt, result)+ if progress_callback:+ progress_callback(phase="download_ready", label=progress_label,+ bytes_done=result["bytes"], bytes_total=result["bytes"])return resultexcept requests.RequestException as error:raise DataUnavailable(f"Download failed: {url}: {error}") from error@@ -479,28 +501,40 @@ def _index_pool(workers, initializer=None, initargs=()):os.environ[name] = value-def archive_staging(paths, class_map, workers=None, max_member_bytes=64 * 1024 * 1024):+def archive_staging(paths, class_map, workers=None, max_member_bytes=64 * 1024 * 1024,+ progress_callback=None):"""Prepare archive rows in parallel, retaining source order and one SQLite writer."""if workers is None:workers = _index_workers()if type(workers) is not int or workers < 1:raise ValueError("workers must be a positive integer")if workers == 1:+ batches = 0for frame in archive_frames(paths, max_member_bytes):+ batches += 1+ if progress_callback:+ progress_callback(phase="decoding_archive", batches=batches)clean, invalid, altitude = _prepare_staging(frame, class_map)yield list(_staging_rows(clean)), invalid, altitudereturntry:with _index_pool(workers, _init_staging_worker, (class_map, max_member_bytes)) as pool:pending = deque()+ batches = 0for raw in _archive_members(paths, max_member_bytes):pending.append(pool.submit(_stage_archive_member, raw))if len(pending) >= workers:batch = pending.popleft().result()+ batches += 1+ if progress_callback:+ progress_callback(phase="decoding_archive", batches=batches)if batch is not None:yield batchwhile pending:batch = pending.popleft().result()+ batches += 1+ if progress_callback:+ progress_callback(phase="decoding_archive", batches=batches)if batch is not None:yield batchexcept (EOFError, OSError, ValueError) as error:@@ -681,12 +715,13 @@ class FlightIndex:return track@staticmethod- def _prepare_tracks(db, scope=None):+ def _prepare_tracks(db, scope=None, progress_callback=None):"""Warm optional constructor inputs without removing observations."""from .flight_payload import encode_trackdb.execute("DROP TABLE IF EXISTS prepared_tracks")db.execute("CREATE TABLE prepared_tracks(id INTEGER PRIMARY KEY, source_hash TEXT, payload BLOB, payload_hash TEXT)")count = size = 0+ last_report = time.monotonic()if (_frame_track is not _CANONICAL_FRAME_TRACKor _segment_velocity is not _CANONICAL_SEGMENT_VELOCITYor _payload_frame is not _CANONICAL_PAYLOAD_FRAME@@ -715,6 +750,10 @@ class FlightIndex:(identifier, expected, packed, hashlib.sha256(packed).hexdigest()))count += 1size += len(packed)+ now = time.monotonic()+ if progress_callback and now - last_report >= 3.0:+ progress_callback(phase="preparing_tracks", tracks=count, bytes=size)+ last_report = nowreport = {"version": 1, "builder": _prepared_track_identity(), "tracks": count, "bytes": size}if scope is not None:report["scope"] = {"bounds": list(bounds), "time_range_s": list(times)}@@ -761,11 +800,12 @@ class FlightIndex:@classmethoddef build(cls, path, frames, *, sources, coverage, max_gap_s=30.0, max_flight_samples=250000, class_map=None,- prepared=False, prepare_scope=None):+ prepared=False, prepare_scope=None, progress_callback=None):"""Disk staging permits arbitrary source order without whole-table RAM.Explicit source coverage is required: cropped or prop-only historicaldata must never become an apparently complete national all-type index.+ The optional callback reports build phases and observed counts only."""path = Path(path)path.parent.mkdir(parents=True, exist_ok=True)@@ -779,11 +819,34 @@ class FlightIndex:result = cls(path)if result.report["input_hash"] != content_hash(identity):raise DataUnavailable("Existing flight index has different inputs/configuration")+ if progress_callback:+ progress_callback(phase="flight_index_ready", rows=result.report.get("rows", 0),+ segments=result.report.get("segments", 0), cached=True)return resulttemporary = path.with_suffix(".building")temporary.unlink(missing_ok=True)db = sqlite3.connect(temporary)+ activity = {"phase": "creating_flight_index"}+ last_progress_report = 0.0++ def emit_progress(force=False, **fields):+ nonlocal last_progress_report+ if progress_callback is None:+ return+ activity.update(fields)+ now = time.monotonic()+ if force or now - last_progress_report >= 3.0:+ progress_callback(**activity)+ last_progress_report = now++ def sqlite_progress():+ emit_progress()+ return 0+try:+ emit_progress(force=True)+ if progress_callback:+ db.set_progress_handler(sqlite_progress, 1_000_000)db.executescript("""PRAGMA journal_mode=OFF; PRAGMA synchronous=OFF; PRAGMA cache_size=-16384;PRAGMA temp_store=FILE;@@ -795,6 +858,9 @@ class FlightIndex:CREATE TABLE metadata(value TEXT);""")rows, invalid, altitude = 0, 0, Counter()+ batches = 0+ emit_progress(phase="staging_samples", rows=rows, batches=batches,+ invalid_rows=invalid, force=True)for frame in frames:if prepared:staged, invalid_count, altitude_counts = frame@@ -806,9 +872,15 @@ class FlightIndex:altitude.update(altitude_counts)db.executemany("INSERT INTO samples VALUES(?,?,?)", staged)rows += count+ batches += 1+ emit_progress(phase="staging_samples", rows=rows, batches=batches, invalid_rows=invalid)+ emit_progress(phase="creating_sample_lookup", rows=rows, batches=batches, force=True)db.execute("CREATE INDEX flight_samples ON samples(flight,time)")db.commit()segments = 0+ flights_processed = 0+ emit_progress(phase="building_segments", rows=rows, segments=segments,+ flights=flights_processed, force=True)namespace = coverage.get("release_tag", "")workers = _index_workers() if prepared else 1if workers > 1:@@ -817,21 +889,32 @@ class FlightIndex:flights = (_build_flight_segments(flight, records, max_gap_s, namespace)for flight, records in _flight_inputs(db, max_flight_samples))for flight_segments in flights:+ flights_processed += 1for name, start, end, payload, payload_hash, bounds in flight_segments:cur = db.execute("INSERT INTO segments(name,start,end,payload,payload_hash) VALUES(?,?,?,?,?)",(name, start, end, payload, payload_hash))db.execute("INSERT INTO spatial VALUES(?,?,?,?,?)", (cur.lastrowid, *bounds))segments += 1+ emit_progress(phase="building_segments", rows=rows, segments=segments,+ flights=flights_processed)db.execute("DROP TABLE samples")report = dict(identity, input_hash=content_hash(identity), rows=rows, invalid_rows=invalid,segments=segments, altitude_basis_counts=dict(altitude), jets_excluded=False,unknown_types_retained=True, geometry_clipped=False, continuous_geometry_bounds=True)- report["prepared_tracks"] = cls._prepare_tracks(db, prepare_scope)+ emit_progress(phase="preparing_tracks", rows=rows, segments=segments, force=True)+ track_progress = None+ if progress_callback:+ def track_progress(**fields):+ emit_progress(force=True, **fields)+ report["prepared_tracks"] = cls._prepare_tracks(+ db, prepare_scope, progress_callback=track_progress)db.execute("INSERT INTO metadata VALUES(?)", (json.dumps(report),))db.commit()+ emit_progress(phase="compacting_flight_index", rows=rows, segments=segments, force=True)db.execute("VACUUM")db.close()temporary.replace(path)+ emit_progress(phase="flight_index_ready", rows=rows, segments=segments, force=True)return cls(path)except BaseException:db.close()@@ -977,7 +1060,7 @@ class FlightCache:self.root, self.offline = Path(root), offlineself.session = session or requests.Session()- def acquire_day(self, day, *, provider="staging-0", prepare_scope=None):+ def acquire_day(self, day, *, provider="staging-0", prepare_scope=None, progress_callback=None):from . import tracks as normalizerday = date.fromisoformat(day)tag = f"v{day:%Y.%m.%d}-planes-readsb-{provider}"@@ -986,6 +1069,11 @@ class FlightCache:normalizer_hash = sha256(normalizer.__file__)index = directory / f"all-types-v{INDEX_VERSION}-{normalizer_hash[:12]}.sqlite"if index.exists():+ if progress_callback:+ existing = FlightIndex(index)+ progress_callback(phase="cached_flight_index", rows=existing.report.get("rows", 0),+ segments=existing.report.get("segments", 0))+ return existingreturn FlightIndex(index)for old_hash in _EQUIVALENT_NORMALIZERS.get(normalizer_hash, ()):old_index = directory / f"all-types-v{INDEX_VERSION}-{old_hash[:12]}.sqlite"@@ -1000,6 +1088,9 @@ class FlightCache:existing.report.get("normalizer_sha256") != old_hash orexisting.report.get("coverage", {}).get("release_tag") != tag or not full_day):raise DataUnavailable("Compatible flight index metadata mismatch")+ if progress_callback:+ progress_callback(phase="cached_flight_index", rows=existing.report.get("rows", 0),+ segments=existing.report.get("segments", 0))return existingrelease_path = directory / "release.json"if not release_path.exists():@@ -1022,12 +1113,14 @@ class FlightCache:if not assets:raise DataUnavailable("Release has no trace archive")sources = []- for asset in assets:+ for number, asset in enumerate(assets, 1):digest = asset.get("digest")if digest and not digest.startswith("sha256:"):raise DataUnavailable("Unsupported archive digest algorithm")sources.append(download(asset["browser_download_url"], directory / asset["name"], size=asset["size"],- digest=digest.split(":", 1)[1] if digest else None, offline=self.offline, session=self.session))+ digest=digest.split(":", 1)[1] if digest else None, offline=self.offline,+ session=self.session, progress_callback=progress_callback,+ progress_label=f"Historical flight archive {number}/{len(assets)}"))start = pd.Timestamp(day.isoformat(), tz="UTC").timestamp()coverage = {"bounds": [-180, -90, 180, 90], "time_range_s": [start, start + 86400],"all_aircraft": True, "provider": "ADSB.lol", "release_tag": tag,@@ -1035,7 +1128,8 @@ class FlightCache:coverage["normalizer_sha256"] = normalizer_hashtypes_path = self.root / "icao_aircraft_types.js"type_source = download("https://raw.githubusercontent.com/wiedehopf/tar1090-db/master/db/icao_aircraft_types.js",- types_path, offline=self.offline, session=self.session)+ types_path, offline=self.offline, session=self.session,+ progress_callback=progress_callback, progress_label="Aircraft type metadata")raw = types_path.read_bytes()if raw[:2] == b"\x1f\x8b":raw = gzip.decompress(raw)@@ -1046,9 +1140,11 @@ class FlightCache:description = value.get("desc", "") if isinstance(value, dict) else (value[2] if len(value) > 2 else "")# ICAO T also covers helicopter turboshafts; keep the family label broad.class_map[code] = {"J": "jet", "T": "turbine", "P": "piston"}.get(description[-1:], "unclassified")- return FlightIndex.build(index, archive_staging([s["path"] for s in sources], class_map), sources=sources + [type_source],+ return FlightIndex.build(index, archive_staging([s["path"] for s in sources], class_map,+ progress_callback=progress_callback),+ sources=sources + [type_source],coverage=coverage, class_map=class_map, prepared=True,- prepare_scope=prepare_scope)+ prepare_scope=prepare_scope, progress_callback=progress_callback)class CachedTerrain:@@ -1203,12 +1299,13 @@ class AcquiredInputs:def acquire_inputs(request, recording, *, cache_root, index_paths=None, offline=False,terrain_options=None, max_sound_range_m=20000.0, earliest_emission_s=None,- latest_emission_s=None):+ latest_emission_s=None, progress_callback=None):"""Contract adapter for orchestration; no hand-prepared Parquet is required.`index_paths` is an operator-owned explicit offline override, not a usersearch input. Its source scope remains visible. The default obtains completedaily all-type archives once and reuses their disk indexes on later jobs.+ The optional progress callback reports acquisition activity, not coverage."""from .contracts import utc_timestamp, validate_request, validate_recordingfrom .geography import profile_for_search@@ -1243,7 +1340,8 @@ def acquire_inputs(request, recording, *, cache_root, index_paths=None, offline=indexes = []day = firstwhile day <= last:- indexes.append(cache.acquire_day(day.isoformat(), prepare_scope=(bounds, times)))+ indexes.append(cache.acquire_day(day.isoformat(), prepare_scope=(bounds, times),+ progress_callback=progress_callback))day += timedelta(days=1)options = dict(terrain_options or {})terrain = CachedTerrain(root / "terrain", offline=offline, **options)diff --git a/src/dopplergeist/index_compatibility.py b/src/dopplergeist/index_compatibility.pyindex 637087d..afa8dc5 100644--- a/src/dopplergeist/index_compatibility.py+++ b/src/dopplergeist/index_compatibility.py@@ -1,8 +1,21 @@"""Source-pinned equivalent prepared-index normalization implementations."""-# The cache-size-only change and this exact compatibility check do not change-# decoded observations or normalization. Any source edit disables this mapping.+# Listed source revisions have been checked against recorded observations;+# unlisted edits disable the prepared-track fast path.PREPARED_DATA_ACCESS_EQUIVALENCE = {+ # Acquisition progress callbacks report counters and do not change recorded+ # observation decoding, normalization, or prepared track construction.+ "6f1927a9daa638748207abb0479031baa01f6aee1fcedcfbf74280111cb0b47d": (+ "690caf2155bcc17905a5d71ea3b3d4111485451cc734b827c9c4ac5f2997d22b",+ "23aa4ac1781cb5be921f5243f421886d2f2e549a4ded6dcc1986ade651883f7e",+ "d88c4be6668b92c9a1a31b5cd0b4dbdcb3d7cf73c5891ae414f44deca6088644",+ "77da0d39e1e95476fdf02e29e9a345914545af7e62227a93b4cb6786edb37a71",+ "ddd77e9734fff6a70cc9195bd1305002f9f78e39ea601f27e0f596dcb336033a",+ "275e38a37f82993b609b6b78c113d35c4da269083ceb3afcb1d94ada48bdc0d4",+ "dad02d09d89ad9d267cd8d0424b8e018d0399a28d6b43903b5a55867290b3957",+ "d664caf4b496599d29a81360c4a9d9736a3248c157e71d885463b349aad6dd36",+ "ce78ad66f2d478e1f7ea158ea04c0d3b25021339e34f090652740fe55e46e5ec",+ ),# Only GLO-90 terrain tile selection changed; prepared aircraft# observation decoding and normalization remain byte-for-byte unchanged."690caf2155bcc17905a5d71ea3b3d4111485451cc734b827c9c4ac5f2997d22b": (diff --git a/tests/test_real_prepared_index.py b/tests/test_real_prepared_index.pyindex 2a5882b..1b31507 100644--- a/tests/test_real_prepared_index.py+++ b/tests/test_real_prepared_index.py@@ -60,6 +60,24 @@ def queried(index, coverage):return list(index.query(coverage["bounds"], coverage["time_range_s"]))+def test_real_index_build_reports_acquisition_phases(tmp_path):+ source = Path(__file__).resolve().parents[1] / "research/trajectory-motion/frozen/canonical-observations.json"+ frame = pd.DataFrame(json.loads(source.read_text()))+ coverage = {"bounds": [-180., -90., 180., 90.],+ "time_range_s": [float(frame.time_s.min()), float(frame.time_s.max())],+ "all_aircraft": False, "source_note": "Archived real observations; incomplete excerpt"}+ updates = []++ index = FlightIndex.build(tmp_path / "progress.sqlite", [frame], sources=[], coverage=coverage,+ progress_callback=lambda **fields: updates.append(dict(fields)))++ phases = {update["phase"] for update in updates}+ assert {"staging_samples", "creating_sample_lookup", "building_segments",+ "preparing_tracks", "compacting_flight_index", "flight_index_ready"} <= phases+ assert updates[-1]["rows"] == index.report["rows"]+ assert updates[-1]["segments"] == index.report["segments"]++def test_prepared_query_and_lookup_match_original_bytes(observed_index, monkeypatch):index, coverage = observed_indexlegacy = FlightIndex(index.path, track_cache_bytes=0)