diff --git a/CHANGELOG.md b/CHANGELOG.md index 611407a..5a52ebe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,24 @@ All notable changes to seismo-relay are documented here. ## Unreleased ### Fixed +- **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 and `0x0A` on every one of its 63,535 histograms. + - The decoder used to read the type from the filename's last letter. So an + event with no Blastware name came out "Waveform" whatever it was: SFM's + own downloads, the call-in path, and a manual download (`.QZ0`). + - The filename is now only a cross-check that logs a disagreement. + - An unknown byte gives `record_type` None and a `decode_problems` entry, + not a guess. Manual, Event and Combo types never occur in DL2, so their + bytes are unknown. + - Decode checker: `EventType` matches on 70,042 / 70,042 events (was 6,524), + 0 regressions. + - ⚠ **Codec change, covered by the same release bump as #44/#45.** The + backfill rewrites stored `record_type`. Files named `…W` / `…H` keep their + type. A histogram stored from a 3-character manual-download name flips + from Waveform to Histogram, which also changes how it pairs with its + waveform twin. No schema change. - **A Blastware event imported without its ASCII report now carries its setup and notes (#44, #45).** Until now such an event stored no sample rate, a record time of "70 s" (the STRT record-type byte, 0x46), a 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():