fix(decode): a Blastware file's geophone range comes from its setup -- Sensitive events no longer read 8x

In the setup's channel blocks, the byte 29 after each geophone label is
the range: 0x00 Normal (10 in/s), 0x01 Sensitive (1.25 in/s).  It matches
Blastware's GeoRange on 70,042 / 70,042 DL2 events, all three channels
agreeing on every one (35 Sensitive).  SFM's live 1A has the same byte
at label+33.  SFM assumed Normal, so every Sensitive event's peaks,
vector sum and .h5 samples read 8x high.

- Event.geo_range (0 / 1, None when not read -- channels disagreeing,
  an unknown value or a missing block -- with a decode_problems entry).
- decode_blastware_bytes scales peaks and the histogram vector sum by it.
- event_hdf5.write_event_hdf5: geo_range defaults to None = the event's
  own, else Normal; an explicit argument still wins.
- waveform_store.save_imported_bw and scripts/backfill_sidecars.py stop
  forcing 'normal', so imports and the backfill store the file's range.
  The waveform_store line was agreed with the parity session, which owns
  that file; its own branch keeps a geo_range parameter, defaulting to
  None at its next dev merge.
- The checker's SFM side converts at the event's range and reports
  GeoRange; tests that pinned the old 8x gap now pin the fix, and the
  saturation carve-out is still tested against an 8x value.

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-01 16:26:15 -05:00
1 parent 6c7c6b0a15
commit 4844135f01
13 files changed
+3549 -32

No files matched your search

+70
View File
@@ -206,3 +206,73 @@ def test_mic_peak_in_psi():
# Blastware: MicL PSPL 106.0 dB(L) = 20 uPa x 10^(106/20) = 3.990 Pa = 5.787e-4 psi.
ev = read_blastware_file(_FIX / "decode-checker-10-29-26/M529L4XQ.R30H")
assert ev.peak_values.micl == pytest.approx(5.787e-4, rel=0.01)
# ── The geophone range: Normal or Sensitive ─────────────────────────────────
# In the setup's channel blocks, the byte 29 after each geophone label is the
# range: 0x00 Normal (10 in/s full scale), 0x01 Sensitive (1.25 in/s). It
# matches Blastware's GeoRange on 70,042 / 70,042 DL2 events, all three
# channels agreeing on every one (35 Sensitive), measured 2026-11-01. Until
# now SFM assumed Normal, so a Sensitive event read 8x high.
from minimateplus.event_file_io import _setup_name_at # noqa: E402
@pytest.mark.parametrize("rel,want", [
("bw-import-fields-10-29-26/R352HIUI.F40W", 0),
("geo-range-11-01-26/K557J1KB.RQ0W", 1), # waveform, XML GeoRange 1.250
("geo-range-11-01-26/M010HR6K.OA0H", 1), # histogram, XML GeoRange 1.250
])
def test_geo_range_comes_from_the_file(rel, want):
assert read_blastware_file(_FIX / rel).geo_range == want
# (file, Blastware's Tran / Vert / Long PPV and Peak Vector Sum, from its XML)
@pytest.mark.parametrize("rel,peaks", [
("geo-range-11-01-26/K557J1KB.RQ0W", ("0.019", "0.055", "0.019", "0.061")),
("geo-range-11-01-26/M010HR6K.OA0H", ("0.008", "0.009", "0.005", "0.009")),
])
def test_sensitive_range_peaks_are_not_8x(rel, peaks):
pv = read_blastware_file(_FIX / rel).peak_values
for got, want in zip((pv.tran, pv.vert, pv.long, pv.peak_vector_sum), peaks):
assert abs(got - float(want)) <= 0.0005 + 1e-9, (got, want)
def test_channels_that_disagree_give_no_range_and_a_problem():
raw = bytearray((_FIX / "geo-range-11-01-26/K557J1KB.RQ0W").read_bytes())
i = raw.find(b"\x00\x00\x01Vert", _setup_name_at(bytes(raw)))
raw[i + 3 + 29] = 0x00 # Vert Normal, Tran / Long Sensitive
ev = decode_blastware_bytes(bytes(raw), name=None)
assert ev.geo_range is None
assert any("range" in p for p in ev.decode_problems)
def test_the_h5_takes_the_events_range(tmp_path):
import h5py
from sfm import event_hdf5
ev = read_blastware_file(_FIX / "geo-range-11-01-26/K557J1KB.RQ0W")
event_hdf5.write_event_hdf5(tmp_path / "e.h5", ev, serial="BE9557")
with h5py.File(tmp_path / "e.h5", "r") as f:
assert f.attrs["geo_range"] == "sensitive"
assert abs(float(abs(f["samples/Vert"][:]).max()) - 0.055) <= 0.0005 + 1e-9
def test_an_imported_file_and_the_backfill_store_its_range(tmp_path):
import h5py
import scripts.backfill_sidecars as backfill
from sfm import event_hdf5
from sfm.waveform_store import WaveformStore
src = _FIX / "geo-range-11-01-26/K557J1KB.RQ0W"
store = WaveformStore(tmp_path / "waveforms")
ev, _ = store.save_imported_bw(src.read_bytes(), source_path=src)
h5 = store.hdf5_path_for("BE9557", src.name)
with h5py.File(h5, "r") as f:
assert f.attrs["geo_range"] == "sensitive"
# A file stored before this fix carries "normal"; the backfill rewrites it.
event_hdf5.write_event_hdf5(h5, ev, serial="BE9557", geo_range="normal")
assert backfill.main(["--store-root", str(tmp_path / "waveforms"),
"--db-path", str(tmp_path / "seismo_relay.db"), "--force"]) == 0
with h5py.File(h5, "r") as f:
assert f.attrs["geo_range"] == "sensitive"