""" Tests for a Thor (Micromate) event file's own settings and notes (#44, #45). Fixtures (tests/fixtures/idf-metadata-11-20-26/) are real files: - UM12947_20261025141012.IDFW: SFM's own 2026-10-25 bench download, no report. Set to 4096 S/s, a 1.0 s pre-trigger and a 3 s record time (record length 16,384 samples = 1 s + 3 s at 4096; issue #44/#45). - three corpus files with Thor's own report beside them, the ground truth: a 0.25 s pre-trigger waveform (UM11719), a waveform whose pre-trigger is 193 samples (UM12947), and a histogram with a chance byte match ahead of its settings record (UM11402). Measured 2026-11-20 on 2,027 corpus files with reports: the settings record is found by its `00 2a 04 00 00` signature in every file, its sample rate matches the report in all of them, record time and pre-trigger in all 291 waveforms (the report rounds the pre-trigger to 0.01 s), and the four notes at 0x23c in all 2,027. """ from __future__ import annotations import datetime import os import sys from pathlib import Path import pytest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from micromate.idf_ascii_report import parse_idf_report from micromate.idf_file import extract_binary_metadata, read_idf_file FIX = Path(__file__).parent / "fixtures" / "idf-metadata-11-20-26" BENCH = FIX / "UM12947_20261025141012.IDFW" REPORTED = ["UM11719_20231219162723.IDFW", "UM12947_20250929144014.IDFW", "UM11402_20250610173039.IDFH"] def _md(name): p = FIX / name return extract_binary_metadata(p.read_bytes(), histogram=p.suffix.upper() == ".IDFH") def test_settings_found_without_a_quarter_second_pretrigger(): md = _md(BENCH.name) assert (md.sample_rate, md.pre_trigger_sec, md.record_time_sec) == (4096, 1.0, 3.0) def test_notes_read_from_the_file(): md = _md(BENCH.name) assert (md.project, md.client, md.operator, md.notes) == ( "Test Unit 2", "Terra-Mechanics", "Terra-Mechanics", "Test Unit 2") @pytest.mark.parametrize("name", REPORTED) def test_binary_metadata_matches_thors_report(name): rep = parse_idf_report((FIX / (name + ".txt")).read_bytes()) md = _md(name) assert md.sample_rate == rep["sample_rate"] for field in ("project", "client", "operator", "notes"): assert (getattr(md, field) or "") == (rep.get(field) or ""), field if name.upper().endswith(".IDFW"): assert md.record_time_sec == pytest.approx(rep["record_time_sec"], abs=1e-3) assert md.pre_trigger_sec == pytest.approx(rep["pre_trigger_sec"], abs=0.005) # report rounds def test_a_histogram_has_no_record_time_or_pretrigger(): md = _md("UM11402_20250610173039.IDFH") assert md.sample_rate == 1024 # not the chance match at 0x915 (16384) assert md.record_time_sec is None and md.pre_trigger_sec is None def test_the_event_carries_the_files_pretrigger_in_samples(): ev = read_idf_file(FIX / "UM12947_20250929144014.IDFW").event.to_minimateplus_event(bytes(4)) assert (ev.sample_rate, ev.pretrig_samples) == (1024, 193) bench = read_idf_file(BENCH).event.to_minimateplus_event(bytes(4)) assert (bench.sample_rate, bench.pretrig_samples, bench.rectime_seconds) == (4096, 4096, 3.0) assert (bench.project_info.project, bench.project_info.notes) == ("Test Unit 2", "Test Unit 2") def test_sfm_download_without_a_report_stores_the_files_settings(tmp_path): """Issue #44 / #45's own reproduction: an IDF stored with no report.""" import h5py from sfm.waveform_store import WaveformStore store = WaveformStore(tmp_path / "w") ev, rec = store.save_imported_idf(BENCH.read_bytes(), source_path=Path(BENCH.name)) with h5py.File(store.hdf5_path_for(rec.get("serial") or "UM12947", rec["filename"]), "r") as f: a = dict(f.attrs) assert (a["sample_rate"], a["pretrig_samples"], a["rectime_seconds"]) == (4096, 4096, 3.0) assert (a["project"], a["client"], a["operator"]) == ("Test Unit 2", "Terra-Mechanics", "Terra-Mechanics") def test_a_reported_event_gets_its_pretrigger_in_samples_too(tmp_path): """Before, every Micromate .h5 stored pre-trigger 0, report or not.""" import h5py from sfm.waveform_store import WaveformStore name = "UM11719_20231219162723.IDFW" store = WaveformStore(tmp_path / "w") ev, rec = store.save_imported_idf((FIX / name).read_bytes(), source_path=Path(name), idf_report_text=(FIX / (name + ".txt")).read_bytes()) rep = parse_idf_report((FIX / (name + ".txt")).read_bytes()) with h5py.File(store.hdf5_path_for(rec.get("serial") or "UM11719", rec["filename"]), "r") as f: assert f.attrs["pretrig_samples"] == round(rep["pre_trigger_sec"] * rep["sample_rate"]) > 0 def test_the_event_time_comes_from_the_file(): """The time sits 0x34 after the settings record (2,384 / 2,384 files equal their filename's time). The bench file's layout put it at neither old fixed offset.""" assert _md(BENCH.name).event_datetime == datetime.datetime(2026, 10, 25, 14, 10, 12) assert read_idf_file(BENCH).event.timestamp == datetime.datetime(2026, 10, 25, 14, 10, 12)