diff --git a/minimateplus/decode_check.py b/minimateplus/decode_check.py index 0eda17a..a7af9f0 100644 --- a/minimateplus/decode_check.py +++ b/minimateplus/decode_check.py @@ -137,6 +137,17 @@ def sfm_side(raw: bytes) -> SfmSide: if pv is not None: values.update(TranPPV=pv.tran, VertPPV=pv.vert, LongPPV=pv.long, PeakVectorSum=pv.peak_vector_sum) + # A waveform's peak detail (None on a histogram, so not reported). + for tag, value in (("TranTimeofPeak", pv.tran_time_of_peak), + ("VertTimeofPeak", pv.vert_time_of_peak), + ("LongTimeofPeak", pv.long_time_of_peak), + ("MicLTimeofPeak", pv.micl_time_of_peak), + ("PeakVectorSumTimeSum", pv.pvs_time), + ("TranPeakAcceleration", pv.tran_peak_accel_g), + ("VertPeakAcceleration", pv.vert_peak_accel_g), + ("LongPeakAcceleration", pv.long_peak_accel_g)): + if value is not None: + values[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 diff --git a/minimateplus/event_file_io.py b/minimateplus/event_file_io.py index f1d0262..4cffcab 100644 --- a/minimateplus/event_file_io.py +++ b/minimateplus/event_file_io.py @@ -794,6 +794,41 @@ def _peaks_from_samples(samples: dict[str, list[int]], ) +# Blastware's g for peak acceleration: 9.81 m/s^2. It matches 1,190 / 1,197 +# DL2 channel values; the standard 9.80665 m/s^2 gives 1,073 (2026-11-04). +_ACCEL_IPS2_PER_G = 9.81 / 0.0254 + + +def _waveform_peak_detail(pv: PeakValues, samples: dict[str, list[int]], geo_fs_ins: float, + sample_rate: int, pretrig: int) -> None: + """A waveform's time of peak per channel, the vector sum's time and each + geophone's peak acceleration, on `pv`. Measured on 400 DL2 waveforms + (2026-11-04): + * time of peak = (first peak sample - pre-trigger samples) / sample rate, + 1,197 / 1,197; the vector-sum time is the same on sqrt(T^2 + V^2 + L^2), + 398 / 399 + * peak acceleration = the largest |v[i+1] - v[i]| x sample rate / g, + 1,190 / 1,197 -- Blastware's manuals: "subtracts two velocity readings + and divides the result by the elapsed time between them".""" + scale = geo_fs_ins / _GEO_INT16_FS + geo = {} + for ch in ("Tran", "Vert", "Long", "MicL"): + c = samples.get(ch) or [] + if not c: + continue + i = max(range(len(c)), key=lambda k: abs(c[k])) + setattr(pv, f"{ch.lower()}_time_of_peak", (i - pretrig) / sample_rate) + if ch != "MicL": + geo[ch] = c + step = max((abs(c[k + 1] - c[k]) for k in range(len(c) - 1)), default=0) + setattr(pv, f"{ch.lower()}_peak_accel_g", step * scale * sample_rate / _ACCEL_IPS2_PER_G) + if len(geo) == 3: + n = min(len(c) for c in geo.values()) + t, v, l = geo["Tran"], geo["Vert"], geo["Long"] + j = max(range(n), key=lambda k: t[k] * t[k] + v[k] * v[k] + l[k] * l[k]) + pv.pvs_time = (j - pretrig) / sample_rate + + # STRT[18], the record-type byte (#41): 0x46 on every one of DL2's 6,577 # waveform files and 0x0A on every one of its 63,535 histograms (measured # 2026-10-29). Manual / Event / Combo never occur there, so their bytes are @@ -1480,6 +1515,9 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event: # 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 (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) if ev.stop_time is None: # a histogram's record stop, else the footer's ev.stop_time = ts2 if hist_records: diff --git a/minimateplus/models.py b/minimateplus/models.py index f65e00c..0a971de 100644 --- a/minimateplus/models.py +++ b/minimateplus/models.py @@ -364,6 +364,15 @@ class PeakValues: vert_zc_above_range: bool = False long_zc_above_range: bool = False mic_zc_above_range: bool = False + # A waveform's peak detail, from its samples (2026-11-04); None for a histogram. + tran_time_of_peak: Optional[float] = None # s from the trigger; negative = pre-trigger + vert_time_of_peak: Optional[float] = None + long_time_of_peak: Optional[float] = None + micl_time_of_peak: Optional[float] = None + pvs_time: Optional[float] = None # s from the trigger, of the vector-sum peak + tran_peak_accel_g: Optional[float] = None + vert_peak_accel_g: Optional[float] = None + long_peak_accel_g: Optional[float] = None # ── Project / operator metadata ─────────────────────────────────────────────── diff --git a/tests/test_bw_file_fields.py b/tests/test_bw_file_fields.py index 3c2b94a..2ae56d9 100644 --- a/tests/test_bw_file_fields.py +++ b/tests/test_bw_file_fields.py @@ -495,3 +495,30 @@ def test_a_histogram_without_its_record_falls_back_to_the_footer_and_says_so(): assert ev.timestamp_source == "footer" assert ev.timestamp.second == 25 # the footer's start, a second early assert any("histogram event record" in p for p in ev.decode_problems) + + +# ── Group D, part 1: time of peak, vector-sum time, peak acceleration ─────── +# Measured on 400 DL2 waveforms (2026-11-04): +# time of peak = (peak sample - pre-trigger samples) / sample rate: 1,197 / 1,197 +# vector-sum time, the same on sqrt(T^2 + V^2 + L^2): 398 / 399 +# peak acceleration = largest |v[i+1] - v[i]| x rate / g, with Blastware's +# g = 9.81 m/s^2 (386.2205 in/s^2): 1,190 / 1,197 (9.80665 gives 1,073) + +@pytest.mark.parametrize("rel,times,pvs_time,accel", [ + (f"{_SF}/T189HUD6.QE0W", + {"tran": 0.004, "vert": 0.001, "long": 0.024, "micl": 0.003}, 0.016, (0.027, 0.013, 0.027)), + ("decode-checker-10-29-26/R352HIUI.F40W", + {"tran": 0.000, "vert": 0.000, "long": 0.000, "micl": 0.005}, 0.000, (1.617, 0.278, 0.318)), +]) +def test_waveform_peak_detail_comes_from_the_samples(rel, times, pvs_time, accel): + pv = read_blastware_file(_FIX / rel).peak_values + for ch, t in times.items(): + assert abs(getattr(pv, f"{ch}_time_of_peak") - t) <= 0.0005 + 1e-9, (ch, getattr(pv, f"{ch}_time_of_peak")) + assert abs(pv.pvs_time - pvs_time) <= 0.0005 + 1e-9 + for got, want in zip((pv.tran_peak_accel_g, pv.vert_peak_accel_g, pv.long_peak_accel_g), accel): + assert abs(got - want) <= 0.0005 + 1e-9, (got, want) + + +def test_a_histogram_has_no_waveform_peak_detail(): + pv = read_blastware_file(_FIX / "decode-checker-10-29-26/T004JTRF.ME0H").peak_values + assert (pv.tran_time_of_peak, pv.pvs_time, pv.tran_peak_accel_g) == (None, None, None) diff --git a/tests/test_decode_check.py b/tests/test_decode_check.py index ac01ebd..fd3961a 100644 --- a/tests/test_decode_check.py +++ b/tests/test_decode_check.py @@ -39,8 +39,9 @@ def test_event_type_comes_from_the_bytes(): def test_report_fields_stay_blank(): s = sfm_side((FIX / "T1905KDW.5F0W").read_bytes()) - # (BatteryLevel and Calibration are read since 2026-11-03: test_the_setup_fields_match_blastware) - for tag in ("TranZCFreq", "TranPeakAcceleration", "TranTestResults"): + # (BatteryLevel and Calibration are read since 2026-11-03, peak acceleration + # since 2026-11-04: test_the_setup_fields_match_blastware, test_waveform_peak_detail_*) + for tag in ("TranZCFreq", "TranPeakDisplacement", "TranTestResults"): assert s.values.get(tag) is None @@ -343,3 +344,14 @@ def test_histogram_times_match_blastware(path): for f in (("HistogramStartDate", "HistogramStartTime", "HistogramStopTime", "intervals.Time") + tuple(t for t in _PEAK_TIMES if t in ref.scalars)): assert res[f].verdict == "match", (f, res[f]) + + +@pytest.mark.parametrize("path", [FIX / "R352HIUI.F40W", + FIX.parent / "setup-fields-11-03-26" / "T189HUD6.QE0W"]) +def test_waveform_peak_detail_matches_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)} + for f in ("TranTimeofPeak", "VertTimeofPeak", "LongTimeofPeak", "MicLTimeofPeak", + "PeakVectorSumTimeSum", "TranPeakAcceleration", "VertPeakAcceleration", + "LongPeakAcceleration"): + assert res[f].verdict == "match", (f, res[f])