arrayprobe 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- arrayprobe/__init__.py +219 -0
- arrayprobe/__main__.py +19 -0
- arrayprobe/_v314_lazy.py +48 -0
- arrayprobe/ai_ready.py +212 -0
- arrayprobe/attributed_export.py +1333 -0
- arrayprobe/authordefaults.py +267 -0
- arrayprobe/cli.py +3510 -0
- arrayprobe/combine.py +12 -0
- arrayprobe/compare.py +950 -0
- arrayprobe/config.py +1239 -0
- arrayprobe/constants.py +12 -0
- arrayprobe/count_closure.py +204 -0
- arrayprobe/diagnostic_views.py +769 -0
- arrayprobe/diode_report.py +265 -0
- arrayprobe/driver/__init__.py +5 -0
- arrayprobe/driver/_load_record.py +331 -0
- arrayprobe/driver/preflight_inputs.py +250 -0
- arrayprobe/driver/reprocess.py +553 -0
- arrayprobe/driver/run_flight.py +8166 -0
- arrayprobe/driver/tas_from_probe_housekeeping.py +124 -0
- arrayprobe/dropsize.py +519 -0
- arrayprobe/friendly.py +206 -0
- arrayprobe/geometry/__init__.py +77 -0
- arrayprobe/geometry/min_rectangle.py +92 -0
- arrayprobe/intarr.py +12 -0
- arrayprobe/intarr_analysis.py +16 -0
- arrayprobe/intarr_plots.py +12 -0
- arrayprobe/inventory.py +129 -0
- arrayprobe/jit_kernels.py +344 -0
- arrayprobe/lwc.py +44 -0
- arrayprobe/masks.py +349 -0
- arrayprobe/matio.py +410 -0
- arrayprobe/matlab_compat.py +13 -0
- arrayprobe/ncio.py +26 -0
- arrayprobe/particle.py +55 -0
- arrayprobe/particle_perimeter.py +12 -0
- arrayprobe/products.py +78 -0
- arrayprobe/profiles.py +179 -0
- arrayprobe/provenance.py +21 -0
- arrayprobe/py.typed +11 -0
- arrayprobe/readers/__init__.py +9 -0
- arrayprobe/readers/dmt.py +603 -0
- arrayprobe/readers/mask_csv.py +12 -0
- arrayprobe/readers/pms.py +12 -0
- arrayprobe/readers/sea.py +433 -0
- arrayprobe/readers/tasinfo.py +12 -0
- arrayprobe/regimes.py +507 -0
- arrayprobe/render.py +728 -0
- arrayprobe/reproduce_report.py +149 -0
- arrayprobe/review_agreement.py +135 -0
- arrayprobe/review_sampling.py +327 -0
- arrayprobe/run_page.py +624 -0
- arrayprobe/sample_area.py +12 -0
- arrayprobe/selector.py +206 -0
- arrayprobe/sizing.py +151 -0
- arrayprobe/time_convert.py +12 -0
- arrayprobe/timing_closure.py +141 -0
- arrayprobe/tools/__init__.py +27 -0
- arrayprobe/tools/make_demo_flight.py +1089 -0
- arrayprobe/tools/make_tas_file.py +760 -0
- arrayprobe/tools/probe_comparison.py +569 -0
- arrayprobe/tools/synthetic_truth.py +698 -0
- arrayprobe/tools/tas_sources.py +705 -0
- arrayprobe/triage_context.py +488 -0
- arrayprobe/triage_server.py +5215 -0
- arrayprobe/tune/__init__.py +15 -0
- arrayprobe/tune/diodes.py +400 -0
- arrayprobe/tune/distributions.py +300 -0
- arrayprobe/tune/sample.py +243 -0
- arrayprobe/tune/score.py +230 -0
- arrayprobe/tune/settings.py +354 -0
- arrayprobe/validate_by_condition.py +208 -0
- arrayprobe/validation_report.py +166 -0
- arrayprobe-0.1.0.dist-info/METADATA +260 -0
- arrayprobe-0.1.0.dist-info/RECORD +189 -0
- arrayprobe-0.1.0.dist-info/WHEEL +5 -0
- arrayprobe-0.1.0.dist-info/entry_points.txt +2 -0
- arrayprobe-0.1.0.dist-info/licenses/LICENSE +31 -0
- arrayprobe-0.1.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +47 -0
- arrayprobe-0.1.0.dist-info/licenses/NOTICE.md +369 -0
- arrayprobe-0.1.0.dist-info/top_level.txt +4 -0
- arrayprobe_soda2/__init__.py +131 -0
- arrayprobe_soda2/angles.py +143 -0
- arrayprobe_soda2/aspect_ratio.py +541 -0
- arrayprobe_soda2/backfill.py +100 -0
- arrayprobe_soda2/binary_dof.py +181 -0
- arrayprobe_soda2/buildindex.py +113 -0
- arrayprobe_soda2/decompress_dmt.py +285 -0
- arrayprobe_soda2/enhancement_hooks.py +275 -0
- arrayprobe_soda2/export_ascii.py +732 -0
- arrayprobe_soda2/export_ncdf.py +683 -0
- arrayprobe_soda2/export_ncdf_raf.py +396 -0
- arrayprobe_soda2/fillholes.py +161 -0
- arrayprobe_soda2/find_radius.py +105 -0
- arrayprobe_soda2/findsize.py +407 -0
- arrayprobe_soda2/fit_poisson.py +170 -0
- arrayprobe_soda2/fix2dimage.py +63 -0
- arrayprobe_soda2/fixraking.py +162 -0
- arrayprobe_soda2/fixstuckbits.py +146 -0
- arrayprobe_soda2/hvps4_read_frame.py +121 -0
- arrayprobe_soda2/jit_kernels.py +152 -0
- arrayprobe_soda2/label_blobs.py +177 -0
- arrayprobe_soda2/min_circle_fast.py +320 -0
- arrayprobe_soda2/particlesort.py +703 -0
- arrayprobe_soda2/pbp_writer.py +558 -0
- arrayprobe_soda2/poisson_spot_correct.py +183 -0
- arrayprobe_soda2/princomp.py +107 -0
- arrayprobe_soda2/probespecs.py +361 -0
- arrayprobe_soda2/process_2d.py +1364 -0
- arrayprobe_soda2/process_2d_parallel.py +626 -0
- arrayprobe_soda2/process_hk.py +512 -0
- arrayprobe_soda2/processbuffer_2dc.py +155 -0
- arrayprobe_soda2/processbuffer_cip.py +213 -0
- arrayprobe_soda2/processbuffer_f2dc.py +139 -0
- arrayprobe_soda2/processbuffer_spec.py +441 -0
- arrayprobe_soda2/processbuffer_tail.py +350 -0
- arrayprobe_soda2/pth_readers.py +245 -0
- arrayprobe_soda2/py.typed +11 -0
- arrayprobe_soda2/read2dbuffer.py +371 -0
- arrayprobe_soda2/reject.py +246 -0
- arrayprobe_soda2/reprocess_pbp.py +156 -0
- arrayprobe_soda2/sample_area.py +264 -0
- arrayprobe_soda2/save_writer.py +480 -0
- arrayprobe_soda2/sea.py +324 -0
- arrayprobe_soda2/spec_absolutepointers.py +109 -0
- arrayprobe_soda2/spec_decompress.py +203 -0
- arrayprobe_soda2/spec_index.py +476 -0
- arrayprobe_soda2/spec_read_frame.py +286 -0
- arrayprobe_soda2/spec_read_hk.py +301 -0
- arrayprobe_soda2/spec_readint.py +127 -0
- arrayprobe_soda2/timeconv.py +77 -0
- arrayprobe_soda2/update_op.py +101 -0
- arrayprobe_uiops/__init__.py +45 -0
- arrayprobe_uiops/combine.py +192 -0
- arrayprobe_uiops/combine_fork.py +359 -0
- arrayprobe_uiops/constants.py +830 -0
- arrayprobe_uiops/decode_naming.py +113 -0
- arrayprobe_uiops/diode_mask.py +250 -0
- arrayprobe_uiops/engine_absence.py +41 -0
- arrayprobe_uiops/export_oap.py +325 -0
- arrayprobe_uiops/geometry_core.py +366 -0
- arrayprobe_uiops/habit.py +749 -0
- arrayprobe_uiops/habit_core.py +248 -0
- arrayprobe_uiops/hollow_size_rule.py +376 -0
- arrayprobe_uiops/imgproc.py +2081 -0
- arrayprobe_uiops/imgproc_core.py +275 -0
- arrayprobe_uiops/intarr.py +167 -0
- arrayprobe_uiops/intarr_analysis.py +569 -0
- arrayprobe_uiops/intarr_plots.py +119 -0
- arrayprobe_uiops/jit_kernels.py +430 -0
- arrayprobe_uiops/lwc.py +70 -0
- arrayprobe_uiops/matlab_compat.py +860 -0
- arrayprobe_uiops/measure.py +346 -0
- arrayprobe_uiops/min_circle.py +204 -0
- arrayprobe_uiops/min_ellipse.py +161 -0
- arrayprobe_uiops/min_rectangle.py +200 -0
- arrayprobe_uiops/ncar_2dc_timing.py +100 -0
- arrayprobe_uiops/ncio.py +810 -0
- arrayprobe_uiops/oap_read.py +228 -0
- arrayprobe_uiops/particle.py +51 -0
- arrayprobe_uiops/particle_perimeter.py +94 -0
- arrayprobe_uiops/particle_physics.py +199 -0
- arrayprobe_uiops/pecan_params.py +134 -0
- arrayprobe_uiops/probe_cases.py +298 -0
- arrayprobe_uiops/product_sanity.py +337 -0
- arrayprobe_uiops/profile.py +71 -0
- arrayprobe_uiops/profiles.py +756 -0
- arrayprobe_uiops/provenance.py +804 -0
- arrayprobe_uiops/py.typed +11 -0
- arrayprobe_uiops/qc.py +169 -0
- arrayprobe_uiops/readers/__init__.py +8 -0
- arrayprobe_uiops/readers/mask_csv.py +74 -0
- arrayprobe_uiops/readers/pms.py +239 -0
- arrayprobe_uiops/readers/tasinfo.py +57 -0
- arrayprobe_uiops/rect_ellipse.py +129 -0
- arrayprobe_uiops/register_image_assisted.py +25 -0
- arrayprobe_uiops/reject.py +501 -0
- arrayprobe_uiops/reject_core.py +220 -0
- arrayprobe_uiops/sample_area.py +93 -0
- arrayprobe_uiops/sizedist.py +1923 -0
- arrayprobe_uiops/sizedist_core.py +814 -0
- arrayprobe_uiops/sizedist_tables.py +642 -0
- arrayprobe_uiops/spec.py +1094 -0
- arrayprobe_uiops/spec_core.py +703 -0
- arrayprobe_uiops/stray_pixel.py +103 -0
- arrayprobe_uiops/tas.py +191 -0
- arrayprobe_uiops/time_convert.py +41 -0
- uiops/__init__.py +402 -0
- uiops/py.typed +11 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
"""SEA (CIP/2DC) raw decoder -- ``read_binary_SEA.m``.
|
|
3
|
+
|
|
4
|
+
**"This only works for MC3E field campaign"** is the MATLAB's own comment
|
|
5
|
+
(``:4``), and the port inherits that scope: this is one probe family's raw
|
|
6
|
+
format, distinct from SPEC/DMT/PMS, read in a tagged, offset-navigated
|
|
7
|
+
container (a directory record of 16 bytes -- ``tagNumber``, ``dataOffset``,
|
|
8
|
+
``numberBytes``, ``samples``, ``bytesPerSample``, ``type``, three ``param``
|
|
9
|
+
bytes, ``address`` -- precedes each chunk). Byte order is **little-endian**
|
|
10
|
+
(``fopen(...,'r','l')``), like DMT, unlike PMS.
|
|
11
|
+
|
|
12
|
+
Two directory-tag values are handled; everything else is walked past without
|
|
13
|
+
action (the source never errors or logs on an unrecognized tag -- only
|
|
14
|
+
``tagNumber == 999`` is a recognized "end of this run" marker):
|
|
15
|
+
|
|
16
|
+
- **``tagNumber == 33000, numberBytes == 4098``** -- a CIP image record.
|
|
17
|
+
Writes to the ``.CIP.cdf`` output. Its image payload uses **the exact same
|
|
18
|
+
byte-level run-length scheme as DMT** (:mod:`arrayprobe.readers.dmt`) --
|
|
19
|
+
same marker bit, same literal/zero-run/fill-run encoding -- reused here via
|
|
20
|
+
:func:`arrayprobe.readers.dmt._decompress_record` rather than
|
|
21
|
+
reimplemented, since the two are the same algorithm, not merely similar.
|
|
22
|
+
- **``tagNumber == 5000, numberBytes == 4096``** -- a 2DC record.
|
|
23
|
+
**The source cannot run this branch**: ``:270`` calls
|
|
24
|
+
``netcdf.putVar(f1, varid91, ...)``, and ``varid91`` is never assigned by
|
|
25
|
+
any ``netcdf.defVar`` anywhere in the file -- the ``.2DC.cdf`` output (see
|
|
26
|
+
``:56-64``) declares no ``tas`` variable at all. MATLAB raises an
|
|
27
|
+
"undefined variable" error the first time this branch is reached. This
|
|
28
|
+
port raises :class:`SeaUndefinedVariableError` at the same point, on the
|
|
29
|
+
theory used throughout this package: a MATLAB crash is a real, checkable
|
|
30
|
+
fact about the source, not something to paper over. Flagged for the UIOOPS
|
|
31
|
+
authors.
|
|
32
|
+
|
|
33
|
+
**The peek-and-rewind pattern, twice, more elaborately than DMT's or PMS's.**
|
|
34
|
+
Both branches read a directory record, a timestamp block, and the image
|
|
35
|
+
payload as a *lookahead* -- extracting the values into variables -- and then
|
|
36
|
+
``fseek`` back by the exact byte count just consumed, before re-reading only
|
|
37
|
+
the *last* directory record. The net effect on the file position is as if
|
|
38
|
+
only the directory records had been read at all; the timestamp and image
|
|
39
|
+
bytes are peeked, not permanently consumed from the stream. The CIP branch's
|
|
40
|
+
rewind is a fixed 4150 bytes, so it is transcribed literally: a payload cut
|
|
41
|
+
short by the end of the file moves where the re-read lands.
|
|
42
|
+
|
|
43
|
+
**``readTime`` reads two 18-byte timestamp groups and returns only the
|
|
44
|
+
second** (``:329-343`` -- the loop variable ``time`` is overwritten each
|
|
45
|
+
iteration). The first group's bytes are consumed and discarded; preserved
|
|
46
|
+
here as a genuine double-read, not simplified to a single read, since a
|
|
47
|
+
record whose stream runs out between the two groups behaves differently
|
|
48
|
+
under each interpretation.
|
|
49
|
+
|
|
50
|
+
**A CIP record with no complete run of eight sync bytes in its decompressed
|
|
51
|
+
stream is silently dropped, not an error** (``:173-180``) -- unlike DMT,
|
|
52
|
+
whose search runs on past the array and dies. A sync byte is any byte
|
|
53
|
+
``0xA0``-``0xAF``, since ``:161`` compares one hex digit
|
|
54
|
+
(``exact_sync=True`` opts into ``0xAA`` alone), and a run that ends on the
|
|
55
|
+
stream's last byte counts as none. SEA's own search caps at
|
|
56
|
+
``i == length(decomp)`` and sets a "weird" flag; the record is skipped
|
|
57
|
+
entirely (nothing written, the write-counter does not advance), and a
|
|
58
|
+
running total of how many records this happened to is printed once at the
|
|
59
|
+
very end. This port counts the same total and returns it rather than
|
|
60
|
+
printing it. A record that decompresses to **nothing** (every byte a no-op) is
|
|
61
|
+
different: ``decomp`` is then undefined at ``:161``, because ``:280`` clears it after every record,
|
|
62
|
+
and MATLAB dies; the port raises :class:`SeaUndefinedVariableError` there.
|
|
63
|
+
|
|
64
|
+
**A SEA-specific reassembly quirk, different from DMT's.** ``:198-200``
|
|
65
|
+
pads a short image with ``-1`` starting at index ``length(decomp_convert)``
|
|
66
|
+
(1-based) through ``1700`` -- which, being an inclusive MATLAB range,
|
|
67
|
+
**overwrites the last real decoded row as well as the padding**, unlike
|
|
68
|
+
DMT's ``len+1`` convention which leaves the last real row alone. Preserved
|
|
69
|
+
verbatim: a CIP record of 8 to 1699 groups loses its final row of real data
|
|
70
|
+
to the padding value. Below eight groups ``length`` is 8, so every row
|
|
71
|
+
survives and rows up to 7 are zeros (:func:`_reassemble_sea`).
|
|
72
|
+
|
|
73
|
+
Not ported: the ``'*'``-glob and ``outfilename == '1'`` auto-naming branches
|
|
74
|
+
(``:8-27``), for the same reason as DMT and PMS -- this port's callers
|
|
75
|
+
always pass an explicit output base path.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
from __future__ import annotations
|
|
79
|
+
|
|
80
|
+
import os
|
|
81
|
+
import struct
|
|
82
|
+
|
|
83
|
+
import numpy as np
|
|
84
|
+
|
|
85
|
+
from .. import ncio
|
|
86
|
+
from ..matlab_compat import MatlabIndexError
|
|
87
|
+
from .dmt import _decompress_record, _sync_bytes
|
|
88
|
+
|
|
89
|
+
__all__ = ["read_binary_SEA", "SeaUndefinedVariableError"]
|
|
90
|
+
|
|
91
|
+
_DIR_BYTES = 16
|
|
92
|
+
_TIME_GROUP_BYTES = 18 # 9 * uint16
|
|
93
|
+
_SENTINEL = 0xAA
|
|
94
|
+
_IMG_ROWLEN = 8
|
|
95
|
+
_IMG_BLOCKLEN = 1700
|
|
96
|
+
|
|
97
|
+
_TAG_CIP = 33000
|
|
98
|
+
_NBYTES_CIP = 4098
|
|
99
|
+
_TAG_2DC = 5000
|
|
100
|
+
_NBYTES_2DC = 4096
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class SeaUndefinedVariableError(RuntimeError):
|
|
104
|
+
"""``read_binary_SEA.m:270`` references ``varid91``, never assigned.
|
|
105
|
+
|
|
106
|
+
MATLAB errors here the first time a 5000/4096-tagged record is reached;
|
|
107
|
+
see the module docstring for the full trace.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _read_dir(fh) -> tuple[int, ...] | None:
|
|
112
|
+
"""``readDir`` -- 16-byte little-endian directory record, or ``None`` at EOF."""
|
|
113
|
+
raw = fh.read(_DIR_BYTES)
|
|
114
|
+
if len(raw) < _DIR_BYTES:
|
|
115
|
+
return None
|
|
116
|
+
tag, offset, nbytes, samples, bps = struct.unpack_from("<5H", raw, 0)
|
|
117
|
+
typ, p1, p2, p3 = raw[10], raw[11], raw[12], raw[13]
|
|
118
|
+
addr, = struct.unpack_from("<H", raw, 14)
|
|
119
|
+
return (tag, offset, nbytes, samples, bps, typ, p1, p2, p3, addr)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _read_time(fh) -> tuple[int, ...]:
|
|
123
|
+
"""``readTime`` -- two 18-byte groups; only the second is returned."""
|
|
124
|
+
# Seeded with the empty tuple rather than ``None``: the loop always runs twice,
|
|
125
|
+
# so the seed can never be returned, and typing it as a tuple is what lets the
|
|
126
|
+
# declared ``tuple[int, ...]`` return type stand without an ignore. Behaviour
|
|
127
|
+
# is unchanged -- the only reachable return is the second group's unpack.
|
|
128
|
+
last: tuple[int, ...] = ()
|
|
129
|
+
for _ in range(2):
|
|
130
|
+
raw = fh.read(_TIME_GROUP_BYTES)
|
|
131
|
+
last = struct.unpack("<9H", raw)
|
|
132
|
+
return last # year, month, day, hour, minute, second, fracsec, maxfreq, bls
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _inner_loop_eof(partial: bytes) -> tuple[int, int]:
|
|
136
|
+
"""What ``read_binary_SEA.m`` does when ``readDir`` (``:88``) hits EOF.
|
|
137
|
+
|
|
138
|
+
``readDir`` (``:312-326``) concatenates whatever ``fread`` calls still
|
|
139
|
+
returned, so ``datatemp`` holds only the complete fields of ``partial``
|
|
140
|
+
(empty when fewer than two bytes remain). MATLAB then dies at the first
|
|
141
|
+
index past its end:
|
|
142
|
+
|
|
143
|
+
- ``:90`` ``datatemp(1)`` on an empty vector (measured under Octave 8.4:
|
|
144
|
+
``datatemp(1): out of bound 0``), or ``datatemp(3)`` when the tag is
|
|
145
|
+
33000 and fewer than three fields survived; any other tag loops once
|
|
146
|
+
more and the next ``readDir`` returns empty, so ``:90`` again;
|
|
147
|
+
- ``:97`` ``ttt(1)`` when a partial 33000/4098 record enters the CIP
|
|
148
|
+
branch and ``readTime`` reads nothing; ``:221``/``:229`` likewise for
|
|
149
|
+
5000/4096;
|
|
150
|
+
- ``:283`` ``datatemp(2)`` for a tag-999 record cut before its offset.
|
|
151
|
+
|
|
152
|
+
A tag-999 record whose offset survived leaves the inner loop normally:
|
|
153
|
+
``(999, offset)`` is returned for the caller's ``:282-286`` handling.
|
|
154
|
+
"""
|
|
155
|
+
vals = struct.unpack_from(f"<{min(len(partial) // 2, 5)}H", partial)
|
|
156
|
+
|
|
157
|
+
def die(line: str, what: str) -> MatlabIndexError:
|
|
158
|
+
return MatlabIndexError(
|
|
159
|
+
f"read_binary_SEA.m:{line}: {what} -- EOF inside the inner directory "
|
|
160
|
+
f"loop ({len(partial)}-byte partial directory record)")
|
|
161
|
+
|
|
162
|
+
if not vals:
|
|
163
|
+
raise die("90", "datatemp(1) of an empty directory record")
|
|
164
|
+
tag = vals[0]
|
|
165
|
+
if tag == _TAG_CIP:
|
|
166
|
+
if len(vals) < 3:
|
|
167
|
+
raise die("90", "datatemp(3) of a partial directory record")
|
|
168
|
+
if vals[2] == _NBYTES_CIP:
|
|
169
|
+
raise die("97", "ttt(1) of an empty timestamp")
|
|
170
|
+
if tag == _TAG_2DC:
|
|
171
|
+
if len(vals) < 3:
|
|
172
|
+
raise die("221", "datatemp(3) of a partial directory record")
|
|
173
|
+
if vals[2] == _NBYTES_2DC:
|
|
174
|
+
raise die("229", "ttt(1) of an empty timestamp")
|
|
175
|
+
if tag == 999:
|
|
176
|
+
if len(vals) < 2:
|
|
177
|
+
raise die("283", "datatemp(2) of a partial directory record")
|
|
178
|
+
return (999, vals[1])
|
|
179
|
+
raise die("90", "datatemp(1) of an empty directory record")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _outer_partial_dir(partial: bytes) -> tuple[int, int] | None:
|
|
183
|
+
"""What ``:78`` makes of a directory record cut short by the end of the file.
|
|
184
|
+
|
|
185
|
+
``readDir`` keeps the fields ``fread`` still returned. With fewer than two,
|
|
186
|
+
``:79``'s ``-datatemp(2)`` dies (an empty file included: ``feof`` is false
|
|
187
|
+
before the first read). A tag of 999 skips the inner loop, and the probe
|
|
188
|
+
then ends the run; any other tag enters it, whose ``readDir`` gets nothing,
|
|
189
|
+
so ``:90`` dies. Returns ``None`` for the clean end.
|
|
190
|
+
"""
|
|
191
|
+
vals = struct.unpack_from(f"<{min(len(partial) // 2, 5)}H", partial)
|
|
192
|
+
if len(vals) < 2:
|
|
193
|
+
raise MatlabIndexError(
|
|
194
|
+
f"read_binary_SEA.m:79: datatemp(2) of a {len(partial)}-byte directory record")
|
|
195
|
+
if vals[0] == 999:
|
|
196
|
+
return None
|
|
197
|
+
raise MatlabIndexError(
|
|
198
|
+
"read_binary_SEA.m:90: datatemp(1) of an empty directory record -- EOF inside "
|
|
199
|
+
f"the inner directory loop ({len(partial)}-byte partial outer record)")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _find_boundary_or_none(decomp: np.ndarray, *, exact_sync: bool = False) -> int | None:
|
|
203
|
+
"""``:157-180`` -- SEA's own boundary search: ``None`` in place of a crash.
|
|
204
|
+
|
|
205
|
+
A sync byte is any byte ``0xA0``-``0xAF`` (``decomp(i)=='AA'`` reads one
|
|
206
|
+
hex digit; :func:`arrayprobe.readers.dmt._sync_bytes`), or only ``0xAA``
|
|
207
|
+
under the opt-in ``exact_sync``. ``None`` is MATLAB's ``nWierd = 1``: no
|
|
208
|
+
run of eight, **or a run that ends on the last byte** -- ``:173``'s
|
|
209
|
+
``i==length(decomp)`` test runs after ``:167``'s ``count == 8`` test in the
|
|
210
|
+
same pass and overrides it (Octave-measured).
|
|
211
|
+
"""
|
|
212
|
+
accepted = _sync_bytes(decomp, exact_sync)
|
|
213
|
+
if len(accepted) >= 8:
|
|
214
|
+
window = np.convolve(accepted.astype(np.int64), np.ones(8, dtype=np.int64),
|
|
215
|
+
mode="valid")
|
|
216
|
+
hit = np.flatnonzero(window == 8)
|
|
217
|
+
if hit.size and int(hit[0]) + 8 < len(decomp):
|
|
218
|
+
return int(hit[0]) + 8
|
|
219
|
+
return None
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _reassemble_sea(decomp: np.ndarray, start: int) -> np.ndarray:
|
|
223
|
+
"""``:183-200`` -- 8-byte groups, byte-reversed, then the off-by-one pad.
|
|
224
|
+
|
|
225
|
+
``decomp_convert(L:1700,:) = -1`` with ``L = length(decomp_convert)``
|
|
226
|
+
(MATLAB, 1-based inclusive). For *n* >= 8 groups ``L`` is *n*, so the pad
|
|
227
|
+
overwrites the last real row -- see the module docstring. For *n* < 8,
|
|
228
|
+
``length`` of the *n*-by-8 matrix is 8: all *n* rows survive, rows *n*+1
|
|
229
|
+
to 7 are the zeros MATLAB grows the matrix with, and the pad starts at row
|
|
230
|
+
8. No groups at all gives the same shape as one all-zero group, because
|
|
231
|
+
``hex2dec`` of an empty selection is 0 under Octave. 1700 groups or more
|
|
232
|
+
are written as their first 1700. All measured under Octave
|
|
233
|
+
(``tests/test_reader_edge_octave.py::SeaAgainstOctave``).
|
|
234
|
+
"""
|
|
235
|
+
n_groups = max(0, (len(decomp) - start) // 8)
|
|
236
|
+
canvas = np.full((_IMG_BLOCKLEN, _IMG_ROWLEN), -1, dtype=np.int64)
|
|
237
|
+
usable = min(n_groups, _IMG_BLOCKLEN)
|
|
238
|
+
if usable:
|
|
239
|
+
block = decomp[start : start + usable * 8].reshape(usable, 8)
|
|
240
|
+
canvas[:usable, :] = block[:, ::-1]
|
|
241
|
+
if usable < _IMG_BLOCKLEN:
|
|
242
|
+
length = max(usable, 8) # MATLAB length() of an n-by-8 matrix
|
|
243
|
+
canvas[usable : length - 1, :] = 0
|
|
244
|
+
canvas[length - 1 :, :] = -1 # :198-200 -- overwrites the last real row too
|
|
245
|
+
return canvas
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def read_binary_SEA(infilename, outfilename, *, exact_sync: bool = False): # noqa: N802,N803
|
|
249
|
+
"""``read_binary_SEA.m`` -- raw SEA file to ``.CIP.cdf`` and ``.2DC.cdf``.
|
|
250
|
+
|
|
251
|
+
Returns ``(n_cip, n_weird_cip, n_2dc)`` -- ``n_weird_cip`` counts CIP
|
|
252
|
+
records silently dropped for lack of a boundary run (MATLAB's
|
|
253
|
+
``nWierdTotal``). Raises :class:`SeaUndefinedVariableError` if a
|
|
254
|
+
5000/4096-tagged record is ever reached, matching the MATLAB crash.
|
|
255
|
+
|
|
256
|
+
``exact_sync`` (default ``False``, MATLAB's behaviour) makes the boundary
|
|
257
|
+
search count only the byte ``0xAA`` rather than every byte whose high hex
|
|
258
|
+
digit is ``A`` -- see :func:`_find_boundary_or_none`.
|
|
259
|
+
"""
|
|
260
|
+
f_cip = ncio.create(f"{outfilename}.CIP.cdf", "clobber")
|
|
261
|
+
dim_time = f_cip.def_dim("time", ncio.NC_UNLIMITED)
|
|
262
|
+
dim_row = f_cip.def_dim("ImgRowlen", _IMG_ROWLEN)
|
|
263
|
+
dim_block = f_cip.def_dim("ImgBlocklen", _IMG_BLOCKLEN)
|
|
264
|
+
v_cip = {
|
|
265
|
+
"year": f_cip.def_var("year", "double", dim_time),
|
|
266
|
+
"month": f_cip.def_var("month", "double", dim_time),
|
|
267
|
+
"day": f_cip.def_var("day", "double", dim_time),
|
|
268
|
+
"hour": f_cip.def_var("hour", "double", dim_time),
|
|
269
|
+
"minute": f_cip.def_var("minute", "double", dim_time),
|
|
270
|
+
"second": f_cip.def_var("second", "double", dim_time),
|
|
271
|
+
"millisec": f_cip.def_var("millisec", "double", dim_time),
|
|
272
|
+
"wkday": f_cip.def_var("wkday", "double", dim_time),
|
|
273
|
+
"data": f_cip.def_var("data", "double", [dim_row, dim_block, dim_time]),
|
|
274
|
+
}
|
|
275
|
+
f_cip.end_def()
|
|
276
|
+
|
|
277
|
+
# The .2DC.cdf file and its variables are declared unconditionally, exactly
|
|
278
|
+
# as :50-64 does -- even though the 5000/4096 branch that would write to
|
|
279
|
+
# them can never run (SeaUndefinedVariableError, see module docstring).
|
|
280
|
+
# Nothing here is written to; the dict of ids is intentionally discarded.
|
|
281
|
+
f_2dc = ncio.create(f"{outfilename}.2DC.cdf", "clobber")
|
|
282
|
+
dim_time2 = f_2dc.def_dim("time", ncio.NC_UNLIMITED)
|
|
283
|
+
dim_row2 = f_2dc.def_dim("ImgRowlen", _IMG_ROWLEN)
|
|
284
|
+
dim_block2 = f_2dc.def_dim("ImgBlocklen", _IMG_BLOCKLEN)
|
|
285
|
+
f_2dc.def_var("year", "double", dim_time2)
|
|
286
|
+
f_2dc.def_var("month", "double", dim_time2)
|
|
287
|
+
f_2dc.def_var("day", "double", dim_time2)
|
|
288
|
+
f_2dc.def_var("hour", "double", dim_time2)
|
|
289
|
+
f_2dc.def_var("minute", "double", dim_time2)
|
|
290
|
+
f_2dc.def_var("second", "double", dim_time2)
|
|
291
|
+
f_2dc.def_var("millisec", "double", dim_time2)
|
|
292
|
+
f_2dc.def_var("wkday", "double", dim_time2)
|
|
293
|
+
f_2dc.def_var("data", "double", [dim_row2, dim_block2, dim_time2])
|
|
294
|
+
f_2dc.end_def()
|
|
295
|
+
|
|
296
|
+
kk_cip = kk_2dc = 0
|
|
297
|
+
n_weird = 0
|
|
298
|
+
|
|
299
|
+
try:
|
|
300
|
+
with open(infilename, "rb") as fh:
|
|
301
|
+
endfile = False
|
|
302
|
+
while not endfile:
|
|
303
|
+
here = fh.tell()
|
|
304
|
+
rec = _read_dir(fh)
|
|
305
|
+
if rec is None:
|
|
306
|
+
# Only the first pass can get here: after it, :290-297's
|
|
307
|
+
# probe has seen 16 more bytes before the loop goes on.
|
|
308
|
+
fh.seek(here)
|
|
309
|
+
rec = _outer_partial_dir(fh.read())
|
|
310
|
+
if rec is None:
|
|
311
|
+
break
|
|
312
|
+
tag = rec[0]
|
|
313
|
+
# doffset1/doffset2 (:79, :283) are computed but only ever used
|
|
314
|
+
# relative to a later tag==999 record within the SAME inner
|
|
315
|
+
# loop -- see :286's fseek. No action at the outer level itself.
|
|
316
|
+
doffset1 = -rec[1]
|
|
317
|
+
|
|
318
|
+
n_tag_next = tag == 999
|
|
319
|
+
while not n_tag_next:
|
|
320
|
+
here = fh.tell()
|
|
321
|
+
rec = _read_dir(fh)
|
|
322
|
+
if rec is None:
|
|
323
|
+
# F5: EOF inside the inner directory loop. MATLAB has
|
|
324
|
+
# no guard here; it dies (or, for a partial tag-999
|
|
325
|
+
# record, leaves the loop) -- see _inner_loop_eof.
|
|
326
|
+
fh.seek(here)
|
|
327
|
+
rec = _inner_loop_eof(fh.read())
|
|
328
|
+
tag, offset = rec[0], rec[1]
|
|
329
|
+
|
|
330
|
+
if tag == _TAG_CIP and rec[2] == _NBYTES_CIP:
|
|
331
|
+
# :93-110 -- one more dir record (its tag/offset are
|
|
332
|
+
# what the tag==999 check below actually sees, since
|
|
333
|
+
# :109 re-reads the identical bytes), then a peek at
|
|
334
|
+
# the timestamp and 4098-byte payload that :108
|
|
335
|
+
# rewinds past -- net file-position effect is as if
|
|
336
|
+
# only the one dir record had been read. The rewind
|
|
337
|
+
# is real here, not simplified away: skipping it
|
|
338
|
+
# desyncs every read after the first CIP record in a
|
|
339
|
+
# file, which is exactly the bug this comment used to
|
|
340
|
+
# paper over.
|
|
341
|
+
rec2 = _read_dir(fh)
|
|
342
|
+
if rec2 is None:
|
|
343
|
+
# F5: :93's readDir returns short at EOF and :95's
|
|
344
|
+
# readTime reads nothing, so MATLAB dies at :97.
|
|
345
|
+
raise MatlabIndexError(
|
|
346
|
+
"read_binary_SEA.m:97: ttt(1) of an empty timestamp "
|
|
347
|
+
"-- EOF after a 33000/4098 directory record")
|
|
348
|
+
ttt = _read_time(fh)
|
|
349
|
+
payload = fh.read(4098)
|
|
350
|
+
# :108 rewinds a fixed 4150 bytes (16 + 36 + 4098) and
|
|
351
|
+
# :109 re-reads the directory record there. A payload
|
|
352
|
+
# cut to 4096 or 4097 bytes by the end of the file
|
|
353
|
+
# lands the rewind that many bytes early, and the
|
|
354
|
+
# re-read sees shifted bytes; the literal seek keeps
|
|
355
|
+
# that. The position is always past 4150 here.
|
|
356
|
+
fh.seek(fh.tell() - 4150)
|
|
357
|
+
reread = _read_dir(fh)
|
|
358
|
+
if len(payload) < 4096:
|
|
359
|
+
# :110 ``data = data1(1:4096)`` -- MATLAB dies with
|
|
360
|
+
# 'Index exceeds matrix dimensions' on a short read.
|
|
361
|
+
raise MatlabIndexError(
|
|
362
|
+
"read_binary_SEA.m:110: data1(1:4096) on a "
|
|
363
|
+
f"{len(payload)}-byte payload (truncated file)")
|
|
364
|
+
if reread is None: # pragma: no cover -- 16 bytes always follow
|
|
365
|
+
raise MatlabIndexError("read_binary_SEA.m:109: short re-read")
|
|
366
|
+
rec2 = reread
|
|
367
|
+
tag, offset = rec2[0], rec2[1]
|
|
368
|
+
data = np.frombuffer(payload[:4096], dtype=np.uint8)
|
|
369
|
+
|
|
370
|
+
decomp = _decompress_record(data)
|
|
371
|
+
if len(decomp) == 0:
|
|
372
|
+
# Every byte a no-op: :124-150 never assign `decomp`, :280 cleared
|
|
373
|
+
# the last record's, so :161's `decomp(i)` is an undefined variable
|
|
374
|
+
# and MATLAB dies (measured under Octave 11.3, 2026-09-27). A stream
|
|
375
|
+
# that decodes to something but has no 0xAA boundary is the graceful
|
|
376
|
+
# "weird" case below.
|
|
377
|
+
raise SeaUndefinedVariableError(
|
|
378
|
+
"read_binary_SEA.m:161: 'decomp' is undefined -- the CIP "
|
|
379
|
+
"record decompressed to nothing (every byte a no-op)")
|
|
380
|
+
start = _find_boundary_or_none(decomp, exact_sync=exact_sync)
|
|
381
|
+
if start is None:
|
|
382
|
+
n_weird += 1
|
|
383
|
+
else:
|
|
384
|
+
canvas = _reassemble_sea(decomp, start)
|
|
385
|
+
header = {
|
|
386
|
+
"year": ttt[0], "month": ttt[1], "day": ttt[2],
|
|
387
|
+
"hour": ttt[3], "minute": ttt[4], "second": ttt[5],
|
|
388
|
+
"millisec": ttt[6], "wkday": 1,
|
|
389
|
+
}
|
|
390
|
+
for name in ("year", "month", "day", "hour", "minute",
|
|
391
|
+
"second", "millisec", "wkday"):
|
|
392
|
+
f_cip.put_var(v_cip[name], kk_cip, 1, header[name])
|
|
393
|
+
f_cip.put_var(v_cip["data"], [0, 0, kk_cip],
|
|
394
|
+
[_IMG_ROWLEN, _IMG_BLOCKLEN, 1], canvas.T)
|
|
395
|
+
kk_cip += 1
|
|
396
|
+
|
|
397
|
+
elif tag == _TAG_2DC and rec[2] == _NBYTES_2DC:
|
|
398
|
+
raise SeaUndefinedVariableError(
|
|
399
|
+
"read_binary_SEA.m:270 references varid91, which is "
|
|
400
|
+
"never assigned by any netcdf.defVar in the source -- "
|
|
401
|
+
"MATLAB errors here, on the first 5000/4096-tagged "
|
|
402
|
+
"record; see arrayprobe.readers.sea's module docstring"
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
if tag == 999:
|
|
406
|
+
doffset2 = offset
|
|
407
|
+
n_tag_next = True
|
|
408
|
+
seek_by = doffset2 + doffset1
|
|
409
|
+
if seek_by:
|
|
410
|
+
# :285 MATLAB's fseek returns -1 and leaves the
|
|
411
|
+
# position unchanged for a target before BOF or
|
|
412
|
+
# past EOF (measured under Octave 8.4); Python's
|
|
413
|
+
# seek raises (before BOF) or succeeds (past EOF).
|
|
414
|
+
target = fh.tell() + seek_by
|
|
415
|
+
if 0 <= target <= os.fstat(fh.fileno()).st_size:
|
|
416
|
+
fh.seek(target)
|
|
417
|
+
|
|
418
|
+
if endfile:
|
|
419
|
+
break
|
|
420
|
+
|
|
421
|
+
# :290-297 -- the trailing EOF probe; see DMT's/PMS's module
|
|
422
|
+
# docstrings for why the outcome (not the byte-by-byte
|
|
423
|
+
# mechanism) is what this port reproduces.
|
|
424
|
+
peek = fh.read(16)
|
|
425
|
+
if len(peek) < 16:
|
|
426
|
+
endfile = True
|
|
427
|
+
else:
|
|
428
|
+
fh.seek(-16, 1)
|
|
429
|
+
finally:
|
|
430
|
+
f_cip.close()
|
|
431
|
+
f_2dc.close()
|
|
432
|
+
|
|
433
|
+
return kk_cip, n_weird, kk_2dc
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
import sys as _sys
|
|
3
|
+
import typing as _typing
|
|
4
|
+
|
|
5
|
+
from arrayprobe_uiops.readers import tasinfo as _impl
|
|
6
|
+
|
|
7
|
+
# Static view for type checkers only: the alias below replaces this module at run time,
|
|
8
|
+
# so mypy would otherwise see an empty module. No run-time effect.
|
|
9
|
+
if _typing.TYPE_CHECKING:
|
|
10
|
+
from arrayprobe_uiops.readers.tasinfo import * # noqa: F403
|
|
11
|
+
|
|
12
|
+
_sys.modules[__name__] = _impl
|