fix(decode): a histogram's start and stop come from its own event record (#51)
The footer's start runs a second early on 97% of histograms. Each
histogram carries an event record like a waveform's -- lead 00 28 05 00,
source 04 00, marker fe f5, then start and stop -- and it holds
Blastware's Histogram Start and Stop on 63,490 / 63,490 DL2 histograms.
On all 63,500 there is exactly one, its start 0 or 1 s after the
footer's and its stop 0 or 1 s before; that is the validation rule.
Without a validated record the footer's start stands, as a decode
problem.
Ships with the unit + time + type key (ea2dfc2), without which a
waveform and its histogram at the same second would collide. Three
tests that pinned the old footer start (one said 'for now', pointing at
#51) now pin the record's, which equals the file's Blastware name.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9
This commit is contained in:
1 parent
e24e77bfd0
commit
a9bb9f08c0
5 files changed
+98
-18
No files matched your search
@@ -939,6 +939,41 @@ def _embedded_event_record(
|
||||
return None, (f"{rejected} candidate(s), none valid" if rejected else "no candidate"), None
|
||||
|
||||
|
||||
# A histogram's event record: the same lead, the source 0x0400, marker fe f5,
|
||||
# then its start and stop -- Blastware's Histogram Start / Stop on 63,490 /
|
||||
# 63,490 DL2 histograms. On all 63,500 its start is 0 or 1 s after the footer's
|
||||
# start and its stop 0 or 1 s before the footer's stop (2026-11-03).
|
||||
_HISTOGRAM_RECORD_MARKER = b"\xfe\xf5"
|
||||
_HISTOGRAM_RECORD_SOURCE = b"\x04\x00"
|
||||
|
||||
|
||||
def _histogram_event_record(
|
||||
raw: bytes, end: int, start: Optional[datetime.datetime],
|
||||
stop: Optional[datetime.datetime],
|
||||
) -> tuple[Optional[datetime.datetime], Optional[datetime.datetime], str]:
|
||||
"""(start, stop) from a histogram's own event record, or (None, None, why).
|
||||
Exactly one record may validate against the footer's start and stop."""
|
||||
if start is None or stop is None:
|
||||
return None, None, "the footer has no start and stop to check one against"
|
||||
found, rejected = [], 0
|
||||
i = raw.find(_HISTOGRAM_RECORD_MARKER, 6)
|
||||
while 0 <= i and i + 18 <= end:
|
||||
a, b = _ts8(raw[i + 2:i + 10]), _ts8(raw[i + 10:i + 18])
|
||||
if (raw[i - 6:i - 2] == _EVENT_RECORD_LEAD and raw[i - 2:i] == _HISTOGRAM_RECORD_SOURCE
|
||||
and a is not None and b is not None and a <= b
|
||||
and 0 <= (a - start).total_seconds() <= 1
|
||||
and 0 <= (stop - b).total_seconds() <= 1):
|
||||
found.append((a, b))
|
||||
elif raw[i - 6:i - 2] == _EVENT_RECORD_LEAD:
|
||||
rejected += 1
|
||||
i = raw.find(_HISTOGRAM_RECORD_MARKER, i + 1)
|
||||
if len(found) == 1:
|
||||
return found[0][0], found[0][1], ""
|
||||
if found:
|
||||
return None, None, f"{len(found)} candidates validate"
|
||||
return None, None, (f"{rejected} candidate(s), none valid" if rejected else "no candidate")
|
||||
|
||||
|
||||
def _parse_record_time_seconds(raw: bytes) -> Optional[float]:
|
||||
"""The configured post-trigger record time in seconds, from the recording-
|
||||
setup config block, or None when absent / implausible."""
|
||||
@@ -1358,10 +1393,12 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
# embedded in the 5A stream (_embedded_event_record), so a waveform takes
|
||||
# it from there. Without a validated one the estimate stands, reported as a
|
||||
# decode problem -- never a filename's time, which is only a cross-check.
|
||||
# Histograms stay on ts1 although it runs a second early on 97% (their
|
||||
# record, marker fe f5, holds the exact start): in Blastware's timing a
|
||||
# waveform and its histogram share a second 209 times in prod's store, and
|
||||
# events is unique on (serial, timestamp) -- #51.
|
||||
# A histogram's footer start runs a second early on 97%. Its own event
|
||||
# record (marker fe f5) holds Blastware's exact start and stop, so it takes
|
||||
# them from there (#51); events are keyed unit + time + type, so a waveform
|
||||
# and its histogram may share a second.
|
||||
# ⚠ RETRACTED 2026-11-03: "histograms stay on ts1" -- that held only while
|
||||
# events were unique on (serial, timestamp).
|
||||
if is_waveform_body and ts2 is not None:
|
||||
_stop = datetime.datetime(ts2.year, ts2.month, ts2.day,
|
||||
ts2.hour, ts2.minute, ts2.second)
|
||||
@@ -1390,13 +1427,25 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
hour=_trig.hour, minute=_trig.minute, second=_trig.second,
|
||||
)
|
||||
elif ts1 is not None:
|
||||
_start, _hstop, _why = (_histogram_event_record(raw, footer_pos, ts1, ts2)
|
||||
if record_type == "Histogram" else (None, None, ""))
|
||||
if _start is not None:
|
||||
ev.timestamp_source = "event_record"
|
||||
ev.stop_time = _hstop
|
||||
else:
|
||||
_start = ts1
|
||||
ev.timestamp_source = "footer"
|
||||
if record_type == "Histogram":
|
||||
_problem = (f"no validated histogram event record ({_why}); the start is the "
|
||||
"footer's, a second early on 97% of measured files")
|
||||
ev.decode_problems.append(_problem)
|
||||
log.warning("%s: %s", label, _problem)
|
||||
ev.timestamp = Timestamp(
|
||||
raw=footer[2:10],
|
||||
flag=0x10,
|
||||
year=ts1.year, unknown_byte=0, month=ts1.month, day=ts1.day,
|
||||
hour=ts1.hour, minute=ts1.minute, second=ts1.second,
|
||||
year=_start.year, unknown_byte=0, month=_start.month, day=_start.day,
|
||||
hour=_start.hour, minute=_start.minute, second=_start.second,
|
||||
)
|
||||
ev.timestamp_source = "footer"
|
||||
|
||||
ev.project_info = _setup_notes(raw)
|
||||
ev.raw_samples = samples
|
||||
@@ -1419,7 +1468,8 @@ 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
|
||||
ev.stop_time = ts2
|
||||
if ev.stop_time is None: # a histogram's record stop, else the footer's
|
||||
ev.stop_time = ts2
|
||||
if hist_records:
|
||||
# A histogram stores each interval's vector sum (squared), and each
|
||||
# channel's half-period: Blastware's per-interval Geo PVS (771,354 /
|
||||
|
||||
@@ -141,10 +141,12 @@ def test_a_report_fills_in_when_the_record_is_missing():
|
||||
assert (_dt(ev.timestamp), ev.timestamp_source) == (TRIGGER, "report")
|
||||
|
||||
|
||||
def test_a_histogram_keeps_its_footer_start_for_now():
|
||||
# Its record (marker fe f5) also holds Blastware's start -- that change is #51.
|
||||
def test_a_histogram_takes_its_event_records_start():
|
||||
# #51: its record (marker fe f5) holds Blastware's start; the footer's
|
||||
# 19:04:50 is a second early.
|
||||
ev = read_blastware_file(HISTOGRAM)
|
||||
assert (_dt(ev.timestamp), ev.timestamp_source) == (datetime.datetime(2026, 5, 10, 19, 4, 50), "footer")
|
||||
assert (_dt(ev.timestamp), ev.timestamp_source) == (datetime.datetime(2026, 5, 10, 19, 4, 51),
|
||||
"event_record")
|
||||
|
||||
|
||||
def test_the_sidecar_keeps_the_instruments_time_and_says_where_it_came_from():
|
||||
|
||||
@@ -471,3 +471,27 @@ def test_the_h5_carries_the_interval_detail(tmp_path):
|
||||
assert int(f.attrs["histogram_interval_sec"]) == 60
|
||||
assert list(f["intervals/half_period/Vert"][:]) == ev.interval_half_periods["Vert"]
|
||||
assert abs(float(f["intervals/pvs_ips"][0]) - 2.644) <= 0.0005 + 1e-9
|
||||
|
||||
|
||||
# ── #51: a histogram's start and stop from its own event record ─────────────
|
||||
# The footer's start runs a second early on 97%. The histogram's event record
|
||||
# (lead 00 28 05 00, source 04 00, marker fe f5) holds Blastware's start and
|
||||
# stop: 63,490 / 63,490 DL2 histograms each (2026-11-03).
|
||||
|
||||
def test_a_histogram_starts_at_its_event_records_start():
|
||||
ev = read_blastware_file(_FIX / "decode-checker-10-29-26/T004JTRF.ME0H")
|
||||
t, s = ev.timestamp, ev.stop_time
|
||||
# Blastware: start 2022-12-28 12:49:26, stop 12:49:33
|
||||
assert (t.year, t.month, t.day, t.hour, t.minute, t.second) == (2022, 12, 28, 12, 49, 26)
|
||||
assert (s.hour, s.minute, s.second) == (12, 49, 33)
|
||||
assert ev.timestamp_source == "event_record"
|
||||
|
||||
|
||||
def test_a_histogram_without_its_record_falls_back_to_the_footer_and_says_so():
|
||||
raw = bytearray((_FIX / "decode-checker-10-29-26/T004JTRF.ME0H").read_bytes())
|
||||
i = raw.find(b"\xfe\xf5")
|
||||
raw[i] = 0x00 # break the record's marker
|
||||
ev = decode_blastware_bytes(bytes(raw), name=None)
|
||||
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)
|
||||
@@ -83,9 +83,12 @@ def test_a_waveform_read_under_another_name_keeps_its_time(tmp_path):
|
||||
datetime.datetime(2026, 10, 26, 14, 23, 42)
|
||||
|
||||
|
||||
def test_a_histogram_keeps_the_binarys_window_start():
|
||||
assert blastware_filename_time(HISTOGRAM.name) != datetime.datetime(2026, 5, 10, 19, 4, 50)
|
||||
assert _dt(read_blastware_file(HISTOGRAM).timestamp) == datetime.datetime(2026, 5, 10, 19, 4, 50)
|
||||
def test_a_histogram_now_agrees_with_its_filename():
|
||||
# #51: the start comes from the histogram's own event record, which is
|
||||
# Blastware's -- and so the filename's. The footer's window start (19:04:50)
|
||||
# was a second early.
|
||||
assert blastware_filename_time(HISTOGRAM.name) == datetime.datetime(2026, 5, 10, 19, 4, 51)
|
||||
assert _dt(read_blastware_file(HISTOGRAM).timestamp) == datetime.datetime(2026, 5, 10, 19, 4, 51)
|
||||
|
||||
|
||||
def test_an_import_no_longer_lands_a_waveform_on_its_histogram_twins_row(tmp_path):
|
||||
|
||||
@@ -22,7 +22,7 @@ from minimateplus.models import Event
|
||||
|
||||
FIX = Path(__file__).parent / "fixtures"
|
||||
WAVEFORM = FIX / "fft-oracle-2026-09-14" / "N844LQHB.ZT0W" # footer ts2 = 2026-08-25 10:33:32
|
||||
HISTOGRAM = FIX / "ts-fix" / "K441LKZU.C30H" # window start 2026-05-10 19:04:50
|
||||
HISTOGRAM = FIX / "ts-fix" / "K441LKZU.C30H" # start 2026-05-10 19:04:51 (#51)
|
||||
|
||||
|
||||
def _tuple(ts):
|
||||
@@ -39,9 +39,10 @@ def test_waveform_timestamp_is_exact_trigger_from_binary():
|
||||
|
||||
def test_histogram_timestamp_is_window_start_unchanged():
|
||||
ev = read_blastware_file(HISTOGRAM)
|
||||
# Histogram event time = the window start (ts1); must NOT get the waveform
|
||||
# ts2 treatment (that would land ~24 h off).
|
||||
assert _tuple(ev.timestamp) == (2026, 5, 10, 19, 4, 50), _tuple(ev.timestamp)
|
||||
# Histogram event time = the window start, from its own event record (#51;
|
||||
# the footer's ts1 is a second earlier); must NOT get the waveform ts2
|
||||
# treatment (that would land ~24 h off).
|
||||
assert _tuple(ev.timestamp) == (2026, 5, 10, 19, 4, 51), _tuple(ev.timestamp)
|
||||
|
||||
|
||||
def test_a_report_does_not_override_the_instruments_event_record():
|
||||
|
||||
Reference in new issue
Block a user