diff --git a/minimateplus/decode_check.py b/minimateplus/decode_check.py new file mode 100644 index 0000000..3e1654d --- /dev/null +++ b/minimateplus/decode_check.py @@ -0,0 +1,79 @@ +""" +minimateplus/decode_check.py — SFM's file-only decode, set beside Blastware's +report of the same event, field by field. + +The decode checker's core (`docs/superpowers/specs/2026-10-29-s3-decode-checker-design.md`). +SFM's side comes from the event bytes alone, through +`decode_blastware_bytes(raw, name=None)`: no filename, because SFM's own +download and call-in paths have none, and no report overlay. Nothing from the +reference reaches it. SFM's own assumptions stay in -- geophone counts become +in/s at the Normal range -- because the point is to judge what SFM produces. + +Pure: no I/O, and no import from `sfm/`. +""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import Optional + +from .event_file_io import _GEO_INT16_FS, _GEO_NORMAL_FS_INS, decode_blastware_bytes +from .models import Event +from .waveform_codec import mic_count_to_db + +# SFM's conversion: it cannot yet read the geophone range from the file, and +# assumes Normal (10 in/s full scale = 32000 counts). +GEO_IPS_PER_COUNT: float = _GEO_NORMAL_FS_INS / _GEO_INT16_FS + +_CHANNELS = ("Tran", "Vert", "Long", "MicL") +_EVENT_TYPE = {"Waveform": "Full Waveform", "Histogram": "Full Histogram"} + + +@dataclass +class SfmSide: + event: Event + values: dict[str, object] # reference tag -> SFM's value; absent or None = blank + notes: list[str] # non-empty user-note slots, in order + job_number: Optional[str] # None until the decoder reads it + channels: list[str] # channels with decoded data, in Tran/Vert/Long/MicL order + counts: dict[str, list[int]] # waveform samples or histogram interval peaks + + +def sfm_side(raw: bytes) -> SfmSide: + """What SFM makes of an event from its bytes alone.""" + ev = decode_blastware_bytes(raw, name=None) + counts = {ch: list((ev.raw_samples or {}).get(ch) or []) for ch in _CHANNELS} + pv, pi, ts = ev.peak_values, ev.project_info, ev.timestamp + + values: dict[str, object] = { + "EventType": _EVENT_TYPE.get(ev.record_type, ev.record_type), + "SampleRate": ev.sample_rate, + "RecordTime": ev.rectime_seconds, + "Pre-triggerLength": (-ev.pretrig_samples / ev.sample_rate + if ev.pretrig_samples is not None and ev.sample_rate else None), + # SFM's interval count is its decoded entry count; the reference + # carries this field for histograms only. + "NumberofIntervals": len(counts["Tran"]) or None, + } + if ts is not None: + date = f"{ts.year:04d}-{ts.month:02d}-{ts.day:02d}" + time = f"{ts.hour:02d}:{ts.minute:02d}:{ts.second:02d}" + # The event's time, under both of Blastware's names for it, so a time + # is judged as a time even while the type is wrong (#41). + values.update(EventDate=date, EventTime=time, + HistogramStartDate=date, HistogramStartTime=time) + if pv is not None: + values.update(TranPPV=pv.tran, VertPPV=pv.vert, LongPPV=pv.long, + PeakVectorSum=pv.peak_vector_sum) + if counts["MicL"]: + values["MicLPSPL dB(L)"] = mic_count_to_db(max(abs(c) for c in counts["MicL"])) + values["MicLPSPL psi(L)"] = pv.micl if pv is not None else None + + return SfmSide( + event=ev, + values=values, + notes=[v for v in (pi.project, pi.client, pi.operator, pi.sensor_location) if v] + if pi is not None else [], + job_number=None, + channels=[ch for ch in _CHANNELS if counts[ch]], + counts=counts, + ) diff --git a/tests/test_decode_check.py b/tests/test_decode_check.py new file mode 100644 index 0000000..dadcee9 --- /dev/null +++ b/tests/test_decode_check.py @@ -0,0 +1,44 @@ +""" +The decode checker's SFM side and comparison (decode checker, Tasks 2-4). + +Expected values come from the fixtures' Blastware XML reports. SFM's side is +built from the event bytes alone -- no filename, no report. +""" +from __future__ import annotations + +from pathlib import Path + +import pytest + +from minimateplus.decode_check import sfm_side + +FIX = Path(__file__).parent / "fixtures/decode-checker-10-29-26" + + +def test_waveform_side_from_bytes(): + s = sfm_side((FIX / "R352HIUI.F40W").read_bytes()) + assert s.values["SampleRate"] == 1024 and s.values["RecordTime"] == pytest.approx(1.0) + assert s.values["Pre-triggerLength"] == pytest.approx(-253 / 1024) + assert (s.values["EventDate"], s.values["EventTime"]) == ("2018-07-30", "16:13:52") + assert s.notes == ["JV 241 - 1425 Spanlers Mill Rad", "Walsh Granite JV", + "Terra-Mechanics Inc- D. Harrison", "Loc #1 - 1425 Spanlers Mill Rd"] + assert len(s.counts["Tran"]) == 1277 and s.job_number is None + + +def test_histogram_interval_peak_in_counts(): + s = sfm_side((FIX / "M529L4XQ.R30H").read_bytes()) + # Blastware: Tran interval peak 2.430 in/s = 7776 counts at 10/32000 in/s per count + 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. + s = sfm_side((FIX / "M529L4XQ.R30H").read_bytes()) + assert s.values["EventType"] == "Full Waveform" + + +def test_report_fields_stay_blank(): + s = sfm_side((FIX / "T1905KDW.5F0W").read_bytes()) + for tag in ("TranZCFreq", "TranPeakAcceleration", "BatteryLevel", "Calibration", "TranTestResults"): + assert s.values.get(tag) is None