Skip to content

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

Add validated historical acquisition and whole-segment flight indexes (#6)

Cinna Davisda031d3f302a
diff --git a/scripts/index_flights.py b/scripts/index_flights.py
new file mode 100644
index 0000000..bb6749b
--- /dev/null
+++ b/scripts/index_flights.py
@@ -0,0 +1,37 @@
+"""Build/reuse a provenance-labelled index from an operator-supplied Parquet.
+
+Normal uploads use FlightCache acquisition. This adapter restores checkpoint
+data without re-downloading a daily archive and records its actual limitations.
+"""
+import argparse
+import json
+import resource
+import time
+
+from aircraft_geoloc.data_access import FlightIndex, write_json
+from aircraft_geoloc.tracks import unix_time
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("parquet")
+ parser.add_argument("index")
+ parser.add_argument("--bounds", type=float, nargs=4, required=True)
+ parser.add_argument("--time-range", nargs=2, required=True, metavar=("START_UTC", "END_UTC"))
+ parser.add_argument("--all-aircraft", action="store_true", help="Only if source ingestion retained all classes")
+ parser.add_argument("--source-note", required=True)
+ parser.add_argument("--report")
+ args = parser.parse_args()
+ began = time.monotonic()
+ coverage = {"bounds": args.bounds, "time_range_s": [unix_time(t) for t in args.time_range],
+ "all_aircraft": args.all_aircraft, "source_note": args.source_note}
+ index = FlightIndex.from_parquet(args.index, args.parquet, coverage=coverage)
+ report = dict(index.report, index_path=str(index.path), elapsed_s=time.monotonic() - began,
+ process_peak_rss_mib=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024)
+ if args.report:
+ write_json(args.report, report)
+ print(json.dumps(report, indent=2))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/aircraft_geoloc/data_access.py b/src/aircraft_geoloc/data_access.py
new file mode 100644
index 0000000..7d63752
--- /dev/null
+++ b/src/aircraft_geoloc/data_access.py
@@ -0,0 +1,485 @@
+"""Validated historical inputs and disk-backed whole-segment spatial/time queries.
+
+These are internal data adapters, not a second pipeline request/result contract.
+Coverage describes the *source query*, never receiver/network completeness.
+"""
+from collections import Counter
+from contextlib import contextmanager
+from dataclasses import dataclass
+from datetime import date, datetime, timedelta, timezone
+import gzip
+import hashlib
+import io
+import json
+from pathlib import Path
+import sqlite3
+import tarfile
+
+import numpy as np
+import pandas as pd
+import requests
+
+from .geometry import Track, velocity_ecef
+from .tracks import adsblol_frame
+
+CONUS = (-125.0, 24.0, -66.0, 50.0)
+INDEX_VERSION = 1
+
+
+class DataUnavailable(RuntimeError):
+ """Missing/corrupt provider input; distinct from an acoustic non-detection."""
+
+
+def sha256(path):
+ with Path(path).open("rb") as stream:
+ return hashlib.file_digest(stream, "sha256").hexdigest()
+
+
+def content_hash(value):
+ return hashlib.sha256(json.dumps(value, sort_keys=True, allow_nan=False).encode()).hexdigest()
+
+
+def write_json(path, value):
+ path = Path(path)
+ temporary = path.with_name(path.name + ".tmp")
+ temporary.write_text(json.dumps(value, indent=2, allow_nan=False) + "\n")
+ temporary.replace(path)
+
+
+@contextmanager
+def cache_lock(path):
+ import fcntl
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ with path.open("a") as lock:
+ fcntl.flock(lock, fcntl.LOCK_EX)
+ yield
+
+
+def download(url, path, *, size=None, digest=None, offline=False, session=None):
+ """Resume only verified byte ranges; atomically publish validated files.
+
+ A provider SHA-256 is preferred. Otherwise a locally computed hash receipt
+ detects subsequent cache corruption, but does not authenticate the provider.
+ """
+ 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():
+ 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}")
+ return {"url": url, "path": str(path), "sha256": actual, "bytes": path.stat().st_size}
+ if offline:
+ raise DataUnavailable(f"Offline cache miss: {path}")
+ temporary = path.with_name(path.name + ".part")
+ offset = temporary.stat().st_size if temporary.exists() else 0
+ headers = {"Range": f"bytes={offset}-", "Accept-Encoding": "identity"} if offset else {"Accept-Encoding": "identity"}
+ try:
+ 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 = size
+ if expected_size is None and response.headers.get("Content-Length"):
+ expected_size = offset + int(response.headers["Content-Length"])
+ 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)
+ if digest and actual != digest:
+ raise DataUnavailable(f"Provider checksum mismatch: {temporary}")
+ temporary.replace(path)
+ result = {"url": url, "path": str(path), "sha256": actual, "bytes": path.stat().st_size}
+ write_json(receipt, result)
+ return result
+ except requests.RequestException as error:
+ raise DataUnavailable(f"Download failed: {url}: {error}") from error
+
+
+class SplitReader(io.RawIOBase):
+ """Read archive parts as one stream without copying/extracting the archive."""
+ def __init__(self, paths):
+ self.paths, self.current = iter(paths), None
+
+ def readable(self):
+ return True
+
+ def readinto(self, buffer):
+ while True:
+ if self.current is None:
+ path = next(self.paths, None)
+ if path is None:
+ return 0
+ self.current = Path(path).open("rb")
+ count = self.current.readinto(buffer)
+ if count:
+ return count
+ self.current.close()
+ self.current = None
+
+ def close(self):
+ if self.current:
+ self.current.close()
+ super().close()
+
+
+def archive_frames(paths, max_member_bytes=64 * 1024 * 1024):
+ """Preserve all types, full geometry and altitude labels before indexing."""
+ try:
+ with io.BufferedReader(SplitReader(paths)) as stream:
+ with tarfile.open(fileobj=stream, mode="r|*") as archive:
+ for member in archive:
+ if not member.isfile() or "trace_full_" not in member.name:
+ continue
+ if member.size > max_member_bytes:
+ raise DataUnavailable(f"Oversized trace member: {member.name}")
+ raw = archive.extractfile(member).read()
+ if raw[:2] == b"\x1f\x8b":
+ with gzip.GzipFile(fileobj=io.BytesIO(raw)) as compressed:
+ raw = compressed.read(max_member_bytes + 1)
+ if len(raw) > max_member_bytes:
+ raise DataUnavailable(f"Oversized expanded trace: {member.name}")
+ frame = adsblol_frame(json.loads(raw))
+ if not frame.empty:
+ yield frame
+ except (tarfile.TarError, EOFError, OSError, ValueError) as error:
+ raise DataUnavailable(f"Invalid/truncated ADS-B archive: {error}") from error
+
+
+class FlightIndex:
+ """SQLite R-tree of complete, gap-split flight segments; lazy payload reads."""
+ def __init__(self, path):
+ self.path = Path(path)
+ if not self.path.exists():
+ raise DataUnavailable(f"Missing flight index: {path}")
+ try:
+ with self.connect() as db:
+ self.report = json.loads(db.execute("SELECT value FROM metadata").fetchone()[0])
+ if self.report["index_version"] != INDEX_VERSION:
+ raise DataUnavailable("Flight index version mismatch")
+ except (sqlite3.DatabaseError, TypeError, KeyError, json.JSONDecodeError) as error:
+ raise DataUnavailable(f"Invalid flight index: {path}") from error
+
+ def connect(self):
+ return sqlite3.connect(f"{self.path.resolve().as_uri()}?mode=ro", uri=True)
+
+ @classmethod
+ def build(cls, path, frames, *, sources, coverage, max_gap_s=30.0, max_flight_samples=250000, class_map=None):
+ """Disk staging permits arbitrary source order without whole-table RAM.
+
+ Explicit source coverage is required: cropped or prop-only historical
+ data must never become an apparently complete national all-type index.
+ """
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ class_map = class_map or {}
+ identity = {"index_version": INDEX_VERSION, "sources": sources, "coverage": coverage,
+ "classification_map_hash": content_hash(class_map),
+ "max_gap_s": max_gap_s, "max_flight_samples": max_flight_samples}
+ with cache_lock(path.with_suffix(".lock")):
+ if path.exists():
+ result = cls(path)
+ if result.report["input_hash"] != content_hash(identity):
+ raise DataUnavailable("Existing flight index has different inputs/configuration")
+ return result
+ temporary = path.with_suffix(".building")
+ temporary.unlink(missing_ok=True)
+ db = sqlite3.connect(temporary)
+ try:
+ db.executescript("""
+ PRAGMA journal_mode=OFF; PRAGMA synchronous=OFF; PRAGMA cache_size=-16384;
+ PRAGMA temp_store=FILE;
+ CREATE TABLE samples(flight TEXT, time REAL, row TEXT);
+ CREATE TABLE segments(id INTEGER PRIMARY KEY, name TEXT UNIQUE, start REAL, end REAL,
+ payload BLOB, payload_hash TEXT);
+ CREATE VIRTUAL TABLE spatial USING rtree(id, west, east, south, north);
+ CREATE INDEX time_index ON segments(start,end);
+ CREATE TABLE metadata(value TEXT);
+ """)
+ rows, invalid, altitude = 0, 0, Counter()
+ for frame in frames:
+ required = ["flight_id", "time_s", "lat", "lon", "alt_m"]
+ if not set(required).issubset(frame):
+ raise DataUnavailable(f"Missing canonical columns: {required}")
+ valid = np.isfinite(frame[["time_s", "lat", "lon", "alt_m"]].to_numpy(dtype=float)).all(axis=1)
+ valid &= frame.lat.between(-90, 90) & frame.lon.between(-180, 180) & frame.flight_id.notna()
+ invalid += int((~valid).sum())
+ clean = frame.loc[valid]
+ clean = clean.copy()
+ clean["aircraft_class"] = clean.typecode.map(class_map).fillna("unclassified") if "typecode" in clean else "unclassified"
+ altitude.update(clean.altitude_basis.value_counts().to_dict() if "altitude_basis" in clean else {"unspecified": len(clean)})
+ # pandas normalizes numpy scalars/NaNs to JSON numbers/null.
+ records = json.loads(clean.to_json(orient="records", double_precision=15))
+ db.executemany("INSERT INTO samples VALUES(?,?,?)", [(str(r["flight_id"]), r["time_s"], json.dumps(r)) for r in records])
+ rows += len(records)
+ db.execute("CREATE INDEX flight_samples ON samples(flight,time)")
+ db.commit()
+ segments = 0
+ for (flight,) in db.execute("SELECT DISTINCT flight FROM samples ORDER BY flight"):
+ count = db.execute("SELECT count(*) FROM samples WHERE flight=?", (flight,)).fetchone()[0]
+ if count > max_flight_samples:
+ raise DataUnavailable(f"Flight {flight} exceeds {max_flight_samples} sample memory limit")
+ records = [json.loads(r[0]) for r in db.execute("SELECT row FROM samples WHERE flight=? ORDER BY time,rowid", (flight,))]
+ frame = pd.DataFrame(records).drop_duplicates("time_s")
+ groups = (frame.time_s.diff() > max_gap_s).cumsum()
+ for index, (_, chunk) in enumerate(frame.groupby(groups)):
+ if len(chunk) < 3:
+ continue
+ namespace = coverage.get("release_tag", "")
+ name = flight + (f"@{namespace}" if namespace else "") + (f":{index}" if groups.max() else "")
+ payload = gzip.compress(chunk.to_json(orient="records", double_precision=15).encode(), mtime=0)
+ cur = db.execute("INSERT INTO segments(name,start,end,payload,payload_hash) VALUES(?,?,?,?,?)",
+ (name, float(chunk.time_s.iloc[0]), float(chunk.time_s.iloc[-1]), payload, hashlib.sha256(payload).hexdigest()))
+ db.execute("INSERT INTO spatial VALUES(?,?,?,?,?)", (cur.lastrowid, float(chunk.lon.min()), float(chunk.lon.max()), float(chunk.lat.min()), float(chunk.lat.max())))
+ segments += 1
+ 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)
+ db.execute("INSERT INTO metadata VALUES(?)", (json.dumps(report),))
+ db.commit()
+ db.execute("VACUUM")
+ db.close()
+ temporary.replace(path)
+ return cls(path)
+ except BaseException:
+ db.close()
+ temporary.unlink(missing_ok=True)
+ raise
+
+ @classmethod
+ def from_parquet(cls, path, parquet, *, coverage, **kwargs):
+ import pyarrow.parquet as pq
+ parquet = Path(parquet)
+ source = {"path": str(parquet.resolve()), "sha256": sha256(parquet), "bytes": parquet.stat().st_size}
+ try:
+ frames = (batch.to_pandas() for batch in pq.ParquetFile(parquet).iter_batches(batch_size=16384))
+ return cls.build(path, frames, sources=[source], coverage=coverage, **kwargs)
+ except (OSError, ValueError) as error:
+ raise DataUnavailable(f"Invalid/truncated Parquet: {parquet}: {error}") from error
+
+ def query(self, bounds, time_range):
+ """Yield complete segments touching the query, retaining gaps and labels.
+
+ Callers expand receiver bounds for acoustic range and event duration.
+ An R-tree rectangle can return harmless extra tracks, never clipped ones.
+ """
+ west, south, east, north = bounds
+ start, end = time_range
+ with self.connect() as db:
+ sql = """SELECT s.name,s.payload,s.payload_hash FROM segments s JOIN spatial r ON s.id=r.id
+ WHERE r.west<=? AND r.east>=? AND r.south<=? AND r.north>=?
+ AND s.start<=? AND s.end>=? ORDER BY s.name"""
+ for name, payload, expected in db.execute(sql, (east, west, north, south, end, start)):
+ if hashlib.sha256(payload).hexdigest() != expected:
+ raise DataUnavailable(f"Corrupt flight payload: {name}")
+ frame = pd.DataFrame(json.loads(gzip.decompress(payload)))
+ velocity = None
+ cols = ["east_mps", "north_mps", "up_mps"]
+ if set(cols).issubset(frame) and np.isfinite(frame[cols].to_numpy(dtype=float)).all():
+ velocity = velocity_ecef(frame.lat.to_numpy(), frame.lon.to_numpy(), *[frame[c].to_numpy() for c in cols])
+ track = Track(name, frame.time_s.to_numpy(), frame.lat.to_numpy(), frame.lon.to_numpy(), frame.alt_m.to_numpy(),
+ str(frame.typecode.iloc[0] or "") if "typecode" in frame else "", velocity)
+ track.altitude_basis = frame.altitude_basis.tolist() if "altitude_basis" in frame else ["unspecified"] * len(frame)
+ track.aircraft_class = frame.aircraft_class.iloc[0] if "aircraft_class" in frame else "unclassified"
+ yield track
+
+ def coverage_for(self, bounds, time_range):
+ source = self.report["coverage"]
+ box, times = source["bounds"], source["time_range_s"]
+ complete = (box[0] <= bounds[0] <= bounds[2] <= box[2] and box[1] <= bounds[1] <= bounds[3] <= box[3]
+ and times[0] <= time_range[0] <= time_range[1] <= times[1] and source.get("all_aircraft", False))
+ return {"requested_bounds": list(bounds), "requested_time_range_s": list(time_range),
+ "source": source, "scope_complete": bool(complete), "receiver_completeness_known": False,
+ "input_hash": self.report["input_hash"]}
+
+
+class FlightCache:
+ """Date-to-index acquisition; immutable index per source/version/scope."""
+ def __init__(self, root, *, offline=False, session=None):
+ self.root, self.offline = Path(root), offline
+ self.session = session or requests.Session()
+
+ def acquire_day(self, day, *, provider="staging-0"):
+ day = date.fromisoformat(day)
+ tag = f"v{day:%Y.%m.%d}-planes-readsb-{provider}"
+ directory = self.root / day.isoformat() / provider
+ directory.mkdir(parents=True, exist_ok=True)
+ index = directory / "all-types-v1.sqlite"
+ if index.exists():
+ return FlightIndex(index)
+ release_path = directory / "release.json"
+ if not release_path.exists():
+ if self.offline:
+ raise DataUnavailable(f"Offline release metadata missing: {day}")
+ try:
+ response = self.session.get(f"https://api.github.com/repos/adsblol/globe_history_{day.year}/releases/tags/{tag}", timeout=60)
+ response.raise_for_status()
+ write_json(release_path, response.json())
+ except requests.RequestException as error:
+ raise DataUnavailable(f"Historical release unavailable: {day}") from error
+ release = json.loads(release_path.read_text())
+ assets = sorted((a for a in release["assets"] if ".tar." in a["name"]), key=lambda a: a["name"])
+ if not assets:
+ raise DataUnavailable("Release has no split trace archive")
+ sources = []
+ for asset in assets:
+ 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))
+ 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,
+ "release_sha256": sha256(release_path), "license": "ODbL", "missing_receiver_data_possible": True}
+ types_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)
+ raw = types_path.read_bytes()
+ if raw[:2] == b"\x1f\x8b":
+ raw = gzip.decompress(raw)
+ text = raw.decode()
+ types = json.loads(text[text.index("{"):text.rindex("}") + 1])
+ class_map = {}
+ for code, value in types.items():
+ description = value.get("desc", "") if isinstance(value, dict) else (value[2] if len(value) > 2 else "")
+ class_map[code] = {"J": "jet", "T": "turboprop", "P": "piston"}.get(description[-1:], "unclassified")
+ return FlightIndex.build(index, archive_frames([s["path"] for s in sources]), sources=sources + [type_source],
+ coverage=coverage, class_map=class_map)
+
+
+class CachedTerrain:
+ """GLO-90 + EGM2008 with explicit offline operation and file provenance.
+
+ Only catalogue-absent tiles are treated as ocean. Download errors and raster
+ nodata remain missing coverage. Existing operator-supplied cache files can be
+ adopted explicitly after format validation; receipts then detect corruption.
+ """
+ base = "https://copernicus-dem-90m.s3.amazonaws.com"
+
+ def __init__(self, root, *, offline=False, geoid_path=None, adopt_existing=False):
+ self.root, self.offline = Path(root), offline
+ self.root.mkdir(parents=True, exist_ok=True)
+ self.files, self.ocean_tiles, self.failures = {}, set(), []
+ self.adopt_existing = adopt_existing
+ catalog = self.root / "tileList.txt"
+ self._file(self.base + "/tileList.txt", catalog, raster=False)
+ self.catalog = {line.rstrip("/") for line in catalog.read_text().splitlines()}
+ if not self.catalog or not all(s.startswith("Copernicus_DSM_COG_30_") for s in self.catalog):
+ raise DataUnavailable("Invalid GLO-90 catalogue")
+ self.geoid_path = Path(geoid_path) if geoid_path else self.root / "us_nga_egm08_25.tif"
+ self._file("https://cdn.proj.org/us_nga_egm08_25.tif", self.geoid_path, raster=True)
+
+ def _file(self, url, path, *, raster):
+ if path.name in self.files:
+ return path
+ receipt = path.with_name(path.name + ".sha256.json")
+ if path.exists() and not receipt.exists() and self.adopt_existing:
+ if raster:
+ import rasterio
+ with rasterio.open(path) as dataset:
+ dataset.read(1, window=((0, 1), (0, 1)))
+ write_json(receipt, {"sha256": sha256(path), "origin": "operator-supplied-cache", "url": url})
+ result = download(url, path, offline=self.offline)
+ if raster:
+ import rasterio
+ try:
+ with rasterio.open(path) as dataset:
+ dataset.read(1, window=((0, 1), (0, 1)))
+ except rasterio.errors.RasterioError as error:
+ raise DataUnavailable(f"Invalid terrain raster: {path}") from error
+ self.files[path.name] = result
+ return path
+
+ def sample(self, lat, lon):
+ from .terrain import Terrain
+ lat, lon = np.broadcast_arrays(lat, lon)
+ shape = lat.shape
+ lat, lon = lat.ravel(), lon.ravel()
+ degrees = np.column_stack((np.floor(lat).astype(int), np.floor(lon).astype(int)))
+ self.last_sample_files = {"tileList.txt", self.geoid_path.name}
+ tiles, inverse = np.unique(degrees, axis=0, return_inverse=True)
+ height = np.zeros(len(lat))
+ try:
+ for i, (north, east) in enumerate(tiles):
+ tag = f"Copernicus_DSM_COG_30_{'N' if north >= 0 else 'S'}{abs(north):02d}_00_{'E' if east >= 0 else 'W'}{abs(east):03d}_00_DEM"
+ if tag not in self.catalog:
+ self.ocean_tiles.add(tag)
+ continue
+ path = self._file(f"{self.base}/{tag}/{tag}.tif", self.root / f"{tag}.tif", raster=True)
+ self.last_sample_files.add(path.name)
+ selected = inverse == i
+ sampler = Terrain([path])
+ try:
+ height[selected] = sampler.sample(lat[selected], lon[selected])
+ finally:
+ for raster in sampler.rasters:
+ raster.close()
+ sampler = Terrain([self.geoid_path])
+ try:
+ height += sampler.sample(lat, lon)
+ finally:
+ for raster in sampler.rasters:
+ raster.close()
+ except (DataUnavailable, ValueError) as error:
+ self.failures.append(str(error))
+ raise DataUnavailable(f"Terrain coverage unavailable: {error}") from error
+ return height.reshape(shape)
+
+ def provenance(self):
+ return {"provider": "Copernicus GLO-90", "vertical_datum": "EGM2008 converted to ellipsoidal",
+ "files": list(self.files.values()), "catalogue_absent_ocean_tiles": sorted(self.ocean_tiles),
+ "failures": list(self.failures), "offline": self.offline}
+
+
+@dataclass
+class AcquiredInputs:
+ indexes: list
+ terrain: object
+ bounds: tuple
+ time_range_s: tuple
+ recording_start_window_s: tuple
+ coverage: dict
+
+
+def acquire_inputs(request, recording, *, cache_root, index_paths=None, offline=False,
+ terrain_options=None, max_sound_range_m=20000.0):
+ """Contract adapter for orchestration; no hand-prepared Parquet is required.
+
+ `index_paths` is an operator-owned explicit offline override, not a user
+ search input. Its source scope remains visible. The default obtains complete
+ daily all-type archives once and reuses their disk indexes on later jobs.
+ """
+ from .contracts import utc_timestamp, validate_request, validate_recording
+ request, recording = validate_request(request), validate_recording(recording)
+ window = request.get("time_window") or recording.get("time_window")
+ if window is None:
+ raise ValueError("A defensible recording-start time window is required")
+ start, end = (utc_timestamp(window[k]).timestamp() for k in ("start_utc", "end_utc"))
+ times = (start - max_sound_range_m / 300.0 - 30.0, end + recording["duration_s"] + 30.0)
+ root = Path(cache_root)
+ if index_paths:
+ indexes = [FlightIndex(path) for path in index_paths]
+ else:
+ cache = FlightCache(root / "flights", offline=offline)
+ first, last = (datetime.fromtimestamp(t, timezone.utc).date() for t in times)
+ indexes = []
+ day = first
+ while day <= last:
+ indexes.append(cache.acquire_day(day.isoformat()))
+ day += timedelta(days=1)
+ bounds = tuple(request["search"].get("bounds_wsen", CONUS))
+ options = dict(terrain_options or {})
+ terrain = CachedTerrain(root / "terrain", offline=offline, **options)
+ coverage = {"requested_area": request["search"], "sources": [i.coverage_for(bounds, times) for i in indexes],
+ "limitations": ["ADS-B receiver completeness is unknown; unobserved flights remain possible.",
+ "Copernicus is a surface model and cannot establish microphone height."]}
+ return AcquiredInputs(indexes, terrain, bounds, times, (start, end), coverage)
diff --git a/tests/test_data_access.py b/tests/test_data_access.py
new file mode 100644
index 0000000..fcf3572
--- /dev/null
+++ b/tests/test_data_access.py
@@ -0,0 +1,137 @@
+import gzip
+import hashlib
+import io
+import json
+import tarfile
+
+import numpy as np
+import pandas as pd
+import pytest
+
+from aircraft_geoloc.data_access import CachedTerrain, DataUnavailable, FlightIndex, archive_frames, download
+
+
+def samples():
+ return pd.DataFrame({"flight_id": ["jet"] * 6 + ["unknown"] * 3,
+ "time_s": [0, 5, 10, 100, 105, 110, 0, 5, 10],
+ "lat": [40.] * 9, "lon": np.linspace(-100.2, -99.8, 9),
+ "alt_m": [1000.] * 9, "typecode": ["B738"] * 6 + [""] * 3,
+ "altitude_basis": ["ellipsoidal"] * 3 + ["barometric"] * 6})
+
+
+def test_whole_segments_jets_unknown_altitude_and_repeat(tmp_path):
+ parquet = tmp_path / "input.parquet"
+ frame = samples()
+ frame.to_parquet(parquet)
+ coverage = {"bounds": [-125, 24, -66, 50], "time_range_s": [0, 200], "all_aircraft": True}
+ index = FlightIndex.from_parquet(tmp_path / "index.sqlite", parquet, coverage=coverage, class_map={"B738": "jet"})
+ tracks = list(index.query([-101, 39, -99, 41], [4, 6]))
+ assert [t.id for t in tracks] == ["jet:0", "unknown"]
+ assert tracks[0].time.tolist() == [0, 5, 10] # query does not clip
+ assert tracks[0].typecode == "B738"
+ assert tracks[0].aircraft_class == "jet" and tracks[1].aircraft_class == "unclassified"
+ assert tracks[0].altitude_basis == ["ellipsoidal"] * 3
+ assert np.isnan(tracks[0].position(11)).all() # no gap extrapolation
+ later = list(index.query([-101, 39, -99, 41], [104, 106]))
+ assert later[0].id == "jet:1"
+ assert later[0].altitude_basis == ["barometric"] * 3
+ before = index.path.stat().st_mtime_ns
+ replay = FlightIndex.from_parquet(index.path, parquet, coverage=coverage, class_map={"B738": "jet"})
+ assert replay.path.stat().st_mtime_ns == before
+ assert replay.coverage_for([-101, 39, -99, 41], [0, 200])["scope_complete"]
+ assert not replay.coverage_for([-130, 39, -99, 41], [0, 200])["scope_complete"]
+ with pytest.raises(DataUnavailable, match="different inputs"):
+ FlightIndex.from_parquet(index.path, parquet, coverage=dict(coverage, all_aircraft=False), class_map={"B738": "jet"})
+
+
+def test_truncated_parquet_and_payload_corruption(tmp_path):
+ path = tmp_path / "bad.parquet"
+ path.write_bytes(b"PAR1truncated")
+ with pytest.raises(DataUnavailable, match="Parquet"):
+ FlightIndex.from_parquet(tmp_path / "bad.sqlite", path, coverage={})
+ index = FlightIndex.build(tmp_path / "index.sqlite", [samples()], sources=[], coverage={})
+ import sqlite3
+ with sqlite3.connect(index.path) as db:
+ db.execute("UPDATE segments SET payload=?", (b"corrupt",))
+ with pytest.raises(DataUnavailable, match="Corrupt flight payload"):
+ list(index.query([-180, -90, 180, 90], [0, 200]))
+
+
+class Response:
+ def __init__(self, data, status=200, headers=None):
+ self.data, self.status_code, self.headers = data, status, headers or {}
+ def __enter__(self): return self
+ def __exit__(self, *args): pass
+ def raise_for_status(self): pass
+ def iter_content(self, size): yield self.data
+
+
+class Session:
+ def __init__(self, response): self.response, self.calls = response, []
+ def get(self, url, **kwargs):
+ self.calls.append(kwargs)
+ return self.response
+
+
+def test_resumable_download_validates_ranges_and_offline_cache(tmp_path):
+ path = tmp_path / "asset"
+ path.with_name("asset.part").write_bytes(b"abc")
+ session = Session(Response(b"def", 206, {"Content-Range": "bytes 3-5/6"}))
+ digest = hashlib.sha256(b"abcdef").hexdigest()
+ result = download("url", path, size=6, digest=digest, session=session)
+ assert path.read_bytes() == b"abcdef" and result["sha256"] == digest
+ assert session.calls[0]["headers"]["Range"] == "bytes=3-"
+ download("url", path, size=6, offline=True)
+ path.write_bytes(b"broken")
+ with pytest.raises(DataUnavailable, match="corrupt"):
+ download("url", path, size=6, offline=True)
+ with pytest.raises(DataUnavailable, match="Offline"):
+ download("url", tmp_path / "absent", offline=True)
+ other = tmp_path / "other"
+ other.with_name("other.part").write_bytes(b"abc")
+ with pytest.raises(DataUnavailable, match="byte range"):
+ download("url", other, size=6, session=Session(Response(b"abcdef")))
+ assert not other.exists()
+ with pytest.raises(DataUnavailable, match="Truncated download"):
+ download("url", tmp_path / "truncated", size=6, session=Session(Response(b"abc")))
+
+
+def test_streaming_split_readsb_archive(tmp_path):
+ trace = {"icao": "a12345", "timestamp": 1000, "t": "B738", "trace": [
+ [i, 40, -100 + i * .001, 3000, 100, 90, 0, 0, {}, None, 3200, 0] for i in [0, 5, 10]]}
+ data = gzip.compress(json.dumps(trace).encode())
+ stream = io.BytesIO()
+ with tarfile.open(fileobj=stream, mode="w") as archive:
+ member = tarfile.TarInfo("traces/trace_full_a12345.json")
+ member.size = len(data)
+ archive.addfile(member, io.BytesIO(data))
+ raw = stream.getvalue()
+ paths = [tmp_path / "archive.aa", tmp_path / "archive.ab"]
+ paths[0].write_bytes(raw[:600]); paths[1].write_bytes(raw[600:])
+ frames = list(archive_frames(paths))
+ assert frames[0].typecode.tolist() == ["B738"] * 3
+ assert frames[0].altitude_basis.tolist() == ["ellipsoidal"] * 3
+ assert np.allclose(frames[0].alt_m, 3200 * .3048)
+ paths[1].write_bytes(b"broken")
+ with pytest.raises(DataUnavailable, match="archive"):
+ list(archive_frames(paths))
+
+
+def test_terrain_offline_datum_missing_and_corruption(tmp_path):
+ import rasterio
+ from rasterio.transform import from_origin
+ tag = "Copernicus_DSM_COG_30_N40_00_W100_00_DEM"
+ (tmp_path / "tileList.txt").write_text(tag + "/\n")
+ for name, value in [(tag + ".tif", 100.), ("geoid.tif", -30.)]:
+ with rasterio.open(tmp_path / name, "w", driver="GTiff", height=2, width=2, count=1,
+ dtype="float32", crs="EPSG:4326", transform=from_origin(-100, 41, .5, .5)) as raster:
+ raster.write(np.full((2, 2), value, dtype="float32"), 1)
+ terrain = CachedTerrain(tmp_path, offline=True, geoid_path=tmp_path / "geoid.tif", adopt_existing=True)
+ assert terrain.sample([40.5], [-99.5]).tolist() == [70.]
+ assert terrain.provenance()["vertical_datum"] == "EGM2008 converted to ellipsoidal"
+ (tmp_path / (tag + ".tif")).unlink()
+ with pytest.raises(DataUnavailable, match="Offline"):
+ CachedTerrain(tmp_path, offline=True, geoid_path=tmp_path / "geoid.tif").sample([40.5], [-99.5])
+ (tmp_path / "tileList.txt").write_text("broken")
+ with pytest.raises(DataUnavailable, match="corrupt"):
+ CachedTerrain(tmp_path, offline=True, geoid_path=tmp_path / "geoid.tif")