Write the aircraft down, and draw where they went
ADS-B was a live table and nothing else: an aircraft was overhead for four minutes and then gone, with nothing kept. Now everything heard goes into adsb_<time>.jsonl as it arrives -- one object per frame, the raw hex beside what was read out of it, flushed per line because a listening session ends with control-C -- with a readable report beside it. flights.py asks who the aircraft are: adsbdb for the airframe and the route, hexdb behind it, cached for a month. What needs no website is answered without one, because the ICAO address block says which country registered the aircraft and the first three letters of an airline callsign are its designator. Nothing but the address and the callsign heard on the air is ever sent. bandsaunter flights [LOG...] --out sky.gif reads a log back and draws the evening as a map with the clock running. Every frame is a moment: each aircraft is where it actually was then, interpolated between the position reports either side of it and dead-reckoned from its last speed and heading between them, and dropped rather than guessed at once it has not been heard for --stale seconds. The GIF is written here -- palette, LZW, frame differencing against a transparent index -- so nothing but numpy is needed; ffmpeg writes an MP4 where it happens to be installed, and .png draws the whole evening at once. The decoder needed 6.3 s to read a second of sky, so a live capture was losing six frames in seven. Reading the bits off a running total instead of summing each window takes that to 0.6 s, with identical output. --simulate flies six aircraft that are not there past a receiver that is not there, through the real encoder, the real checksum and the real decoder, so all of this can be tried without an aerial. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PsWPTweCT6pwxKngvVxcg
This commit is contained in:
parent
a8a8548369
commit
eae60cb04d
15 changed files with 3857 additions and 177 deletions
400
tests/test_flightmap.py
Normal file
400
tests/test_flightmap.py
Normal file
|
|
@ -0,0 +1,400 @@
|
|||
"""The moving map: what is drawn, and whether the file is really a GIF.
|
||||
|
||||
A picture is checked here the way a picture has to be -- by reading the pixels
|
||||
back -- and the file by taking it apart with a reader written from the GIF
|
||||
specification rather than from the encoder in the program. An encoder checked
|
||||
against itself is not checked.
|
||||
"""
|
||||
import struct
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from bandsaunter import flightmap as fm
|
||||
from bandsaunter.flightlog import Fix, Track
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# A GIF reader, built from the specification
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def lzw_decode(data: bytes, code_bits: int) -> bytes:
|
||||
"""GIF's LZW, the reading side, written from the standard."""
|
||||
clear, end = 1 << code_bits, (1 << code_bits) + 1
|
||||
table = {i: bytes([i]) for i in range(clear)}
|
||||
width = code_bits + 1
|
||||
counter = end + 1 # counts codes read, not entries added
|
||||
out = bytearray()
|
||||
previous = None
|
||||
value = held = at = 0
|
||||
while True:
|
||||
while held < width and at < len(data):
|
||||
value |= data[at] << held
|
||||
held += 8
|
||||
at += 1
|
||||
if held < width:
|
||||
break
|
||||
code = value & ((1 << width) - 1)
|
||||
value >>= width
|
||||
held -= width
|
||||
if code == clear:
|
||||
table = {i: bytes([i]) for i in range(clear)}
|
||||
width, counter, previous = code_bits + 1, end + 1, None
|
||||
continue
|
||||
if code == end:
|
||||
break
|
||||
# The width goes up as codes are read rather than as the table is
|
||||
# filled: the decoder builds its table one code behind the encoder,
|
||||
# and counting entries instead would widen the codes one too late.
|
||||
read_at = counter
|
||||
if counter < 4096:
|
||||
counter += 1
|
||||
if counter > (1 << width) and width < 12:
|
||||
width += 1
|
||||
if code in table:
|
||||
entry = table[code]
|
||||
elif previous is not None:
|
||||
entry = previous + previous[:1]
|
||||
else:
|
||||
raise ValueError(f"code {code} before anything defined it")
|
||||
out += entry
|
||||
if previous is not None and read_at - 1 < 4096:
|
||||
table[read_at - 1] = previous + entry[:1]
|
||||
previous = entry
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def read_gif(path: Path) -> dict:
|
||||
"""Take a GIF apart: the screen, the palette and every frame in it."""
|
||||
raw = Path(path).read_bytes()
|
||||
assert raw[:6] == b"GIF89a", "not a GIF89a"
|
||||
width, height, packed, _bg, _aspect = struct.unpack("<HHBBB", raw[6:13])
|
||||
at = 13
|
||||
table_size = 2 ** ((packed & 0x07) + 1)
|
||||
assert packed & 0x80, "no global colour table"
|
||||
palette = np.frombuffer(raw[at:at + table_size * 3],
|
||||
dtype=np.uint8).reshape(-1, 3)
|
||||
at += table_size * 3
|
||||
|
||||
frames, loops, control = [], False, {}
|
||||
while at < len(raw):
|
||||
block = raw[at]
|
||||
if block == 0x3B: # trailer
|
||||
break
|
||||
if block == 0x21: # extension
|
||||
label = raw[at + 1]
|
||||
at += 2
|
||||
body = b""
|
||||
while raw[at]:
|
||||
size = raw[at]
|
||||
body += raw[at + 1:at + 1 + size]
|
||||
at += size + 1
|
||||
at += 1
|
||||
if label == 0xF9:
|
||||
control = {"disposal": (body[0] >> 2) & 0x07,
|
||||
"transparent": body[3] if body[0] & 1 else None,
|
||||
"delay": struct.unpack("<H", body[1:3])[0]}
|
||||
elif label == 0xFF and body.startswith(b"NETSCAPE2.0"):
|
||||
loops = True
|
||||
continue
|
||||
if block == 0x2C: # image descriptor
|
||||
left, top, w, h, flags = struct.unpack("<HHHHB", raw[at + 1:at + 10])
|
||||
at += 10
|
||||
assert not flags & 0x80, "a local colour table was not expected"
|
||||
code_bits = raw[at]
|
||||
at += 1
|
||||
data = b""
|
||||
while raw[at]:
|
||||
size = raw[at]
|
||||
data += raw[at + 1:at + 1 + size]
|
||||
at += size + 1
|
||||
at += 1
|
||||
pixels = np.frombuffer(lzw_decode(data, code_bits),
|
||||
dtype=np.uint8)
|
||||
assert pixels.size == w * h, f"{pixels.size} pixels for {w}x{h}"
|
||||
frames.append({"left": left, "top": top, "width": w, "height": h,
|
||||
"pixels": pixels.reshape(h, w), **control})
|
||||
continue
|
||||
raise ValueError(f"unknown block {block:#x} at {at}")
|
||||
return {"width": width, "height": height, "palette": palette,
|
||||
"frames": frames, "loops": loops}
|
||||
|
||||
|
||||
def played(gif: dict) -> list[np.ndarray]:
|
||||
"""Every frame as it appears on the screen, patches laid over each other."""
|
||||
canvas = np.zeros((gif["height"], gif["width"]), dtype=np.uint8)
|
||||
out = []
|
||||
for frame in gif["frames"]:
|
||||
patch = frame["pixels"]
|
||||
top, left = frame["top"], frame["left"]
|
||||
area = canvas[top:top + frame["height"], left:left + frame["width"]]
|
||||
if frame.get("transparent") is None:
|
||||
area[:, :] = patch
|
||||
else:
|
||||
keep = patch != frame["transparent"]
|
||||
area[keep] = patch[keep]
|
||||
out.append(canvas.copy())
|
||||
return out
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tracks to draw
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def straight(icao="4CA1FA", callsign="RYR1234", lat=51.0, lon=-1.0,
|
||||
heading=90.0, speed=480.0, altitude=35_000, seconds=600.0,
|
||||
every=20.0, start=1_000_000.0) -> Track:
|
||||
"""One aircraft flying a straight line at a steady speed."""
|
||||
from bandsaunter.flightlog import move
|
||||
|
||||
fixes = []
|
||||
when = 0.0
|
||||
while when <= seconds:
|
||||
step = speed * when / 3600.0
|
||||
here = move(lat, lon, heading, step)
|
||||
fixes.append(Fix(at=start + when, latitude=here[0], longitude=here[1],
|
||||
altitude_ft=altitude, ground_speed_kt=speed,
|
||||
track_deg=heading))
|
||||
when += every
|
||||
return Track(icao=icao, callsign=callsign, fixes=fixes,
|
||||
frames=len(fixes) * 3, first_seen=start,
|
||||
last_seen=start + seconds)
|
||||
|
||||
|
||||
def two_aircraft() -> list[Track]:
|
||||
return [straight(),
|
||||
straight(icao="A835AF", callsign="UAL1902", lat=51.4, lon=-0.6,
|
||||
heading=250.0, speed=300.0, altitude=9_000)]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Colours and geometry
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_the_palette_is_a_full_table_with_room_for_transparency():
|
||||
assert fm.PALETTE.shape == (256, 3)
|
||||
assert fm.TRANSPARENT == 255
|
||||
|
||||
|
||||
def test_altitude_becomes_a_colour_that_climbs_with_it():
|
||||
steps = [fm.altitude_step(ft) for ft in (0, 5_000, 20_000, 35_000, 45_000)]
|
||||
assert steps == sorted(steps)
|
||||
assert steps[0] == 0 and steps[-1] == fm.RAMP_STEPS - 1
|
||||
assert fm.altitude_step(90_000) == fm.RAMP_STEPS - 1 # clamped
|
||||
|
||||
|
||||
def test_the_map_holds_every_position_that_was_reported():
|
||||
tracks = two_aircraft()
|
||||
view = fm.fit(tracks, width=800)
|
||||
for track in tracks:
|
||||
for fix in track.fixes:
|
||||
assert view.inside(fix.latitude, fix.longitude)
|
||||
x, y = view.xy(fix.latitude, fix.longitude)
|
||||
assert view.left <= x < view.left + view.width
|
||||
assert view.top <= y < view.top + view.height
|
||||
|
||||
|
||||
def test_a_mile_across_is_a_mile_up_the_picture():
|
||||
"""Longitude is squeezed by the cosine, or everything at fifty degrees
|
||||
north comes out stretched half as wide again."""
|
||||
view = fm.fit(two_aircraft(), width=800)
|
||||
per_nm_x = view.width / view.width_nm
|
||||
tall_nm = (view.north - view.south) * 60.0
|
||||
per_nm_y = view.height / tall_nm
|
||||
assert per_nm_x == pytest.approx(per_nm_y, rel=0.02)
|
||||
|
||||
|
||||
def test_one_aircraft_heard_once_still_gets_a_map():
|
||||
track = Track(icao="4CA1FA", fixes=[Fix(at=1.0, latitude=51.0,
|
||||
longitude=-1.0)])
|
||||
view = fm.fit([track], width=400)
|
||||
assert view is not None and view.north > view.south
|
||||
|
||||
|
||||
def test_nothing_to_draw_draws_nothing(tmp_path):
|
||||
assert fm.fit([Track(icao="4CA1FA")]) is None
|
||||
assert fm.animate([Track(icao="4CA1FA")], tmp_path / "x.gif") is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# What ends up on the picture
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_the_background_has_a_grid_a_scale_and_a_title():
|
||||
view = fm.fit(two_aircraft(), width=800)
|
||||
base = fm.background(view, title="6 AIRCRAFT 2026-09-03")
|
||||
assert (base == fm.GRID).sum() > 200 # the graticule and the border
|
||||
assert (base == fm.INK).sum() > 40 # the title
|
||||
assert (base == fm.DIM).sum() > 40 # scale bar and axis labels
|
||||
assert (base == fm.RAMP + fm.RAMP_STEPS - 1).any() # the key
|
||||
|
||||
|
||||
def test_an_aircraft_is_drawn_where_it_was_at_that_moment():
|
||||
tracks = [straight()]
|
||||
view = fm.fit(tracks, width=800)
|
||||
base = fm.background(view)
|
||||
when = tracks[0].first_seen + 300.0
|
||||
frame = fm.render_frame(base, view, tracks, when)
|
||||
fix = tracks[0].at(when)
|
||||
x, y = view.xy(fix.latitude, fix.longitude)
|
||||
patch = frame[y - 6:y + 7, x - 6:x + 7]
|
||||
assert (patch >= fm.RAMP).any() and (patch < fm.TRAIL).any()
|
||||
|
||||
|
||||
def test_the_aircraft_moves_between_frames_and_leaves_a_trail():
|
||||
tracks = [straight()]
|
||||
view = fm.fit(tracks, width=800)
|
||||
base = fm.background(view)
|
||||
early = fm.render_frame(base, view, tracks, tracks[0].first_seen + 60)
|
||||
late = fm.render_frame(base, view, tracks, tracks[0].first_seen + 540)
|
||||
assert not np.array_equal(early, late)
|
||||
trail_early = (early >= fm.TRAIL) & (early < fm.TRANSPARENT)
|
||||
trail_late = (late >= fm.TRAIL) & (late < fm.TRANSPARENT)
|
||||
assert trail_late.sum() > trail_early.sum()
|
||||
|
||||
|
||||
def test_an_aircraft_is_not_drawn_before_it_was_ever_heard():
|
||||
tracks = [straight()]
|
||||
view = fm.fit(tracks, width=800)
|
||||
base = fm.background(view)
|
||||
frame = fm.render_frame(base, view, tracks, tracks[0].first_seen - 10)
|
||||
assert np.array_equal(frame, base)
|
||||
|
||||
|
||||
def test_an_aircraft_long_gone_is_not_drawn_at_a_guessed_position():
|
||||
tracks = [straight()]
|
||||
view = fm.fit(tracks, width=800)
|
||||
base = fm.background(view)
|
||||
frame = fm.render_frame(base, view, tracks,
|
||||
tracks[0].last_seen + 900, stale=300)
|
||||
assert np.array_equal(frame, base)
|
||||
|
||||
|
||||
def test_the_clock_and_the_count_are_drawn_over_the_map():
|
||||
tracks = two_aircraft()
|
||||
view = fm.fit(tracks, width=800)
|
||||
base = fm.background(view)
|
||||
frame = fm.render_frame(base, view, tracks, tracks[0].first_seen + 60,
|
||||
clock="19:45:02")
|
||||
assert (frame == fm.PANEL).any()
|
||||
assert (frame == fm.INK).sum() > (base == fm.INK).sum()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# The file
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_the_lzw_stream_reads_back_as_what_went_in():
|
||||
for body in (b"\x00" * 300, bytes(range(256)) * 3,
|
||||
bytes([7, 7, 7, 8, 9, 7, 7, 8]) * 40):
|
||||
assert lzw_decode(fm._lzw(body, 8), 8) == body
|
||||
|
||||
|
||||
def test_a_long_stream_survives_the_table_filling_up():
|
||||
rng = np.random.default_rng(4)
|
||||
body = rng.integers(0, 60, size=200_000, dtype=np.uint8).tobytes()
|
||||
assert lzw_decode(fm._lzw(body, 8), 8) == body
|
||||
|
||||
|
||||
def test_the_animation_is_a_gif_that_loops(tmp_path):
|
||||
out = fm.animate(two_aircraft(), tmp_path / "flights.gif", fps=10,
|
||||
seconds=4, width=480)
|
||||
assert out is not None and out.path.exists()
|
||||
gif = read_gif(out.path)
|
||||
assert gif["loops"], "a map that plays once and stops"
|
||||
assert len(gif["frames"]) == out.frames
|
||||
assert (gif["width"], gif["height"]) == (out.width, out.height)
|
||||
assert gif["frames"][0]["delay"] == 10 # hundredths, so 10 a second
|
||||
|
||||
|
||||
def test_only_what_changed_is_written_after_the_first_frame(tmp_path):
|
||||
out = fm.animate(two_aircraft(), tmp_path / "flights.gif", fps=8,
|
||||
seconds=4, width=480)
|
||||
gif = read_gif(out.path)
|
||||
first = gif["frames"][0]
|
||||
assert (first["width"], first["height"]) == (out.width, out.height)
|
||||
later = gif["frames"][1:]
|
||||
assert later, "one frame is not an animation"
|
||||
assert all(f["width"] * f["height"] < out.width * out.height for f in later)
|
||||
assert all(f["transparent"] is not None for f in later)
|
||||
|
||||
|
||||
def test_the_frames_played_back_show_the_aircraft_moving(tmp_path):
|
||||
tracks = [straight()]
|
||||
out = fm.animate(tracks, tmp_path / "one.gif", fps=8, seconds=4, width=480)
|
||||
screens = played(read_gif(out.path))
|
||||
assert len(screens) == out.frames
|
||||
|
||||
def where(frame):
|
||||
lit = np.argwhere((frame >= fm.RAMP) & (frame < fm.TRAIL))
|
||||
return lit.mean(axis=0)
|
||||
|
||||
start, end = where(screens[1]), where(screens[-1])
|
||||
assert abs(end[1] - start[1]) > 20 # it went east across the picture
|
||||
for frame in screens:
|
||||
assert frame.shape == (out.height, out.width)
|
||||
|
||||
|
||||
def test_the_animation_says_how_much_flying_it_covers(tmp_path):
|
||||
tracks = two_aircraft()
|
||||
out = fm.animate(tracks, tmp_path / "flights.gif", fps=10, seconds=5,
|
||||
width=400)
|
||||
assert out.covers == pytest.approx(600.0, abs=1.0)
|
||||
played_for = out.frames / out.fps
|
||||
assert played_for == pytest.approx(5.0, rel=0.3)
|
||||
assert out.speed == pytest.approx(out.covers / played_for, rel=0.05)
|
||||
assert "aircraft" in out.summary()
|
||||
|
||||
|
||||
def test_asking_for_a_speed_gives_that_speed(tmp_path):
|
||||
out = fm.animate(two_aircraft(), tmp_path / "flights.gif", fps=10,
|
||||
speed=60.0, width=400)
|
||||
assert out.speed == pytest.approx(60.0, rel=0.1)
|
||||
assert out.frames == pytest.approx(600 / 60 * 10, abs=2)
|
||||
|
||||
|
||||
def test_a_still_picture_is_a_png_of_the_whole_evening(tmp_path):
|
||||
from bandsaunter.images import PNG_SIGNATURE
|
||||
|
||||
out = fm.animate(two_aircraft(), tmp_path / "flights.png", width=400)
|
||||
assert out.kind == "png"
|
||||
assert out.path.read_bytes()[:8] == PNG_SIGNATURE
|
||||
|
||||
|
||||
def test_pillow_agrees_that_it_is_an_animation(tmp_path):
|
||||
"""Not a dependency; when it happens to be installed it is a second
|
||||
opinion from a decoder nobody here wrote."""
|
||||
Image = pytest.importorskip("PIL.Image")
|
||||
out = fm.animate(two_aircraft(), tmp_path / "flights.gif", fps=10,
|
||||
seconds=3, width=400)
|
||||
with Image.open(out.path) as picture:
|
||||
assert picture.n_frames == out.frames
|
||||
assert picture.size == (out.width, out.height)
|
||||
picture.seek(0)
|
||||
first = np.array(picture.convert("RGB"))
|
||||
picture.seek(picture.n_frames - 1)
|
||||
last = np.array(picture.convert("RGB"))
|
||||
assert not np.array_equal(first, last)
|
||||
|
||||
|
||||
@pytest.mark.skipif(not fm.ffmpeg_available(), reason="ffmpeg is not installed")
|
||||
def test_a_video_can_be_written_where_ffmpeg_exists(tmp_path):
|
||||
out = fm.animate(two_aircraft(), tmp_path / "flights.mp4", fps=10,
|
||||
seconds=3, width=400)
|
||||
assert out.kind == "mp4" and out.path.stat().st_size > 1000
|
||||
probe = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0",
|
||||
"-show_entries", "stream=width,height",
|
||||
"-of", "csv=p=0", str(out.path)],
|
||||
capture_output=True, text=True)
|
||||
if probe.returncode == 0:
|
||||
assert probe.stdout.strip() == f"{out.width},{out.height}"
|
||||
|
||||
|
||||
def test_the_picture_is_an_even_number_of_pixels_across(tmp_path):
|
||||
"""Video encoders refuse an odd width, and it costs nothing to be even."""
|
||||
for width in (401, 402, 555):
|
||||
view = fm.fit(two_aircraft(), width=width)
|
||||
w, h = fm.canvas_size(view)
|
||||
assert w % 2 == 0 and h % 2 == 0
|
||||
488
tests/test_flights.py
Normal file
488
tests/test_flights.py
Normal file
|
|
@ -0,0 +1,488 @@
|
|||
"""Aircraft: who they are, what was written down, and what it reads back as.
|
||||
|
||||
Nothing here goes near a network. The registers are stubbed with the shapes
|
||||
they really answer with -- checked against the live services when this was
|
||||
written -- so a test failing means the reader changed, not that a website is
|
||||
down.
|
||||
"""
|
||||
import json
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from bandsaunter.flights import (Flight, FlightBook, airline_of,
|
||||
describe_address)
|
||||
from bandsaunter.flightlog import (Fix, FlightLog, Track, bearing_deg,
|
||||
distance_nm, move, read_logs, report,
|
||||
write_kml)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# What the address alone says
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest.mark.parametrize("icao,country", [
|
||||
("A835AF", "United States"), # N628TS
|
||||
("406B3B", "United Kingdom"), # G-DGEF
|
||||
("3C6444", "Germany"), # D-AIBD
|
||||
("4CA1FA", "Ireland"), # EI-DDH
|
||||
("7C4A1B", "Australia"),
|
||||
("C01234", "Canada"),
|
||||
("484200", "Netherlands"),
|
||||
])
|
||||
def test_the_address_says_which_country_registered_it(icao, country):
|
||||
"""Fixed by treaty, so no website is needed and none is asked."""
|
||||
assert describe_address(icao) == country
|
||||
|
||||
|
||||
def test_an_address_in_no_block_is_not_guessed_at():
|
||||
assert describe_address("F00000") == ""
|
||||
assert describe_address("") == ""
|
||||
assert describe_address("nonsense") == ""
|
||||
|
||||
|
||||
def test_a_military_block_is_named_before_the_country_it_sits_inside():
|
||||
assert describe_address("ADFFFF") == "United States military"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("callsign,airline", [
|
||||
("RYR1234", "Ryanair"),
|
||||
("BAW49", "British Airways"),
|
||||
("UAL1902", "United Airlines"),
|
||||
("RCH445", "United States Air Mobility Command (Reach)"),
|
||||
])
|
||||
def test_the_callsign_says_which_airline_is_flying(callsign, airline):
|
||||
assert airline_of(callsign) == airline
|
||||
|
||||
|
||||
@pytest.mark.parametrize("callsign", ["N517HP", "G-ABCD", "", "XX"])
|
||||
def test_a_registration_flown_as_a_callsign_is_not_an_airline(callsign):
|
||||
"""A private aircraft uses its registration, and reading three letters of
|
||||
that as an airline designator would invent one."""
|
||||
assert airline_of(callsign) == ""
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# The registers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
ADSBDB_AIRCRAFT = {"response": {"aircraft": {
|
||||
"type": "737 8AS", "icao_type": "B738", "manufacturer": "Boeing",
|
||||
"mode_s": "4CA1FA", "registration": "EI-DYP",
|
||||
"registered_owner_country_iso_name": "IE",
|
||||
"registered_owner_country_name": "Ireland",
|
||||
"registered_owner": "Ryanair"}}}
|
||||
|
||||
ADSBDB_ROUTE = {"response": {"flightroute": {
|
||||
"callsign": "RYR1234",
|
||||
"airline": {"name": "Ryanair", "icao": "RYR"},
|
||||
"origin": {"icao_code": "EGSS", "iata_code": "STN",
|
||||
"name": "London Stansted Airport", "municipality": "London",
|
||||
"latitude": 51.885, "longitude": 0.235},
|
||||
"destination": {"icao_code": "EGNX", "iata_code": "EMA",
|
||||
"name": "East Midlands Airport", "municipality": "Nottingham",
|
||||
"latitude": 52.8311, "longitude": -1.32806}}}}
|
||||
|
||||
HEXDB_AIRCRAFT = {"ModeS": "3C6444", "Registration": "D-AIBD",
|
||||
"Manufacturer": "Airbus", "ICAOTypeCode": "A319",
|
||||
"Type": "A319 112", "RegisteredOwners": "Lufthansa",
|
||||
"OperatorFlagCode": "DLH"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def register(monkeypatch, tmp_path):
|
||||
"""A book that answers from a table instead of the internet."""
|
||||
asked: list[str] = []
|
||||
answers: dict[str, object] = {}
|
||||
|
||||
def request(self, url):
|
||||
asked.append(url)
|
||||
for fragment, body in answers.items():
|
||||
if fragment in url:
|
||||
return body
|
||||
raise OSError("not found")
|
||||
|
||||
monkeypatch.setattr(FlightBook, "_request", request)
|
||||
book = FlightBook(cache=tmp_path / "flights.json")
|
||||
return book, asked, answers
|
||||
|
||||
|
||||
def test_a_register_answers_with_the_airframe_and_the_route(register):
|
||||
book, asked, answers = register
|
||||
answers["adsbdb.com/v0/aircraft"] = ADSBDB_AIRCRAFT
|
||||
answers["adsbdb.com/v0/callsign"] = ADSBDB_ROUTE
|
||||
entry = book.get("4CA1FA", "RYR1234")
|
||||
book.wait(5.0)
|
||||
assert entry.status == "found"
|
||||
assert entry.registration == "EI-DYP"
|
||||
assert entry.type_code == "B738"
|
||||
assert entry.operator == "Ryanair"
|
||||
assert entry.origin_code == "EGSS" and entry.destination_code == "EGNX"
|
||||
assert "Stansted" in entry.route and "East Midlands" in entry.route
|
||||
assert entry.origin_lat == pytest.approx(51.885)
|
||||
|
||||
|
||||
def test_the_second_register_is_asked_when_the_first_has_nothing(register):
|
||||
book, asked, answers = register
|
||||
answers["hexdb.io/api/v1/aircraft"] = HEXDB_AIRCRAFT
|
||||
entry = book.get("3C6444")
|
||||
book.wait(5.0)
|
||||
assert entry.status == "found"
|
||||
assert entry.registration == "D-AIBD"
|
||||
assert entry.manufacturer == "Airbus"
|
||||
assert any("adsbdb" in url for url in asked), "the first was skipped"
|
||||
|
||||
|
||||
def test_a_route_written_as_one_string_is_read_as_two_airports(register):
|
||||
book, asked, answers = register
|
||||
answers["hexdb.io/api/v1/route"] = {"flight": "BAW123",
|
||||
"route": "EGLL-OTHH"}
|
||||
entry = book.get("400001", "BAW123")
|
||||
book.wait(5.0)
|
||||
assert (entry.origin_code, entry.destination_code) == ("EGLL", "OTHH")
|
||||
|
||||
|
||||
def test_an_aircraft_no_register_holds_is_remembered_as_unlisted(register):
|
||||
book, asked, answers = register
|
||||
answers["adsbdb.com/v0/aircraft"] = {"response": {"aircraft": None}}
|
||||
entry = book.get("ABCDEF")
|
||||
book.wait(5.0)
|
||||
assert entry.status == "unlisted"
|
||||
assert entry.country == "United States" # the address still says this
|
||||
|
||||
|
||||
def test_nothing_reachable_leaves_what_the_address_said(register):
|
||||
book, asked, answers = register # no answers at all
|
||||
entry = book.get("4CA1FA", "RYR1234")
|
||||
book.wait(5.0)
|
||||
assert entry.status == "offline"
|
||||
assert entry.country == "Ireland"
|
||||
assert entry.airline == "Ryanair" # from the callsign, not a website
|
||||
|
||||
|
||||
def test_offline_asks_nobody(tmp_path, monkeypatch):
|
||||
def refuse(self, url):
|
||||
raise AssertionError(f"asked {url} while offline")
|
||||
|
||||
monkeypatch.setattr(FlightBook, "_request", refuse)
|
||||
book = FlightBook(online=False, cache=tmp_path / "c.json")
|
||||
entry = book.get("A835AF", "UAL1902")
|
||||
book.wait(1.0)
|
||||
assert entry.status == "local"
|
||||
assert entry.country == "United States"
|
||||
assert entry.airline == "United Airlines"
|
||||
|
||||
|
||||
def test_only_the_address_and_the_callsign_are_ever_sent(register):
|
||||
"""A register is told what was heard on the air and nothing else."""
|
||||
book, asked, answers = register
|
||||
answers["adsbdb.com"] = ADSBDB_AIRCRAFT
|
||||
book.get("4CA1FA", "RYR1234")
|
||||
book.wait(5.0)
|
||||
assert asked
|
||||
for url in asked:
|
||||
tail = url.split("://", 1)[1]
|
||||
for piece in tail.split("/")[1:]:
|
||||
assert piece in ("v0", "aircraft", "callsign", "api", "v1",
|
||||
"route", "icao", "4CA1FA", "RYR1234"), url
|
||||
|
||||
|
||||
def test_an_answer_is_kept_and_the_register_is_not_asked_twice(register,
|
||||
tmp_path):
|
||||
book, asked, answers = register
|
||||
answers["adsbdb.com/v0/aircraft"] = ADSBDB_AIRCRAFT
|
||||
book.get("4CA1FA")
|
||||
book.wait(5.0)
|
||||
book.save()
|
||||
again = FlightBook(cache=tmp_path / "flights.json")
|
||||
entry = again.get("4CA1FA")
|
||||
assert entry.registration == "EI-DYP"
|
||||
assert entry.status == "found"
|
||||
|
||||
|
||||
def test_a_cache_from_another_version_is_ignored(tmp_path):
|
||||
body = {"aircraft": {"4CA1FA": {"icao": "4CA1FA", "registration": "EI-DYP",
|
||||
"status": "found", "version": 0,
|
||||
"fetched_at": time.time()}}}
|
||||
(tmp_path / "c.json").write_text(json.dumps(body))
|
||||
book = FlightBook(online=False, cache=tmp_path / "c.json")
|
||||
assert book.get("4CA1FA").registration == ""
|
||||
|
||||
|
||||
def test_what_is_printed_says_the_aircraft_the_operator_and_the_route():
|
||||
flight = Flight(icao="4CA1FA", callsign="RYR1234", registration="EI-DYP",
|
||||
manufacturer="Boeing", model="737-8AS", operator="Ryanair",
|
||||
origin="Stansted", destination="East Midlands")
|
||||
assert flight.aircraft == "Boeing 737-8AS (EI-DYP)"
|
||||
assert flight.route == "Stansted → East Midlands"
|
||||
assert "Ryanair" in flight.summary()
|
||||
assert "registration: EI-DYP" in flight.details()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# The log
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class _Frame:
|
||||
"""Just enough of a decoded frame for the log to write one down."""
|
||||
|
||||
def __init__(self, icao="4CA1FA", df=17, tc=11, data=b"\x8d\x4c\xa1\xfa",
|
||||
callsign="", altitude_ft=0, ground_speed_kt=0.0,
|
||||
track_deg=0.0, vertical_rate_fpm=0):
|
||||
self.icao, self.df, self.type_code, self.data = icao, df, tc, data
|
||||
self.callsign = callsign
|
||||
self.altitude_ft = altitude_ft
|
||||
self.ground_speed_kt = ground_speed_kt
|
||||
self.track_deg = track_deg
|
||||
self.vertical_rate_fpm = vertical_rate_fpm
|
||||
|
||||
|
||||
class _Craft:
|
||||
def __init__(self, lat=0.0, lon=0.0, callsign=""):
|
||||
self.latitude, self.longitude, self.callsign = lat, lon, callsign
|
||||
|
||||
@property
|
||||
def located(self):
|
||||
return bool(self.latitude or self.longitude)
|
||||
|
||||
|
||||
def _log(tmp_path, name="adsb_test.jsonl"):
|
||||
return FlightLog(tmp_path / name, receiver="test", frequency=1090e6,
|
||||
sample_rate=2e6, started=1_000_000.0)
|
||||
|
||||
|
||||
def test_the_log_keeps_the_raw_frame_next_to_what_was_read_out_of_it(tmp_path):
|
||||
log = _log(tmp_path)
|
||||
log.append(_Frame(data=bytes.fromhex("8D4CA1FA9905A11E202C00D3450D"),
|
||||
altitude_ft=35000),
|
||||
_Craft(51.5, -0.12), when=1_000_001.0)
|
||||
log.close()
|
||||
lines = [json.loads(x) for x in
|
||||
log.path.read_text().splitlines() if x.strip()]
|
||||
assert lines[0]["log"] == "bandsaunter-adsb"
|
||||
assert lines[1]["hex"] == "8D4CA1FA9905A11E202C00D3450D"
|
||||
assert lines[1]["lat"] == 51.5 and lines[1]["alt_ft"] == 35000
|
||||
|
||||
|
||||
def test_the_log_is_on_the_disk_before_the_session_ends(tmp_path):
|
||||
"""A listening session ends with control-C, so nothing may wait for a
|
||||
clean shutdown."""
|
||||
log = _log(tmp_path)
|
||||
log.append(_Frame(), _Craft(51.5, -0.12), when=1_000_001.0)
|
||||
assert "4CA1FA" in log.path.read_text() # not closed, already written
|
||||
log.close()
|
||||
|
||||
|
||||
def test_what_was_written_reads_back_as_a_track(tmp_path):
|
||||
log = _log(tmp_path)
|
||||
for i in range(5):
|
||||
log.append(_Frame(altitude_ft=30000 + i * 100, ground_speed_kt=420.0,
|
||||
track_deg=90.0, callsign="RYR1234"),
|
||||
_Craft(51.5 + i * 0.01, -0.12 + i * 0.02, "RYR1234"),
|
||||
when=1_000_000.0 + i * 10)
|
||||
log.close()
|
||||
tracks = read_logs(log.path)
|
||||
assert len(tracks) == 1
|
||||
track = tracks[0]
|
||||
assert track.icao == "4CA1FA" and track.callsign == "RYR1234"
|
||||
assert len(track.fixes) == 5
|
||||
assert track.frames == 5
|
||||
assert track.distance_nm > 0
|
||||
assert track.altitude_range == (30000, 30400)
|
||||
|
||||
|
||||
def test_an_aircraft_that_never_moved_is_one_point_not_seven_thousand(tmp_path):
|
||||
"""A transponder on a stand reports the same place twice a second."""
|
||||
log = _log(tmp_path)
|
||||
for i in range(50):
|
||||
log.append(_Frame(altitude_ft=0), _Craft(51.5, -0.12),
|
||||
when=1_000_000.0 + i)
|
||||
log.close()
|
||||
track = read_logs(log.path)[0]
|
||||
assert len(track.fixes) == 1
|
||||
assert track.frames == 50
|
||||
|
||||
|
||||
def test_two_logs_read_as_one_evening(tmp_path):
|
||||
first = _log(tmp_path, "a.jsonl")
|
||||
first.append(_Frame(), _Craft(51.5, -0.12), when=1_000_000.0)
|
||||
first.close()
|
||||
second = _log(tmp_path, "b.jsonl")
|
||||
second.append(_Frame(icao="3C6444"), _Craft(50.0, 8.0), when=1_000_100.0)
|
||||
second.close()
|
||||
tracks = read_logs([first.path, second.path])
|
||||
assert [t.icao for t in tracks] == ["4CA1FA", "3C6444"]
|
||||
|
||||
|
||||
def test_a_half_written_last_line_does_not_lose_the_rest(tmp_path):
|
||||
"""Control-C during a write, or a full disk: the evening still reads."""
|
||||
log = _log(tmp_path)
|
||||
log.append(_Frame(), _Craft(51.5, -0.12), when=1_000_000.0)
|
||||
log.close()
|
||||
with log.path.open("a") as handle:
|
||||
handle.write('{"t": 1000001.0, "icao": "3C64')
|
||||
assert len(read_logs(log.path)) == 1
|
||||
|
||||
|
||||
def test_a_position_fix_is_given_the_speed_the_aircraft_last_reported(tmp_path):
|
||||
"""Position and velocity arrive in different frames; the aeroplane is the
|
||||
same aeroplane a second later."""
|
||||
log = _log(tmp_path)
|
||||
log.append(_Frame(ground_speed_kt=420.0, track_deg=90.0), None,
|
||||
when=1_000_000.0)
|
||||
log.append(_Frame(), _Craft(51.5, -0.12), when=1_000_001.0)
|
||||
log.append(_Frame(), _Craft(51.6, -0.10), when=1_000_011.0)
|
||||
log.close()
|
||||
track = read_logs(log.path)[0]
|
||||
assert track.fixes[0].ground_speed_kt == 0.0 or track.fixes[0].track_deg
|
||||
assert track.fixes[-1].ground_speed_kt == 420.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Time and speed: where an aircraft was between two reports
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _track(**over) -> Track:
|
||||
fixes = [Fix(at=0.0, latitude=51.0, longitude=0.0, altitude_ft=10_000,
|
||||
ground_speed_kt=600.0, track_deg=90.0),
|
||||
Fix(at=60.0, latitude=51.0, longitude=0.2648, altitude_ft=12_000,
|
||||
ground_speed_kt=600.0, track_deg=90.0)]
|
||||
track = Track(icao="4CA1FA", callsign="RYR1234", fixes=fixes,
|
||||
first_seen=0.0, last_seen=60.0)
|
||||
for key, value in over.items():
|
||||
setattr(track, key, value)
|
||||
return track
|
||||
|
||||
|
||||
def test_halfway_between_two_reports_is_halfway_along():
|
||||
fix = _track().at(30.0)
|
||||
assert fix.longitude == pytest.approx(0.1324, abs=1e-3)
|
||||
assert fix.altitude_ft == pytest.approx(11_000, abs=20)
|
||||
|
||||
|
||||
def test_before_the_first_report_the_aircraft_is_not_drawn():
|
||||
assert _track().at(-1.0) is None
|
||||
|
||||
|
||||
def test_after_the_last_report_it_carries_on_at_the_speed_it_said():
|
||||
"""Ten knots-minutes on: dead reckoning, not a jump."""
|
||||
fix = _track().at(120.0, stale=300.0)
|
||||
assert fix is not None
|
||||
flown = distance_nm(51.0, 0.2648, fix.latitude, fix.longitude)
|
||||
assert flown == pytest.approx(10.0, rel=0.05) # 600 kt for a minute
|
||||
|
||||
|
||||
def test_an_aircraft_not_heard_for_a_long_time_stops_being_drawn():
|
||||
"""Five minutes on it has flown fifty miles and is a guess."""
|
||||
assert _track().at(60.0 + 400.0, stale=300.0) is None
|
||||
|
||||
|
||||
def test_the_trail_is_everywhere_it_had_been_by_then():
|
||||
track = _track()
|
||||
assert len(track.trail(30.0)) == 2 # one fix and where it is
|
||||
assert track.trail(30.0)[-1].longitude == pytest.approx(0.1324, abs=1e-3)
|
||||
|
||||
|
||||
def test_a_bearing_and_a_distance_agree_with_the_move_they_describe():
|
||||
lat, lon = move(51.0, 0.0, 90.0, 60.0)
|
||||
assert distance_nm(51.0, 0.0, lat, lon) == pytest.approx(60.0, rel=1e-3)
|
||||
assert bearing_deg(51.0, 0.0, lat, lon) == pytest.approx(90.0, abs=0.5)
|
||||
|
||||
|
||||
def test_a_track_across_the_date_line_does_not_go_the_long_way_round():
|
||||
track = Track(fixes=[Fix(at=0.0, latitude=0.0, longitude=179.9),
|
||||
Fix(at=10.0, latitude=0.0, longitude=-179.9)])
|
||||
fix = track.at(5.0)
|
||||
assert abs(fix.longitude) > 179.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# The readable report, and the map for Google Earth
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_the_report_says_who_where_and_how_far(tmp_path):
|
||||
log = _log(tmp_path)
|
||||
for i in range(4):
|
||||
log.append(_Frame(callsign="RYR1234", altitude_ft=30_000,
|
||||
ground_speed_kt=420.0, track_deg=90.0),
|
||||
_Craft(51.5, -0.12 + i * 0.05, "RYR1234"),
|
||||
when=1_000_000.0 + i * 30)
|
||||
log.close()
|
||||
lines = report(read_logs(log.path), title="test")
|
||||
text = "\n".join(lines)
|
||||
assert "1 aircraft" in text
|
||||
assert "4CA1FA" in text and "RYR1234" in text
|
||||
assert "flew:" in text and "altitude: 30,000 ft" in text
|
||||
|
||||
|
||||
def test_the_report_keeps_the_register_apart_from_the_air(register, tmp_path):
|
||||
book, asked, answers = register
|
||||
answers["adsbdb.com/v0/aircraft"] = ADSBDB_AIRCRAFT
|
||||
book.get("4CA1FA")
|
||||
book.wait(5.0)
|
||||
log = _log(tmp_path)
|
||||
log.append(_Frame(), _Craft(51.5, -0.12), when=1_000_000.0)
|
||||
log.close()
|
||||
text = "\n".join(report(read_logs(log.path), book))
|
||||
assert "registration: EI-DYP" in text
|
||||
assert "heard:" in text
|
||||
|
||||
|
||||
def test_nothing_heard_is_said_rather_than_drawn_as_an_empty_table():
|
||||
assert report([]) == ["nothing heard."]
|
||||
|
||||
|
||||
def test_google_earth_gets_the_whole_path_not_just_the_last_place(tmp_path):
|
||||
log = _log(tmp_path)
|
||||
for i in range(3):
|
||||
log.append(_Frame(altitude_ft=10_000), _Craft(51.5 + i * 0.1, -0.12),
|
||||
when=1_000_000.0 + i * 20)
|
||||
log.close()
|
||||
out = write_kml(tmp_path / "flights.kml", read_logs(log.path))
|
||||
body = out.read_text()
|
||||
assert "<LineString>" in body
|
||||
assert body.count(",") > 3
|
||||
assert "3048" in body or "3048" in body.replace(" ", "") # feet to metres
|
||||
|
||||
|
||||
def test_a_log_with_no_positions_makes_no_map(tmp_path):
|
||||
log = _log(tmp_path)
|
||||
log.append(_Frame(), None, when=1_000_000.0)
|
||||
log.close()
|
||||
assert write_kml(tmp_path / "flights.kml", read_logs(log.path)) is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# End to end, through the real decoder
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_a_simulated_sky_is_heard_recorded_and_read_back(tmp_path):
|
||||
"""The whole path: frames encoded, modulated, decoded, logged, re-read."""
|
||||
from bandsaunter.adsb import AircraftRegistry, SAMPLE_RATE, decode_frames
|
||||
from bandsaunter.adsb import SimulatedSky, default_sky
|
||||
|
||||
sky = SimulatedSky(default_sky(seed=3), noise=0.02, seed=1)
|
||||
registry = AircraftRegistry()
|
||||
log = _log(tmp_path)
|
||||
when = 1_000_000.0
|
||||
for _ in range(8):
|
||||
block = sky.read_samples(int(SAMPLE_RATE))
|
||||
for frame in decode_frames(block, SAMPLE_RATE):
|
||||
craft = registry.add(frame, when=when + frame.at_sample / SAMPLE_RATE)
|
||||
log.append(frame, craft, when=when + frame.at_sample / SAMPLE_RATE)
|
||||
when += 1.0
|
||||
log.close()
|
||||
|
||||
tracks = read_logs(log.path)
|
||||
assert len(tracks) == len(default_sky())
|
||||
assert all(t.callsign for t in tracks)
|
||||
assert all(t.located for t in tracks)
|
||||
for track in tracks:
|
||||
low, high = track.altitude_range
|
||||
assert 0 < high < 50_000
|
||||
assert track.top_speed_kt > 50
|
||||
# It flew at the speed it said it was flying, give or take the
|
||||
# quarter of a knot the encoding rounds to.
|
||||
flown = track.distance_nm
|
||||
expected = track.top_speed_kt * track.seconds / 3600.0
|
||||
assert flown == pytest.approx(expected, rel=0.35, abs=0.2)
|
||||
|
|
@ -91,6 +91,32 @@ def test_every_frame_survives_a_noisy_receiver(noise):
|
|||
assert registry.aircraft["4CA1FA"].located
|
||||
|
||||
|
||||
def test_a_frame_at_the_very_end_of_the_block_is_not_half_read():
|
||||
"""The bit reader runs off the end of the samples, and a long frame that
|
||||
does not fit must not be read as the short frame that does."""
|
||||
frame = gen.identification(0x4CA1FA, "RYR1234")
|
||||
samples = gen.modulate([frame], gap_us=8.0)
|
||||
cut = samples[:samples.size - int(0.5 * RATE / 1e6)] # half a bit short
|
||||
got = decode_frames(cut, RATE)
|
||||
assert got == [] or got[0].icao == "4CA1FA"
|
||||
|
||||
|
||||
def test_a_second_of_sky_is_decoded_in_less_than_a_second():
|
||||
"""A capture mode that cannot keep up is not capturing. Timed loosely --
|
||||
this is about the shape of the work, not the speed of the machine."""
|
||||
import time
|
||||
|
||||
from bandsaunter.adsb import SAMPLE_RATE, SimulatedSky, default_sky
|
||||
|
||||
sky = SimulatedSky(default_sky(), noise=0.02, seed=1)
|
||||
block = sky.read_samples(int(SAMPLE_RATE))
|
||||
started = time.perf_counter()
|
||||
got = decode_frames(block, SAMPLE_RATE)
|
||||
took = time.perf_counter() - started
|
||||
assert len(got) >= 18 # six aircraft, three frames each
|
||||
assert took < 3.0, f"a second of sky took {took:.1f} s to decode"
|
||||
|
||||
|
||||
def test_many_aircraft_are_kept_apart():
|
||||
frames = []
|
||||
for i, icao in enumerate((0x4CA1FA, 0xA0B1C2, 0x3C6444, 0x780102)):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue