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:
serversdownandClaude Opus 5.5 committed 2026-11-03 21:33:32 -05:00
1 parent e24e77bfd0
commit a9bb9f08c0
5 files changed
+98 -18

No files matched your search

+58 -8
View File
@@ -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 /