fix(decode): a paired report's event time is read in ISO form and survives a backfill
The event-time fix makes a Blastware report's Event Time + Event Date
authoritative over the binary, which gives the trigger only to the second.
On prod's own data it never applied (measured on the 2026-09-30 store
snapshot, all 81 report-paired Series-3 waveforms):
- every one of those reports writes Event Date as ISO ('2026-08-14');
_parse_event_date read only 'August 14, 2026', so event_datetime was None
(Brian's own laptop writes the long form -- it follows the PC's settings);
- the sidecar's bw_report block never stored the event time, and
apply_bw_report_dict_to_event -- the backfill's overlay -- never set it.
The binary's trigger matched Blastware's printed time on 66 of the 81 and was
a second late on 15; 8 of those 15 hold the right time in prod today, so the
release backfill would have made them a second worse.
- _parse_event_date accepts ISO dates too (81/81 now parse to the time
Blastware printed; 0/81 before).
- _bw_report_to_dict stores event_datetime (additive key).
- apply_bw_report_dict_to_event applies it, as apply_report_to_event does.
Prod's sidecars were written by the old parser, so the deploy runs
backfill_sidecars.py with --reparse-txt to re-read the saved .TXTs.
Tests: both date forms, the round trip through the saved block, a block with
no time, and the backfill (--force --reparse-txt) on a prod-shaped row.
Suite: only the 16 known fixture failures.
Codec-library change (report parser): TOOL_VERSION to be bumped at release.
No schema or DB migration.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MUHR251NLznsFvPSrfegqL
This commit is contained in:
1 parent
847900baeb
commit
abb671a440
3 files changed
+120
-3
No files matched your search
@@ -280,8 +280,15 @@ _MONTHS = {
|
||||
|
||||
|
||||
def _parse_event_date(s: str) -> Optional[datetime.date]:
|
||||
"""Parse "April 23, 2026" or "May 8, 2026" → date."""
|
||||
"""Parse "April 23, 2026", "May 8, 2026" or "2026-08-14" → date.
|
||||
|
||||
Blastware writes whichever the PC's settings say: every one of the 81
|
||||
report-paired waveforms in prod's store (2026-09-30 snapshot) is ISO, and
|
||||
reading only the long form left their event time unread."""
|
||||
s = s.strip()
|
||||
iso = _parse_iso_date(s)
|
||||
if iso is not None:
|
||||
return iso
|
||||
parts = s.replace(",", " ").split()
|
||||
if len(parts) < 3:
|
||||
return None
|
||||
@@ -297,8 +304,8 @@ def _parse_event_date(s: str) -> Optional[datetime.date]:
|
||||
|
||||
def _parse_iso_date(s: str) -> Optional[datetime.date]:
|
||||
"""Parse "2026-05-16" → date. Histograms use ISO format for their
|
||||
Start Date / Stop Date / Peak Date fields; waveforms use the
|
||||
"May 8, 2026" long form which `_parse_event_date` handles."""
|
||||
Start Date / Stop Date / Peak Date fields; a waveform's Event Date may be
|
||||
either form (see `_parse_event_date`)."""
|
||||
s = s.strip()
|
||||
try:
|
||||
return datetime.date.fromisoformat(s)
|
||||
|
||||
@@ -157,6 +157,9 @@ def _bw_report_to_dict(report: BwAsciiReport) -> dict:
|
||||
"available": True,
|
||||
"event_type": report.event_type,
|
||||
"version": report.version,
|
||||
# Blastware's exact trigger, so a backfill can re-apply it
|
||||
# (apply_bw_report_dict_to_event); absent from sidecars before 0.32.0.
|
||||
"event_datetime": report.event_datetime.isoformat() if report.event_datetime else None,
|
||||
"trigger": {
|
||||
"channel": report.trigger_channel,
|
||||
"geo_level_ips": report.geo_trigger_level_ips,
|
||||
@@ -371,6 +374,20 @@ def apply_bw_report_dict_to_event(event: Event, bw_report: dict) -> None:
|
||||
rt = rec.get("record_time_s")
|
||||
if rt is not None:
|
||||
event.rectime_seconds = rt
|
||||
# As apply_report_to_event: Blastware's exact trigger beats the binary's,
|
||||
# which is a second late on 15 of prod's 81 report-paired waveforms.
|
||||
edt = bw_report.get("event_datetime")
|
||||
if edt:
|
||||
try:
|
||||
t = datetime.datetime.fromisoformat(edt)
|
||||
except (TypeError, ValueError):
|
||||
t = None
|
||||
if t is not None:
|
||||
event.timestamp = Timestamp(
|
||||
raw=b"", flag=0x10,
|
||||
year=t.year, unknown_byte=0, month=t.month, day=t.day,
|
||||
hour=t.hour, minute=t.minute, second=t.second,
|
||||
)
|
||||
|
||||
|
||||
def _project_info_to_dict(pi: Optional[ProjectInfo]) -> dict:
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
"""A paired Blastware report's event time must win, in every date format and through a backfill.
|
||||
|
||||
The event-time fix makes a report's Event Time + Event Date authoritative (the
|
||||
binary gives the trigger only to within a second). On prod's own data it never
|
||||
applied (measured on the 2026-09-30 store snapshot, 81 report-paired waveforms):
|
||||
|
||||
* every one of those reports writes the date as ISO ("2026-08-14") -- the
|
||||
parser read only "August 14, 2026", so event_datetime was always None;
|
||||
* the sidecar's bw_report block never stored the event time, so a backfill had
|
||||
nothing to re-apply -- and its overlay ignored the time anyway.
|
||||
|
||||
The binary's trigger matched Blastware's event time on 66 of the 81 and was one
|
||||
second late on 15.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from minimateplus.bw_ascii_report import parse_report # noqa: E402
|
||||
from minimateplus.event_file_io import (_bw_report_to_dict, # noqa: E402
|
||||
apply_bw_report_dict_to_event, read_blastware_file)
|
||||
from minimateplus.models import Event # noqa: E402
|
||||
from sfm.database import SeismoDb # noqa: E402
|
||||
from sfm.waveform_store import WaveformStore # noqa: E402
|
||||
|
||||
FIX = Path(__file__).parent / "fixtures"
|
||||
WAVEFORM = FIX / "fft-oracle-2026-09-14" / "N844LQHB.ZT0W" # the binary's trigger: 10:33:29
|
||||
REPORT_TIME = datetime.datetime(2026, 8, 25, 10, 33, 28) # a report a second earlier
|
||||
|
||||
|
||||
def _report(date_line: str) -> str:
|
||||
return ('"Event Type : Full Waveform"\n"Serial Number : BE12844"\n'
|
||||
'"Event Time : 10:33:28"\n' + date_line + '\n"Record Time : 3.0 sec."\n')
|
||||
|
||||
|
||||
def _tuple(ts):
|
||||
return datetime.datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second)
|
||||
|
||||
|
||||
def test_an_iso_dated_report_gives_its_event_time():
|
||||
# The form all 81 of prod's saved waveform reports use.
|
||||
assert parse_report(_report('"Event Date : 2026-08-25"')).event_datetime == REPORT_TIME
|
||||
|
||||
|
||||
def test_a_long_form_dated_report_still_gives_its_event_time():
|
||||
assert parse_report(_report('"Event Date : August 25, 2026"')).event_datetime == REPORT_TIME
|
||||
|
||||
|
||||
def test_the_saved_report_block_carries_the_event_time_back_onto_the_event():
|
||||
block = _bw_report_to_dict(parse_report(_report('"Event Date : 2026-08-25"')))
|
||||
ev = read_blastware_file(WAVEFORM)
|
||||
assert _tuple(ev.timestamp) == datetime.datetime(2026, 8, 25, 10, 33, 29) # the binary's
|
||||
apply_bw_report_dict_to_event(ev, json.loads(json.dumps(block))) # as a sidecar holds it
|
||||
assert _tuple(ev.timestamp) == REPORT_TIME
|
||||
|
||||
|
||||
def test_a_saved_report_block_without_an_event_time_leaves_the_time_alone():
|
||||
ev = Event(index=0)
|
||||
apply_bw_report_dict_to_event(ev, {"available": True, "peaks": {}})
|
||||
assert ev.timestamp is None
|
||||
|
||||
|
||||
def test_the_backfill_with_reparse_puts_a_v031_row_on_its_reports_time(tmp_path):
|
||||
"""prod's shape: a session-start row, a saved ISO-dated .TXT, a sidecar whose
|
||||
report block has no event time (written by the old parser)."""
|
||||
import scripts.backfill_sidecars as backfill
|
||||
|
||||
store, db = WaveformStore(tmp_path / "waveforms"), SeismoDb(tmp_path / "seismo_relay.db")
|
||||
ev, rec = store.save_imported_bw(WAVEFORM.read_bytes(), source_path=Path(WAVEFORM.name),
|
||||
bw_report_text=_report('"Event Date : 2026-08-25"'))
|
||||
db.insert_events([ev], serial=rec["serial"], device_family="series3",
|
||||
waveform_records={ev._waveform_key.hex(): rec})
|
||||
with sqlite3.connect(str(db.db_path)) as c:
|
||||
c.execute("UPDATE events SET timestamp = '2026-08-25T06:00:13'")
|
||||
car = next((tmp_path / "waveforms").rglob("*.sfm.json"))
|
||||
d = json.loads(car.read_text())
|
||||
assert d["source"].get("txt_filename") # the report is on disk
|
||||
d["bw_report"].pop("event_datetime", None) # as the old parser left it
|
||||
car.write_text(json.dumps(d))
|
||||
|
||||
assert backfill.main(["--store-root", str(tmp_path / "waveforms"),
|
||||
"--db-path", str(db.db_path), "--force", "--reparse-txt"]) == 0
|
||||
|
||||
with sqlite3.connect(str(db.db_path)) as c:
|
||||
assert c.execute("SELECT timestamp FROM events").fetchall() == [(REPORT_TIME.isoformat(),)]
|
||||
assert json.loads(car.read_text())["event"]["timestamp"] == REPORT_TIME.isoformat()
|
||||
Reference in new issue
Block a user