From 46daea17b20508fcb88c3b305b2b1387ec05bf48 Mon Sep 17 00:00:00 2001 From: serversdown Date: Thu, 29 Oct 2026 23:06:25 -0400 Subject: [PATCH] fix(decode): a Blastware event's record type comes from its bytes, not its filename (#41) STRT[18] is the record-type byte: 0x46 on every one of DL2's 6,577 waveform files, 0x0A on every one of its 63,535 histograms. The decoder read the type from the filename's last letter, so an event with no Blastware name -- SFM's own downloads, the call-in path -- or a manual download ('.QZ0') came out 'Waveform' whatever it was. The filename is now a cross-check that logs a disagreement; the bytes win. An unknown byte (Manual / Event / Combo never occur in DL2) is record_type None with a decode_problems entry, not a guess. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9 --- minimateplus/event_file_io.py | 23 +++++++++++++++++------ tests/test_bw_file_fields.py | 35 +++++++++++++++++++++++++++++++++++ tests/test_decode_check.py | 7 +++---- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/minimateplus/event_file_io.py b/minimateplus/event_file_io.py index 11f645a..305cfb5 100644 --- a/minimateplus/event_file_io.py +++ b/minimateplus/event_file_io.py @@ -784,6 +784,12 @@ def _peaks_from_samples(samples: dict[str, list[int]]) -> PeakValues: ) +# STRT[18], the record-type byte (#41): 0x46 on every one of DL2's 6,577 +# waveform files and 0x0A on every one of its 63,535 histograms (measured +# 2026-10-29). Manual / Event / Combo never occur there, so their bytes are +# unknown and decode as None, not as a guess. +_RECORD_TYPE_BY_STRT = {0x46: "Waveform", 0x0A: "Histogram"} + _RECORD_TYPE_BY_EXT_SUFFIX = { 'H': 'Histogram', 'W': 'Waveform', @@ -1128,12 +1134,17 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event: ev = Event(index=-1) if strt_fields.get("waveform_key"): ev._waveform_key = bytes.fromhex(strt_fields["waveform_key"]) - # Derive record_type from the filename's extension suffix (H/W/M/E/C). - # When called from save_imported_bw the path here is a tmp file with a - # ".bw" suffix, so the derivation falls back to "Waveform" and the - # caller overrides ev.record_type using the original filename — see - # waveform_store.save_imported_bw. - ev.record_type = derive_record_type_from_filename(name) + # The record type comes from the bytes: STRT[18] (#41). The filename's + # last letter is only a cross-check -- SFM's own downloads have no name, + # and a manual download's (".QZ0") carries no type at all. + type_byte = strt_raw[18] + ev.record_type = _RECORD_TYPE_BY_STRT.get(type_byte) + if ev.record_type is None: + ev.decode_problems.append(f"unknown record type byte 0x{type_byte:02x} at STRT[18]") + named_type = derive_record_type_from_filename(name, default=None) if name else None + if named_type is not None and ev.record_type is not None and named_type != ev.record_type: + log.warning("%s: record type %s from the file disagrees with the filename's %s; " + "the file's is kept", label, ev.record_type, named_type) # Setup fields come from the recording-setup block (#44/#45), not the STRT # record: STRT[18] is the record-type marker (0x46 / 0x0A), which is why diff --git a/tests/test_bw_file_fields.py b/tests/test_bw_file_fields.py index 2bed577..f09644d 100644 --- a/tests/test_bw_file_fields.py +++ b/tests/test_bw_file_fields.py @@ -111,3 +111,38 @@ def test_import_without_a_report_stores_the_file_fields(tmp_path: Path): assert sc["event"]["pretrig_samples"] == 253 assert sc["project_info"]["project"] == "JV 241 - 1425 Spanlers Mill Rad" assert sc["project_info"]["sensor_location"] == "Loc #1 - 1425 Spanlers Mill Rd" + + +# ── #41: the record type comes from the bytes ─────────────────────────────── +# STRT[18] is the record-type byte: 0x46 on every one of DL2's 6,577 waveform +# files, 0x0A on every one of its 63,535 histograms (measured 2026-10-29). + +from minimateplus import blastware_file as _bw # noqa: E402 +from minimateplus.event_file_io import decode_blastware_bytes # noqa: E402 + +_TYPES = [ + ("bw-import-fields-10-29-26/R352HIUI.F40W", "Waveform"), + ("bw-import-fields-10-29-26/M529L4XQ.R30H", "Histogram"), + ("5-11-26/M529LL1A.SP0", "Waveform"), # name carries no type + ("histogram-mic-off-10-26-26/K441LTOK.QZ0", "Histogram"), # name carries no type +] + + +@pytest.mark.parametrize("rel,want", _TYPES) +def test_record_type_comes_from_the_bytes_without_a_name(rel, want): + assert decode_blastware_bytes((_FIX / rel).read_bytes(), name=None).record_type == want + + +def test_the_bytes_win_over_a_filename_that_disagrees(caplog): + raw = (_FIX / "bw-import-fields-10-29-26/M529L4XQ.R30H").read_bytes() + ev = decode_blastware_bytes(raw, name="M529L4XQ.R30W") + assert ev.record_type == "Histogram" + assert "record type" in caplog.text + + +def test_an_unknown_type_byte_is_unknown_not_a_guess(): + raw = bytearray((_FIX / "bw-import-fields-10-29-26/R352HIUI.F40W").read_bytes()) + raw[_bw._WAVEFORM_HEADER_SIZE + 18] = 0x99 + ev = decode_blastware_bytes(bytes(raw), name=None) + assert ev.record_type is None + assert any("record type" in p for p in ev.decode_problems) diff --git a/tests/test_decode_check.py b/tests/test_decode_check.py index 1654819..efdf68c 100644 --- a/tests/test_decode_check.py +++ b/tests/test_decode_check.py @@ -31,11 +31,10 @@ def test_histogram_interval_peak_in_counts(): assert s.counts["Tran"] == [7776] and s.values["NumberofIntervals"] == 1 -def test_no_filename_reaches_the_decoder(): - # The type must come from the bytes; today they can't tell it (#41), so this - # histogram reads as a waveform. Update to "Full Histogram" when #41 lands. +def test_event_type_comes_from_the_bytes(): + # No filename reaches the decoder; the type is the STRT record-type byte (#41). s = sfm_side((FIX / "M529L4XQ.R30H").read_bytes()) - assert s.values["EventType"] == "Full Waveform" + assert s.values["EventType"] == "Full Histogram" def test_report_fields_stay_blank():