""" 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, )