feat(decode): a Blastware file's identity, battery and trigger settings
Group A of the file-only decode, every field matched against every DL2 report (2026-11-03): - Identity block before the setup name, what the live read gets from SUB FE: serial k-202, firmware k-147, DSP version k-138, calibration date k-132 + 'by' k-128, manufacturer k-94, model k-72, firmware minor k-193. Event.device_info (DeviceInfo gains calibration_date / calibrated_by). Serial, Version and Calibration 69,983 / 69,983. - Battery: uint16 hundredths of a volt at k-190 (Event.battery_volts). 69,729 exact; the other 254 are Blastware rounding half up. - Trigger source: the two bytes between the embedded event record's lead and fe 80, a bitmask (0x01 Tran 0x02 Vert 0x04 Long 0x08 MicL 0x200 Manual): 6,493 / 6,493. Event.triggered_by. - Trigger settings in Series IV's 0x1A layout: sources bitmask k-19, stop mode k-31 (0 = Fixed), each channel's float32 level at label+30 (geo in/s, mic psi), mic type code at MicL+4 (2 = Linear Weighting). Geo level 6,447 / 6,447, mic 4,403 / 4,403; Blastware prints a level when its channel is a trigger source and the event was not Manual. The mic level's dB uses 6895 Pa/psi (4,272 / 4,272 against 4,271 at 6894.757). - .h5 attrs for all of them (additive). - Checker: SFM's side reports the ten fields; the mic trigger level follows the report's dB/psi. Carve-out units-is-a-display-preference: dB or psi is Blastware's PC setting when it wrote the report -- DL2's reports switch fleet-wide in 61 clean runs by write time, and no byte of the file separates them. Two tests that pinned BatteryLevel / Calibration as blank now pin the match. 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
be5bab4787
commit
6c2eda5f63
14 files changed
+8384
-15
No files matched your search
@@ -25,7 +25,7 @@ import struct
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
from .models import Event, PeakValues, ProjectInfo, Timestamp
|
||||
from .models import DeviceInfo, Event, PeakValues, ProjectInfo, Timestamp
|
||||
from . import blastware_file as _bw # avoid circular reference at module load
|
||||
from .bw_ascii_report import BwAsciiReport
|
||||
from . import waveform_codec as _wc
|
||||
@@ -895,17 +895,22 @@ def _ts8(b: bytes) -> Optional[datetime.datetime]:
|
||||
def _embedded_event_record(
|
||||
raw: bytes, end: int, start: Optional[datetime.datetime],
|
||||
stop: Optional[datetime.datetime], record_time: Optional[float],
|
||||
) -> tuple[Optional[datetime.datetime], str]:
|
||||
"""The trigger time from the instrument's embedded event record, or (None, why).
|
||||
) -> tuple[Optional[datetime.datetime], str, Optional[int]]:
|
||||
"""The trigger time and trigger source from the instrument's embedded event
|
||||
record, or (None, why, None).
|
||||
|
||||
Every `fe 80` before `end` (the footer) is a candidate. One counts only if
|
||||
its lead bytes match, both its times are well formed, the footer's session
|
||||
start <= trigger <= its stop, its stop is the footer's stop or a second
|
||||
before it, and its length agrees with the record time. Exactly one may
|
||||
pass: none, or more than one, is reported rather than guessed between.
|
||||
|
||||
The two bytes between the lead and `fe 80` are the trigger source, a uint16
|
||||
BE bitmask: 0x01 Tran, 0x02 Vert, 0x04 Long, 0x08 MicL, 0x200 Manual --
|
||||
Blastware's Trigger on 6,493 / 6,493 DL2 waveforms (2026-11-03).
|
||||
"""
|
||||
if start is None or stop is None:
|
||||
return None, "the footer has no start and stop to check one against"
|
||||
return None, "the footer has no start and stop to check one against", None
|
||||
found, rejected = [], 0
|
||||
i = raw.find(_EVENT_RECORD_MARKER, 6)
|
||||
while 0 <= i and i + 18 <= end:
|
||||
@@ -921,15 +926,15 @@ def _embedded_event_record(
|
||||
<= _EVENT_RECORD_SLACK_S[1])
|
||||
)
|
||||
if valid:
|
||||
found.append(trig)
|
||||
found.append((trig, int.from_bytes(raw[i - 2:i], "big")))
|
||||
else:
|
||||
rejected += 1
|
||||
i = raw.find(_EVENT_RECORD_MARKER, i + 1)
|
||||
if len(found) == 1:
|
||||
return found[0], ""
|
||||
return found[0][0], "", found[0][1]
|
||||
if found:
|
||||
return None, f"{len(found)} candidates validate"
|
||||
return None, (f"{rejected} candidate(s), none valid" if rejected else "no candidate")
|
||||
return None, f"{len(found)} candidates validate", None
|
||||
return None, (f"{rejected} candidate(s), none valid" if rejected else "no candidate"), None
|
||||
|
||||
|
||||
def _parse_record_time_seconds(raw: bytes) -> Optional[float]:
|
||||
@@ -1047,6 +1052,83 @@ def _setup_geo_range(raw: bytes) -> tuple[Optional[int], Optional[str]]:
|
||||
return None, f"geophone range not read: channel range bytes {found}"
|
||||
|
||||
|
||||
# The identity block before the setup: what the live read gets from SUB FE
|
||||
# (DeviceInfo). Offsets back from k, the setup name; matched against every DL2
|
||||
# report 2026-11-03 -- serial, version and calibration 69,983 / 69,983, the
|
||||
# battery 69,729 plus 254 that Blastware rounds half up (6.25 -> "6.3 Volts").
|
||||
_SERIAL_BEFORE_NAME = 202 # ASCII, NUL-terminated
|
||||
_BATTERY_BEFORE_NAME = 190 # uint16 BE, hundredths of a volt
|
||||
_FIRMWARE_MINOR_BEFORE_NAME = 193 # 0x11 = 17 for S338.17, as SUB EA
|
||||
_FIRMWARE_BEFORE_NAME = 147 # "S338.17"
|
||||
_DSP_BEFORE_NAME = 138 # "10.72"
|
||||
_CALIBRATION_BEFORE_NAME = 132 # dd mm yyyy (BE)
|
||||
_CALIBRATED_BY_BEFORE_NAME = 128 # "Instantel"
|
||||
_MANUFACTURER_BEFORE_NAME = 94 # "Instantel"
|
||||
_MODEL_BEFORE_NAME = 72 # "MiniMate Plus" / "BlastMate III"
|
||||
|
||||
|
||||
def _cstr(raw: bytes, i: int, limit: int = 40) -> Optional[str]:
|
||||
if i < 0:
|
||||
return None
|
||||
j = raw.find(b"\x00", i, i + limit)
|
||||
s = raw[i:j if j >= 0 else i + limit].decode("ascii", errors="replace").strip()
|
||||
return s or None
|
||||
|
||||
|
||||
def _setup_identity(raw: bytes, k: int) -> tuple[Optional[DeviceInfo], Optional[float]]:
|
||||
"""(the unit's identity, the battery in volts) from the block before the
|
||||
setup name at k."""
|
||||
if k < _SERIAL_BEFORE_NAME:
|
||||
return None, None
|
||||
serial = _cstr(raw, k - _SERIAL_BEFORE_NAME, 16)
|
||||
if not serial:
|
||||
return None, None
|
||||
c = k - _CALIBRATION_BEFORE_NAME
|
||||
try:
|
||||
cal = datetime.date(int.from_bytes(raw[c + 2:c + 4], "big"), raw[c + 1], raw[c])
|
||||
except ValueError:
|
||||
cal = None
|
||||
info = DeviceInfo(
|
||||
serial=serial,
|
||||
firmware_minor=raw[k - _FIRMWARE_MINOR_BEFORE_NAME],
|
||||
firmware_version=_cstr(raw, k - _FIRMWARE_BEFORE_NAME, 9),
|
||||
dsp_version=_cstr(raw, k - _DSP_BEFORE_NAME, 6),
|
||||
manufacturer=_cstr(raw, k - _MANUFACTURER_BEFORE_NAME),
|
||||
model=_cstr(raw, k - _MODEL_BEFORE_NAME),
|
||||
calibration_date=cal,
|
||||
calibrated_by=_cstr(raw, k - _CALIBRATED_BY_BEFORE_NAME, 32),
|
||||
)
|
||||
b = k - _BATTERY_BEFORE_NAME
|
||||
return info, int.from_bytes(raw[b:b + 2], "big") / 100
|
||||
|
||||
|
||||
# Trigger settings, in Series IV's 0x1A layout again: the trigger-source bitmask
|
||||
# at k - 19 ([29]: 0x07 geophones, 0x08 mic), the stop mode at k - 31 ([17],
|
||||
# 0 = Fixed), and each channel block's trigger level, a float32 at label + 30 --
|
||||
# in/s for a geophone, psi for the mic. The mic block's type code at label + 4
|
||||
# is Series IV's (2 = linear). Against every DL2 waveform (2026-11-03): the geo
|
||||
# level 6,447 / 6,447, the mic level 4,403 / 4,403, and Blastware prints a level
|
||||
# exactly when its channel is a trigger source and the event was not Manual.
|
||||
_TRIGGER_SOURCES_BEFORE_NAME = 19
|
||||
_STOP_MODE_BEFORE_NAME = 31
|
||||
_TRIGGER_LEVEL_AFTER_LABEL = 30
|
||||
_MIC_TYPE_AFTER_LABEL = 4
|
||||
|
||||
|
||||
def _setup_trigger(raw: bytes, k: int) -> dict:
|
||||
out = {"trigger_sources": raw[k - _TRIGGER_SOURCES_BEFORE_NAME],
|
||||
"record_stop_mode": raw[k - _STOP_MODE_BEFORE_NAME]}
|
||||
t = raw.find(b"\x00\x00\x01Tran", k, k + _CHANNEL_BLOCK_SPAN)
|
||||
if t >= 0:
|
||||
p = t + 3 + _TRIGGER_LEVEL_AFTER_LABEL
|
||||
out["trigger_level_geo"] = struct.unpack_from(">f", raw, p)[0]
|
||||
m = raw.find(b"MicL", k, k + _CHANNEL_BLOCK_SPAN)
|
||||
if m >= 0:
|
||||
out["trigger_level_mic_psi"] = struct.unpack_from(">f", raw, m + _TRIGGER_LEVEL_AFTER_LABEL)[0]
|
||||
out["mic_type"] = int.from_bytes(raw[m + _MIC_TYPE_AFTER_LABEL:m + _MIC_TYPE_AFTER_LABEL + 4], "big")
|
||||
return out
|
||||
|
||||
|
||||
def read_blastware_file(path: Union[str, Path], *, name: Optional[str] = None) -> Event:
|
||||
"""Parse a Blastware event file into an Event: `decode_blastware_bytes` on its
|
||||
bytes, `name` defaulting to the file's own (see there -- never a time source)."""
|
||||
@@ -1236,6 +1318,11 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
ev.geo_range, range_problem = _setup_geo_range(raw)
|
||||
if range_problem:
|
||||
ev.decode_problems.append(range_problem + "; peaks assume the Normal range")
|
||||
k = _setup_name_at(raw)
|
||||
if k is not None:
|
||||
ev.device_info, ev.battery_volts = _setup_identity(raw, k)
|
||||
for attr, value in _setup_trigger(raw, k).items():
|
||||
setattr(ev, attr, value)
|
||||
if is_waveform_body:
|
||||
ev.rectime_seconds = _setup_record_time(raw)
|
||||
ev.total_samples = len(samples["Tran"]) or None
|
||||
@@ -1276,7 +1363,7 @@ def decode_blastware_bytes(raw: bytes, *, name: Optional[str] = None) -> Event:
|
||||
_rt = _parse_record_time_seconds(raw)
|
||||
_start = (datetime.datetime(ts1.year, ts1.month, ts1.day, ts1.hour, ts1.minute, ts1.second)
|
||||
if ts1 is not None else None)
|
||||
_trig, _why = _embedded_event_record(raw, footer_pos, _start, _stop, _rt)
|
||||
_trig, _why, ev.triggered_by = _embedded_event_record(raw, footer_pos, _start, _stop, _rt)
|
||||
if _trig is not None:
|
||||
ev.timestamp_source = "event_record"
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user