merge: dev into feat/series4-sfm-parity -- the decode checker's file-only fields (job number, codec by STRT[18], last-26-byte footer)
Brings be5bab4 and its history from the decode checker's branch: a Blastware
file's job number; decode_blastware_bytes choosing the codec by STRT[18] and the
histogram layout check by peak <= PVS <= sqrt(3) peak; the footer as the file's
last 26 bytes (no day-late histograms); docs (reference Appendix D.6, the
file-only status).
Conflicts, both kept whole:
- CLAUDE.md: this branch's 'Narrowed 2026-10-26' 5A block stays, followed by
dev's 'Open, not blocking' line (the 8x sensitive-range item struck out --
resolved by the file's range).
- docs/instantel_protocol_reference.md (CRLF, byte-safe): the change-log rows in
date order -- dev's 2026-11-03 and 2026-11-02, this branch's 2026-11-03
(call home), 2026-10-29 and 2026-10-27 (#46, this branch only).
Suite on the merged tree: 1399 passed, the 16 known failures.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
commit
cbce00f8e1
27 files changed
+1797
-84
No files matched your search
@@ -36,7 +36,7 @@ class SfmSide:
|
||||
event: Event
|
||||
values: dict[str, object] # reference tag -> SFM's value; absent or None = blank
|
||||
notes: list[str] # non-empty user-note slots, in order
|
||||
job_number: Optional[str] # None until the decoder reads it
|
||||
job_number: Optional[str] # only when the file says Blastware shows it
|
||||
channels: list[str] # channels with decoded data, in Tran/Vert/Long/MicL order
|
||||
counts: dict[str, list[int]] # waveform samples or histogram interval peaks
|
||||
geo_ips_per_count: float = GEO_IPS_PER_COUNT # at the event's range, as SFM read it
|
||||
@@ -78,7 +78,7 @@ def sfm_side(raw: bytes) -> SfmSide:
|
||||
values=values,
|
||||
notes=[v for v in (pi.project, pi.client, pi.operator, pi.sensor_location) if v]
|
||||
if pi is not None else [],
|
||||
job_number=None,
|
||||
job_number=str(pi.job_number) if pi is not None and pi.job_number_enabled else None,
|
||||
channels=[ch for ch in _CHANNELS if counts[ch]],
|
||||
counts=counts,
|
||||
geo_ips_per_count=_GEO_FS_BY_RANGE.get(ev.geo_range, _GEO_NORMAL_FS_INS) / _GEO_INT16_FS,
|
||||
@@ -289,6 +289,9 @@ def compare(sfm: SfmSide, ref: XmlReport, carveouts: Sequence["Carveout"] = ())
|
||||
if sfm.job_number is None else
|
||||
FieldResult("JobNumber", "match" if _same_text(sfm.job_number, ref.job_number)
|
||||
else "mismatch", ref.job_number, sfm.job_number))
|
||||
elif sfm.job_number is not None:
|
||||
# SFM says the job number is shown; Blastware printed none.
|
||||
out.append(FieldResult("JobNumber", "mismatch", None, sfm.job_number))
|
||||
out.append(FieldResult("Channels", "match" if ref.channels == sfm.channels else "mismatch",
|
||||
" ".join(ref.channels), " ".join(sfm.channels)))
|
||||
if ref.sample_rows is not None:
|
||||
|
||||
@@ -86,4 +86,24 @@ SATURATION = Carveout(
|
||||
applies=_saturation,
|
||||
)
|
||||
|
||||
CARVEOUTS: tuple[Carveout, ...] = (SATURATION,)
|
||||
def _interval_count_is_duration(sfm: SfmSide, ref: XmlReport, res: FieldResult) -> bool:
|
||||
"""Blastware's count exceeds the intervals in its own table, and SFM decoded
|
||||
exactly the table's."""
|
||||
p = _printed_number(res.bw or "")
|
||||
n = sfm.values.get("NumberofIntervals")
|
||||
rows = len(ref.intervals or [])
|
||||
return p is not None and n is not None and rows == n and p[0] > n
|
||||
|
||||
|
||||
INTERVAL_COUNT_IS_DURATION = Carveout(
|
||||
id="interval-count-is-duration",
|
||||
fields=("NumberofIntervals",),
|
||||
evidence=("Blastware's Number of Intervals is (stop - start) / interval from the file's "
|
||||
"footer times -- exact on T193KSWP.FF0H (2,880), M462KSWR.1R0H (1,650) and "
|
||||
"T193KY0A.740H (6,390). Its own interval table lists the intervals the file "
|
||||
"holds: one block (30 at 2 s) fewer when the unit never wrote the last minute. "
|
||||
"2026-11-02."),
|
||||
applies=_interval_count_is_duration,
|
||||
)
|
||||
|
||||
CARVEOUTS: tuple[Carveout, ...] = (SATURATION, INTERVAL_COUNT_IS_DURATION)
|
||||
@@ -996,8 +996,29 @@ def _setup_notes(raw: bytes) -> ProjectInfo:
|
||||
text = [p.decode("ascii", errors="replace").strip() or None for p in parts]
|
||||
text += [None] * (2 + 2 * _NOTE_SLOTS - len(text))
|
||||
project, client, operator, location = text[2:2 + 2 * _NOTE_SLOTS:2]
|
||||
enabled, number = _setup_job_number(raw, k)
|
||||
return ProjectInfo(setup_name=text[0], project=project, client=client,
|
||||
operator=operator, sensor_location=location)
|
||||
operator=operator, sensor_location=location,
|
||||
job_number_enabled=enabled, job_number=number)
|
||||
|
||||
|
||||
# The job number, in Series IV's 0x1A layout ([39] report flags, [40:42] the
|
||||
# number): flags at k - 9, bit 0x02 = shown; the number, uint16 BE, at k - 8,
|
||||
# kept while switched off. The flag matches Blastware's JobNumber on 2,997 /
|
||||
# 2,997 DL2 events that print one and 66,986 / 66,986 that don't, and every
|
||||
# printed value matches (all are 1; measured 2026-11-02).
|
||||
_REPORT_FLAGS_BEFORE_NAME = 9
|
||||
_JOB_NUMBER_BEFORE_NAME = 8
|
||||
_JOB_NUMBER_ON = 0x02
|
||||
|
||||
|
||||
def _setup_job_number(raw: bytes, k: int) -> tuple[Optional[bool], Optional[int]]:
|
||||
"""(shown, number) from the setup block whose name starts at k."""
|
||||
if k < _REPORT_FLAGS_BEFORE_NAME:
|
||||
return None, None
|
||||
flags = raw[k - _REPORT_FLAGS_BEFORE_NAME]
|
||||
j = k - _JOB_NUMBER_BEFORE_NAME
|
||||
return bool(flags & _JOB_NUMBER_ON), int.from_bytes(raw[j:j + 2], "big")
|
||||
|
||||
|
||||
# The geophone range sits in the setup's channel blocks, which follow the
|
||||
@@ -1077,22 +1098,34 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
|
||||
# Footer: locate the 0e 08 marker, validating the year is in a sane range.
|
||||
body_start = _bw._WAVEFORM_HEADER_SIZE + 21
|
||||
# The 0e 08 + plausible-year footer signature can occur inside the sample
|
||||
# stream. Collect every candidate and prefer the first whose body yields a
|
||||
# waveform record chain terminating on the 0x06 marker; fall back to the
|
||||
# first candidate otherwise. Blast radius measured 2026-08-25: changes the
|
||||
# chosen footer on exactly 1 of 1,388 series-3 waveform files
|
||||
# (BE17353/S353L4O5.OX0W, false positive at 3800, real footer at 8576).
|
||||
footer_candidates = []
|
||||
pos = body_start
|
||||
while True:
|
||||
pos = raw.find(b"\x0e\x08", pos)
|
||||
if pos < 0 or pos + 26 > len(raw):
|
||||
break
|
||||
yr = (raw[pos + 4] << 8) | raw[pos + 5]
|
||||
if 2015 <= yr <= 2050:
|
||||
footer_candidates.append(pos)
|
||||
pos += 1
|
||||
# The footer is the file's last 26 bytes whenever they are one -- 0e 08 and
|
||||
# two valid times: 70,041 / 70,041 DL2 files (measured 2026-11-03). Taking
|
||||
# the first "0e 08 + plausible year" instead found, on 14 of them, an
|
||||
# earlier record ~1.1 KB before the end holding the stop time and zeros: a
|
||||
# histogram then started at its own stop (a day late on BE9558's daily
|
||||
# histograms) and a waveform lost its stop, so its trigger time too.
|
||||
#
|
||||
# Otherwise the search below stands. The signature can occur inside the
|
||||
# sample stream, so it prefers the first candidate whose body yields a
|
||||
# waveform record chain terminating on the 0x06 marker, then the first
|
||||
# candidate (2026-08-25: BE17353/S353L4O5.OX0W, false positive at 3800,
|
||||
# real footer at 8576).
|
||||
end = len(raw) - 26
|
||||
if (end >= body_start and raw[end:end + 2] == b"\x0e\x08"
|
||||
and _bw._decode_ts_be(raw[end + 2:end + 10]) is not None
|
||||
and _bw._decode_ts_be(raw[end + 10:end + 18]) is not None):
|
||||
footer_candidates = [end]
|
||||
else:
|
||||
footer_candidates = []
|
||||
pos = body_start
|
||||
while True:
|
||||
pos = raw.find(b"\x0e\x08", pos)
|
||||
if pos < 0 or pos + 26 > len(raw):
|
||||
break
|
||||
yr = (raw[pos + 4] << 8) | raw[pos + 5]
|
||||
if 2015 <= yr <= 2050:
|
||||
footer_candidates.append(pos)
|
||||
pos += 1
|
||||
|
||||
footer_pos = -1
|
||||
for cand in footer_candidates:
|
||||
@@ -1149,18 +1182,37 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
# mode, synthetic test input), fall back to empty channels — the
|
||||
# rest of the event (timestamp, waveform_key, project strings) is
|
||||
# still recoverable and useful.
|
||||
decoded = decode_waveform_v2(body)
|
||||
# Discriminator for the timestamp logic below: a waveform (trigger) event
|
||||
# vs a histogram window. Keyed on the codec, not the filename — the
|
||||
# save_imported_bw path passes a tmp ".bw" name whose extension lies.
|
||||
is_waveform_body = decoded is not None
|
||||
if decoded is None:
|
||||
#
|
||||
# The record type comes from the bytes: STRT[18] (#41), 0x46 / 0x0A on
|
||||
# 70,041 / 70,041 DL2 files. It picks the codec. Trying the waveform codec
|
||||
# first handed it BE9440 / BE9557 / BE17354's 2018 5 s histograms, which it
|
||||
# accepted and returned ~5x too many "samples" for. Only an unknown type
|
||||
# falls back to the old trial order. The filename's last letter is only a
|
||||
# cross-check -- SFM's own downloads have no name, and a manual download's
|
||||
# (".QZ0") carries no type at all.
|
||||
type_byte = strt_raw[18]
|
||||
record_type = _RECORD_TYPE_BY_STRT.get(type_byte)
|
||||
problems = []
|
||||
if record_type is None:
|
||||
problems.append(f"unknown record type byte 0x{type_byte:02x} at STRT[18]")
|
||||
if record_type == "Histogram":
|
||||
decoded = decode_histogram_body(body)
|
||||
is_waveform_body = False
|
||||
elif record_type == "Waveform":
|
||||
decoded = decode_waveform_v2(body)
|
||||
is_waveform_body = True
|
||||
else:
|
||||
decoded = decode_waveform_v2(body)
|
||||
is_waveform_body = decoded is not None
|
||||
if decoded is None:
|
||||
decoded = decode_histogram_body(body)
|
||||
if decoded is None:
|
||||
log.warning(
|
||||
"%s: body codec failed to decode (body starts %s) — "
|
||||
"raw_samples will be empty", label, body[:8].hex(" "),
|
||||
)
|
||||
if record_type is not None:
|
||||
problems.append(f"the {record_type.lower()} codec could not decode the body")
|
||||
samples = {"Tran": [], "Vert": [], "Long": [], "MicL": []}
|
||||
else:
|
||||
samples = decoded_to_adc_counts(decoded)
|
||||
@@ -1169,13 +1221,8 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
ev = Event(index=-1)
|
||||
if strt_fields.get("waveform_key"):
|
||||
ev._waveform_key = bytes.fromhex(strt_fields["waveform_key"])
|
||||
# The record type comes from the bytes: STRT[18] (#41). The filename's
|
||||
# last letter is only a cross-check -- SFM's own downloads have no name,
|
||||
# and a manual download's (".QZ0") carries no type at all.
|
||||
type_byte = strt_raw[18]
|
||||
ev.record_type = _RECORD_TYPE_BY_STRT.get(type_byte)
|
||||
if ev.record_type is None:
|
||||
ev.decode_problems.append(f"unknown record type byte 0x{type_byte:02x} at STRT[18]")
|
||||
ev.record_type = record_type
|
||||
ev.decode_problems.extend(problems)
|
||||
named_type = derive_record_type_from_filename(name, default=None) if name else None
|
||||
if named_type is not None and ev.record_type is not None and named_type != ev.record_type:
|
||||
log.warning("%s: record type %s from the file disagrees with the filename's %s; "
|
||||
|
||||
@@ -439,16 +439,34 @@ def _stride(rec: int, n: int) -> int:
|
||||
|
||||
|
||||
def _records_plausible(body: bytes, off: int, rec: int, m: int) -> bool:
|
||||
"""Every record's zero word is 0000 -- what tells a 16-byte layout from a
|
||||
20-byte one when both strides happen to fit (492 = 12 + 16x30 = 12 + 20x24)."""
|
||||
"""Every record's last 4 bytes are its vector sum squared, consistent with
|
||||
its channel peaks: at least the largest peak, at most sqrt(3) times it (one
|
||||
unit of rounding either way). That is what tells a 16-byte layout from a
|
||||
20-byte one when both strides happen to fit (492 = 12 + 16x30 = 12 + 20x24);
|
||||
misread, those bytes are other fields. Holds on 34,502,955 / 34,502,955 DL2
|
||||
sub-minute intervals (2026-11-02).
|
||||
|
||||
⚠ RETRACTED 2026-11-02: "every record's zero word is 0000". It is the high
|
||||
word of the PVS², non-zero on any interval above 1.28 in/s, so a short
|
||||
histogram with one loud interval was not recognised at all (T193KR5M.X90H,
|
||||
K440HA3C.JM0H decoded to nothing)."""
|
||||
z = rec - 4
|
||||
for k in range(m):
|
||||
q = off + _MULTI_HEADER_LEN + rec * k
|
||||
if q + rec > len(body) or body[q + z] or body[q + z + 1]:
|
||||
if q + rec > len(body):
|
||||
return False
|
||||
peak = max((body[q + i] << 8) | body[q + i + 1] for i in (0, 4, 8))
|
||||
if not _pvs_consistent(peak, int.from_bytes(body[q + z:q + z + 4], "big")):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _pvs_consistent(peak: int, pvs_sq: int) -> bool:
|
||||
"""A record's vector sum squared against its largest geo peak, one unit of
|
||||
rounding either way: peak <= PVS <= sqrt(3) * peak."""
|
||||
return max(peak - 1, 0) ** 2 <= pvs_sq <= 3 * (peak + 1) ** 2
|
||||
|
||||
|
||||
def detect_multi_interval_layout(body: bytes) -> Optional[Tuple[int, int]]:
|
||||
"""``(record_len, per_block)`` of a multi-interval body, or None.
|
||||
|
||||
@@ -543,8 +561,8 @@ def walk_multi_interval_blocks(body: bytes,
|
||||
recs = [_decode_record(body, off + _MULTI_HEADER_LEN + rec * k, rec, off)
|
||||
for k in range(max(m, 0))]
|
||||
for k, r in enumerate(recs):
|
||||
if (r["meta_var"][:2] != b"\x00\x00"
|
||||
or max(r["t_peak"], r["v_peak"], r["l_peak"]) > _GEO_MAX_COUNTS):
|
||||
peak = max(r["t_peak"], r["v_peak"], r["l_peak"])
|
||||
if not _pvs_consistent(peak, r["pvs_sq"]) or peak > _GEO_MAX_COUNTS:
|
||||
del recs[k:]
|
||||
break
|
||||
out.extend(recs)
|
||||
|
||||
@@ -379,6 +379,8 @@ class ProjectInfo:
|
||||
operator: Optional[str] = None # operator / user name
|
||||
sensor_location: Optional[str] = None # sensor location string
|
||||
notes: Optional[str] = None # extended notes
|
||||
job_number_enabled: Optional[bool] = None # Blastware prints the job number
|
||||
job_number: Optional[int] = None # kept while switched off
|
||||
|
||||
|
||||
# ── Compliance Config ──────────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user