fix(idf): a Thor file's own settings, notes and time (#44, #45)

A Micromate event read without Thor's report (SFM's own downloads) came out
with sample rate, pre-trigger and record time 0, empty notes, and a
1970-01-01 time; every Micromate .h5, report or not, stored pre-trigger 0.

- The settings were found by the bytes be 80 00 00 00 00 -- a -0.25 s
  pre-trigger and Fixed stop mode, not a marker -- so any other pre-trigger
  left them unread.  The event's settings record is now found by its own
  signature 00 2a 04 00 00 (fields at the live 0x1A block's head offsets).
- The event time is read at settings record + 0x34, not two fixed offsets
  that miss a header shape.
- The four notes are read at 0x23c ([label 22][value 42] x 4).
- IdfEvent.to_minimateplus_event sets pretrig_samples.
- save_imported_idf fills the pre-trigger and notes from the file when the
  report lacks them.

Measured against Thor's own reports on 2,027 corpus files: sample rate and
notes 2,027 / 2,027; record time and pre-trigger 291 / 291 waveforms; event
time = filename on 2,384 / 2,384.  The issues' 20 UM12947 bench files now
store 4096 S/s, 4096 pre-trigger samples (1.0 s), 3 s and their notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
This commit is contained in:
serversdownandClaude Opus 5.5 committed 2026-11-20 14:45:52 -05:00
1 parent c0561f9de6
commit f7a02b1fee
12 files changed
+5856 -33

No files matched your search

+110
View File
@@ -0,0 +1,110 @@
"""
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)