diff --git a/minimateplus/event_file_io.py b/minimateplus/event_file_io.py index 305cfb5..983078f 100644 --- a/minimateplus/event_file_io.py +++ b/minimateplus/event_file_io.py @@ -30,7 +30,7 @@ from . import blastware_file as _bw # avoid circular reference at module load from .bw_ascii_report import BwAsciiReport from . import waveform_codec as _wc from .waveform_codec import decode_waveform_v2, decoded_to_adc_counts -from .histogram_codec import decode_histogram_body +from .histogram_codec import decode_histogram_body, interval_pvs # Reference pressure for dB(L) → psi conversion (20 µPa expressed in psi). # Same constant as sfm/sfm_webapp.html so server-side and browser-side @@ -704,6 +704,9 @@ _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 # Microphone scale factor, psi per ADC count. Approximate — exact factor # depends on the geophone-vs-mic ADC scaling and the firmware reference. @@ -1240,6 +1243,13 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event: # 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 + 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._a5_frames = None # not recoverable from BW file return ev diff --git a/minimateplus/histogram_codec.py b/minimateplus/histogram_codec.py index b9daa3e..6978806 100644 --- a/minimateplus/histogram_codec.py +++ b/minimateplus/histogram_codec.py @@ -40,6 +40,13 @@ iterate 32-stride and stop before the tail. [24:28] 4-byte variable purpose unknown (possibly CRC or timestamp delta) [28:32] block-end signature see "Two block tails" below +⚠ CORRECTED 2026-10-30 (measured): [21:25] is the interval's peak vector sum +SQUARED, uint32 BE in 0.005 in/s units. sqrt([21:25]) x 0.005 is the "Geo PVS" +Blastware prints for the interval, on 1,668,073 / 1,668,074 standard DL2 +intervals checked against the _XML.XML reports. [21:23] is zero only while the +square is below 65,536 (PVS < 1.28 in/s), which is why it looked constant; only +[25:28] is still unexplained. `interval_pvs` reads it. + **Every per-channel field is uint16 BIG-endian** (confirmed 2026-08-25). Only ``block_ctr`` at [2:4] is little-endian. @@ -124,6 +131,7 @@ record list. from __future__ import annotations +import math import struct from typing import List, Optional, Tuple @@ -216,6 +224,7 @@ def _decode_block(block: bytes) -> Optional[dict]: "m_peak": m_peak, "m_halfp": m_halfp, "meta_var": var_meta, + "pvs_sq": int.from_bytes(block[21:25], "big"), "is_terminal": block[28:32] == _BLOCK_TAIL_TERMINAL, } @@ -319,6 +328,16 @@ def decode_histogram_body_full(body: bytes) -> Optional[List[dict]]: return records if records else None +def interval_pvs(body: bytes) -> Optional[List[float]]: + """Each interval's peak vector sum, in the geo peak units (0.005 in/s at + Normal range): the square root of the square the device stores with every + interval. This is the vector sum at an instant, as Blastware prints it -- + not the magnitude of the three channel peaks, which can come from + different moments. None when the body has no valid blocks.""" + records = _walk_auto(body) + return [math.sqrt(r["pvs_sq"]) for r in records] if records else None + + def half_period_to_hz(halfp: int) -> Optional[float]: """Convert a half-period in samples to frequency in Hz. @@ -356,7 +375,10 @@ def geo_count_to_ins(count: int) -> float: # [4] 0x0a # [5 ...] one record per interval: per channel a BIG-endian uint16 peak and # half-period (Tran, Vert, Long[, MicL]), then 0x0000, then one more -# word ("meta_var" keeps those 4 bytes) +# word ("meta_var" keeps those 4 bytes). Measured 2026-10-30: those +# 4 bytes are the interval's peak vector sum SQUARED, uint32 BE in +# 0.005 in/s units -- sqrt matches Blastware's "Geo PVS" on 34.5 M +# DL2 sub-minute intervals; the 0x0000 is its high word # [-7:] trailer: 3 bytes, then the 4-byte tail -- 9c 06 00 42 on the # stream's LAST block # @@ -486,6 +508,7 @@ def _decode_record(body: bytes, q: int, rec: int, off: int) -> dict: "m_peak": be16(q + 12) if mic else None, "m_halfp": be16(q + 14) if mic else None, "meta_var": bytes(body[q + z:q + z + 4]), + "pvs_sq": int.from_bytes(body[q + z:q + z + 4], "big"), "is_terminal": False, } diff --git a/tests/test_bw_file_fields.py b/tests/test_bw_file_fields.py index f09644d..a5bb906 100644 --- a/tests/test_bw_file_fields.py +++ b/tests/test_bw_file_fields.py @@ -146,3 +146,36 @@ def test_an_unknown_type_byte_is_unknown_not_a_guess(): ev = decode_blastware_bytes(bytes(raw), name=None) assert ev.record_type is None assert any("record type" in p for p in ev.decode_problems) + + +# ── A histogram's peak vector sum is read, not computed from channel peaks ── +# Each interval stores the square of its vector sum (uint32 BE, in 0.005 in/s +# units): bytes [21:25] of a standard block, the last 4 bytes of a sub-minute +# record. sqrt(stored) x 0.005 matches Blastware's printed interval PVS on +# 1,668,073 / 1,668,074 standard and 34.5 M sub-minute DL2 intervals +# (measured 2026-10-30). The old value combined each channel's interval peak, +# which can come from different moments, and read high on 86% of histograms. + +from minimateplus.histogram_codec import interval_pvs # noqa: E402 + +# (file, Blastware's summary Peak Vector Sum) +_HIST_PVS = [ + ("decode-checker-10-29-26/M529L4XQ.R30H", 3.095), # XML; 1-minute + ("decode-checker-10-29-26/T004JTRF.ME0H", 1.358), # XML; SFM used to say 1.782 + ("histogram-mic-off-10-26-26/K441LTOK.QZ0", 1.083), # ASCII report; 2 s, mic off +] + + +@pytest.mark.parametrize("rel,pvs", _HIST_PVS) +def test_histogram_peak_vector_sum_is_the_stored_one(rel, pvs): + got = read_blastware_file(_FIX / rel).peak_values.peak_vector_sum + assert abs(got - pvs) <= 0.0005 + 1e-9, got + + +def test_interval_pvs_per_interval(): + raw = (_FIX / "histogram-mic-off-10-26-26/K441LTOK.QZ0").read_bytes() + pvs = interval_pvs(raw[_bw._WAVEFORM_HEADER_SIZE + 21:]) + assert len(pvs) == 22 + # Blastware's interval table, first four "Geo PVS" values + for got, want in zip(pvs[:4], (0.065, 0.034, 0.015, 0.461)): + assert abs(got * 0.005 - want) <= 0.0005 + 1e-9, (got, want)