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.
Files changed (189) hide show
  1. arrayprobe/__init__.py +219 -0
  2. arrayprobe/__main__.py +19 -0
  3. arrayprobe/_v314_lazy.py +48 -0
  4. arrayprobe/ai_ready.py +212 -0
  5. arrayprobe/attributed_export.py +1333 -0
  6. arrayprobe/authordefaults.py +267 -0
  7. arrayprobe/cli.py +3510 -0
  8. arrayprobe/combine.py +12 -0
  9. arrayprobe/compare.py +950 -0
  10. arrayprobe/config.py +1239 -0
  11. arrayprobe/constants.py +12 -0
  12. arrayprobe/count_closure.py +204 -0
  13. arrayprobe/diagnostic_views.py +769 -0
  14. arrayprobe/diode_report.py +265 -0
  15. arrayprobe/driver/__init__.py +5 -0
  16. arrayprobe/driver/_load_record.py +331 -0
  17. arrayprobe/driver/preflight_inputs.py +250 -0
  18. arrayprobe/driver/reprocess.py +553 -0
  19. arrayprobe/driver/run_flight.py +8166 -0
  20. arrayprobe/driver/tas_from_probe_housekeeping.py +124 -0
  21. arrayprobe/dropsize.py +519 -0
  22. arrayprobe/friendly.py +206 -0
  23. arrayprobe/geometry/__init__.py +77 -0
  24. arrayprobe/geometry/min_rectangle.py +92 -0
  25. arrayprobe/intarr.py +12 -0
  26. arrayprobe/intarr_analysis.py +16 -0
  27. arrayprobe/intarr_plots.py +12 -0
  28. arrayprobe/inventory.py +129 -0
  29. arrayprobe/jit_kernels.py +344 -0
  30. arrayprobe/lwc.py +44 -0
  31. arrayprobe/masks.py +349 -0
  32. arrayprobe/matio.py +410 -0
  33. arrayprobe/matlab_compat.py +13 -0
  34. arrayprobe/ncio.py +26 -0
  35. arrayprobe/particle.py +55 -0
  36. arrayprobe/particle_perimeter.py +12 -0
  37. arrayprobe/products.py +78 -0
  38. arrayprobe/profiles.py +179 -0
  39. arrayprobe/provenance.py +21 -0
  40. arrayprobe/py.typed +11 -0
  41. arrayprobe/readers/__init__.py +9 -0
  42. arrayprobe/readers/dmt.py +603 -0
  43. arrayprobe/readers/mask_csv.py +12 -0
  44. arrayprobe/readers/pms.py +12 -0
  45. arrayprobe/readers/sea.py +433 -0
  46. arrayprobe/readers/tasinfo.py +12 -0
  47. arrayprobe/regimes.py +507 -0
  48. arrayprobe/render.py +728 -0
  49. arrayprobe/reproduce_report.py +149 -0
  50. arrayprobe/review_agreement.py +135 -0
  51. arrayprobe/review_sampling.py +327 -0
  52. arrayprobe/run_page.py +624 -0
  53. arrayprobe/sample_area.py +12 -0
  54. arrayprobe/selector.py +206 -0
  55. arrayprobe/sizing.py +151 -0
  56. arrayprobe/time_convert.py +12 -0
  57. arrayprobe/timing_closure.py +141 -0
  58. arrayprobe/tools/__init__.py +27 -0
  59. arrayprobe/tools/make_demo_flight.py +1089 -0
  60. arrayprobe/tools/make_tas_file.py +760 -0
  61. arrayprobe/tools/probe_comparison.py +569 -0
  62. arrayprobe/tools/synthetic_truth.py +698 -0
  63. arrayprobe/tools/tas_sources.py +705 -0
  64. arrayprobe/triage_context.py +488 -0
  65. arrayprobe/triage_server.py +5215 -0
  66. arrayprobe/tune/__init__.py +15 -0
  67. arrayprobe/tune/diodes.py +400 -0
  68. arrayprobe/tune/distributions.py +300 -0
  69. arrayprobe/tune/sample.py +243 -0
  70. arrayprobe/tune/score.py +230 -0
  71. arrayprobe/tune/settings.py +354 -0
  72. arrayprobe/validate_by_condition.py +208 -0
  73. arrayprobe/validation_report.py +166 -0
  74. arrayprobe-0.1.0.dist-info/METADATA +260 -0
  75. arrayprobe-0.1.0.dist-info/RECORD +189 -0
  76. arrayprobe-0.1.0.dist-info/WHEEL +5 -0
  77. arrayprobe-0.1.0.dist-info/entry_points.txt +2 -0
  78. arrayprobe-0.1.0.dist-info/licenses/LICENSE +31 -0
  79. arrayprobe-0.1.0.dist-info/licenses/LICENSES/BSD-3-Clause.txt +47 -0
  80. arrayprobe-0.1.0.dist-info/licenses/NOTICE.md +369 -0
  81. arrayprobe-0.1.0.dist-info/top_level.txt +4 -0
  82. arrayprobe_soda2/__init__.py +131 -0
  83. arrayprobe_soda2/angles.py +143 -0
  84. arrayprobe_soda2/aspect_ratio.py +541 -0
  85. arrayprobe_soda2/backfill.py +100 -0
  86. arrayprobe_soda2/binary_dof.py +181 -0
  87. arrayprobe_soda2/buildindex.py +113 -0
  88. arrayprobe_soda2/decompress_dmt.py +285 -0
  89. arrayprobe_soda2/enhancement_hooks.py +275 -0
  90. arrayprobe_soda2/export_ascii.py +732 -0
  91. arrayprobe_soda2/export_ncdf.py +683 -0
  92. arrayprobe_soda2/export_ncdf_raf.py +396 -0
  93. arrayprobe_soda2/fillholes.py +161 -0
  94. arrayprobe_soda2/find_radius.py +105 -0
  95. arrayprobe_soda2/findsize.py +407 -0
  96. arrayprobe_soda2/fit_poisson.py +170 -0
  97. arrayprobe_soda2/fix2dimage.py +63 -0
  98. arrayprobe_soda2/fixraking.py +162 -0
  99. arrayprobe_soda2/fixstuckbits.py +146 -0
  100. arrayprobe_soda2/hvps4_read_frame.py +121 -0
  101. arrayprobe_soda2/jit_kernels.py +152 -0
  102. arrayprobe_soda2/label_blobs.py +177 -0
  103. arrayprobe_soda2/min_circle_fast.py +320 -0
  104. arrayprobe_soda2/particlesort.py +703 -0
  105. arrayprobe_soda2/pbp_writer.py +558 -0
  106. arrayprobe_soda2/poisson_spot_correct.py +183 -0
  107. arrayprobe_soda2/princomp.py +107 -0
  108. arrayprobe_soda2/probespecs.py +361 -0
  109. arrayprobe_soda2/process_2d.py +1364 -0
  110. arrayprobe_soda2/process_2d_parallel.py +626 -0
  111. arrayprobe_soda2/process_hk.py +512 -0
  112. arrayprobe_soda2/processbuffer_2dc.py +155 -0
  113. arrayprobe_soda2/processbuffer_cip.py +213 -0
  114. arrayprobe_soda2/processbuffer_f2dc.py +139 -0
  115. arrayprobe_soda2/processbuffer_spec.py +441 -0
  116. arrayprobe_soda2/processbuffer_tail.py +350 -0
  117. arrayprobe_soda2/pth_readers.py +245 -0
  118. arrayprobe_soda2/py.typed +11 -0
  119. arrayprobe_soda2/read2dbuffer.py +371 -0
  120. arrayprobe_soda2/reject.py +246 -0
  121. arrayprobe_soda2/reprocess_pbp.py +156 -0
  122. arrayprobe_soda2/sample_area.py +264 -0
  123. arrayprobe_soda2/save_writer.py +480 -0
  124. arrayprobe_soda2/sea.py +324 -0
  125. arrayprobe_soda2/spec_absolutepointers.py +109 -0
  126. arrayprobe_soda2/spec_decompress.py +203 -0
  127. arrayprobe_soda2/spec_index.py +476 -0
  128. arrayprobe_soda2/spec_read_frame.py +286 -0
  129. arrayprobe_soda2/spec_read_hk.py +301 -0
  130. arrayprobe_soda2/spec_readint.py +127 -0
  131. arrayprobe_soda2/timeconv.py +77 -0
  132. arrayprobe_soda2/update_op.py +101 -0
  133. arrayprobe_uiops/__init__.py +45 -0
  134. arrayprobe_uiops/combine.py +192 -0
  135. arrayprobe_uiops/combine_fork.py +359 -0
  136. arrayprobe_uiops/constants.py +830 -0
  137. arrayprobe_uiops/decode_naming.py +113 -0
  138. arrayprobe_uiops/diode_mask.py +250 -0
  139. arrayprobe_uiops/engine_absence.py +41 -0
  140. arrayprobe_uiops/export_oap.py +325 -0
  141. arrayprobe_uiops/geometry_core.py +366 -0
  142. arrayprobe_uiops/habit.py +749 -0
  143. arrayprobe_uiops/habit_core.py +248 -0
  144. arrayprobe_uiops/hollow_size_rule.py +376 -0
  145. arrayprobe_uiops/imgproc.py +2081 -0
  146. arrayprobe_uiops/imgproc_core.py +275 -0
  147. arrayprobe_uiops/intarr.py +167 -0
  148. arrayprobe_uiops/intarr_analysis.py +569 -0
  149. arrayprobe_uiops/intarr_plots.py +119 -0
  150. arrayprobe_uiops/jit_kernels.py +430 -0
  151. arrayprobe_uiops/lwc.py +70 -0
  152. arrayprobe_uiops/matlab_compat.py +860 -0
  153. arrayprobe_uiops/measure.py +346 -0
  154. arrayprobe_uiops/min_circle.py +204 -0
  155. arrayprobe_uiops/min_ellipse.py +161 -0
  156. arrayprobe_uiops/min_rectangle.py +200 -0
  157. arrayprobe_uiops/ncar_2dc_timing.py +100 -0
  158. arrayprobe_uiops/ncio.py +810 -0
  159. arrayprobe_uiops/oap_read.py +228 -0
  160. arrayprobe_uiops/particle.py +51 -0
  161. arrayprobe_uiops/particle_perimeter.py +94 -0
  162. arrayprobe_uiops/particle_physics.py +199 -0
  163. arrayprobe_uiops/pecan_params.py +134 -0
  164. arrayprobe_uiops/probe_cases.py +298 -0
  165. arrayprobe_uiops/product_sanity.py +337 -0
  166. arrayprobe_uiops/profile.py +71 -0
  167. arrayprobe_uiops/profiles.py +756 -0
  168. arrayprobe_uiops/provenance.py +804 -0
  169. arrayprobe_uiops/py.typed +11 -0
  170. arrayprobe_uiops/qc.py +169 -0
  171. arrayprobe_uiops/readers/__init__.py +8 -0
  172. arrayprobe_uiops/readers/mask_csv.py +74 -0
  173. arrayprobe_uiops/readers/pms.py +239 -0
  174. arrayprobe_uiops/readers/tasinfo.py +57 -0
  175. arrayprobe_uiops/rect_ellipse.py +129 -0
  176. arrayprobe_uiops/register_image_assisted.py +25 -0
  177. arrayprobe_uiops/reject.py +501 -0
  178. arrayprobe_uiops/reject_core.py +220 -0
  179. arrayprobe_uiops/sample_area.py +93 -0
  180. arrayprobe_uiops/sizedist.py +1923 -0
  181. arrayprobe_uiops/sizedist_core.py +814 -0
  182. arrayprobe_uiops/sizedist_tables.py +642 -0
  183. arrayprobe_uiops/spec.py +1094 -0
  184. arrayprobe_uiops/spec_core.py +703 -0
  185. arrayprobe_uiops/stray_pixel.py +103 -0
  186. arrayprobe_uiops/tas.py +191 -0
  187. arrayprobe_uiops/time_convert.py +41 -0
  188. uiops/__init__.py +402 -0
  189. uiops/py.typed +11 -0
@@ -0,0 +1,324 @@
1
+ # SPDX-License-Identifier: BSD-3-Clause
2
+ # Reproduces SODA-2, Copyright (c) 2016 UCAR; see LICENSES/BSD-3-Clause.txt and NOTICE.md.
3
+ """SODA-2's SEA M300 buffer walk for the CIP : ``readdatadir.pro``,
4
+ ``seaimagepointers.pro`` and ``readseabuffer_caps.pro``, ported whole.
5
+
6
+ Author-directed 2026-09-15 (the open SODA-2 decision answered for the
7
+ CIP): *"Prioritize also the soda2 port and soda processing of the cip data
8
+ using this package alone"*. This is the SODA-2 route through a UND ``.sea``
9
+ file, ported from the IDL, **not** borrowed from ``arrayprobe.readers.sea``
10
+ (the UIOOPS-side reader, which reads the same tag ``33000``/``4098`` records by
11
+ its own logic): the two walks differ in where they look for the next buffer,
12
+ which time tag they accept and when they give up, so sharing code would make
13
+ the SODA-2 port reproduce UIOOPS's reading instead of SODA-2's.
14
+
15
+ Vendored sources (never edited): ``SODA2/abansemer-soda2-afcea15/
16
+ readdatadir.pro`` (9 lines), ``seaimagepointers.pro`` (83),
17
+ ``readseabuffer_caps.pro`` (48), ``sfm.pro`` (10, identical in body to
18
+ ``hms2sfm.pro``, reused from :mod:`timeconv`). GDL bank:
19
+ ``gdl_oracle/run_soda2_cip_bank.py``.
20
+
21
+ **The file layout these routines assume.** An SEA file is a chain of
22
+ *buffers*; each starts with a list of 16-byte data directories
23
+ (``readdatadir``: ``tagNumber, dataOffset, numberBytes, samples,
24
+ bytesPerSample`` UINT, ``type, parameter1..3`` BYTE, ``address`` UINT) ended
25
+ by tag ``999``, whose ``dataOffset`` (plus ``parameter1*65536``) is the
26
+ distance to the next buffer. Tag ``0`` with 36 (or 18) bytes is the time
27
+ record; the probe's image tag (``op.seatag[0]``, ``33000`` for the UND CIP)
28
+ points at the image bytes. All reads are native little-endian (``readu`` on
29
+ x86, no ``swap_endian``).
30
+
31
+ **Line-by-line points worth knowing** (the no-image, short-image, >320000 and end-of-file
32
+ cases are in the bank; the rest are read):
33
+
34
+ - ``seaimagepointers.pro:8`` -- a walk that starts within 5000 bytes of the
35
+ end of file returns ``eof`` without reading anything.
36
+ - ``:17-22`` -- ``imagepoint``, ``timepoint``, ``numberbytes`` are set
37
+ **once**, before the outer loop, and never reset between buffers: a buffer
38
+ with no image tag keeps the previous buffer's values (harmless in the
39
+ measured cases because the stale ``numberbytes`` is the one that made the
40
+ previous buffer skip; no bank case isolates it -- read, not measured).
41
+ - ``:37`` -- the image test is ``tagnumber eq tag[0]`` and is tested
42
+ independently of the time test, so ``tag[0] = 0`` would make every time
43
+ record an "image"; ``readseabuffer_caps.pro:5`` stops on ``tag[0] eq 0``
44
+ before that can happen.
45
+ - ``:56`` -- when any directory in the buffer has ``numberBytes+dataOffset
46
+ >= 65535`` the ``999`` record's own offset is replaced by that maximum.
47
+ - ``:62-64`` -- two end-of-file guards, one against the buffer's own extent
48
+ and one against the next directory read.
49
+ - ``readseabuffer_caps.pro:18`` -- the start time is ``hh*10000 + mm*100 +
50
+ ss + frac_num/frac_den`` in DOUBLE, ``:38`` zeroes it above ``320000``;
51
+ the stop time (``:22``) has no such guard.
52
+ - ``:34`` -- the CIP image is ``bytarr(numberbytes)``: the whole 4098-byte
53
+ record, 2 bytes more than the 4096-byte DMT compressed image
54
+ ``decompress_dmt3`` was written for (it is given all 4098).
55
+ """
56
+ from __future__ import annotations
57
+
58
+ import os
59
+ import struct
60
+ from dataclasses import dataclass
61
+ from typing import BinaryIO
62
+
63
+ import numpy as np
64
+
65
+ __all__ = [
66
+ "DataDir",
67
+ "SeaPointers",
68
+ "SeaBuffer",
69
+ "SeaEOF",
70
+ "readdatadir",
71
+ "seaimagepointers",
72
+ "readseabuffer_caps",
73
+ "idl_sfm",
74
+ "CIP1D_DTYPE",
75
+ ]
76
+
77
+ _DATADIR = struct.Struct("<HHHHHBBBBH")
78
+
79
+ #: ``readseabuffer_caps.pro:26-28`` -- the CIP-1D record, 180 bytes. Every
80
+ #: field is INT except ``particlecounter`` (UINT, ``0us``); ``hostsynccounter``
81
+ #: is declared ``02``, an INT literal.
82
+ CIP1D_DTYPE = np.dtype([
83
+ ("header", "<i2"), ("bytecount", "<i2"), ("oversizereject", "<i2"),
84
+ ("count", "<i2", (62,)), ("dofreject", "<i2"), ("endreject", "<i2"),
85
+ ("housekeeping", "<i2", (16,)), ("particlecounter", "<u2"),
86
+ ("secmsec", "<i2"), ("hourmin", "<i2"), ("hostsynccounter", "<i2"),
87
+ ("resetflag", "<i2"), ("checksum", "<i2"), ("trailer", "<i2"),
88
+ ])
89
+
90
+
91
+ class SeaReadError(RuntimeError):
92
+ """An IDL halt this module reproduces as a raise, naming the line."""
93
+
94
+
95
+ @dataclass
96
+ class DataDir:
97
+ """``readdatadir.pro:4-5``'s ``datadir`` structure."""
98
+
99
+ tagnumber: int
100
+ dataoffset: int
101
+ numberbytes: int
102
+ samples: int
103
+ bytespersample: int
104
+ type: int
105
+ parameter1: int
106
+ parameter2: int
107
+ parameter3: int
108
+ address: int
109
+
110
+
111
+ @dataclass
112
+ class SeaEOF:
113
+ """``{starttime:999999, image:0, eof:1}`` -- the end-of-file return of
114
+ ``seaimagepointers.pro:8,62,64`` and ``readseabuffer_caps.pro:9``."""
115
+
116
+ starttime: int = 999999
117
+ image: int = 0
118
+ eof: int = 1
119
+
120
+
121
+ @dataclass
122
+ class SeaPointers:
123
+ """``seaimagepointers.pro:81-82``'s return."""
124
+
125
+ timepoint: int
126
+ imagepoint: int
127
+ numberbytes: int
128
+ cur_ptr: int
129
+ elapsedpoint: int
130
+ taspoint: int
131
+ nextpointer: int
132
+ eof: int = 0
133
+
134
+
135
+ @dataclass
136
+ class SeaBuffer:
137
+ """``readseabuffer_caps.pro:45-47``'s return (``eof == 0``)."""
138
+
139
+ starttime: float
140
+ stoptime: float
141
+ year: int
142
+ month: int
143
+ day: int
144
+ image: np.ndarray
145
+ difftime: float
146
+ eof: int
147
+ pointer: int
148
+ tas: int
149
+ probetime: float
150
+ imagepoint: int
151
+ numberbytes: int
152
+ timepoint: int
153
+
154
+
155
+ def _fsize(f: BinaryIO) -> int:
156
+ return os.fstat(f.fileno()).st_size
157
+
158
+
159
+ def readdatadir(f: BinaryIO) -> DataDir:
160
+ """``readdatadir.pro`` -- one 16-byte directory at the current position.
161
+ A short read is IDL's ``readu`` "End of file encountered" halt."""
162
+ raw = f.read(16)
163
+ if len(raw) < 16:
164
+ raise SeaReadError(
165
+ f"readdatadir.pro:7: readu hit end of file ({len(raw)} of 16 bytes)")
166
+ return DataDir(*_DATADIR.unpack(raw))
167
+
168
+
169
+ def seaimagepointers(f: BinaryIO, tag: int | np.ndarray | list[int]) -> SeaPointers | SeaEOF:
170
+ """``seaimagepointers.pro:1-83``. ``tag`` is a scalar (``readseabuffer_caps``
171
+ always passes one) or up to three tags ``[image, tas, elapsed]``."""
172
+ tags = np.atleast_1d(np.asarray(tag)).astype(np.int64)
173
+ q_size = _fsize(f)
174
+ q_cur = f.tell()
175
+ lastpointer = q_cur # :7
176
+ if q_size - q_cur <= 5000: # :8
177
+ return SeaEOF()
178
+
179
+ tas_tagexists = tags.size >= 2 and int(tags[1]) > 0 # :13
180
+ elapsed_tagexists = tags.size == 3 and int(tags[2]) > 0 # :14
181
+
182
+ imagepoint = 0 # :17-22, set once
183
+ timepoint = 0
184
+ elapsedpoint = 0
185
+ taspoint = 0
186
+ numberbytes = 4098
187
+ tag0 = int(tags[0])
188
+
189
+ while True: # :24
190
+ maxbytes = 0
191
+ while True: # :26
192
+ buf = readdatadir(f)
193
+ maxbytes = max(maxbytes, buf.numberbytes + buf.dataoffset) # :28
194
+ if buf.tagnumber == 0 and buf.numberbytes in (36, 18): # :31
195
+ timepoint = lastpointer + buf.dataoffset
196
+ if buf.tagnumber == tag0: # :37
197
+ imagepoint = lastpointer + buf.dataoffset
198
+ numberbytes = buf.numberbytes
199
+ if tas_tagexists and buf.tagnumber == int(tags[1]): # :44
200
+ taspoint = lastpointer + buf.dataoffset
201
+ if elapsed_tagexists and buf.tagnumber == int(tags[2]): # :47
202
+ elapsedpoint = lastpointer + buf.dataoffset
203
+ if buf.tagnumber == 999: # :50-57
204
+ dataoffset = buf.dataoffset
205
+ if maxbytes >= 65535:
206
+ dataoffset = maxbytes
207
+ if lastpointer + maxbytes + 16 >= q_size: # :62
208
+ return SeaEOF()
209
+ if f.tell() + 16 >= q_size: # :64
210
+ return SeaEOF()
211
+ if buf.tagnumber == 999: # :65
212
+ break
213
+
214
+ step = dataoffset + buf.parameter1 * 65536
215
+ if imagepoint == 0 or timepoint == 0 or numberbytes < 100: # :68-72
216
+ lastpointer = lastpointer + step
217
+ f.seek(lastpointer)
218
+ else: # :73-77
219
+ nextpointer = lastpointer + step
220
+ break
221
+
222
+ return SeaPointers(timepoint=timepoint, imagepoint=imagepoint,
223
+ numberbytes=numberbytes, cur_ptr=q_cur,
224
+ elapsedpoint=elapsedpoint, taspoint=taspoint,
225
+ nextpointer=nextpointer)
226
+
227
+
228
+ def _hhmmss(t: np.ndarray) -> float:
229
+ """``readseabuffer_caps.pro:18,22`` -- ``t[3]*10000d + t[4]*100d + t[5] +
230
+ t[6]/double(t[7])``, left to right in DOUBLE. A zero denominator is IEEE
231
+ (IDL prints a floating-point warning and continues)."""
232
+ with np.errstate(divide="ignore", invalid="ignore"):
233
+ frac = np.float64(t[6]) / np.float64(t[7])
234
+ return float(np.float64(t[3]) * 10000.0 + np.float64(t[4]) * 100.0
235
+ + np.float64(t[5]) + frac)
236
+
237
+
238
+ def _idl_long(v: float) -> int:
239
+ """IDL ``long()`` of a DOUBLE: truncation toward zero; a NaN, an infinity
240
+ or a value outside the LONG range gives ``-2147483648`` (measured under
241
+ GDL 1.1.2 -- IDL's own value for these is platform-defined)."""
242
+ if not np.isfinite(v) or not (-2147483648.0 <= v < 2147483648.0):
243
+ return -2147483648
244
+ return int(v)
245
+
246
+
247
+ def _idl_ldiv(a: int, b: int) -> int:
248
+ q = abs(a) // abs(b)
249
+ return q if (a >= 0) == (b >= 0) else -q
250
+
251
+
252
+ def idl_sfm(hms: float) -> float:
253
+ """``sfm.pro`` / ``hms2sfm.pro`` (identical bodies) for a DOUBLE ``hms``,
254
+ including a non-finite or out-of-LONG-range one: every step is the IDL
255
+ LONG arithmetic, then ``fraction = double(hms) - long(hms)`` is added, so
256
+ a NaN or infinite ``hms`` gives back the same non-finite value whatever
257
+ ``long()`` returned (derived; the GDL value of ``long()`` is used)."""
258
+ lv = _idl_long(hms)
259
+ hour = _idl_ldiv(lv, 10000)
260
+ minute = _idl_ldiv(lv - hour * 10000, 100)
261
+ second = lv - hour * 10000 - minute * 100
262
+ fraction = float(np.float64(hms) - np.float64(lv))
263
+ return float(hour * 3600 + minute * 60 + second) + fraction
264
+
265
+
266
+ def readseabuffer_caps(f: BinaryIO, tag: int | np.ndarray, probetype: str) -> SeaBuffer | SeaEOF:
267
+ """``readseabuffer_caps.pro:1-48``, ``probetype`` ``'CIP'``/``'CIPG'`` (a
268
+ ``numberbytes``-long BYTE image) or ``'CIP1D'`` (a :data:`CIP1D_DTYPE`
269
+ record). ``'CAS'`` is refused by name (no CAS route in this port)."""
270
+ tags = np.atleast_1d(np.asarray(tag))
271
+ if int(tags[0]) == 0: # :5
272
+ raise SeaReadError(
273
+ "readseabuffer_caps.pro:5: stop -- Enter correct tag numbers into "
274
+ "probeversion.pro or soda2_probespecs.pro")
275
+ if probetype == "CAS":
276
+ raise NotImplementedError(
277
+ "readseabuffer_caps.pro:29-31: the CAS record is not ported")
278
+
279
+ x = seaimagepointers(f, tag) # :8
280
+ if isinstance(x, SeaEOF): # :9
281
+ return SeaEOF()
282
+
283
+ f.seek(x.timepoint) # :12
284
+ raw = f.read(18)
285
+ if len(raw) < 18:
286
+ raise SeaReadError("readseabuffer_caps.pro:14: readu hit end of file")
287
+ t = np.frombuffer(raw, dtype="<i2")
288
+ year, month, day = int(t[0]), int(t[1]), int(t[2])
289
+ hhmmss = _hhmmss(t) # :18
290
+ raw = f.read(18) # :21
291
+ if len(raw) < 18:
292
+ raise SeaReadError("readseabuffer_caps.pro:21: readu hit end of file")
293
+ hhmmss_stop = _hhmmss(np.frombuffer(raw, dtype="<i2")) # :22
294
+
295
+ f.seek(x.imagepoint) # :33
296
+ if probetype == "CIP1D":
297
+ nbytes = CIP1D_DTYPE.itemsize
298
+ else:
299
+ nbytes = x.numberbytes # :34
300
+ raw = f.read(nbytes) # :35
301
+ if len(raw) < nbytes:
302
+ raise SeaReadError("readseabuffer_caps.pro:35: readu hit end of file")
303
+ image: np.ndarray
304
+ if probetype == "CIP1D":
305
+ image = np.frombuffer(raw, dtype=CIP1D_DTYPE)[0:1].copy()
306
+ else:
307
+ image = np.frombuffer(raw, dtype=np.uint8).copy()
308
+
309
+ if hhmmss > 320000: # :38
310
+ hhmmss = 0.0
311
+ probetime = 0.0 # :39 (INT 0)
312
+ if probetype == "CIP1D": # :40-41
313
+ hm = int(image["hourmin"][0]) # type: ignore[call-overload]
314
+ sm = int(image["secmsec"][0]) # type: ignore[call-overload]
315
+ probetime = (((hm & 1984) >> 6) * 10000.0 + (hm & 63) * 100.0
316
+ + ((sm & 64512) >> 10) + (sm & 1023) / 1000.0)
317
+
318
+ f.seek(x.nextpointer) # :44
319
+ return SeaBuffer(starttime=hhmmss, stoptime=hhmmss_stop, year=year,
320
+ month=month, day=day, image=image,
321
+ difftime=idl_sfm(hhmmss_stop) - idl_sfm(hhmmss),
322
+ eof=0, pointer=x.cur_ptr, tas=100, probetime=probetime,
323
+ imagepoint=x.imagepoint, numberbytes=x.numberbytes,
324
+ timepoint=x.timepoint)
@@ -0,0 +1,109 @@
1
+ # SPDX-License-Identifier: BSD-3-Clause
2
+ # Reproduces SODA-2, Copyright (c) 2016 UCAR; see LICENSES/BSD-3-Clause.txt and NOTICE.md.
3
+ """``spec_absolutepointers.pro`` -- turn a `spec_index` result and a list of
4
+ buffer numbers into one flat array of absolute image-frame file offsets
5
+ (P-b).
6
+
7
+ 22 lines of IDL. Signature `spec_absolutepointers(buffnum, p)`, where `p` is
8
+ an IDL *pointer* to the struct `spec_index.pro` returns and `buffnum` is a
9
+ scalar or array of buffer indices. **This port takes the struct itself, not
10
+ a pointer to it** -- Python has no equivalent indirection worth reproducing
11
+ here, and no test distinguishes "the struct" from "a pointer to the
12
+ struct". One caller in the vendored tree: `soda2_bitimage.pro:16`, with an
13
+ index built **without** `/absolutepointer`.
14
+
15
+ Validated against GDL by ``tests/test_soda2_spec_index.py`` (bank at
16
+ ``gdl_oracle/run_soda2_spec_index_bank.py``, 9 cases, all matching).
17
+
18
+ **Line-by-line** (IDL 0-based indexing):
19
+
20
+ - ``:8`` -- `n = (*p).numimages[buffnum]` -- one `numimages` value per
21
+ requested buffer, in `buffnum`'s own order (not sorted).
22
+ - ``:9-10`` -- `nt = total(n)` -- **FLOAT**, the count of images across
23
+ every requested buffer; `:10` returns a zero pointer with a zero count when `nt` is 0 -- two
24
+ scalar **INT** zeros (`n_elements(ap)=1`), not empty arrays. Bank-measured
25
+ (`ap_main_5_empty`, buffer 5's own `numimages=0`, the never-walked last
26
+ buffer): `AP=0 N=0 TN=INT,INT NEL=1`.
27
+ - ``:11`` -- `ap = ulon64arr(nt)`, pre-sized to the total image count.
28
+ - ``:12-20`` -- `FOR i=0, n_elements(buffnum)-1`: skip any buffer with
29
+ `n[i]` (that buffer's own image count) `<= 0`; otherwise `istart =
30
+ firstp[buffnum[i]]`, `istop = firstp[buffnum[i]] + numimages[buffnum[i]] -
31
+ 1` (an **inclusive** IDL slice), and `ap[c:c+n[i]-1] =
32
+ pointer[buffnum[i]] + imagep[istart:istop]` -- `pointer` (ULONG64) plus
33
+ `imagep` (INT or LONG64, signed) promotes to ULONG64 (measured, the same
34
+ rule this project's `spec_read_hk` docstring already names for
35
+ ULONG+INT). Results are written **in `buffnum`'s own order**, not sorted
36
+ by buffer number (bank-measured, `ap_main_41` with `buffnum=[4,1]`:
37
+ buffer 4's four pointers come first, then buffer 1's six).
38
+ - ``:21`` -- returns `{ap: ULONG64[nt], n: FLOAT(nt)}`.
39
+
40
+ **Feeding this an index built WITH `/absolutepointer` double-counts**: the
41
+ index's own `imagep` is already an absolute file offset, and `:17` adds
42
+ `pointer[buffnum[i]]` again on top. Nobody in the vendored tree does this --
43
+ `soda2_bitimage.pro:16` always builds its index without the keyword -- but
44
+ the bank measures it anyway (`ap_main_1_absindex`): the same six images
45
+ that `ap_main_1` reports as `4250,4268,4346,4514,4638,8130` come back as
46
+ `8364,8382,8460,8628,8752,12244`, each exactly `4114` (buffer 1's own
47
+ `pointer` value) higher. Latent, not a finding of its own -- reported under
48
+ "what turned out to be fine" alongside `spec_read_hk`'s own arithmetic
49
+ checks, because the routine is doing exactly what its one caller's own
50
+ index shape asks of it.
51
+ """
52
+ from __future__ import annotations
53
+
54
+ from collections.abc import Sequence
55
+ from dataclasses import dataclass
56
+
57
+ import numpy as np
58
+
59
+ from .spec_index import SpecIndex
60
+
61
+ __all__ = ["AbsolutePointers", "spec_absolutepointers"]
62
+
63
+
64
+ @dataclass
65
+ class AbsolutePointers:
66
+ """`spec_absolutepointers`'s return struct (`:21`). Empty (`nt==0`,
67
+ `:10`) is IDL's own `{ap:0, n:0}`: both fields collapse to a scalar INT
68
+ zero (`n_elements=1`), not a length-0 array -- reproduced here as
69
+ ``np.int16(0)`` rather than ``np.ndarray([])``.
70
+
71
+ Source: ``spec_absolutepointers.pro:21`` (SODA-2)."""
72
+
73
+ ap: np.ndarray | np.int16 # uint64[nt] non-empty; np.int16(0) when nt==0
74
+ n: np.float32 | np.int16 # float32(nt) non-empty; np.int16(0) when nt==0
75
+
76
+
77
+ def spec_absolutepointers(
78
+ buffnum: int | Sequence[int],
79
+ index: SpecIndex,
80
+ ) -> AbsolutePointers:
81
+ """``a = spec_absolutepointers(buffnum, p)`` -- `p` is `index` itself
82
+ here, not a pointer to it (see the module docstring). `buffnum` may be
83
+ a single buffer number or a sequence of them; results are written in
84
+ that exact order, never sorted.
85
+
86
+ Source: ``spec_absolutepointers.pro`` (SODA-2).
87
+ """
88
+ buffnum_arr = np.atleast_1d(np.asarray(buffnum, dtype=np.int64))
89
+ n = index.numimages[buffnum_arr] # `:8` -- one count per requested buffer
90
+
91
+ nt = int(sum(int(v) for v in n))
92
+ if nt == 0: # `:10` -- IDL's `{ap:0, n:0}`, both scalar INT.
93
+ return AbsolutePointers(ap=np.int16(0), n=np.int16(0))
94
+
95
+ ap = np.zeros(nt, dtype=np.uint64)
96
+ c = 0
97
+ for i in range(len(buffnum_arr)):
98
+ ni = int(n[i])
99
+ if ni > 0: # `:14`
100
+ b = int(buffnum_arr[i])
101
+ istart = int(index.firstp[b])
102
+ istop = istart + ni # Python's half-open end == IDL's inclusive istop + 1
103
+ ap[c:c + ni] = (
104
+ np.uint64(index.pointer[b])
105
+ + index.imagep[istart:istop].astype(np.uint64)
106
+ )
107
+ c += ni
108
+
109
+ return AbsolutePointers(ap=ap, n=np.float32(nt))
@@ -0,0 +1,203 @@
1
+ # SPDX-License-Identifier: BSD-3-Clause
2
+ # Reproduces SODA-2, Copyright (c) 2016 UCAR; see LICENSES/BSD-3-Clause.txt and NOTICE.md.
3
+ """``spec_decompress.pro`` -- one buffer's compressed words to a bit image.
4
+
5
+ 66 lines of IDL that turn a run of 16-bit compressed SPEC words into a
6
+ ``bytarr(128, numslices)`` shadow/clear bitimage. This is the first of the two
7
+ SPEC buffer-decode leaves (the other is `spec_readint`); the
8
+ 748-line `soda2_processbuffer.pro` that calls this is out of scope.
9
+
10
+ Provenance: ``SODA2/abansemer-soda2-afcea15/spec_decompress.pro`` (whole
11
+ file), Aaron Bansemer, NCAR, 10/2011. Copyright (C) 2016 UCAR, 3-clause BSD.
12
+ Validated against GDL by ``tests/test_soda2_spec_decompress.py``; bank at
13
+ ``gdl_oracle/run_soda2_spec_decompress_bank.py``, 16 cases, all matching.
14
+
15
+ **Each compressed word packs four bit fields**, tested with masks against a
16
+ UINT value (`spec_readint` is its producer):
17
+
18
+ ====== ========= =========================================================
19
+ bits field meaning
20
+ ====== ========= =========================================================
21
+ 15 timeslice 1 = timing/overflow word; the decode loop skips it whole
22
+ 14 startslice 1 = this word starts a new output slice, resets ``idiode``
23
+ 7-13 numshaded run length of shaded (1) diodes
24
+ 0-6 numclear run length of clear (0) diodes, or the new diode offset
25
+ ====== ========= =========================================================
26
+
27
+ Two IDL constants are magic **values**, checked by equality against the whole
28
+ word rather than by field, because both happen to also be well-formed
29
+ ordinary words under the bit layout above:
30
+
31
+ - ``'7fff'x`` (32767) -- a full clear line. Its own bit-14 is set, so the
32
+ ordinary startslice branch runs FIRST (``islice`` increments, ``idiode``
33
+ is set to its numclear field, 127) and *then* the clear-line special case
34
+ overwrites the whole new slice to 0 and resets ``idiode`` to 0. Under
35
+ ``version=1`` (HVPS4/3VCPI only; never the original 2D-S, which is version 0)
36
+ it additionally means "8 words of uncompressed diode data follow" --
37
+ see below.
38
+ - ``'4000'x`` (16384) -- a full solid line, bit-14 alone. It also carries
39
+ startslice=1 (a new slice), numshaded=0, so it falls into the *else*
40
+ branch of the ``numshaded[i] gt 0`` test and is caught there by value.
41
+
42
+ **The array-layout convention.** IDL's ``bitarr(128, numslices)`` is
43
+ diode-major; this port returns **``(numslices, 128)``**, the package's
44
+ standing transpose (the sizing core's design record, item 1) -- callers index
45
+ ``bitimage[slice, diode]``.
46
+
47
+ **Findings:**
48
+
49
+ - **S4-a** (`:14`, `:21`) -- ``numslices = total(startslice) + 3`` allocates
50
+ three slices beyond the count of startslice-bearing words, and ``islice``
51
+ is incremented *before* any word's data is ever written to it, so **slice
52
+ 0 of the returned array is always blank** -- confirmed against every case
53
+ in the bank, none of which ever writes to row 0.
54
+ - **S4-c** (`:46`) -- the overflow guard (``idiode+numshaded-1 lt 128``)
55
+ drops an over-length run **without rolling `idiode` back**: the next word's
56
+ run starts from wherever the startslice branch left `idiode`, not from
57
+ where the dropped run would have ended. Measured with
58
+ ``overflow_word_dropped``: a run that would end at diode 129 is dropped,
59
+ and the following 3-diode run lands at ``[120, 123)``, not ``[130, 133)``.
60
+
61
+ **U1, resolved by the bank, not assumed.** `:42` (``i+=8``, version=1 only)
62
+ reassigns the FOR loop's own counter inside its body. IDL/GDL FOR loops
63
+ re-read the loop variable each pass, so the reassignment **is honoured**:
64
+ the next iteration resumes at ``i+8`` **plus** the loop's own automatic
65
+ ``+1``, i.e. at ``i+9`` from where the branch started, not merely
66
+ ``i+1``. Measured with ``v1_clear_then_8_words``: the golden's slice content
67
+ is bit-for-bit what you get by (a) treating indices ``[i+1, i+8]`` as one
68
+ consumed block and (b) resuming at ``i+9`` -- and is *not* what you get by
69
+ re-visiting those same indices as ordinary compressed words. This port uses
70
+ an explicit ``while`` loop over a mutable index so the ``i += 8`` can be
71
+ written exactly as the source writes it, rather than a Python ``for`` loop
72
+ over a fixed range, which cannot express a body reassigning its own counter.
73
+
74
+ **The version=1 uncompressed block, decoded exactly.** ``dec2bin16`` maps
75
+ row ``r`` of its output to bit ``15-r`` of the input (MSB first); ``reverse``
76
+ with no dimension argument reverses along IDL's first (fastest-varying)
77
+ dimension, undoing that -- row ``r`` of the reversed array is bit ``r``
78
+ (LSB first). ``1 - reverse(dec2bin16(...))`` then bit-inverts. Assigning an
79
+ IDL ``(16, 8)`` array into a 128-element target reads it column-major (all
80
+ 16 rows of column 0, then column 1, ...), so word ``j`` of the 8-word block
81
+ lands at diodes ``[16*j, 16*j+16)``, LSB first. Net: ``diode[16*j+r] =
82
+ 1 - bit_r(word_j)`` for ``j`` in 0..7, ``r`` in 0..15. Reproduced by
83
+ ``_decode_uncompressed_block`` below rather than through NumPy bit-twiddling
84
+ functions, so the mapping stays legible against the IDL line it replaces.
85
+ """
86
+ from __future__ import annotations
87
+
88
+ import numpy as np
89
+ from numpy.typing import ArrayLike
90
+
91
+ __all__ = ["spec_decompress"]
92
+
93
+ _TIMESLICE_MASK = 0x8000
94
+ _STARTSLICE_MASK = 0x4000
95
+ _NUMSHADED_MASK = 0x3F80
96
+ _NUMCLEAR_MASK = 0x007F
97
+ _CLEAR_LINE = 0x7FFF
98
+ _SOLID_LINE = 0x4000
99
+ _NDIODES = 128
100
+
101
+
102
+ def _decode_uncompressed_block(words: np.ndarray) -> np.ndarray:
103
+ """``1-reverse(dec2bin16(words))``, flattened column-major (`:41`).
104
+
105
+ ``words`` is exactly 8 elements. Returns 128 uint8 values, LSB of
106
+ ``words[0]`` first.
107
+ """
108
+ out = np.empty(_NDIODES, dtype=np.uint8)
109
+ for j in range(8):
110
+ word = int(words[j])
111
+ for r in range(16):
112
+ out[16 * j + r] = 1 - ((word >> r) & 1)
113
+ return out
114
+
115
+
116
+ def spec_decompress(cimage: ArrayLike, overload: int, version: int = 0) -> np.ndarray:
117
+ """``bi = spec_decompress(cimage, overload, version=version)``.
118
+
119
+ Source: ``spec_decompress.pro`` (SODA-2).
120
+
121
+ ``cimage`` is a sequence of 16-bit unsigned compressed words (as produced
122
+ by `spec_readint`). ``overload`` is truthy/1 when the frame's overload
123
+ flag is set, which drops the last two words from the decode (`:23`).
124
+ ``version`` selects the HVPS4/3VCPI 8-word-uncompressed-block convention
125
+ (1) versus the original 2D-S/HVPS3 convention (0).
126
+
127
+ Returns a ``(numslices, 128)`` ``uint8`` array, row-major over slices --
128
+ the transpose of IDL's ``bitarr(128, numslices)``. Row 0 is always blank
129
+ (SODA-2 register entry S4-a); no clamping, no bounds "fix" beyond what `:46` itself
130
+ already does (SODA-2 register entry S4-c is preserved, not corrected).
131
+ """
132
+ cimage_u16 = np.asarray(cimage, dtype=np.uint16)
133
+ n = int(cimage_u16.size)
134
+ # Widen before masking: IDL's UINT `and` an INT hex literal promotes to
135
+ # LONG, and the values here (0-65535) are unaffected by that promotion --
136
+ # int64 keeps every intermediate exact with no sign ambiguity (U3).
137
+ words = cimage_u16.astype(np.int64)
138
+
139
+ timeslice = (words & _TIMESLICE_MASK) >> 15
140
+ startslice = (words & _STARTSLICE_MASK) >> 14
141
+ numshaded = (words & _NUMSHADED_MASK) >> 7
142
+ numclear = words & _NUMCLEAR_MASK
143
+
144
+ # `:14` -- total() over the WHOLE array, regardless of which words the
145
+ # loop below actually visits (overload's skip, or a version=1 block
146
+ # consuming several indices at once, does not change this count).
147
+ numslices = int(startslice.sum()) + 3
148
+ bitimage = np.zeros((numslices, _NDIODES), dtype=np.uint8)
149
+
150
+ islice = 0
151
+ idiode = 0
152
+ skip = int(overload) * 2
153
+ bound = n - 1 - skip # IDL `FOR i=0,bound DO` -- inclusive, no run if bound<0
154
+
155
+ i = 0
156
+ while i <= bound:
157
+ if not timeslice[i]:
158
+ if startslice[i]:
159
+ islice += 1
160
+ idiode = int(numclear[i])
161
+ else:
162
+ idiode += int(numclear[i])
163
+
164
+ shaded = int(numshaded[i])
165
+ if shaded > 0:
166
+ if int(words[i]) == _CLEAR_LINE:
167
+ bitimage[islice, :] = 0
168
+ idiode = 0
169
+ if version == 1:
170
+ istart = i + 1
171
+ istop = i + 8
172
+ if istop < n:
173
+ block = words[istart:istop + 1]
174
+ bitimage[islice, :] = _decode_uncompressed_block(block)
175
+ i += 8 # honoured by GDL's FOR -- see module docstring, U1
176
+ idiode = 0
177
+ else:
178
+ if idiode + shaded - 1 < _NDIODES:
179
+ goodbit = True
180
+ # `:49-53` -- HVPS3/MC3E 32-diode artifact rule.
181
+ if shaded == 32 and (idiode % 32) == 0:
182
+ if islice == 0:
183
+ goodbit = False
184
+ if islice > 0:
185
+ prior = bitimage[islice - 1, idiode:idiode + shaded]
186
+ if int(prior.sum()) < 4:
187
+ goodbit = False
188
+ if goodbit:
189
+ bitimage[islice, idiode:idiode + shaded] = 1
190
+ # `idiode` advances here whenever the overflow check
191
+ # above passed -- even on an MC3E goodbit=False drop.
192
+ # It does NOT advance on the overflow drop itself
193
+ # (finding S4-c): that case never reaches this line,
194
+ # so `idiode` is left wherever the startslice/else
195
+ # branch put it, and the *next* word's run starts
196
+ # there rather than past the dropped run's length.
197
+ idiode += shaded
198
+ else:
199
+ if int(words[i]) == _SOLID_LINE:
200
+ bitimage[islice, :] = 1
201
+ i += 1
202
+
203
+ return bitimage