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,441 @@
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
+ """``soda2_processbuffer.pro``, the SPEC branch (`:264-376`) -- P-c+P-d.
4
+
5
+ Ports the whole `CASE` branch entered for `probetype` in `{'HVPS3','2DS',
6
+ '3VCPI','HVPS4'}`, `:264-376`, as one function. P-c (the per-image loop,
7
+ `:265-340`) and P-d (the time assembly, `:341-372`) share three locals
8
+ (`c`, `inttime`, `lastclock`) and are ported together rather than split.
9
+ The shared tail (`:612-747`) -- `soda2_findsize`, the size/reject fields,
10
+ `dhist` -- and the driver (`soda2_process_2d.pro`) are later pairs; this
11
+ module stops exactly where the branch's own locals are last read, `:376`.
12
+
13
+ The branch reads **nothing from `buffer.image`** -- only `buffer.time`
14
+ (`:344`, `:362`, `:375`). All image data comes through **LUN 1**, a literal
15
+ unit number (`spec_read_hk(1,...)` `:285`, `spec_read_frame(1,...)`
16
+ `:292-293`): the caller has the raw file open as unit 1 before entering
17
+ this branch. This port takes an open binary file object instead.
18
+
19
+ Round 6-A adds :func:`soda2_processbuffer`, composing this branch
20
+ with `processbuffer_tail.process_buffer_tail` (the shared tail,
21
+ `:612-747`) into the whole `soda2_processbuffer.pro`, SPEC path, end to
22
+ end -- a `NullBuffer` from `process_spec_buffer` is passed straight
23
+ through without calling the tail, exactly as IDL returns `nullbuffer` at
24
+ `:267`/`:339` before reaching `:612`.
25
+
26
+ Validated against GDL by ``tests/test_soda2_processbuffer_spec.py``; bank
27
+ at ``gdl_oracle/run_soda2_processbuffer_spec_bank.py``, which runs the
28
+ *real* `soda2_processbuffer` (all 748 lines, via three oracle shims never
29
+ an edit to `SODA2/`) end to end on the tracked SPEC fixtures. Findings
30
+ S4-n through S4-q.
31
+
32
+ **Line-by-line** (IDL 0-based indexing throughout):
33
+
34
+ - `:266-267` -- `num_images = (*pmisc).nimages`; 0 -> return the null buffer
35
+ (`rejectbuffer=1`) immediately.
36
+ - `:268-278` -- locals: `bitimage` starts as a single dummy blank slice
37
+ (kept -- `slicecount=1` counts it), `startline`/`stopline` (uint32),
38
+ `inttime` (float64), `overload` (uint8), `particle_count` (uint16),
39
+ `rawtime` (uint32), `clocktas` (float32), `stretch` (float32, `+1.0`),
40
+ `dof` (int16, `+1`).
41
+ - `:280-282` -- `c=0`; `lastclock = misc.lastclock`; `lasthkpointer = -1`.
42
+ - `:285-286` -- `spec_read_hk` is called **only when the HK pointer
43
+ changed** since the previous image -- `lasthkpointer` is the guard.
44
+ Version is never passed by either vendored caller, so this port never
45
+ passes it either (the ported default, `version=1`).
46
+ - `:287-288` -- when `subformat ne 0`, `hk.tas = (*pmisc).probetas` on
47
+ **every** image (the HK record read at `:285` is still read, version 1,
48
+ from the image file, and its `tas` discarded). Assignment converts to the
49
+ field's type, so `hk.tas` becomes `float32(probetas)` and reaches both
50
+ `clocktas[c]` and the `:307-308` stretch test.
51
+ - `:291-295` -- `subformat` 0 reads `spec_read_frame(f, pointer, probeid)`
52
+ (version 0, the original 2D-S/HVPS3 format); 1 reads
53
+ `spec_read_frame(..., version=1)` (3VCPI/Hawkeye, Fast 2D-S); 2 reads
54
+ `hvps4_read_frame(f, pointer, probeid)`. The `CASE` has no `ELSE`, so any
55
+ other value is an IDL error: :class:`UnsupportedSpecSubformat` naming
56
+ `:291`, raised **before any read**. `hvps4_read_frame` returns no
57
+ `timetrunc`, which is safe only because `:316` is gated on `HVPS3`.
58
+ - `:296` -- a frame is kept only if `x.error == 0` **and**
59
+ `max(x.image.shape) < 1000` -- this port's `image` is `(nslices, 128)`,
60
+ so the IDL `max(size(x.image,/dim))` over `[128, nslices]` reduces, for
61
+ every fixture case reached here, to the same `nslices < 1000` test
62
+ (the 128 side never wins). A frame at or past 1000 slices is dropped
63
+ silently, same as IDL -- no count kept (S4-q).
64
+ - `:297-304` -- kept frame: slices appended to `bitimage`; `startline[c]`,
65
+ `stopline[c]`, `overload[c]`, `particle_count[c]`, `rawtime[c] = x.time`.
66
+ - `:303` -- `dofreject == 3` calls `binary_dof(x.image)` -- **now honoured**
67
+ (`arrayprobe_soda2.binary_dof`), computed per kept frame
68
+ and written into `dof[c]`.
69
+ - `:305-308` -- `clocktas[c] = hk.tas` (float32); `stretch[c] =
70
+ clip(aircrafttas/hk.tas, 0.1, 10.0)` iff `aircrafttas > 0 and hk.tas > 0
71
+ and stretchcorrect == 1` -- float32 arithmetic throughout.
72
+ - `:309` -- `freq = double(yres / (1.0e6 * clocktas[c]))`: the division
73
+ itself runs in **float32** (`yres`, `1.0e6`, `clocktas` are all float32
74
+ in IDL) and is only widened to double afterwards -- so `freq` carries
75
+ float32 rounding baked into a float64 container (finding **S4-p**).
76
+ - `:311`, `:313` -- 3VCPI@50 um (`10.0/...`) and HVPS4@150 um (`50.0/...`)
77
+ clock overrides, each gated on probetype **and** `res`; reached from
78
+ `subformat=0` only by a hand-set op (S4-p's table). GDL-measured freqs:
79
+ `tests/test_soda2_processbuffer_spec.py::TheClockOverridesAtLines311And313`.
80
+ - `:315-318` -- `HVPS3`: `x.time = x.timetrunc`; `rollovervalue = 65536`.
81
+ Else: `rollovervalue = 4294967295.0` -- **2**:sup:`32` **- 1, not**
82
+ 2**32 (finding **S4-n**).
83
+ - `:320-322` -- the rollover-aware interarrival time: `inttime[c] =
84
+ ((x.time + rollovervalue) - lastclock) * freq` if `x.time < lastclock`
85
+ else `(x.time - lastclock) * freq`; `lastclock = x.time`.
86
+ - `:326` -- `misc.lastclock = lastclock`, written **before** the
87
+ `num_images == 0` early return at `:338` -- a buffer whose every frame
88
+ was dropped still writes `lastclock` forward (unchanged, since nothing
89
+ advanced it -- "what turned out to be fine" in the design record).
90
+ - `:327-338` -- `num_images = c`; every per-frame array trimmed to
91
+ `[0:c-1]` (`c == 0` reproduces IDL 8's "to the last element" as an empty
92
+ result rather than an error); `max(stopline) > 10000` also rejects the
93
+ whole buffer (a distinct path from `num_images == 0`).
94
+ - `:344` -- `reftime = misc.maxsfm` if `buffer.time == misc.lastbufftime`
95
+ else `buffer.time`.
96
+ - `:352-357` -- the **backward** count: `elaptime` accumulates `inttime`
97
+ left to right; `time_sfm[i] = reftime - (total(inttime) - elaptime)`.
98
+ `total()` of a float64 array is a plain left-to-right sum under GDL,
99
+ reproduced here with an explicit left-to-right loop. `np.sum` is not one
100
+ (pairwise from eight elements); it agreed with the golden's
101
+ `frame_h_all18` only because those interarrivals sum exactly.
102
+ - `:362-365` -- if `buffer.time == lastbufftime`: **overwrite**
103
+ `time_sfm[0] = maxsfm`, then `time_sfm[i] = time_sfm[i-1] + inttime[i]`
104
+ for `i >= 1` -- the first particle's own `inttime[0]` is never added
105
+ (finding **S4-o**).
106
+ - `:371-375` -- `restore_slice = 0`; `missed = 0`; `misc.maxsfm =
107
+ max(time_sfm)`; `misc.lastbufftime = buffer.time`.
108
+
109
+ **Package convention**: `bitimage` is `(nslices + 1, 128)` uint8, the dummy
110
+ first slice included (matching `spec_decompress`'s own row-major, "row 0
111
+ always blank" convention -- see that module's docstring).
112
+ """
113
+ from __future__ import annotations
114
+
115
+ from collections.abc import Sequence
116
+ from dataclasses import dataclass
117
+ from typing import TYPE_CHECKING, Any, BinaryIO
118
+
119
+ import numpy as np
120
+
121
+ from .binary_dof import binary_dof
122
+ from .hvps4_read_frame import hvps4_read_frame
123
+ from .spec_read_frame import spec_read_frame
124
+ from .spec_read_hk import spec_read_hk
125
+
126
+ if TYPE_CHECKING:
127
+ # Deferred to break the import cycle: processbuffer_tail imports
128
+ # SpecBufferParticles from this module, so importing ProcessedBuffer back
129
+ # from it at module level would be circular. Safe under type-checking
130
+ # only because `from __future__ import annotations` (above) never
131
+ # evaluates this annotation at runtime.
132
+ from .processbuffer_tail import ProcessedBuffer
133
+
134
+ __all__ = [
135
+ "SpecOp",
136
+ "SpecMisc",
137
+ "SpecBufferParticles",
138
+ "NullBuffer",
139
+ "UnsupportedSpecSubformat",
140
+ "process_spec_buffer",
141
+ "soda2_processbuffer",
142
+ ]
143
+
144
+ _MAX_KEPT_STOPLINE = 10000
145
+ _MAX_FRAME_SLICES = 1000
146
+
147
+
148
+ class UnsupportedSpecSubformat(NotImplementedError):
149
+ """`:291-295` -- a `subformat` outside 0-2. The IDL `CASE` has no
150
+ `ELSE`, so such a value is an IDL error; raised before any read
151
+ (`soda2_processbuffer.pro:291`)."""
152
+
153
+
154
+ @dataclass(frozen=True)
155
+ class SpecOp:
156
+ """The `(*pop)` fields the branch reads, `:264-318`. IDL types in
157
+ comments; `yres`/`res` are FLOAT (`soda2_probespecs.pro:9`) -- kept as
158
+ Python floats but the `:309` division reproduces float32 semantics
159
+ explicitly (SODA-2 register entry S4-p) regardless of this field's own storage."""
160
+
161
+ probetype: str
162
+ subformat: int
163
+ probeid: str
164
+ dofreject: int
165
+ stretchcorrect: int
166
+ yres: float # FLOAT
167
+ res: float # FLOAT
168
+
169
+
170
+ @dataclass
171
+ class SpecMisc:
172
+ """The `(*pmisc)` fields the branch reads and mutates in place, exactly
173
+ where the IDL writes them (`:326`, `:374`, `:375`).
174
+
175
+ Source: ``soda2_processbuffer.pro`` (SODA-2); bare line references here are into that file."""
176
+
177
+ nimages: int
178
+ hkpointers: Sequence[int]
179
+ imagepointers: Sequence[int]
180
+ probetas: float # FLOAT
181
+ aircrafttas: float # FLOAT
182
+ lastclock: float # DOUBLE
183
+ lastbufftime: float # DOUBLE
184
+ maxsfm: float # DOUBLE
185
+
186
+
187
+ @dataclass
188
+ class NullBuffer:
189
+ """`:18-20`'s `nullbuffer` -- the branch's `rejectbuffer=1` return, at
190
+ `:267` (no images) or `:339` (kept slices exceed 10 000).
191
+
192
+ Source: ``soda2_processbuffer.pro`` (SODA-2); bare line references here are into that file."""
193
+
194
+ rejectbuffer: int = 1
195
+
196
+
197
+ @dataclass
198
+ class SpecBufferParticles:
199
+ """The branch's locals at `:376`, what the shared tail consumes.
200
+ `rejectbuffer` is always 0 here -- a rejected buffer is a
201
+ :class:`NullBuffer` instead.
202
+
203
+ Source: ``soda2_processbuffer.pro`` (SODA-2); bare line references here are into that file."""
204
+
205
+ rejectbuffer: int
206
+ num_images: int
207
+ bitimage: np.ndarray # uint8 (num_images+1, 128), dummy slice kept
208
+ startline: np.ndarray # uint32
209
+ stopline: np.ndarray # uint32
210
+ overload: np.ndarray # uint8
211
+ particle_count: np.ndarray # uint16
212
+ rawtime: np.ndarray # uint32
213
+ clocktas: np.ndarray # float32
214
+ stretch: np.ndarray # float32
215
+ inttime: np.ndarray # float64
216
+ dof: np.ndarray # int16
217
+ time_sfm: np.ndarray # float64
218
+ reftime: float # DOUBLE
219
+ restore_slice: int = 0
220
+ missed: int = 0
221
+
222
+
223
+ def _freq(yres: float, clocktas: float) -> float:
224
+ """`:309` -- `freq = double(yres / (1.0e6 * clocktas))`, computed in
225
+ float32 and only then widened (S4-p)."""
226
+ y32 = np.float32(yres)
227
+ c32 = np.float32(clocktas)
228
+ m32 = np.float32(1.0e6)
229
+ return float(y32 / (m32 * c32))
230
+
231
+
232
+ def process_spec_buffer(
233
+ f: BinaryIO, buffer_time: float, op: SpecOp, misc: SpecMisc,
234
+ ) -> SpecBufferParticles | NullBuffer:
235
+ """``p = soda2_processbuffer(buffer, pop, pmisc)``, the SPEC branch
236
+ only (`:264-376`). ``f`` is the open raw file IDL has as LUN 1;
237
+ ``buffer_time`` is `buffer.time`. ``misc`` is mutated in place, exactly
238
+ where the IDL writes `(*pmisc)`.
239
+
240
+ Source: ``soda2_processbuffer.pro:264-376`` (SODA-2); bare line references here are into that
241
+ file.
242
+ """
243
+ if op.subformat not in (0, 1, 2):
244
+ raise UnsupportedSpecSubformat(
245
+ f"soda2_processbuffer.pro:291: subformat={op.subformat!r} matches "
246
+ "no CASE arm (0, 1, 2) and the CASE has no ELSE")
247
+ # `:266-267`.
248
+ num_images_in = misc.nimages
249
+ if num_images_in == 0:
250
+ return NullBuffer()
251
+
252
+ # `:268-278` -- locals, one dummy blank slice kept at index 0.
253
+ bitimage_slices: list[np.ndarray] = [np.zeros(128, dtype=np.uint8)]
254
+ startline = np.zeros(num_images_in, dtype=np.uint32)
255
+ stopline = np.zeros(num_images_in, dtype=np.uint32)
256
+ inttime = np.zeros(num_images_in, dtype=np.float64)
257
+ overload = np.zeros(num_images_in, dtype=np.uint8)
258
+ particle_count = np.zeros(num_images_in, dtype=np.uint16)
259
+ rawtime = np.zeros(num_images_in, dtype=np.uint32)
260
+ clocktas = np.zeros(num_images_in, dtype=np.float32)
261
+ stretch = np.ones(num_images_in, dtype=np.float32)
262
+ dof = np.ones(num_images_in, dtype=np.int16)
263
+
264
+ # `:280-282`.
265
+ c = 0
266
+ slicecount = 1
267
+ lastclock = misc.lastclock
268
+ lasthkpointer = -1
269
+ hk = None
270
+
271
+ for i in range(num_images_in):
272
+ hk_pointer = misc.hkpointers[i]
273
+ img_pointer = misc.imagepointers[i]
274
+
275
+ # `:285-286`.
276
+ if hk_pointer != lasthkpointer:
277
+ hk = spec_read_hk(f, hk_pointer)
278
+ lasthkpointer = hk_pointer
279
+ assert hk is not None
280
+
281
+ # `:287-288` -- every iteration, not only after a fresh `:285` read.
282
+ if op.subformat != 0:
283
+ hk.tas = np.float32(misc.probetas)
284
+
285
+ # `:291-295`.
286
+ x: Any
287
+ if op.subformat == 0:
288
+ x = spec_read_frame(f, img_pointer, op.probeid)
289
+ elif op.subformat == 1:
290
+ x = spec_read_frame(f, img_pointer, op.probeid, version=1)
291
+ else:
292
+ x = hvps4_read_frame(f, img_pointer, op.probeid)
293
+
294
+ # `:296` -- keep iff error==0 and slice count < 1000 (S4-q).
295
+ if x.error != 0 or x.image.shape[0] >= _MAX_FRAME_SLICES:
296
+ continue
297
+
298
+ # `:297-304`.
299
+ for row in range(x.image.shape[0]):
300
+ bitimage_slices.append(x.image[row].astype(np.uint8))
301
+ startline[c] = slicecount
302
+ slicecount += x.image.shape[0]
303
+ stopline[c] = slicecount - 1
304
+ overload[c] = x.overload
305
+ particle_count[c] = x.particlecount
306
+ rawtime[c] = x.time
307
+
308
+ # `:303` -- `dofreject == 3` calls `binary_dof(x.image)` per kept
309
+ # frame; every other `dofreject` value leaves `dof[c]` at its `:278`
310
+ # initial value of 1.
311
+ if op.dofreject == 3:
312
+ dof[c] = binary_dof(x.image).dof
313
+
314
+ # `:305-308`.
315
+ clocktas[c] = hk.tas
316
+ if (misc.aircrafttas > 0 and float(hk.tas) > 0
317
+ and op.stretchcorrect == 1):
318
+ ratio = np.float32(misc.aircrafttas) / np.float32(hk.tas)
319
+ stretch[c] = np.clip(ratio, np.float32(0.1), np.float32(10.0))
320
+
321
+ # `:309`.
322
+ freq = _freq(op.yres, float(clocktas[c]))
323
+
324
+ # `:311`, `:313` -- the 50 um Hawkeye array runs on the 10 um clock
325
+ # and the 150 um HVPS-4 array on the 50 um clock: each override
326
+ # needs its `res` as well as its probetype, and the numerator is the
327
+ # clock's resolution, not the array's. probespecs gives these two
328
+ # probes subformat 1 and 2.
329
+ if op.probetype == "3VCPI" and op.res == 50:
330
+ freq = _freq(10.0, float(clocktas[c]))
331
+ if op.probetype == "HVPS4" and op.res == 150:
332
+ freq = _freq(50.0, float(clocktas[c]))
333
+
334
+ # `:315-318`.
335
+ xtime = int(x.time)
336
+ if op.probetype == "HVPS3":
337
+ xtime = int(x.timetrunc)
338
+ rollovervalue = 65536.0
339
+ else:
340
+ rollovervalue = 4294967295.0 # 2**32 - 1, not 2**32 (S4-n)
341
+
342
+ # `:320-322`.
343
+ if xtime < lastclock:
344
+ inttime[c] = ((xtime + rollovervalue) - lastclock) * freq
345
+ else:
346
+ inttime[c] = (xtime - lastclock) * freq
347
+ lastclock = xtime
348
+
349
+ c += 1
350
+
351
+ # `:326` -- written before the `num_images == 0` early return below.
352
+ misc.lastclock = lastclock
353
+
354
+ # `:327-338`.
355
+ num_images = c
356
+ startline = startline[:num_images]
357
+ stopline = stopline[:num_images]
358
+ overload = overload[:num_images]
359
+ particle_count = particle_count[:num_images]
360
+ rawtime = rawtime[:num_images]
361
+ clocktas = clocktas[:num_images]
362
+ stretch = stretch[:num_images]
363
+ inttime = inttime[:num_images]
364
+ dof = dof[:num_images]
365
+
366
+ if num_images == 0:
367
+ return NullBuffer()
368
+ if int(stopline.max()) > _MAX_KEPT_STOPLINE:
369
+ return NullBuffer()
370
+
371
+ bitimage = np.stack(bitimage_slices, axis=0)
372
+
373
+ # `:344`.
374
+ if buffer_time == misc.lastbufftime:
375
+ reftime = misc.maxsfm
376
+ else:
377
+ reftime = buffer_time
378
+
379
+ # `:352-357` -- backward count, left-to-right accumulation (U1).
380
+ # `total(inttime)` is a sequential double sum under GDL; `np.sum` is
381
+ # pairwise from eight elements up and differs from it once an addend is
382
+ # inexact, which the first buffer's `x.time - 0` interarrival (a 32-bit
383
+ # count times a FLOAT period) makes it.
384
+ total_inttime = 0.0
385
+ for value in inttime:
386
+ total_inttime += float(value)
387
+ time_sfm = np.zeros(num_images, dtype=np.float64)
388
+ elaptime = 0.0
389
+ for i in range(num_images):
390
+ elaptime += float(inttime[i])
391
+ time_sfm[i] = reftime - (total_inttime - elaptime)
392
+
393
+ # `:362-365` -- overwrite; inttime[0] never added (S4-o).
394
+ if buffer_time == misc.lastbufftime:
395
+ time_sfm[0] = misc.maxsfm
396
+ for i in range(1, num_images):
397
+ time_sfm[i] = time_sfm[i - 1] + inttime[i]
398
+
399
+ # `:371-375`.
400
+ misc.maxsfm = float(time_sfm.max())
401
+ misc.lastbufftime = buffer_time
402
+
403
+ return SpecBufferParticles(
404
+ rejectbuffer=0,
405
+ num_images=num_images,
406
+ bitimage=bitimage,
407
+ startline=startline,
408
+ stopline=stopline,
409
+ overload=overload,
410
+ particle_count=particle_count,
411
+ rawtime=rawtime,
412
+ clocktas=clocktas,
413
+ stretch=stretch,
414
+ inttime=inttime,
415
+ dof=dof,
416
+ time_sfm=time_sfm,
417
+ reftime=reftime,
418
+ restore_slice=0,
419
+ missed=0,
420
+ )
421
+
422
+
423
+ def soda2_processbuffer(
424
+ f: BinaryIO, buffer_time: float, op: SpecOp, tail_op: Any,
425
+ misc: SpecMisc, lastdhist: Any,
426
+ ) -> NullBuffer | ProcessedBuffer:
427
+ """``p = soda2_processbuffer(buffer, pop, pmisc)``, SPEC path, whole
428
+ function (`:264-747`) -- composes :func:`process_spec_buffer` with
429
+ `processbuffer_tail.process_buffer_tail`. A `NullBuffer` from the
430
+ branch is returned immediately, without calling the tail, matching
431
+ IDL's early `nullbuffer` returns at `:267`/`:339`.
432
+
433
+ Source: ``soda2_processbuffer.pro:264-747`` (SODA-2); bare line references here are into that
434
+ file.
435
+ """
436
+ from .processbuffer_tail import process_buffer_tail
437
+
438
+ result = process_spec_buffer(f, buffer_time, op, misc)
439
+ if isinstance(result, NullBuffer):
440
+ return result
441
+ return process_buffer_tail(result, tail_op, lastdhist)