merge: dev into feat/series4-sfm-parity -- the geophone range from the file, and v0.33.0

Brings 057df47 (fix 4844135, changelog be58b4f) from the decode checker's branch
-- Event.geo_range from the file's setup channel block (label+29: 0 Normal,
1 Sensitive, None not read), peaks and histogram PVS scaled by it,
write_event_hdf5(geo_range=None) using it -- and dev's v0.33.0 release commit
(6c7c6b0).

Conflicts, resolved as agreed with the decode session:
- sfm/waveform_store.py save_imported_bw: dev dropped the forced
  geo_range="normal"; this branch keeps its geo_range parameter, now
  Optional, default None -- an import uses the file's own range, an explicit
  value (a live download's) still wins.
- CLAUDE.md: dev's newer 'Where things stand (updated 2026-11-01)' date.

Measured on the merged tree: K557J1KB.RQ0W (Sensitive waveform) decodes
0.019 / 0.055 / 0.019, PVS 0.061 -- Blastware's report -- and its imported
.h5 peaks 0.0194 / 0.055 / 0.0194 (forcing 'normal' gives 8x: 0.155 / 0.44).
M010HR6K.OA0H (Sensitive histogram) likewise.  The live path still passes the
unit's 1A range or 'normal' when unread -- unchanged by this merge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-11-02 12:06:35 -05:00
commit d0c14aaec8
17 files changed
+3724 -62

No files matched your search

+14 -9
View File
@@ -18,12 +18,13 @@ from dataclasses import dataclass
from typing import Callable, Optional, Sequence
from .bw_xml_report import XmlReport
from .event_file_io import _GEO_INT16_FS, _GEO_NORMAL_FS_INS, _MIC_PSI_PER_COUNT, decode_blastware_bytes
from .event_file_io import (_GEO_FS_BY_RANGE, _GEO_INT16_FS, _GEO_NORMAL_FS_INS, _MIC_PSI_PER_COUNT,
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).
# SFM's conversion at the Normal range (10 in/s full scale = 32000 counts),
# which SFM assumes when it cannot read the event's range from the file.
GEO_IPS_PER_COUNT: float = _GEO_NORMAL_FS_INS / _GEO_INT16_FS
_CHANNELS = ("Tran", "Vert", "Long", "MicL")
@@ -38,6 +39,7 @@ class SfmSide:
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
geo_ips_per_count: float = GEO_IPS_PER_COUNT # at the event's range, as SFM read it
def sfm_side(raw: bytes) -> SfmSide:
@@ -55,6 +57,7 @@ def sfm_side(raw: bytes) -> SfmSide:
# SFM's interval count is its decoded entry count; the reference
# carries this field for histograms only.
"NumberofIntervals": len(counts["Tran"]) or None,
"GeoRange": _GEO_FS_BY_RANGE.get(ev.geo_range),
}
if ts is not None:
date = f"{ts.year:04d}-{ts.month:02d}-{ts.day:02d}"
@@ -78,6 +81,7 @@ def sfm_side(raw: bytes) -> SfmSide:
job_number=None,
channels=[ch for ch in _CHANNELS if counts[ch]],
counts=counts,
geo_ips_per_count=_GEO_FS_BY_RANGE.get(ev.geo_range, _GEO_NORMAL_FS_INS) / _GEO_INT16_FS,
)
@@ -184,19 +188,20 @@ def _series_result(field: str, n_bw: int, n_sfm: int, diffs: list[tuple[int, flo
)
def _sample_converters(ref: XmlReport) -> dict[str, Callable[[int], float]]:
convert: dict[str, Callable[[int], float]] = {ch: (lambda c: c * GEO_IPS_PER_COUNT) for ch in _GEO}
def _sample_converters(sfm: SfmSide, ref: XmlReport) -> dict[str, Callable[[int], float]]:
k = sfm.geo_ips_per_count
convert: dict[str, Callable[[int], float]] = {ch: (lambda c: c * k) for ch in _GEO}
mic = _mic_converter(ref)
if mic is not None:
convert["MicL"] = mic
return convert
def _interval_converter(title: str) -> Optional[Callable[[int], float]]:
def _interval_converter(title: str, sfm: SfmSide) -> Optional[Callable[[int], float]]:
"""SFM keeps each interval's peaks; it drops their frequencies and geo PVS."""
ch, _, rest = title.partition(" ")
if rest.startswith("Peak") and ch in _GEO:
return lambda c: c * GEO_IPS_PER_COUNT
return lambda c: c * sfm.geo_ips_per_count
if ch == "MicL" and rest == "Peak dB(L)":
return mic_count_to_db
if ch == "MicL" and rest == "Peak psi(L)":
@@ -215,7 +220,7 @@ def series_pairs(sfm: SfmSide, ref: XmlReport,
a row whose cell count differs is a disagreement for every channel."""
if field.startswith("samples."):
ch = field[len("samples."):]
convert = _sample_converters(ref).get(ch)
convert = _sample_converters(sfm, ref).get(ch)
counts = sfm.counts.get(ch, [])
if convert is None or not counts:
return None
@@ -232,7 +237,7 @@ def series_pairs(sfm: SfmSide, ref: XmlReport,
if field.startswith("intervals."):
title = field[len("intervals."):]
columns = ref.interval_columns or []
convert = _interval_converter(title)
convert = _interval_converter(title, sfm)
counts = sfm.counts.get(title.partition(" ")[0], [])
if convert is None or not counts or title not in columns:
return None
+48 -12
View File
@@ -51,7 +51,7 @@ SIDECAR_KIND = "sfm.event"
# bumped without a `pip install` re-run — leading to confusing stale
# version stamps in sidecars. Bump this constant and CHANGELOG.md
# together at release time.
TOOL_VERSION = "0.32.0" # waveform times from the event record, report dates, histogram reader; gates the backfill regen
TOOL_VERSION = "0.33.0" # setup + notes, record type, histogram PVS and mic from the file; gates the backfill regen
try:
# Best-effort: prefer the installed metadata when it's NEWER than the
@@ -704,9 +704,11 @@ _INT16_FS = 32768.0
# sfm.event_hdf5._GEO_INT16_FS or sidecar peaks disagree with the plotted
# waveform by 2.3%. Confirmed 2026-08-25 against the BW ASCII corpus.
_GEO_INT16_FS = 32000.0
# A histogram's geo values are in decoder units of 16 counts = 0.005 in/s at
# the Normal range (the same assumption as the samples above).
_HIST_GEO_UNIT_INS = 16 * _GEO_NORMAL_FS_INS / _GEO_INT16_FS
# Full scale per geo_range: 0 Normal, 1 Sensitive.
_GEO_FS_BY_RANGE = {0: _GEO_NORMAL_FS_INS, 1: _GEO_SENSITIVE_FS_INS}
# A histogram's geo values are decoder units of 16 counts: 0.005 in/s at the
# Normal range, scaled by the range's full scale like the samples above.
_HIST_GEO_COUNTS_PER_UNIT = 16
# Microphone scale factor, psi per MicL count: one count is 0.25 Pa
# (waveform_codec.MIC_PA_PER_COUNT, measured against Blastware's psi(L) on
@@ -749,17 +751,19 @@ def _decode_samples_4ch_int16_le(stream: bytes) -> dict[str, list[int]]:
}
def _peaks_from_samples(samples: dict[str, list[int]]) -> PeakValues:
def _peaks_from_samples(samples: dict[str, list[int]],
geo_fs_ins: float = _GEO_NORMAL_FS_INS) -> PeakValues:
"""
Compute approximate peaks from raw int16 samples assuming Normal-range
geophone sensitivity. Used by the BW-importer when the 0C waveform
record (the device's authoritative peaks) is unavailable.
Compute peaks from raw int16 samples at the given geophone full scale
(Normal, 10 in/s, unless the file's range says otherwise). Used by the
BW-importer when the 0C waveform record (the device's authoritative peaks)
is unavailable.
"""
def _peak_ins(ch: list[int]) -> float:
if not ch:
return 0.0
m = max(abs(int(v)) for v in ch)
return m / _GEO_INT16_FS * _GEO_NORMAL_FS_INS
return m / _GEO_INT16_FS * geo_fs_ins
tran = _peak_ins(samples.get("Tran", []))
vert = _peak_ins(samples.get("Vert", []))
@@ -773,7 +777,7 @@ def _peaks_from_samples(samples: dict[str, list[int]]) -> PeakValues:
pvs = 0.0
n = min(len(samples.get("Tran", [])), len(samples.get("Vert", [])), len(samples.get("Long", [])))
if n:
scale = _GEO_NORMAL_FS_INS / _GEO_INT16_FS
scale = geo_fs_ins / _GEO_INT16_FS
T = samples["Tran"]; V = samples["Vert"]; L = samples["Long"]
for i in range(n):
t = T[i] * scale
@@ -996,6 +1000,32 @@ def _setup_notes(raw: bytes) -> ProjectInfo:
operator=operator, sensor_location=location)
# The geophone range sits in the setup's channel blocks, which follow the
# notes: the byte 29 after each geophone label (`00 00 01 Tran ...`). 0x00 is
# Normal, 0x01 Sensitive -- Blastware's GeoRange on 70,042 / 70,042 DL2 events,
# all three channels agreeing on every one (measured 2026-11-01). SFM's live
# 1A read has the same byte at label+33.
_RANGE_AFTER_LABEL = 29
_CHANNEL_BLOCK_SPAN = 2000 # how far past the setup name the blocks may sit
def _setup_geo_range(raw: bytes) -> tuple[Optional[int], Optional[str]]:
"""(geo_range, problem): 0 Normal or 1 Sensitive when all three geophone
channels say the same, else None and why."""
k = _setup_name_at(raw)
if k is None:
return None, "no recording-setup block to read the geophone range from"
found = {}
for label in (b"Tran", b"Vert", b"Long"):
i = raw.find(b"\x00\x00\x01" + label, k, k + _CHANNEL_BLOCK_SPAN)
p = i + 3 + _RANGE_AFTER_LABEL
found[label.decode()] = raw[p] if i >= 0 and p < len(raw) else None
values = set(found.values())
if len(values) == 1 and values <= {0, 1}:
return values.pop(), None
return None, f"geophone range not read: channel range bytes {found}"
def read_blastware_file(path: Union[str, Path], *, name: Optional[str] = None) -> Event:
"""Parse a Blastware event file into an Event: `decode_blastware_bytes` on its
bytes, `name` defaulting to the file's own (see there -- never a time source)."""
@@ -1156,6 +1186,9 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
# every waveform used to read "70 s". A histogram has no record time or
# pre-trigger -- Blastware reports neither.
ev.sample_rate = _setup_sample_rate(raw)
ev.geo_range, range_problem = _setup_geo_range(raw)
if range_problem:
ev.decode_problems.append(range_problem + "; peaks assume the Normal range")
if is_waveform_body:
ev.rectime_seconds = _setup_record_time(raw)
ev.total_samples = len(samples["Tran"]) or None
@@ -1244,14 +1277,17 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
# None, and ``apply_report_to_event`` overlays from a paired ASCII
# report when one is supplied.
has_samples = any(samples.get(ch) for ch in ("Tran", "Vert", "Long", "MicL"))
ev.peak_values = _peaks_from_samples(samples) if has_samples else None
# The geophone full scale is the file's range; Normal when it wasn't read.
geo_fs = _GEO_FS_BY_RANGE.get(ev.geo_range, _GEO_NORMAL_FS_INS)
ev.peak_values = _peaks_from_samples(samples, geo_fs) if has_samples else None
if not is_waveform_body and ev.peak_values is not None:
# A histogram stores each interval's vector sum (squared); the event's
# is the largest. Combining the channel peaks instead read high on 86%
# of DL2's histograms -- they can come from different moments.
pvs = interval_pvs(body)
if pvs:
ev.peak_values.peak_vector_sum = max(pvs) * _HIST_GEO_UNIT_INS
ev.peak_values.peak_vector_sum = (
max(pvs) * _HIST_GEO_COUNTS_PER_UNIT * geo_fs / _GEO_INT16_FS)
ev._a5_frames = None # not recoverable from BW file
return ev
+3
View File
@@ -543,6 +543,9 @@ class Event:
total_samples: Optional[int] = None # from STRT record: expected total sample-sets
pretrig_samples: Optional[int] = None # from STRT record: pre-trigger sample count
rectime_seconds: Optional[int] = None # from STRT record: record duration (seconds)
# Geophone range from the recording setup, as ComplianceConfig.geo_range:
# 0 Normal (10 in/s full scale), 1 Sensitive (1.25 in/s). None: not read.
geo_range: Optional[int] = None
# Sensor self-check traces keyed by channel label — the short diagnostic
# waveforms the unit records when it pulses each sensor before monitoring