fix(decode): histograms go to the histogram codec, and a loud interval no longer hides a short one

Three causes behind the histograms the checker found decoding wrongly:

1. The file decoder tried the waveform codec first, whatever the record
   type.  It accepted BE9440 / BE9557 / BE17354's 2018 5 s histograms and
   returned ~5x too many 'samples' (K440HDXX.XH0H: SFM 44,504, Blastware
   6,976).  The codec now follows STRT[18] (0x46 / 0x0A on 70,041 /
   70,041 DL2 files); only an unknown type falls back to trial order, and a
   chosen codec that fails is a decode problem rather than a silent switch.

2. The sub-minute layout check required each record's 'zero word' to be
   0000.  It is the high word of the interval's PVS squared, non-zero above
   1.28 in/s, so a one-block histogram with a loud interval was not
   recognised (T193KR5M.X90H, K440HA3C.JM0H, R352HFZ7.530H decoded to
   nothing).  The check is now the measured relation peak <= PVS <=
   sqrt(3) * peak, which holds on 34,502,955 / 34,502,955 DL2 sub-minute
   intervals.  The no-terminal-tail trim uses the same test.  Two test
   builders wrote PVS bytes no real record has (one still in the retracted
   little-endian layout); they now write peak^2 in the measured layout, and
   the retraction test keeps a legacy builder of its own.

3. Checker carve-out interval-count-is-duration: Blastware's Number of
   Intervals is (stop - start) / interval from the footer times, and its
   own interval table lists one block fewer when the unit never wrote the
   last minute.  SFM matches the table (T193KSWP.FF0H: 2,880 vs 2,850).

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-02 16:20:32 -05:00
1 parent 99b6b315d6
commit aec4a38473
12 files changed
+434 -29

No files matched your search

+21 -1
View File
@@ -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)
+27 -13
View File
@@ -1170,18 +1170,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)
@@ -1190,13 +1209,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; "
+23 -5
View File
@@ -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)
Binary file not shown.
+137
View File
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<Event>
<FileVersion>8.0</FileVersion>
<EventType>Full Histogram</EventType>
<SerialNumber>BE9440</SerialNumber>
<Version>V 10.72-8.17 MiniMate Plus</Version>
<FileName>K440HA3C.JM0H</FileName>
<HistogramStartTime>11:42:58</HistogramStartTime>
<HistogramStartDate>2018-02-10</HistogramStartDate>
<HistogramStopTime>11:43:34</HistogramStopTime>
<HistogramStopDate>2018-02-10</HistogramStopDate>
<Channels>
<Channel>Tran</Channel>
<Channel>Vert</Channel>
<Channel>Long</Channel>
<Channel>MicL</Channel>
</Channels>
<NumberofIntervals>8.00</NumberofIntervals>
<IntervalSize>5 seconds</IntervalSize>
<SampleRate>1024 sps</SampleRate>
<BatteryLevel>6.8 Volts</BatteryLevel>
<Calibration>January 11, 2018 by Instantel</Calibration>
<Units>in/s and dB(L)</Units>
<Project>Villanova Stadium Station - Seis. Loc #2</Project>
<Client>D&apos;Angleo Brothers, Inc</Client>
<UserName>Terra-Mechanics, Inc - D Harrison</UserName>
<Seis.Location>Loc # 2 - OutBound Track</Seis.Location>
<GeoRange>
<Value>10.000</Value>
<Units>in/s</Units>
</GeoRange>
<TranPPV>
<Value>0.390</Value>
<Units>in/s</Units>
</TranPPV>
<VertPPV>
<Value>2.645</Value>
<Units>in/s</Units>
</VertPPV>
<LongPPV>
<Value>0.095</Value>
<Units>in/s</Units>
</LongPPV>
<TranZCFreq>
<Value>85</Value>
<Units>Hz</Units>
</TranZCFreq>
<VertZCFreq>
<Value>73</Value>
<Units>Hz</Units>
</VertZCFreq>
<LongZCFreq>
<Value>43</Value>
<Units>Hz</Units>
</LongZCFreq>
<TranPeakTime>11:43:33</TranPeakTime>
<TranPeakDate>2018-02-10</TranPeakDate>
<VertPeakTime>11:43:33</VertPeakTime>
<VertPeakDate>2018-02-10</VertPeakDate>
<LongPeakTime>11:43:33</LongPeakTime>
<LongPeakDate>2018-02-10</LongPeakDate>
<PeakVectorSum>
<Value>2.656</Value>
<Units>in/s</Units>
</PeakVectorSum>
<PeakVectorSumTime>11:43:33</PeakVectorSumTime>
<PeakVectorSumDate>2018-02-10</PeakVectorSumDate>
<Microphone>Linear Weighting</Microphone>
<MicLPSPL>
<Value>126.4</Value>
<Units>dB(L)</Units>
</MicLPSPL>
<MicLTime>11:43:18</MicLTime>
<MicLDate>2018-02-10</MicLDate>
<MicLZCFreq>
<Value>&gt;100</Value>
<Units>Hz</Units>
</MicLZCFreq>
<TranTestFreq>7.2 Hz.</TranTestFreq>
<TranTestRatio>4.3</TranTestRatio>
<TranTestResults>Passed</TranTestResults>
<VertTestFreq>7.5 Hz.</VertTestFreq>
<VertTestRatio>3.7</VertTestRatio>
<VertTestResults>Passed</VertTestResults>
<LongTestFreq>7.4 Hz.</LongTestFreq>
<LongTestRatio>4.1</LongTestRatio>
<LongTestResults>Passed</LongTestResults>
<MicLTestFreq>19.7 Hz.</MicLTestFreq>
<MicLTestAmplitude>634 mv</MicLTestAmplitude>
<MicLTestResults>Passed</MicLTestResults>
<UsbSummaryChannels>
</UsbSummaryChannels>
<MonitorLog>2018-02-10 11:42:57 - 2018-02-10 11:43:34 &quot;Event Recorded Modem Exit&quot;</MonitorLog>
<HistogramData>
<HistogramDataColumnTitle>
<ColumnTitle> Tran Peak in/s </ColumnTitle>
<ColumnTitle> Tran Freq Hz </ColumnTitle>
<ColumnTitle> Vert Peak in/s </ColumnTitle>
<ColumnTitle> Vert Freq Hz </ColumnTitle>
<ColumnTitle> Long Peak in/s </ColumnTitle>
<ColumnTitle> Long Freq Hz </ColumnTitle>
<ColumnTitle> Geo PVS in/s </ColumnTitle>
<ColumnTitle> MicL Peak psi(L) </ColumnTitle>
<ColumnTitle> MicL Peak dB(L) </ColumnTitle>
<ColumnTitle> MicL Freq Hz </ColumnTitle>
</HistogramDataColumnTitle>
<HistogramDataIntervals>
<Time>2018-02-10 11:43:03</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>N/A</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>91.48</Cell><Cell>34</Cell></Row>
<Time>2018-02-10 11:43:08</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>100.0</Cell><Cell>&gt;100</Cell></Row>
<Time>2018-02-10 11:43:13</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>87.96</Cell><Cell>&gt;100</Cell></Row>
<Time>2018-02-10 11:43:18</Time>
<Row>
<Cell>0.010</Cell><Cell>&gt;100</Cell><Cell>0.015</Cell><Cell>&gt;100</Cell><Cell>0.010</Cell><Cell>&gt;100</Cell><Cell>0.016</Cell><Cell>0.006</Cell><Cell>126.4</Cell><Cell>&gt;100</Cell></Row>
<Time>2018-02-10 11:43:23</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.004</Cell><Cell>122.0</Cell><Cell>43</Cell></Row>
<Time>2018-02-10 11:43:28</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.010</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.011</Cell><Cell>0.000</Cell><Cell>87.96</Cell><Cell>&gt;100</Cell></Row>
<Time>2018-02-10 11:43:33</Time>
<Row>
<Cell>0.390</Cell><Cell>85</Cell><Cell>2.645</Cell><Cell>73</Cell><Cell>0.095</Cell><Cell>43</Cell><Cell>2.656</Cell><Cell>0.000</Cell><Cell>93.98</Cell><Cell>&gt;100</Cell></Row>
<Time>2018-02-10 11:43:34</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.007</Cell><Cell>0.000</Cell><Cell>87.96</Cell><Cell>&gt;100</Cell></Row>
</HistogramDataIntervals>
</HistogramData>
<HistogramDataUsbChannels>
</HistogramDataUsbChannels>
</Event>
Binary file not shown.
Binary file not shown.
+140
View File
@@ -0,0 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<Event>
<FileVersion>8.0</FileVersion>
<EventType>Full Histogram</EventType>
<SerialNumber>BE18193</SerialNumber>
<Version>V 10.72-8.17 MiniMate Plus</Version>
<FileName>T193KR5M.X90H</FileName>
<HistogramStartTime>17:22:21</HistogramStartTime>
<HistogramStartDate>2024-10-07</HistogramStartDate>
<HistogramStopTime>17:22:38</HistogramStopTime>
<HistogramStopDate>2024-10-07</HistogramStopDate>
<Channels>
<Channel>Tran</Channel>
<Channel>Vert</Channel>
<Channel>Long</Channel>
<Channel>MicL</Channel>
</Channels>
<NumberofIntervals>9.00</NumberofIntervals>
<IntervalSize>2 seconds</IntervalSize>
<SampleRate>1024 sps</SampleRate>
<BatteryLevel>6.3 Volts</BatteryLevel>
<Calibration>July 10, 2024 by Instantel</Calibration>
<Units>in/s and dB(L)</Units>
<Project>I-80- North Fork Bridges-543 Plank Rd</Project>
<Client>Fay Company</Client>
<UserName>Terra-Mechanics Inc - Harrison</UserName>
<Seis.Loc>Loc #1 -Main Line- Area 2 - 543 Plank Rd</Seis.Loc>
<GeoRange>
<Value>10.000</Value>
<Units>in/s</Units>
</GeoRange>
<TranPPV>
<Value>2.290</Value>
<Units>in/s</Units>
</TranPPV>
<VertPPV>
<Value>5.515</Value>
<Units>in/s</Units>
</VertPPV>
<LongPPV>
<Value>1.520</Value>
<Units>in/s</Units>
</LongPPV>
<TranZCFreq>
<Value>11</Value>
<Units>Hz</Units>
</TranZCFreq>
<VertZCFreq>
<Value>34</Value>
<Units>Hz</Units>
</VertZCFreq>
<LongZCFreq>
<Value>18</Value>
<Units>Hz</Units>
</LongZCFreq>
<TranPeakTime>17:22:37</TranPeakTime>
<TranPeakDate>2024-10-07</TranPeakDate>
<VertPeakTime>17:22:37</VertPeakTime>
<VertPeakDate>2024-10-07</VertPeakDate>
<LongPeakTime>17:22:37</LongPeakTime>
<LongPeakDate>2024-10-07</LongPeakDate>
<PeakVectorSum>
<Value>5.818</Value>
<Units>in/s</Units>
</PeakVectorSum>
<PeakVectorSumTime>17:22:37</PeakVectorSumTime>
<PeakVectorSumDate>2024-10-07</PeakVectorSumDate>
<Microphone>Linear Weighting</Microphone>
<MicLPSPL>
<Value>108.0</Value>
<Units>dB(L)</Units>
</MicLPSPL>
<MicLTime>17:22:37</MicLTime>
<MicLDate>2024-10-07</MicLDate>
<MicLZCFreq>
<Value>57</Value>
<Units>Hz</Units>
</MicLZCFreq>
<TranTestFreq>7.6 Hz.</TranTestFreq>
<TranTestRatio>3.7</TranTestRatio>
<TranTestResults>Passed</TranTestResults>
<VertTestFreq>7.7 Hz.</VertTestFreq>
<VertTestRatio>3.5</VertTestRatio>
<VertTestResults>Passed</VertTestResults>
<LongTestFreq>7.5 Hz.</LongTestFreq>
<LongTestRatio>3.6</LongTestRatio>
<LongTestResults>Passed</LongTestResults>
<MicLTestFreq>0.0 Hz.</MicLTestFreq>
<MicLTestAmplitude>0 mv</MicLTestAmplitude>
<MicLTestResults>Check</MicLTestResults>
<UsbSummaryChannels>
</UsbSummaryChannels>
<MonitorLog>2024-10-07 17:22:20 - 2024-10-07 17:22:38 &quot;Event Recorded Modem Exit&quot;</MonitorLog>
<HistogramData>
<HistogramDataColumnTitle>
<ColumnTitle> Tran Peak in/s </ColumnTitle>
<ColumnTitle> Tran Freq Hz </ColumnTitle>
<ColumnTitle> Vert Peak in/s </ColumnTitle>
<ColumnTitle> Vert Freq Hz </ColumnTitle>
<ColumnTitle> Long Peak in/s </ColumnTitle>
<ColumnTitle> Long Freq Hz </ColumnTitle>
<ColumnTitle> Geo PVS in/s </ColumnTitle>
<ColumnTitle> MicL Peak psi(L) </ColumnTitle>
<ColumnTitle> MicL Peak dB(L) </ColumnTitle>
<ColumnTitle> MicL Freq Hz </ColumnTitle>
</HistogramDataColumnTitle>
<HistogramDataIntervals>
<Time>2024-10-07 17:22:23</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.010</Cell><Cell>&gt;100</Cell><Cell>0.011</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>64</Cell></Row>
<Time>2024-10-07 17:22:25</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>64</Cell></Row>
<Time>2024-10-07 17:22:27</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>64</Cell></Row>
<Time>2024-10-07 17:22:29</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>64</Cell></Row>
<Time>2024-10-07 17:22:31</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>64</Cell></Row>
<Time>2024-10-07 17:22:33</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.009</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>73</Cell></Row>
<Time>2024-10-07 17:22:35</Time>
<Row>
<Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.010</Cell><Cell>&gt;100</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.011</Cell><Cell>0.000</Cell><Cell>104.2</Cell><Cell>64</Cell></Row>
<Time>2024-10-07 17:22:37</Time>
<Row>
<Cell>2.290</Cell><Cell>11</Cell><Cell>5.515</Cell><Cell>34</Cell><Cell>1.520</Cell><Cell>18</Cell><Cell>5.818</Cell><Cell>0.001</Cell><Cell>108.0</Cell><Cell>57</Cell></Row>
<Time>2024-10-07 17:22:38</Time>
<Row>
<Cell>0.010</Cell><Cell>&gt;100</Cell><Cell>0.020</Cell><Cell>2.9</Cell><Cell>0.005</Cell><Cell>&gt;100</Cell><Cell>0.022</Cell><Cell>0.001</Cell><Cell>104.9</Cell><Cell>64</Cell></Row>
</HistogramDataIntervals>
</HistogramData>
<HistogramDataUsbChannels>
</HistogramDataUsbChannels>
</Event>
+32
View File
@@ -303,3 +303,35 @@ def test_the_h5_carries_the_job_number(tmp_path):
event_hdf5.write_event_hdf5(tmp_path / "e.h5", ev, serial="BE9557")
with h5py.File(tmp_path / "e.h5", "r") as f:
assert (bool(f.attrs["job_number_enabled"]), int(f.attrs["job_number"])) == (False, 802)
# ── Histograms: the codec follows the record type, and loud short blocks ─────
_HD = "histogram-decode-11-02-26"
def test_a_histogram_body_goes_to_the_histogram_codec():
# BE9440, 2018, 5 s intervals. The waveform codec accepted this body when
# it was tried first, and returned ~5x too many "samples". Blastware's
# report: 6,976 intervals, PPV 0.180 / 0.195 / 0.215, PVS 0.270, start
# 2018-04-26 07:53:41.
ev = read_blastware_file(_FIX / _HD / "K440HDXX.XH0H")
assert ev.record_type == "Histogram"
assert len(ev.raw_samples["Tran"]) == 6976
pv = ev.peak_values
for got, want in zip((pv.tran, pv.vert, pv.long, pv.peak_vector_sum),
(0.180, 0.195, 0.215, 0.270)):
assert abs(got - want) <= 0.0005 + 1e-9, (got, want)
ts = ev.timestamp
assert (ts.year, ts.month, ts.day) == (2018, 4, 26)
@pytest.mark.parametrize("name,n", [
("T193KR5M.X90H", 9), # 2 s; its 8th interval is 5.8 in/s of vector sum
("K440HA3C.JM0H", 8), # 5 s
])
def test_a_loud_interval_does_not_hide_a_short_histogram(name, n):
# A short (one-block) sub-minute histogram is found by its record layout.
# An interval louder than 1.28 in/s has a vector sum squared above 65,535,
# which the old "first word is 0000" test rejected.
ev = read_blastware_file(_FIX / _HD / name)
assert len(ev.raw_samples["Tran"]) == n
+34
View File
@@ -187,3 +187,37 @@ def test_a_job_number_only_sfm_shows_is_a_mismatch():
side.job_number = "1"
res = {r.field: r for r in compare(side, parse_xml_report_file(FIX / "R352HFZ7_530H_XML.XML"))}
assert res["JobNumber"].verdict == "mismatch"
@pytest.mark.parametrize("stem", ["T193KR5M.X90H", "K440HA3C.JM0H"])
def test_a_loud_short_histogram_matches_blastware(stem):
hd = FIX.parent / "histogram-decode-11-02-26"
res = {r.field: r for r in compare(sfm_side((hd / stem).read_bytes()),
parse_xml_report_file(hd / (stem.replace(".", "_", 1) + "_XML.XML")),
CARVEOUTS)}
for f in ("NumberofIntervals", "intervals.Tran Peak in/s", "intervals.Vert Peak in/s",
"intervals.Long Peak in/s"):
assert res[f].verdict == "match", (f, res[f])
def _count_case(extra, drop_rows=0):
jf = FIX.parent / "job-number-11-02-26"
ref = parse_xml_report_file(jf / "T438I4V5_DJ0H_XML.XML")
rows = len(ref.intervals)
ref.scalars["NumberofIntervals"] = f"{rows + extra}.00"
if drop_rows:
ref.intervals = ref.intervals[:-drop_rows]
side = sfm_side((jf / "T438I4V5.DJ0H").read_bytes())
return {r.field: r for r in compare(side, ref, CARVEOUTS)}["NumberofIntervals"]
def test_blastware_counting_the_monitoring_time_is_accepted():
# Blastware's Number of Intervals is (stop - start) / interval. When the
# unit never wrote the last minute, Blastware's own table lists one block
# fewer, and SFM matches the table (T193KSWP.FF0H: 2,880 against 2,850 rows).
r = _count_case(30)
assert (r.verdict, r.carveout) == ("accepted", "interval-count-is-duration")
def test_the_count_carveout_needs_blastwares_own_table_to_agree_with_sfm():
assert _count_case(30, drop_rows=1).verdict == "mismatch"
+19 -9
View File
@@ -525,18 +525,28 @@ from minimateplus.histogram_codec import ( # noqa: E402
def _mk_multi_block(intervals, seg=0, ctr=256):
"""Build one multi-interval block from a list of 8-tuples."""
b = bytearray()
b += bytes([0x00, seg])
b += int(ctr).to_bytes(2, "little")
b += bytes([0x0A, 0x00])
"""Build one multi-interval block from a list of 8-tuples (T, Tf, V, Vf, L,
Lf, M, Mf) in the measured layout: big-endian from byte 5, each record ending
in its PVS squared -- here the largest geo peak squared, which every real
record is consistent with (34,502,955 / 34,502,955 DL2 intervals)."""
b = bytearray([0x00, seg]) + int(ctr).to_bytes(2, "little") + bytes([0x0A])
for iv in intervals:
for v in iv:
b += int(v).to_bytes(2, "big")
b += (max(iv[0], iv[2], iv[4]) ** 2).to_bytes(4, "big")
b += bytes(7)
assert len(b) == 12 + 20 * len(intervals)
return bytes(b)
def _mk_legacy_le_block(intervals, seg=0, ctr=256):
"""The retracted model's bytes: 8 x uint16 LITTLE-endian from byte 6."""
b = bytearray([0x00, seg]) + int(ctr).to_bytes(2, "little") + bytes([0x0A, 0x00])
for iv in intervals:
for v in iv:
b += int(v).to_bytes(2, "little")
b += (0).to_bytes(2, "little")
b += (5).to_bytes(2, "little")
b += (0).to_bytes(2, "little") + (5).to_bytes(2, "little")
b += bytes(6)
assert len(b) == 12 + 20 * len(intervals)
return bytes(b)
@@ -569,7 +579,7 @@ def test_multi_interval_values_are_not_little_endian_from_byte_6():
tests/test_histogram_multi_interval.py for the measured cases.
"""
ivs = [(0x0100, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1)]
body = _mk_multi_block(ivs) + _mk_multi_block(ivs, ctr=257)
body = _mk_legacy_le_block(ivs) + _mk_legacy_le_block(ivs, ctr=257)
assert walk_multi_interval_blocks(body)[0]["t_peak"] != 0x0100
+1 -1
View File
@@ -56,7 +56,7 @@ def _block(records, *, ctr=0x0100, seg=0, mic=True, last=False) -> bytes:
assert len(r) == (8 if mic else 6)
for v in r:
b += int(v).to_bytes(2, "big")
b += b"\x00\x00" + b"\x00\x2d" # the zero word, then one more
b += (max(r[0], r[2], r[4]) ** 2).to_bytes(4, "big") # PVS², consistent as on real data
b += b"\xfc\xf3\x00" + (TERMINAL if last else b"\x5a\x0a\x00\x00")
return bytes(b)