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:
1 parent
c0561f9de6
commit
f7a02b1fee
12 files changed
+5856
-33
No files matched your search
+92
-33
@@ -42,6 +42,7 @@ The full reverse-engineering writeup lives in
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import math
|
||||
import struct
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
@@ -66,7 +67,7 @@ from typing import Optional, Union
|
||||
# error. Do not re-pin it.
|
||||
from minimateplus.waveform_codec import _MODES, decode_waveform_v2, is_record
|
||||
|
||||
from .models import IdfEvent, IdfPeaks, IdfReport
|
||||
from .models import IdfEvent, IdfPeaks, IdfProjectInfo, IdfReport
|
||||
|
||||
|
||||
# Genuine Series IV / Thor IDF binary signature: 6 bytes, then ASCII "Instantel".
|
||||
@@ -170,8 +171,46 @@ class IdfBinaryMetadata:
|
||||
serial: Optional[str] = None
|
||||
event_datetime: Optional[datetime.datetime] = None
|
||||
sample_rate: Optional[int] = None
|
||||
record_time_sec: Optional[float] = None
|
||||
record_time_sec: Optional[float] = None # waveforms only
|
||||
pre_trigger_sec: Optional[float] = None # waveforms only; this event's own, positive
|
||||
calibration_date: Optional[datetime.date] = None
|
||||
# The four notes, as Thor's report maps them (TitleString1..4).
|
||||
project: Optional[str] = None
|
||||
client: Optional[str] = None
|
||||
operator: Optional[str] = None
|
||||
notes: Optional[str] = None
|
||||
|
||||
|
||||
# The event's settings record: `[00 2a][04 00 00][mode][rate u16 BE][…]
|
||||
# [pre-trigger f32 BE, negative s at +12][…][record time f32 BE at +18]`, the
|
||||
# same field offsets as the head of the live 0x1A setup block (setup_block.py).
|
||||
# Found at 0x910, 0x946, 0x98e or 0x9c4 depending on the header's shape, so it is
|
||||
# located by its signature, not its offset (measured on 2,384 files, 2026-11-20).
|
||||
_SETTINGS_SIG = b"\x00\x2a\x04\x00\x00"
|
||||
_SETTINGS_WINDOW = (0x800, 0xC00)
|
||||
_SETTINGS_RATES = {256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}
|
||||
_EVENT_TIME_AFTER_SETTINGS = 0x34
|
||||
# Four notes, each a 22-byte label then its 42-byte value, 64 bytes apart: the
|
||||
# live setup block's note layout, at a fixed offset in every file seen.
|
||||
_NOTES_AT, _NOTE_LABEL_LEN, _NOTE_VALUE_LEN, _NOTE_STRIDE = 0x23C, 22, 42, 64
|
||||
_NOTE_FIELDS = ("project", "client", "operator", "notes")
|
||||
|
||||
|
||||
def _settings_record(buf: bytes) -> Optional[int]:
|
||||
"""Offset of the event's settings record, or None. Every field must be
|
||||
plausible, which rejects a chance match of the signature."""
|
||||
start, stop = _SETTINGS_WINDOW
|
||||
o = buf.find(_SETTINGS_SIG, start, stop)
|
||||
while o >= 0 and o + 22 <= len(buf):
|
||||
rate = int.from_bytes(buf[o + 6 : o + 8], "big")
|
||||
pre, = struct.unpack(">f", buf[o + 12 : o + 16])
|
||||
rt, = struct.unpack(">f", buf[o + 18 : o + 22])
|
||||
if (1 <= buf[o + 5] <= 4 and rate in _SETTINGS_RATES
|
||||
and math.isfinite(pre) and -600.0 <= pre <= 0.0
|
||||
and math.isfinite(rt) and rt >= 0.0):
|
||||
return o
|
||||
o = buf.find(_SETTINGS_SIG, o + 1, stop)
|
||||
return None
|
||||
|
||||
|
||||
def _read_ascii_z(buf: bytes, off: int, maxlen: int = 64) -> Optional[str]:
|
||||
@@ -199,44 +238,47 @@ def _decode_8byte_timestamp(buf: bytes, off: int) -> Optional[datetime.datetime]
|
||||
return None
|
||||
|
||||
|
||||
def extract_binary_metadata(buf: bytes) -> IdfBinaryMetadata:
|
||||
"""Pull serial/timestamp/sample_rate/record_time/calibration from the
|
||||
sig-A binary header.
|
||||
def extract_binary_metadata(buf: bytes, *, histogram: bool = False) -> IdfBinaryMetadata:
|
||||
"""Pull serial, event time, sample rate, record time, pre-trigger,
|
||||
calibration date and the four notes from a Thor binary (no .txt needed).
|
||||
|
||||
Field positions confirmed against UM11719_20231219162723.IDFW; stable
|
||||
across the 151-file sig-A corpus.
|
||||
Measured 2026-11-20 on 2,027 corpus files against Thor's own reports:
|
||||
sample rate 2,027 / 2,027; record time and pre-trigger 291 / 291 waveforms
|
||||
(the report rounds the pre-trigger to 0.01 s; the file holds this event's
|
||||
exact value, e.g. 193 samples); the notes 2,027 / 2,027; the event time
|
||||
equals the filename's on 2,384 / 2,384 files.
|
||||
|
||||
A histogram's settings record holds the sample rate but no record time or
|
||||
pre-trigger (its [18:22] varies file to file), so `histogram=True` leaves
|
||||
those None. Before 2026-11-20 the settings were found by the bytes
|
||||
`be 80 00 00 00 00`, which are a -0.25 s pre-trigger and Fixed stop mode:
|
||||
any other pre-trigger left them all None (#44, #45).
|
||||
"""
|
||||
md = IdfBinaryMetadata()
|
||||
|
||||
# Serial: null-terminated ASCII at 0x14E.
|
||||
md.serial = _read_ascii_z(buf, 0x14E, maxlen=16)
|
||||
|
||||
# Sample rate + record time live in a BW-compatible compliance block.
|
||||
# Locate the 6-byte anchor `be 80 00 00 00 00` and read offsets relative
|
||||
# to it: anchor-6 = sample_rate uint16 BE; anchor+6 = record_time float32 BE.
|
||||
anchor = buf.find(b"\xbe\x80\x00\x00\x00\x00", 0x800, 0xA00)
|
||||
if anchor > 0:
|
||||
sr_bytes = buf[anchor - 6 : anchor - 4]
|
||||
if len(sr_bytes) == 2:
|
||||
sr = int.from_bytes(sr_bytes, "big")
|
||||
if sr in (256, 512, 1024, 2048, 4096):
|
||||
md.sample_rate = sr
|
||||
rt_bytes = buf[anchor + 6 : anchor + 10]
|
||||
if len(rt_bytes) == 4:
|
||||
try:
|
||||
rt = struct.unpack(">f", rt_bytes)[0]
|
||||
if 0.1 <= rt <= 600.0:
|
||||
md.record_time_sec = float(rt)
|
||||
except struct.error:
|
||||
pass
|
||||
rec = _settings_record(buf)
|
||||
if rec is not None:
|
||||
md.sample_rate = int.from_bytes(buf[rec + 6 : rec + 8], "big")
|
||||
if not histogram:
|
||||
md.pre_trigger_sec = -struct.unpack(">f", buf[rec + 12 : rec + 16])[0] + 0.0
|
||||
md.record_time_sec = float(struct.unpack(">f", buf[rec + 18 : rec + 22])[0])
|
||||
md.event_datetime = _decode_8byte_timestamp(buf, rec + _EVENT_TIME_AFTER_SETTINGS)
|
||||
|
||||
# Event timestamp: 8 bytes. Position differs between IDFW (0x97A) and
|
||||
# IDFH (0x9F8); scan a small range and accept the first valid decode.
|
||||
for off in (0x97A, 0x9F8):
|
||||
ts = _decode_8byte_timestamp(buf, off)
|
||||
if ts is not None:
|
||||
md.event_datetime = ts
|
||||
break
|
||||
# Event timestamp fallback: the two positions seen before the record was
|
||||
# known (record + 0x34 in its two commonest places).
|
||||
if md.event_datetime is None:
|
||||
for off in (0x97A, 0x9F8):
|
||||
ts = _decode_8byte_timestamp(buf, off)
|
||||
if ts is not None:
|
||||
md.event_datetime = ts
|
||||
break
|
||||
|
||||
for n, name in enumerate(_NOTE_FIELDS):
|
||||
value_at = _NOTES_AT + n * _NOTE_STRIDE + _NOTE_LABEL_LEN
|
||||
setattr(md, name, _read_ascii_z(buf, value_at, maxlen=_NOTE_VALUE_LEN))
|
||||
|
||||
# Calibration date: day, month, year_be at 0x194-0x197.
|
||||
if len(buf) > 0x197:
|
||||
@@ -251,6 +293,17 @@ def extract_binary_metadata(buf: bytes) -> IdfBinaryMetadata:
|
||||
return md
|
||||
|
||||
|
||||
def _notes_and_pretrigger(md: IdfBinaryMetadata) -> dict:
|
||||
"""The file's own pre-trigger and notes, for an IdfReport built with no .txt."""
|
||||
return {"pre_trigger_sec": md.pre_trigger_sec, "project": md.project,
|
||||
"client": md.client, "operator": md.operator, "notes": md.notes}
|
||||
|
||||
|
||||
def _project_info(md: IdfBinaryMetadata) -> IdfProjectInfo:
|
||||
return IdfProjectInfo(project=md.project, client=md.client,
|
||||
operator=md.operator, notes=md.notes)
|
||||
|
||||
|
||||
# ─── Sample decoder + unit conversion ───────────────────────────────────────
|
||||
|
||||
|
||||
@@ -615,7 +668,7 @@ def read_idf_file(
|
||||
raise ValueError(f"{p.name}: unknown IDF signature {sig_prefix.hex()}")
|
||||
|
||||
is_histogram = p.suffix.upper() == ".IDFH"
|
||||
md = extract_binary_metadata(buf)
|
||||
md = extract_binary_metadata(buf, histogram=is_histogram)
|
||||
|
||||
if is_histogram:
|
||||
intervals = decode_idfh_body(buf)
|
||||
@@ -642,6 +695,7 @@ def read_idf_file(
|
||||
filename=p.name,
|
||||
sample_rate=md.sample_rate,
|
||||
record_time_sec=md.record_time_sec,
|
||||
**_notes_and_pretrigger(md),
|
||||
)
|
||||
peaks = IdfPeaks(
|
||||
transverse_ips=peak_tran,
|
||||
@@ -658,6 +712,8 @@ def read_idf_file(
|
||||
filename=p.name,
|
||||
sample_rate=md.sample_rate,
|
||||
record_time_sec=md.record_time_sec,
|
||||
pre_trigger_sec=md.pre_trigger_sec,
|
||||
project_info=_project_info(md),
|
||||
peaks=peaks,
|
||||
report=rep,
|
||||
)
|
||||
@@ -681,6 +737,7 @@ def read_idf_file(
|
||||
filename=p.name,
|
||||
sample_rate=md.sample_rate,
|
||||
record_time_sec=md.record_time_sec,
|
||||
**_notes_and_pretrigger(md),
|
||||
)
|
||||
|
||||
def _peak_ips(ch: str) -> float:
|
||||
@@ -711,6 +768,8 @@ def read_idf_file(
|
||||
filename=p.name,
|
||||
sample_rate=md.sample_rate,
|
||||
record_time_sec=md.record_time_sec,
|
||||
pre_trigger_sec=md.pre_trigger_sec,
|
||||
project_info=_project_info(md),
|
||||
peaks=peaks,
|
||||
report=rep,
|
||||
)
|
||||
|
||||
@@ -402,6 +402,10 @@ class IdfEvent:
|
||||
project_info=pi,
|
||||
record_type=self.kind,
|
||||
rectime_seconds=self.record_time_sec,
|
||||
# The .h5 stores the pre-trigger in samples; it was 0 for every
|
||||
# Micromate event, report or not (#45).
|
||||
pretrig_samples=(round(self.pre_trigger_sec * self.sample_rate)
|
||||
if self.pre_trigger_sec is not None and self.sample_rate else None),
|
||||
)
|
||||
ev._waveform_key = waveform_key
|
||||
return ev
|
||||
|
||||
Reference in new issue
Block a user