feat(decode): sensor check results from the unit's own records (group E)

The record chain that starts with the embedded event record runs to the
file's end on all 70,041 DL2 files: the event record 0x05, the unit's
summary 0x32-0x35, its sensor checks 0x3c-0x3f, then the footer 0x0e
(no 0x35 / 0x3f on a unit without a mic, 8 files).  _record_chain walks
it and refuses a chain that does not end exactly at the file's end; the
unit's summary is now read from it too.

Each sensor-check record's header holds the results (DL2, 2026-11-06):
[2] channel type (1 geophone, 2 mic, 0 disabled), [4] status (0 =
passed), [5:7] frequency x 10, [7:9] ratio x 10, [9:11] amplitude mV.
Blastware prints them as they are: 209,943 / 209,943 geophone
frequencies and ratios, 69,975 / 69,975 mic frequencies and amplitudes;
"Passed" on status 0, "Check" on every other (its bits not decoded:
a Check can sit inside every passing range), "Disabled" on type 0.

Event.sensor_check_results: channel -> SensorCheckResult (result,
status, freq_hz, ratio, amplitude_mv).  The checker compares them.
Behaviour change: an unknown id inside an intact chain now leaves only
that record unread (was: everything after it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-11-06 12:58:31 -05:00
1 parent d2fe675650
commit f7b310b130
7 files changed
+4158 -30

No files matched your search

+7
View File
@@ -157,6 +157,13 @@ def sfm_side(raw: bytes) -> SfmSide:
("LongZCFreq", pv.long_zc_freq), ("MicLZCFreq", pv.mic_zc_freq)):
if hz is not None:
values[tag] = "<1.0" if hz < 1.0 else hz
# The unit's sensor check (2026-11-06), every record type.
for ch, r in (ev.sensor_check_results or {}).items():
values[f"{ch}TestResults"] = r.result
for tag, value in (("TestFreq", r.freq_hz), ("TestRatio", r.ratio),
("TestAmplitude", r.amplitude_mv)):
if value is not None:
values[f"{ch}{tag}"] = value
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
+77 -26
View File
@@ -26,7 +26,7 @@ import struct
from pathlib import Path
from typing import Optional, Union
from .models import DeviceInfo, Event, PeakValues, ProjectInfo, Timestamp
from .models import DeviceInfo, Event, PeakValues, ProjectInfo, SensorCheckResult, Timestamp
from . import blastware_file as _bw # avoid circular reference at module load
from .bw_ascii_report import BwAsciiReport
from . import waveform_codec as _wc
@@ -1069,12 +1069,34 @@ def _histogram_event_record(
return None, None, (f"{rejected} candidate(s), none valid" if rejected else "no candidate"), None
# The unit's own event summary (measured on DL2, 2026-11-05). The event record
# is the first of a chain of [length uint16 BE][id][payload] records, so the
# next record starts `length` + 2 bytes after its lead. Its payload holds the
# vector-sum peak's sample at [22:26] past the marker. The four that follow,
# 22 bytes each, are one per channel (ids 0x32 Tran .. 0x35 MicL; a unit
# without a mic has no 0x35):
# The event record is the first of a chain of [length uint16 BE][id][payload]
# records, the next starting `length` + 2 bytes after a record's lead (measured
# on all 70,041 DL2 files, 2026-11-06): the event record 0x05, the unit's
# summary 0x32-0x35, its sensor checks 0x3c-0x3f, then the footer 0x0e -- the
# file's last 26 bytes. A unit without a mic has no 0x35 or 0x3f (8 files).
# The walk ends exactly at the file's end on every one, so a chain that does
# not is not read at all.
_RECORD_LEAD_TO_MARKER = 6 # the lead `00 28 05 00` and the source sit before the marker
def _record_chain(raw: bytes, marker_at: int) -> Optional[dict[int, bytes]]:
"""Each record's payload by id (the first of each), for the chain that
starts with the validated event record whose marker is at `marker_at`; None
unless the chain ends exactly at the end of the file."""
off = marker_at - _RECORD_LEAD_TO_MARKER
out: dict[int, bytes] = {}
while off < len(raw):
ln = int.from_bytes(raw[off:off + 2], "big")
if off + 3 > len(raw) or ln < 1 or off + 2 + ln > len(raw):
return None
out.setdefault(raw[off + 2], raw[off + 3:off + 2 + ln])
off += 2 + ln
return out
# The unit's own event summary (measured on DL2, 2026-11-05). The event
# record's payload holds the vector-sum peak's sample at [25:29] (its marker
# + 22). Then one record per channel, 22 bytes (ids 0x32 Tran .. 0x35 MicL):
# [0] 0 [1:3] min [3:5] max (int16 BE; geophones in units of 16 counts)
# [5:9] peak sample [9:11] half-period, samples [11:15] displacement
# integral, units x samples [15:21] probably the unit's own largest sample step
@@ -1082,26 +1104,22 @@ def _histogram_event_record(
# computation from the samples (_waveform_peak_detail)
# min, max and peak sample equal the samples' own on 25,962 / 25,966 DL2
# channels (the 4 others: O117I1NK.R70W, whose samples wrap at the trigger).
_EVENT_RECORD_ID = 0x05
_SUMMARY_PVS_SAMPLE = slice(25, 29)
_SUMMARY_CHANNELS = {0x32: "Tran", 0x33: "Vert", 0x34: "Long", 0x35: "MicL"}
_SUMMARY_CHANNEL_LEN = 22
_RECORD_LEAD_TO_MARKER = 6 # the lead `00 28 05 00` and the source sit before the marker
_SUMMARY_PVS_SAMPLE_AT = 22 # past the marker
_SUMMARY_CHANNEL_LEN = 21 # the payload; the record is 22 with its id
def _unit_summary(raw: bytes, marker_at: int) -> Optional[dict]:
"""The unit's per-channel event summary after the validated event record
whose marker is at `marker_at`: {"pvs_sample": int, "Tran": {...}, ...},
each channel {"min", "max", "peak_sample", "half_period",
"disp_integral"}. None when no channel record follows the event record."""
lead = marker_at - _RECORD_LEAD_TO_MARKER
off = lead + 2 + int.from_bytes(raw[lead:lead + 2], "big")
out: dict = {"pvs_sample": int.from_bytes(
raw[marker_at + _SUMMARY_PVS_SAMPLE_AT:marker_at + _SUMMARY_PVS_SAMPLE_AT + 4], "big")}
while off + 2 + _SUMMARY_CHANNEL_LEN <= len(raw):
ch = _SUMMARY_CHANNELS.get(raw[off + 2])
if int.from_bytes(raw[off:off + 2], "big") != _SUMMARY_CHANNEL_LEN or ch is None or ch in out:
break
p = raw[off + 3:off + 2 + _SUMMARY_CHANNEL_LEN]
def _unit_summary(chain: dict[int, bytes]) -> Optional[dict]:
"""The unit's per-channel event summary from a record chain:
{"pvs_sample": int, "Tran": {...}, ...}, each channel {"min", "max",
"peak_sample", "half_period", "disp_integral"}. None when the chain has no
channel record."""
out: dict = {"pvs_sample": int.from_bytes(chain.get(_EVENT_RECORD_ID, b"")[_SUMMARY_PVS_SAMPLE], "big")}
for rid, ch in _SUMMARY_CHANNELS.items():
p = chain.get(rid)
if p is None or len(p) != _SUMMARY_CHANNEL_LEN:
continue
out[ch] = {
"min": struct.unpack(">h", p[1:3])[0],
"max": struct.unpack(">h", p[3:5])[0],
@@ -1109,10 +1127,41 @@ def _unit_summary(raw: bytes, marker_at: int) -> Optional[dict]:
"half_period": int.from_bytes(p[9:11], "big"),
"disp_integral": int.from_bytes(p[11:15], "big"),
}
off += 2 + _SUMMARY_CHANNEL_LEN
return out if len(out) > 1 else None
# The sensor check's results, in the header of each sensor-check record (ids
# 0x3c Tran .. 0x3f MicL, whose traces minimateplus.sensor_check decodes).
# Payload: [2] channel type (1 geophone, 2 mic, 0 the check disabled) [4] status,
# 0 = passed [5:7] frequency x 10 [7:9] ratio x 10 [9:11] amplitude, mV.
# Blastware prints them as they are (DL2, 2026-11-06): 209,943 / 209,943
# geophone frequencies and ratios, 69,975 / 69,975 mic frequencies and
# amplitudes; "Passed" on status 0 and "Check" on every other (1 .. 15: its bits
# are not decoded), "Disabled", with nothing else, on type 0.
_SENSOR_CHECK_CHANNELS = {0x3c: "Tran", 0x3d: "Vert", 0x3e: "Long", 0x3f: "MicL"}
_SENSOR_CHECK_HEADER_LEN = 11
def _sensor_check_results(chain: dict[int, bytes]) -> Optional[dict[str, SensorCheckResult]]:
out: dict[str, SensorCheckResult] = {}
for rid, ch in _SENSOR_CHECK_CHANNELS.items():
p = chain.get(rid)
if p is None or len(p) < _SENSOR_CHECK_HEADER_LEN:
continue
kind, status = p[2], p[4]
if kind == 0:
out[ch] = SensorCheckResult("Disabled", status)
continue
r = SensorCheckResult("Passed" if status == 0 else "Check", status,
freq_hz=int.from_bytes(p[5:7], "big") / 10)
if ch == "MicL":
r.amplitude_mv = int.from_bytes(p[9:11], "big")
else:
r.ratio = int.from_bytes(p[7:9], "big") / 10
out[ch] = r
return out or None
def _parse_record_time_seconds(raw: bytes) -> Optional[float]:
"""The configured post-trigger record time in seconds, from the recording-
setup config block, or None when absent / implausible."""
@@ -1612,7 +1661,9 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
if (is_waveform_body and ev.peak_values is not None and ev.sample_rate
and ev.pretrig_samples is not None):
_waveform_peak_detail(ev.peak_values, samples, geo_fs, ev.sample_rate, ev.pretrig_samples)
_summary = _unit_summary(raw, _record_at) if _record_at is not None else None
_chain = _record_chain(raw, _record_at) if _record_at is not None else None
_summary = _unit_summary(_chain) if _chain is not None else None
ev.sensor_check_results = _sensor_check_results(_chain) if _chain is not None else None
if _summary is not None and ev.peak_values is not None and ev.sample_rate:
_apply_unit_summary(ev.peak_values, _summary, geo_fs, ev.sample_rate,
ev.pretrig_samples if is_waveform_body else None)
+14
View File
@@ -383,6 +383,17 @@ class PeakValues:
long_peak_disp_in: Optional[float] = None
@dataclass
class SensorCheckResult:
"""One channel's sensor check, as the unit stores it in the event file
(2026-11-06). `result` is Blastware's word for it."""
result: str # "Passed", "Check" or "Disabled"
status: int # the unit's code: 0 passed; its bits are not decoded
freq_hz: Optional[float] = None # geophone resonance / mic test frequency; None when Disabled
ratio: Optional[float] = None # geophones only
amplitude_mv: Optional[int] = None # the mic only
# ── Project / operator metadata ───────────────────────────────────────────────
@dataclass
@@ -592,6 +603,9 @@ class Event:
# Raw ADC counts; the source series' scale differs but the trace is a
# shape diagnostic (rendered fit-to-box). None when absent.
sensor_check: Optional[dict] = None # {"Tran": [...], ..., "MicL": [...]}
# And its results per channel, from the same records (2026-11-06): None
# when the file's record chain is not whole.
sensor_check_results: Optional[dict] = None # channel -> SensorCheckResult
# Where `timestamp` came from, when decoded from a Blastware binary
# (event_file_io): "event_record" -- the instrument's own event record in
Binary file not shown.
File diff suppressed because it is too large. Load diff
+59 -3
View File
@@ -595,10 +595,66 @@ def test_time_of_peak_is_the_units_own_peak_sample():
assert pv.pvs_time == 0.0
def test_without_the_units_summary_nothing_is_guessed():
def test_an_unreadable_channel_record_leaves_only_that_channel_unread():
raw = bytearray((_FIX / f"{_US}/I145L6UJ.3Y0W").read_bytes())
i = raw.find(b"\x00\x16\x32")
raw[i + 2] = 0x00 # break the Tran record's id
raw[i + 2] = 0x00 # break the Tran record's id; the chain holds
pv = decode_blastware_bytes(bytes(raw), name=None).peak_values
assert (pv.tran_zc_freq, pv.mic_zc_freq, pv.tran_peak_disp_in) == (None, None, None)
assert (pv.tran_zc_freq, pv.tran_peak_disp_in) == (None, None)
assert abs(pv.tran_time_of_peak - 2.281) <= 0.0005 # the samples' peak, as before
assert _prints_as(pv.mic_zc_freq, "43") # the others are read
def test_without_a_whole_record_chain_nothing_is_guessed():
# The chain from the event record ends exactly at the file's end on all
# 70,041 DL2 files; one that does not is not read at all.
raw = bytearray((_FIX / f"{_US}/I145L6UJ.3Y0W").read_bytes())
i = raw.find(b"\x00\xd4\x3c") # the Tran sensor-check record, length 212
raw[i + 1] = 0xd3 # one byte short
ev = decode_blastware_bytes(bytes(raw), name=None)
assert ev.sensor_check_results is None
pv = ev.peak_values
assert (pv.tran_zc_freq, pv.mic_zc_freq, pv.tran_peak_disp_in) == (None, None, None)
# ── Group E: the sensor check results ───────────────────────────────────────
# Records 0x3c Tran .. 0x3f MicL, after the unit's summary, carry the sensor
# check the unit ran: its trace (minimateplus.sensor_check) and, in the record's
# header, its results (measured on DL2, 2026-11-06):
# [2] channel type (1 geophone, 2 mic, 0 check disabled) [4] status, 0 = passed
# [5:7] frequency x 10 [7:9] ratio x 10 [9:11] amplitude, mV
# Blastware prints them as they are: 209,943 / 209,943 geophone frequencies and
# ratios, 69,975 / 69,975 mic frequencies and amplitudes; "Passed" on status 0,
# "Check" on every other status, "Disabled" on type 0.
@pytest.mark.parametrize("rel,want", [
# channel: (result, frequency Hz, ratio, amplitude mV), Blastware's _XML.XML
(f"{_US}/I145L6UJ.3Y0W", {"Tran": ("Passed", 7.3, 3.7, None), "Vert": ("Passed", 7.2, 3.9, None),
"Long": ("Passed", 7.2, 3.9, None), "MicL": ("Passed", 19.7, None, 592)}),
(f"{_US}/K440HJCN.AS0W", {"Tran": ("Check", 9.6, 13.8, None), "Vert": ("Check", 8.5, 7.2, None),
"Long": ("Passed", 7.3, 1.9, None), "MicL": ("Passed", 19.7, None, 519)}),
(f"{_US}/R349KMF7.9O0W", {"Tran": ("Passed", 7.4, 3.8, None), "Vert": ("Passed", 7.5, 3.5, None),
"Long": ("Passed", 7.8, 3.6, None), "MicL": ("Check", 0.0, None, 0)}),
(f"{_US}/R352HFZ7.5D0W", {"Tran": ("Disabled", None, None, None), # no mic; check disabled
"Vert": ("Disabled", None, None, None),
"Long": ("Disabled", None, None, None)}),
(f"{_US}/T193HLJ3.X80H", {"Tran": ("Passed", 7.4, 3.9, None), "Vert": ("Passed", 7.2, 3.9, None),
"Long": ("Passed", 7.5, 4.1, None), "MicL": ("Passed", 20.1, None, 556)}),
])
def test_sensor_check_results_are_the_units_own(rel, want):
got = read_blastware_file(_FIX / rel).sensor_check_results
assert set(got) == set(want)
for ch, (result, freq, ratio, amp) in want.items():
r = got[ch]
assert r.result == result, ch
assert r.freq_hz == (pytest.approx(freq) if freq is not None else None), ch
assert r.ratio == (pytest.approx(ratio) if ratio is not None else None), ch
assert r.amplitude_mv == amp, ch
def test_a_check_keeps_the_units_status_code():
# Blastware prints only "Check"; the code's bits are not decoded (K559L6N1's
# Tran is a Check at 8.1 Hz and 3.3, inside every passing range).
got = read_blastware_file(_FIX / f"{_US}/K440HJCN.AS0W").sensor_check_results
assert [got[ch].status for ch in ("Tran", "Vert", "Long", "MicL")] == [11, 2, 0, 0]
+24 -1
View File
@@ -43,7 +43,7 @@ def test_report_fields_stay_blank():
# since 2026-11-04, ZC frequency and displacement since 2026-11-05:
# test_the_setup_fields_match_blastware, test_waveform_peak_detail_*,
# test_zc_frequency_and_displacement_match_blastware. Its Tran ZC is N/A.)
for tag in ("TranZCFreq", "TranTestResults"):
for tag in ("TranZCFreq",): # (sensor check results since 2026-11-06)
assert s.values.get(tag) is None
@@ -393,3 +393,26 @@ def test_a_clipped_events_peak_times_match_blastware():
for f in ("TranTimeofPeak", "VertTimeofPeak", "LongTimeofPeak", "MicLTimeofPeak",
"PeakVectorSumTimeSum"):
assert res[f].verdict == "match", (f, res[f])
# ── Group E: the sensor check results (2026-11-06) ──────────────────────────
_SENSOR_CHECK = [f"{ch}Test{f}" for ch in ("Tran", "Vert", "Long", "MicL")
for f in ("Results", "Freq", "Ratio", "Amplitude")]
@pytest.mark.parametrize("path", [
FIX / "R352HIUI.F40W", FIX / "T1905KDW.5F0W", FIX / "T004JTRF.ME0H", FIX / "M529L4XQ.R30H",
_US / "K440HJCN.AS0W", # two Checks
_US / "K558IKSJ.P20W", _US / "O117I1NK.R70W",
])
def test_sensor_check_results_match_blastware(path):
ref = parse_xml_report_file(path.with_name(path.name.replace(".", "_", 1) + "_XML.XML"))
res = {r.field: r for r in compare(sfm_side(path.read_bytes()), ref, CARVEOUTS)}
checked = 0
for f in _SENSOR_CHECK:
if ref.scalars.get(f) is None:
continue
assert res[f].verdict == "match", (f, res[f])
checked += 1
assert checked >= 4