""" Setup and note fields read from a Blastware event file alone (#44, #45). Every expected value below comes from Blastware's own output for the same file -- its ASCII report or its `_XML.XML` event report -- or, for the bench file with neither, from the record length and the MiniMate Plus spec, as written up in #44/#45. None is derived from the decoder under test. The STRT record's bytes are not these fields: STRT[18] is the record-type marker (0x46 waveform, 0x0A histogram), which is why every waveform used to read "70 s". """ from __future__ import annotations from pathlib import Path import pytest from minimateplus.event_file_io import read_blastware_file _FIX = Path(__file__).resolve().parent / "fixtures" # (file, sample rate, record time s, pre-trigger samples, total samples) _WAVEFORMS = [ # ASCII report: 1024 sps, 3.0 s, -0.25 s; 3328 samples per channel. ("5-11-26/M529LL1A.SP0", 1024, 3.0, 256, 3328), # ASCII report: 1024 sps, 1.0 s, -0.25 s. ("decode-re-5-8-26/event-c/M529LK44.AB0", 1024, 1.0, 256, 1280), # #44/#45: BE9441 bench at 4096 sps, 5 s, 0.25 s -> 21,504 samples. ("minimate-bench-10-26-26-blastware/blastware/K441LTOF.ZI0", 4096, 5.0, 1024, 21504), # XML: setup "BE16352.set", 1024 sps, 1.0 s, -0.247 s, 1277 sample rows. ("bw-import-fields-10-29-26/R352HIUI.F40W", 1024, 1.0, 253, 1277), ] @pytest.mark.parametrize("rel,sr,rt,pretrig,total", _WAVEFORMS) def test_waveform_setup_fields_come_from_the_file(rel, sr, rt, pretrig, total): ev = read_blastware_file(_FIX / rel) assert ev.sample_rate == sr assert ev.rectime_seconds == pytest.approx(rt) assert ev.pretrig_samples == pretrig assert ev.total_samples == total # (file, sample rate) -- ASCII report / XML _HISTOGRAMS = [ ("histogram-mic-off-10-26-26/K441LTOK.QZ0", 2048), ("ts-fix/K441LKZU.C30H", 1024), ("bw-import-fields-10-29-26/M529L4XQ.R30H", 1024), ] @pytest.mark.parametrize("rel,sr", _HISTOGRAMS) def test_histogram_sample_rate_comes_from_the_file(rel, sr): ev = read_blastware_file(_FIX / rel) assert ev.sample_rate == sr @pytest.mark.parametrize("rel,sr", _HISTOGRAMS) def test_histogram_has_no_record_time_or_pretrigger(rel, sr): # Blastware reports neither for a histogram; the STRT record-type byte # (0x0A = 10) is not a record time. ev = read_blastware_file(_FIX / rel) assert ev.rectime_seconds is None assert ev.pretrig_samples is None # (file, setup name, project, client, operator, sensor location) _NOTES = [ # ASCII report; default labels, location spelled "Seis Loc:". ("5-11-26/M529LL1A.SP0", "Standard Recording Setup", "Test-5-8-26", "New Client", "Terra-Mechanics - Harrison", "Still Catbed."), # ASCII report; location spelled "Seis. Loc:". BE9441's bench setup is # saved as "Standard Recording Setup.set" (#45). ("histogram-mic-off-10-26-26/K441LTOK.QZ0", "Standard Recording Setup.set", "TestUnit-Minimate", "Terra-Mechanics", "Terra-Mechanics Inc. - Harrison", "Test unit 1"), # XML; a setup not named "Standard Recording Setup". ("bw-import-fields-10-29-26/R352HIUI.F40W", "BE16352.set", "JV 241 - 1425 Spanlers Mill Rad", "Walsh Granite JV", "Terra-Mechanics Inc- D. Harrison", "Loc #1 - 1425 Spanlers Mill Rd"), # XML (Test Loc only); the operator relabelled the # four note slots "Location:", "Client:", "User Name:", "General:" and # filled in only the first. Slots are positional, as in the ASCII parser. ("bw-import-fields-10-29-26/M529L4XQ.R30H", "Standard Recording Setup", "Test Loc", None, None, None), ] @pytest.mark.parametrize("rel,setup,project,client,operator,location", _NOTES) def test_notes_come_from_the_file_by_position(rel, setup, project, client, operator, location): pi = read_blastware_file(_FIX / rel).project_info assert pi.setup_name == setup assert pi.project == project assert pi.client == client assert pi.operator == operator assert pi.sensor_location == location def test_import_without_a_report_stores_the_file_fields(tmp_path: Path): """The watcher path with no ASCII report: the sidecar carries what the file holds, not blanks and not the STRT bytes.""" from sfm.waveform_store import WaveformStore src = _FIX / "bw-import-fields-10-29-26/R352HIUI.F40W" store = WaveformStore(tmp_path / "waveforms") store.save_imported_bw(src.read_bytes(), source_path=src) sc = store.load_sidecar("BE16352", src.name) assert sc["event"]["sample_rate"] == 1024 assert sc["event"]["rectime_seconds"] == pytest.approx(1.0) assert sc["event"]["pretrig_samples"] == 253 assert sc["project_info"]["project"] == "JV 241 - 1425 Spanlers Mill Rad" assert sc["project_info"]["sensor_location"] == "Loc #1 - 1425 Spanlers Mill Rd" # ── #41: the record type comes from the bytes ─────────────────────────────── # STRT[18] is the record-type byte: 0x46 on every one of DL2's 6,577 waveform # files, 0x0A on every one of its 63,535 histograms (measured 2026-10-29). from minimateplus import blastware_file as _bw # noqa: E402 from minimateplus.event_file_io import decode_blastware_bytes # noqa: E402 _TYPES = [ ("bw-import-fields-10-29-26/R352HIUI.F40W", "Waveform"), ("bw-import-fields-10-29-26/M529L4XQ.R30H", "Histogram"), ("5-11-26/M529LL1A.SP0", "Waveform"), # name carries no type ("histogram-mic-off-10-26-26/K441LTOK.QZ0", "Histogram"), # name carries no type ] @pytest.mark.parametrize("rel,want", _TYPES) def test_record_type_comes_from_the_bytes_without_a_name(rel, want): assert decode_blastware_bytes((_FIX / rel).read_bytes(), name=None).record_type == want def test_the_bytes_win_over_a_filename_that_disagrees(caplog): raw = (_FIX / "bw-import-fields-10-29-26/M529L4XQ.R30H").read_bytes() ev = decode_blastware_bytes(raw, name="M529L4XQ.R30W") assert ev.record_type == "Histogram" assert "record type" in caplog.text def test_an_unknown_type_byte_is_unknown_not_a_guess(): raw = bytearray((_FIX / "bw-import-fields-10-29-26/R352HIUI.F40W").read_bytes()) raw[_bw._WAVEFORM_HEADER_SIZE + 18] = 0x99 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) # ── The mic: one count = 0.25 Pa ──────────────────────────────────────────── # Measured 2026-10-31 against 1,729,253 DL2 histogram intervals' "MicL Peak" # columns: count x 0.25 Pa matches Blastware's psi(L) on every one, and # 20 log10(count x 0.25 Pa / 20 uPa) its dB(L) on all but 17 exact half-way # cases. The old psi factor (a "~0.5 psi full-scale" guess) was ~95x small. from minimateplus.waveform_codec import mic_count_to_db # noqa: E402 @pytest.mark.parametrize("count,printed", [ (1, "81.94"), # XML sample cell (119, "123.4"), # Blastware interval tables, where 81.94 + 20 log10 gave 123.451 (132, "124.3"), # ... and 124.351 (129, "124.2"), # 162 intervals; exactly 0.25 Pa gives 124.149997, a hair under half-way (813, "140.14"), # V70 fixture, Blastware display ]) def test_mic_db_matches_blastware(count, printed): d = len(printed.split(".")[1]) assert abs(mic_count_to_db(count) - float(printed)) <= 0.5 * 10 ** -d + 1e-9 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" # ── The job number ────────────────────────────────────────────────────────── # Series IV's 0x1A layout again: report flags at k - 9 (bit 0x02 = job number # shown) and the number, uint16 BE, at k - 8, kept while switched off. The flag # matches Blastware's JobNumber on 2,997 / 2,997 DL2 events that print one and # 66,986 / 66,986 that don't (measured 2026-11-02). Every printed value is 1; # hidden ones hold real numbers (802, 1012, 1013, 622). @pytest.mark.parametrize("rel,enabled,number", [ ("decode-checker-10-29-26/T1905KDW.5F0W", True, 1), # XML JobNumber 1 ("job-number-11-02-26/T438I4V5.DJ0H", True, 1), # histogram, XML JobNumber 1 ("job-number-11-02-26/K557I55Y.I20H", False, 802), # no JobNumber in the XML ("decode-checker-10-29-26/R352HFZ7.530H", False, 1), ]) def test_job_number_comes_from_the_file(rel, enabled, number): pi = read_blastware_file(_FIX / rel).project_info assert (pi.job_number_enabled, pi.job_number) == (enabled, number) def test_the_h5_carries_the_job_number(tmp_path): import h5py from sfm import event_hdf5 ev = read_blastware_file(_FIX / "job-number-11-02-26/K557I55Y.I20H") event_hdf5.write_event_hdf5(tmp_path / "e.h5", ev, serial="BE9557") with h5py.File(tmp_path / "e.h5", "r") as f: assert (bool(f.attrs["job_number_enabled"]), int(f.attrs["job_number"])) == (False, 802) # ── Histograms: the codec follows the record type, and loud short blocks ───── _HD = "histogram-decode-11-02-26" def test_a_histogram_body_goes_to_the_histogram_codec(): # BE9440, 2018, 5 s intervals. The waveform codec accepted this body when # it was tried first, and returned ~5x too many "samples". Blastware's # report: 6,976 intervals, PPV 0.180 / 0.195 / 0.215, PVS 0.270, start # 2018-04-26 07:53:41. ev = read_blastware_file(_FIX / _HD / "K440HDXX.XH0H") assert ev.record_type == "Histogram" assert len(ev.raw_samples["Tran"]) == 6976 pv = ev.peak_values for got, want in zip((pv.tran, pv.vert, pv.long, pv.peak_vector_sum), (0.180, 0.195, 0.215, 0.270)): assert abs(got - want) <= 0.0005 + 1e-9, (got, want) ts = ev.timestamp assert (ts.year, ts.month, ts.day) == (2018, 4, 26) @pytest.mark.parametrize("name,n", [ ("T193KR5M.X90H", 9), # 2 s; its 8th interval is 5.8 in/s of vector sum ("K440HA3C.JM0H", 8), # 5 s ]) def test_a_loud_interval_does_not_hide_a_short_histogram(name, n): # A short (one-block) sub-minute histogram is found by its record layout. # An interval louder than 1.28 in/s has a vector sum squared above 65,535, # which the old "first word is 0000" test rejected. ev = read_blastware_file(_FIX / _HD / name) assert len(ev.raw_samples["Tran"]) == n # ── The footer is the file's last 26 bytes ────────────────────────────────── # Every DL2 file ends in a valid footer (70,041 / 70,041: 0e 08 and two valid # times). A few also hold an earlier "0e 08 + date" record, ~1.1 KB before # the end: the stop time followed by zeros. The first-match footer search took # it, so a histogram started at its own stop -- a day late on BE9558's daily # histograms -- and a waveform lost its stop and its trigger time. _FT = "footer-11-03-26" def test_a_histogram_starts_at_its_footer_not_at_an_earlier_stop_record(): ts = read_blastware_file(_FIX / _FT / "K558L9XP.FK0H").timestamp # Blastware: 2025-10-07 19:14:08 (SFM read 2025-10-08 19:10:01) assert (ts.year, ts.month, ts.day, ts.hour, ts.minute) == (2025, 10, 7, 19, 14) def test_a_histogram_with_the_early_record_gets_its_time(): ts = read_blastware_file(_FIX / _FT / "T003LCYV.JD0H").timestamp # Blastware: 2025-12-05 18:04:25 (SFM had no time at all) assert ts is not None assert (ts.year, ts.month, ts.day, ts.hour, ts.minute) == (2025, 12, 5, 18, 4) def test_a_waveform_with_the_early_record_is_timed_from_its_trigger(): ev = read_blastware_file(_FIX / _FT / "K440I4LG.JK0W") ts = ev.timestamp # Blastware: 2019-09-26 11:14:08, record time 4 s (SFM read the stop, 11:14:12) assert (ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second) == (2019, 9, 26, 11, 14, 8) assert ev.timestamp_source in ("event_record", "estimate")