diff --git a/minimateplus/event_file_io.py b/minimateplus/event_file_io.py index f86311a..e692e68 100644 --- a/minimateplus/event_file_io.py +++ b/minimateplus/event_file_io.py @@ -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 / diff --git a/tests/test_bw_event_record_time.py b/tests/test_bw_event_record_time.py index f171ee9..6e0a9ec 100644 --- a/tests/test_bw_event_record_time.py +++ b/tests/test_bw_event_record_time.py @@ -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(): diff --git a/tests/test_bw_file_fields.py b/tests/test_bw_file_fields.py index 401182d..3c2b94a 100644 --- a/tests/test_bw_file_fields.py +++ b/tests/test_bw_file_fields.py @@ -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) diff --git a/tests/test_bw_filename_time.py b/tests/test_bw_filename_time.py index 70a1ddd..0a6312a 100644 --- a/tests/test_bw_filename_time.py +++ b/tests/test_bw_filename_time.py @@ -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): diff --git a/tests/test_event_timestamp.py b/tests/test_event_timestamp.py index 0f08be6..3086534 100644 --- a/tests/test_event_timestamp.py +++ b/tests/test_event_timestamp.py @@ -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():