mosca 2.0.2__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.
- mosca/__init__.py +11 -0
- mosca/common/__init__.py +9 -0
- mosca/common/enums.py +79 -0
- mosca/common/logger.py +139 -0
- mosca/common/threads.py +199 -0
- mosca/common/utils.py +70 -0
- mosca/control/__init__.py +4 -0
- mosca/control/buffer.py +526 -0
- mosca/control/controller.py +278 -0
- mosca/control/datahandler.py +261 -0
- mosca/control/detectors.py +116 -0
- mosca/control/mosca.py +693 -0
- mosca/control/spectra.py +36 -0
- mosca/control/stats.py +43 -0
- mosca/control/tasks/__init__.py +3 -0
- mosca/control/tasks/background.py +118 -0
- mosca/control/tasks/rois.py +466 -0
- mosca/control/tasks/saving.py +391 -0
- mosca/control/tasks/spectrasums.py +185 -0
- mosca/control/tasks/task.py +201 -0
- mosca/control/tasks/taskmanager.py +133 -0
- mosca/devices/__init__.py +28 -0
- mosca/devices/hamamatsu/__init__.py +0 -0
- mosca/devices/hamamatsu/hamamatsu.py +88 -0
- mosca/devices/hamamatsu/hamamatsuhw.py +1298 -0
- mosca/devices/oceanoptics/__init__.py +0 -0
- mosca/devices/oceanoptics/oceanoptics.py +59 -0
- mosca/devices/oceanoptics/oceanopticshw.py +379 -0
- mosca/devices/simulation/__init__.py +0 -0
- mosca/devices/simulation/dante/__init__.py +1 -0
- mosca/devices/simulation/dante/simdante.py +33 -0
- mosca/devices/simulation/dante/simdantehw.py +506 -0
- mosca/devices/simulation/dummy/__init__.py +1 -0
- mosca/devices/simulation/dummy/dummy.py +299 -0
- mosca/devices/simulation/generator.py +607 -0
- mosca/devices/simulation/xia/__init__.py +1 -0
- mosca/devices/simulation/xia/iniparser.py +99 -0
- mosca/devices/simulation/xia/simfalconx.py +26 -0
- mosca/devices/simulation/xia/simfalconxhw.py +599 -0
- mosca/devices/xglab/__init__.py +0 -0
- mosca/devices/xglab/dante.py +126 -0
- mosca/devices/xglab/dantehw.py +1178 -0
- mosca/devices/xia/__init__.py +0 -0
- mosca/devices/xia/falconx.py +232 -0
- mosca/devices/xia/handel/__init__.py +14 -0
- mosca/devices/xia/handel/_cffi.py +62 -0
- mosca/devices/xia/handel/error.py +272 -0
- mosca/devices/xia/handel/handel_inc/handel_constants.h +127 -0
- mosca/devices/xia/handel/handel_inc/handel_errors.h +254 -0
- mosca/devices/xia/handel/handel_inc/handel_generic.h +66 -0
- mosca/devices/xia/handel/interface.py +1025 -0
- mosca/devices/xia/handel/mapping.py +107 -0
- mosca/devices/xia/handel/parser.py +62 -0
- mosca/devices/xia/handel/stats.py +85 -0
- mosca/devices/xia/mercury.py +160 -0
- mosca/devices/xia/utils.py +47 -0
- mosca/devices/xia/xia.py +575 -0
- mosca/devices/xia/xiads.py +161 -0
- mosca/mosca.py +0 -0
- mosca/tango/__init__.py +2 -0
- mosca/tango/controller.py +80 -0
- mosca/tango/main.py +163 -0
- mosca/tango/mosca.py +513 -0
- mosca/tango/moscav1.py +408 -0
- mosca/tango/shared.py +23 -0
- mosca/tango/tasks/__init__.py +0 -0
- mosca/tango/tasks/background.py +55 -0
- mosca/tango/utils.py +109 -0
- mosca-2.0.2.dist-info/METADATA +32 -0
- mosca-2.0.2.dist-info/RECORD +73 -0
- mosca-2.0.2.dist-info/WHEEL +4 -0
- mosca-2.0.2.dist-info/entry_points.txt +2 -0
- mosca-2.0.2.dist-info/licenses/LICENSE +166 -0
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
logger = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
# import gevent
|
|
5
|
+
import enum
|
|
6
|
+
import copy
|
|
7
|
+
import time
|
|
8
|
+
import queue
|
|
9
|
+
import threading
|
|
10
|
+
|
|
11
|
+
from ...common import AsyncTask
|
|
12
|
+
from ...common.enums import TriggerMode, to_enum
|
|
13
|
+
|
|
14
|
+
from tango.server import Device, command, attribute, device_property
|
|
15
|
+
from mosca.tango.shared import get_controller, get_mosca
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
import numpy as np
|
|
19
|
+
import h5py
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def load_data_h5(filename, spectrum_size):
|
|
23
|
+
data = {}
|
|
24
|
+
stats = {}
|
|
25
|
+
with h5py.File(filename, mode="r") as h5f:
|
|
26
|
+
dset = h5f["data"]
|
|
27
|
+
sset = h5f["stats"]
|
|
28
|
+
n_det = len(dset)
|
|
29
|
+
for i_det in range(n_det):
|
|
30
|
+
spectra = dset[f"det{i_det}"][:]
|
|
31
|
+
dstats = sset[f"det{i_det}"]
|
|
32
|
+
data_size = spectra.shape[1]
|
|
33
|
+
if data_size != spectrum_size:
|
|
34
|
+
if data_size % spectrum_size != 0 or data_size < spectrum_size:
|
|
35
|
+
raise RuntimeError(
|
|
36
|
+
"spectrum_size must be a divider of the input data."
|
|
37
|
+
)
|
|
38
|
+
spectra = spectra[:, 0 :: data_size // spectrum_size]
|
|
39
|
+
data[f"det{i_det}"] = spectra
|
|
40
|
+
rtime = dstats["realtime"]
|
|
41
|
+
trigger_livetime = dstats["trigger_livetime"]
|
|
42
|
+
triggers = dstats["triggers"]
|
|
43
|
+
events = dstats["events"]
|
|
44
|
+
stats[f"det{i_det}"] = np.column_stack(
|
|
45
|
+
(rtime, trigger_livetime, triggers, events)
|
|
46
|
+
)
|
|
47
|
+
print(f"Loaded simu data from {filename}: {len(data)} detectors.")
|
|
48
|
+
return data, stats
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_pixels(data_dict, stats_dict, detector, f_pix, l_pix=-1):
|
|
52
|
+
if l_pix == -1:
|
|
53
|
+
l_pix = f_pix
|
|
54
|
+
assert f_pix <= l_pix
|
|
55
|
+
n_det = len(data_dict)
|
|
56
|
+
i_det = detector % n_det
|
|
57
|
+
data = data_dict[f"det{i_det}"]
|
|
58
|
+
stats = stats_dict[f"det{i_det}"]
|
|
59
|
+
n_data = data.shape[0]
|
|
60
|
+
idx = np.arange(f_pix, l_pix + 1) % n_data
|
|
61
|
+
new_data = data[idx]
|
|
62
|
+
new_stats = stats[idx]
|
|
63
|
+
return new_data, new_stats
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def gen_data(n_det, n_pixels, spectrum_size, stats_dtype=np.int32):
|
|
67
|
+
data = {}
|
|
68
|
+
stats = {}
|
|
69
|
+
for i_det in range(n_det):
|
|
70
|
+
data[f"det{i_det}"] = np.random.randint(
|
|
71
|
+
1,
|
|
72
|
+
100,
|
|
73
|
+
size=(
|
|
74
|
+
n_pixels,
|
|
75
|
+
spectrum_size,
|
|
76
|
+
),
|
|
77
|
+
dtype=np.uint32,
|
|
78
|
+
)
|
|
79
|
+
stats[f"det{i_det}"] = np.zeros((n_pixels, 4), dtype=stats_dtype)
|
|
80
|
+
return data, stats
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class DataGenerator:
|
|
84
|
+
n_detectors = property(lambda self: self._n_detectors)
|
|
85
|
+
spectrum_size = property(lambda self: self._spectrum_size)
|
|
86
|
+
use_external_trigger = property(lambda self: self._use_external_trigger)
|
|
87
|
+
acq_nb_points = property(lambda self: self._acq_nb_points)
|
|
88
|
+
finished = property(lambda self: self._finished)
|
|
89
|
+
prepared = property(lambda self: self._prepared)
|
|
90
|
+
acq_started = property(lambda self: self._acq_started)
|
|
91
|
+
|
|
92
|
+
def __init__(self):
|
|
93
|
+
super().__init__()
|
|
94
|
+
|
|
95
|
+
logger.info("New generator.")
|
|
96
|
+
|
|
97
|
+
self._prepared = False
|
|
98
|
+
|
|
99
|
+
self._start_t = None
|
|
100
|
+
self._thread = None
|
|
101
|
+
|
|
102
|
+
self._trigger_mode = TriggerMode.INT_TRIG_READOUT
|
|
103
|
+
|
|
104
|
+
self._acq_nb_points = 0
|
|
105
|
+
self._detectors = tuple()
|
|
106
|
+
self._n_detectors = 0
|
|
107
|
+
self._spectrum_size = 0
|
|
108
|
+
self._block_size = 1
|
|
109
|
+
self._map_refresh_t = 0.05
|
|
110
|
+
self._data_file = None
|
|
111
|
+
self._use_external_trigger = False
|
|
112
|
+
self._start_delay = 0.05
|
|
113
|
+
self._acq_realtime = 1.
|
|
114
|
+
|
|
115
|
+
self._current_pixel = 0
|
|
116
|
+
self._data = None
|
|
117
|
+
self._stats = None
|
|
118
|
+
self._n_stats = None
|
|
119
|
+
|
|
120
|
+
self._point_offset = 0
|
|
121
|
+
self._last_start_date = -1
|
|
122
|
+
self._acq_started = False
|
|
123
|
+
self._finished = False
|
|
124
|
+
self._chan_running = None
|
|
125
|
+
|
|
126
|
+
@use_external_trigger.setter
|
|
127
|
+
def use_external_trigger(self, use_external_trigger):
|
|
128
|
+
self._use_external_trigger = use_external_trigger
|
|
129
|
+
|
|
130
|
+
@acq_nb_points.setter
|
|
131
|
+
def acq_nb_points(self, acq_nb_points):
|
|
132
|
+
if self.is_running():
|
|
133
|
+
raise RuntimeError("Can't set acq_nb_points while an acquisition is running.")
|
|
134
|
+
self._acq_nb_points = acq_nb_points
|
|
135
|
+
|
|
136
|
+
@property
|
|
137
|
+
def trigger_mode(self):
|
|
138
|
+
return self._trigger_mode
|
|
139
|
+
|
|
140
|
+
@trigger_mode.setter
|
|
141
|
+
def trigger_mode(self, trigger_mode):
|
|
142
|
+
if self.is_running():
|
|
143
|
+
raise RuntimeError("Can't set trigger_mode while an acquisition is running.")
|
|
144
|
+
self._trigger_mode = to_enum(trigger_mode, TriggerMode)
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def block_size(self):
|
|
148
|
+
return self._block_size
|
|
149
|
+
|
|
150
|
+
@block_size.setter
|
|
151
|
+
def block_size(self, block_size):
|
|
152
|
+
if self.is_running():
|
|
153
|
+
raise RuntimeError("Can't set block_size while an acquisition is running.")
|
|
154
|
+
if block_size < 1:
|
|
155
|
+
raise ValueError(f"block_size must be >= 1 (was: {block_size}).")
|
|
156
|
+
self._block_size = block_size
|
|
157
|
+
|
|
158
|
+
@property
|
|
159
|
+
def map_refresh_t(self):
|
|
160
|
+
return self._map_refresh_t
|
|
161
|
+
|
|
162
|
+
@map_refresh_t.setter
|
|
163
|
+
def map_refresh_t(self, map_refresh_t):
|
|
164
|
+
self._map_refresh_t = map_refresh_t
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def data_file(self):
|
|
168
|
+
return self._data_file
|
|
169
|
+
|
|
170
|
+
@data_file.setter
|
|
171
|
+
def data_file(self, data_file):
|
|
172
|
+
self._data_file = data_file
|
|
173
|
+
|
|
174
|
+
@property
|
|
175
|
+
def acq_realtime(self):
|
|
176
|
+
return self._acq_realtime
|
|
177
|
+
|
|
178
|
+
@acq_realtime.setter
|
|
179
|
+
def acq_realtime(self, acq_realtime):
|
|
180
|
+
if self.is_running():
|
|
181
|
+
raise RuntimeError("Can't set acq_realtime while an acquisition is running.")
|
|
182
|
+
self._acq_realtime = acq_realtime
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def point_offset(self):
|
|
186
|
+
return self._point_offset
|
|
187
|
+
|
|
188
|
+
@point_offset.setter
|
|
189
|
+
def point_offset(self, point_offset):
|
|
190
|
+
self._point_offset = point_offset
|
|
191
|
+
|
|
192
|
+
@property
|
|
193
|
+
def start_delay(self):
|
|
194
|
+
return self._start_delay
|
|
195
|
+
|
|
196
|
+
@start_delay.setter
|
|
197
|
+
def start_delay(self, start_delay):
|
|
198
|
+
self._start_delay = start_delay
|
|
199
|
+
|
|
200
|
+
def apply_mosca(self,
|
|
201
|
+
*args,
|
|
202
|
+
trigger_mode=None,
|
|
203
|
+
acq_nb_points=None,
|
|
204
|
+
acq_realtime=None,
|
|
205
|
+
acq_nb_triggers=None,
|
|
206
|
+
spectrum_size=None,
|
|
207
|
+
gate_mode=None,
|
|
208
|
+
block_size=None,
|
|
209
|
+
resume=False):
|
|
210
|
+
|
|
211
|
+
if self.is_running():
|
|
212
|
+
raise RuntimeError("Can't apply_mosca while an acquisition is running.")
|
|
213
|
+
if acq_realtime is not None:
|
|
214
|
+
self.acq_realtime = acq_realtime
|
|
215
|
+
if trigger_mode is not None:
|
|
216
|
+
self.trigger_mode = trigger_mode
|
|
217
|
+
if acq_nb_points is not None:
|
|
218
|
+
self.acq_nb_points = acq_nb_points
|
|
219
|
+
if block_size is not None:
|
|
220
|
+
self.block_size = block_size
|
|
221
|
+
self._data = None
|
|
222
|
+
self._stats = None
|
|
223
|
+
|
|
224
|
+
def prepare(self, detectors, spectrum_size):
|
|
225
|
+
|
|
226
|
+
if self.is_running():
|
|
227
|
+
raise RuntimeError("Can't set prepare while an acquisition is running.")
|
|
228
|
+
|
|
229
|
+
if (not self._use_external_trigger
|
|
230
|
+
or self._trigger_mode == TriggerMode.INT_TRIG_READOUT) and self.acq_realtime <= 0:
|
|
231
|
+
raise RuntimeError(
|
|
232
|
+
"Can't have trigger_mode=INT_TRIG_READOUT (or use_external_trigger=False) "
|
|
233
|
+
"and acq_realtime <= 0."
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
self._n_detectors = len(detectors)
|
|
237
|
+
self._spectrum_size = spectrum_size
|
|
238
|
+
|
|
239
|
+
logger.info("acq_nb_points=%s\n"
|
|
240
|
+
"acq_realtime=%f\n"
|
|
241
|
+
"trigger_mode=%s\n"
|
|
242
|
+
"spectrum_size=%d\n"
|
|
243
|
+
"detectors=%s\n"
|
|
244
|
+
"use_external_trigger=%s\n"
|
|
245
|
+
"map_refresh_t=%f\n"
|
|
246
|
+
"block_size=%d\n"
|
|
247
|
+
"point_offset=%d\n"
|
|
248
|
+
"data_file=%s\n",
|
|
249
|
+
self.acq_nb_points, self.acq_realtime,
|
|
250
|
+
self.trigger_mode.name, self.spectrum_size,
|
|
251
|
+
detectors,
|
|
252
|
+
self.use_external_trigger, self.map_refresh_t,
|
|
253
|
+
self.block_size, self.point_offset,
|
|
254
|
+
self.data_file)
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
self._detectors = copy.copy(detectors)
|
|
258
|
+
self._data, self._stats = self._init_data()
|
|
259
|
+
self._n_stats = self._stats[list(self._stats.keys())[0]].shape[-1]
|
|
260
|
+
|
|
261
|
+
self._current_pixel = 0
|
|
262
|
+
self._last_start_date = -1
|
|
263
|
+
self._acq_started = False
|
|
264
|
+
self._finished = False
|
|
265
|
+
|
|
266
|
+
self._chan_running = [False] * self.n_detectors
|
|
267
|
+
self._prepared = True
|
|
268
|
+
self._finished = False
|
|
269
|
+
|
|
270
|
+
def _init_data(self):
|
|
271
|
+
if self.data_file:
|
|
272
|
+
logger.info("Initializing simulation data from file %s.", self.data_file)
|
|
273
|
+
data, stats = load_data_h5(self.data_file, self._spectrum_size)
|
|
274
|
+
logger.info(
|
|
275
|
+
"Loaded %d detectors, %d points.", len(data), data["det0"].shape[0]
|
|
276
|
+
)
|
|
277
|
+
else:
|
|
278
|
+
logger.info("Initializing simulation data (random).")
|
|
279
|
+
data, stats = gen_data(self.n_detectors, self.block_size, self.spectrum_size)
|
|
280
|
+
return self.prepare_data(data, stats)
|
|
281
|
+
|
|
282
|
+
def prepare_data(self, spectra, stats):
|
|
283
|
+
return spectra, stats
|
|
284
|
+
|
|
285
|
+
def _get_data(self, detector, f_pix, l_pix):
|
|
286
|
+
return get_pixels(self._data, self._stats, detector, f_pix, l_pix=l_pix)
|
|
287
|
+
|
|
288
|
+
def is_running(self):
|
|
289
|
+
return self._thread is not None and not self._thread.ready()
|
|
290
|
+
|
|
291
|
+
def current_pixel(self, detector):
|
|
292
|
+
return self._current_pixel
|
|
293
|
+
|
|
294
|
+
def detector_running(self, detector):
|
|
295
|
+
return self._chan_running is not None and self._chan_running[detector]
|
|
296
|
+
|
|
297
|
+
def stop(self):
|
|
298
|
+
if self._thread:
|
|
299
|
+
self._thread.put(None)
|
|
300
|
+
self._thread.stop()
|
|
301
|
+
|
|
302
|
+
def hw_trig(self):
|
|
303
|
+
if self._thread:
|
|
304
|
+
self._thread.put(1)
|
|
305
|
+
else:
|
|
306
|
+
raise RuntimeError("Acquisition not started.")
|
|
307
|
+
|
|
308
|
+
def run(self):
|
|
309
|
+
if self.is_running():
|
|
310
|
+
raise RuntimeError("This generator is already running...")
|
|
311
|
+
if self._finished:
|
|
312
|
+
logger.warning("This generator has already been used. Assuming it's a step scan... (re-preparing).")
|
|
313
|
+
self.prepare(self._detectors, self._spectrum_size)
|
|
314
|
+
self._acq_started = False
|
|
315
|
+
self._last_start_date = -1.
|
|
316
|
+
self._thread = AsyncTask(run_func=self._run,
|
|
317
|
+
prepare_func=self._prepare_task,
|
|
318
|
+
cleanup_func=self._cleanup_task)
|
|
319
|
+
self._prepared = False
|
|
320
|
+
|
|
321
|
+
def start_detector(self, detector):
|
|
322
|
+
if self._acq_started:
|
|
323
|
+
raise RuntimeError("Acquisition is already started (start_delay too short?).")
|
|
324
|
+
if not self.is_running():
|
|
325
|
+
self.run()
|
|
326
|
+
self._chan_running[detector] = True
|
|
327
|
+
self._last_start_date = time.perf_counter()
|
|
328
|
+
logger.info("detector %s started (overall=%s).",
|
|
329
|
+
detector, self.acq_started)
|
|
330
|
+
|
|
331
|
+
def _prepare_task(self, task, *args, **kwargs):
|
|
332
|
+
self._current_pixel = 0
|
|
333
|
+
self._chan_running = [False] * self.n_detectors
|
|
334
|
+
|
|
335
|
+
def _cleanup_task(self, *args, **kwargs):
|
|
336
|
+
self._acq_finished()
|
|
337
|
+
self._finished = True
|
|
338
|
+
self._prepared = False
|
|
339
|
+
|
|
340
|
+
def _acq_finished(self):
|
|
341
|
+
pass
|
|
342
|
+
|
|
343
|
+
def _run(self, task, *args, **kwargs):
|
|
344
|
+
try:
|
|
345
|
+
logger.info(
|
|
346
|
+
f"Running. use_external_trigger={self.use_external_trigger}, Acq_time = {self.acq_realtime}"
|
|
347
|
+
)
|
|
348
|
+
n_pix = 0
|
|
349
|
+
block_size = self.block_size
|
|
350
|
+
acq_nb_points = self.acq_nb_points
|
|
351
|
+
|
|
352
|
+
# startup delay, to wait for MCAs that start they channels individually
|
|
353
|
+
wait = 0
|
|
354
|
+
while not task.stop_set \
|
|
355
|
+
and (self._last_start_date <= 0
|
|
356
|
+
or wait < self._start_delay
|
|
357
|
+
or all(ch is False for ch in self._chan_running)):
|
|
358
|
+
task._wait_stop(max(wait, self._start_delay))
|
|
359
|
+
wait = time.perf_counter() - self._last_start_date
|
|
360
|
+
logger.info("Acquisition started. (acq_nb_points=%s)", self.acq_nb_points)
|
|
361
|
+
self._acq_started = True
|
|
362
|
+
|
|
363
|
+
start_t = time.perf_counter()
|
|
364
|
+
|
|
365
|
+
if self._use_external_trigger:
|
|
366
|
+
acq_fn = {TriggerMode.INT_TRIG_READOUT: int_trig_readout_acq(task,
|
|
367
|
+
self._acq_realtime,
|
|
368
|
+
acq_nb_points,
|
|
369
|
+
refresh_time=self.map_refresh_t),
|
|
370
|
+
TriggerMode.GATE: gated_acq(task, acq_nb_points),
|
|
371
|
+
TriggerMode.EXT_TRIG: ext_trig_acq(task, acq_nb_points),
|
|
372
|
+
TriggerMode.EXT_TRIG_READOUT: ext_trig_readout_acq(task,
|
|
373
|
+
self._acq_realtime,
|
|
374
|
+
acq_nb_points)}
|
|
375
|
+
try:
|
|
376
|
+
acq_fn = acq_fn[self._trigger_mode]
|
|
377
|
+
except KeyError:
|
|
378
|
+
msg = f"Unsupported trigger_mode {self._trigger_mode}."
|
|
379
|
+
logger.error(msg)
|
|
380
|
+
raise RuntimeError(msg)
|
|
381
|
+
else:
|
|
382
|
+
acq_fn = int_trig_readout_acq(task,
|
|
383
|
+
self._acq_realtime,
|
|
384
|
+
self.acq_nb_points,
|
|
385
|
+
block_size=block_size,
|
|
386
|
+
refresh_time=self.map_refresh_t)
|
|
387
|
+
|
|
388
|
+
block_pix = 0
|
|
389
|
+
# data acquisition loop
|
|
390
|
+
while n_pix < self.acq_nb_points:
|
|
391
|
+
try:
|
|
392
|
+
next_pix = next(acq_fn)
|
|
393
|
+
except StopIteration:
|
|
394
|
+
print("Stopping!")
|
|
395
|
+
next_pix = None
|
|
396
|
+
if next_pix is not None:
|
|
397
|
+
block_pix += next_pix
|
|
398
|
+
n_pix += next_pix
|
|
399
|
+
self._current_pixel = n_pix
|
|
400
|
+
if block_pix == block_size or n_pix == acq_nb_points:
|
|
401
|
+
f_pix, l_pix = n_pix - block_pix, n_pix - 1
|
|
402
|
+
logger.warning("New pixels! %s, %s.", n_pix, block_pix)
|
|
403
|
+
for detector in range(self.n_detectors):
|
|
404
|
+
data, stats = self._get_data(detector, f_pix + self._point_offset, l_pix + self._point_offset)
|
|
405
|
+
self.update(detector, data, stats, f_pix, l_pix)
|
|
406
|
+
block_pix = 0
|
|
407
|
+
else:
|
|
408
|
+
logger.warning("Stop requested.")
|
|
409
|
+
break
|
|
410
|
+
except Exception as ex:
|
|
411
|
+
logger.exception(ex)
|
|
412
|
+
finally:
|
|
413
|
+
total_t = time.perf_counter() - start_t
|
|
414
|
+
self._chan_running = [False] * self.n_detectors
|
|
415
|
+
print(
|
|
416
|
+
f"======> Generator out. Total time {total_t}s. {self._current_pixel} pixels (asked: {self.acq_nb_points})."
|
|
417
|
+
)
|
|
418
|
+
self._acq_started = False
|
|
419
|
+
|
|
420
|
+
def update(self, detector, data, stats, f_pix, l_pix):
|
|
421
|
+
pass
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def gated_acq(task, acq_nb_points):
|
|
426
|
+
logger.info("Starting GATED acq. (Expecting %s trigs)", acq_nb_points)
|
|
427
|
+
gate = False
|
|
428
|
+
point = 0
|
|
429
|
+
while task.get() is not None and point < acq_nb_points:
|
|
430
|
+
gate = not gate
|
|
431
|
+
if not gate:
|
|
432
|
+
yield 1
|
|
433
|
+
point += 1
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def ext_trig_acq(task, acq_nb_points):
|
|
437
|
+
logger.info("Starting EXT_TRIG acq. (Expecting %s trigs)", acq_nb_points)
|
|
438
|
+
point = 0
|
|
439
|
+
while task.get() is not None and point < acq_nb_points:
|
|
440
|
+
yield 1
|
|
441
|
+
point += 1
|
|
442
|
+
logger.info("EXT_TRIG acq exiting.")
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def ext_trig_readout_acq(task, acq_realtime, acq_nb_points):
|
|
446
|
+
point = 0
|
|
447
|
+
while task.get() is not None and point < acq_nb_points:
|
|
448
|
+
while not task._wait_stop(acq_realtime) and point < acq_nb_points:
|
|
449
|
+
yield 1
|
|
450
|
+
point += 1
|
|
451
|
+
|
|
452
|
+
|
|
453
|
+
def int_trig_readout_acq(task, acq_realtime, acq_nb_points, block_size=1, refresh_time=0.01):
|
|
454
|
+
# total number of points acquired
|
|
455
|
+
n_pix = 0
|
|
456
|
+
# extra points generated from the loop, happens when the wait takes
|
|
457
|
+
# a bit longer than requested
|
|
458
|
+
extra = 0
|
|
459
|
+
# number of points to return per yield
|
|
460
|
+
block_pix = 0
|
|
461
|
+
|
|
462
|
+
start_t = time.perf_counter()
|
|
463
|
+
updt_t = start_t
|
|
464
|
+
# data acquisition loop
|
|
465
|
+
while n_pix < acq_nb_points:
|
|
466
|
+
# keeping extra points from the previous loop, if any
|
|
467
|
+
block_pix = extra
|
|
468
|
+
|
|
469
|
+
# number of points that need to be generated this turn
|
|
470
|
+
# either block_size, or maybe less if we're at the last turn
|
|
471
|
+
# and the total number of points is not a multiple of block_size
|
|
472
|
+
block_size = min(block_size, acq_nb_points - n_pix)
|
|
473
|
+
|
|
474
|
+
if block_pix >= block_size:
|
|
475
|
+
# for some reason sometimes we get twice as many pixels as
|
|
476
|
+
# requested (stop_event.wait not returning in time?)
|
|
477
|
+
# so, we just wait the normal amount of time that we normally
|
|
478
|
+
# would to get block_size pixels
|
|
479
|
+
logger.warning("Extra pixels! (%s)", block_pix)
|
|
480
|
+
wait_t = max(
|
|
481
|
+
block_size * acq_realtime - (time.perf_counter() - updt_t),
|
|
482
|
+
0)
|
|
483
|
+
task._wait_stop(wait_t)
|
|
484
|
+
|
|
485
|
+
if task.stop_set:
|
|
486
|
+
total_t = time.perf_counter() - start_t
|
|
487
|
+
logger.info(f"Acq. stopped after {total_t}s")
|
|
488
|
+
yield None
|
|
489
|
+
else:
|
|
490
|
+
while block_pix < block_size:
|
|
491
|
+
# generating points at regular interval (e.g every acq_realtime seconds)
|
|
492
|
+
wait_t = min(
|
|
493
|
+
refresh_time,
|
|
494
|
+
(block_size - block_pix) * acq_realtime,
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
# wait the amount of time, or the wait event
|
|
498
|
+
task._wait_stop(wait_t)
|
|
499
|
+
|
|
500
|
+
if task.stop_set:
|
|
501
|
+
# stop requested
|
|
502
|
+
# TODO: set data
|
|
503
|
+
total_t = time.perf_counter() - start_t
|
|
504
|
+
logger.info(f"Acq. stopped after {total_t}s")
|
|
505
|
+
# self._chan_running = [False] * self.n_detectors
|
|
506
|
+
# self._acq_started = False
|
|
507
|
+
yield None
|
|
508
|
+
|
|
509
|
+
# calculate the total number of points that
|
|
510
|
+
# would have been generated from that wait
|
|
511
|
+
# => necessary to correct for drifts
|
|
512
|
+
block_pix = int((time.perf_counter() - start_t) // acq_realtime) - n_pix
|
|
513
|
+
|
|
514
|
+
# new current pixel value
|
|
515
|
+
# self._current_pixel = n_pix + block_pix
|
|
516
|
+
|
|
517
|
+
if block_pix > block_size:
|
|
518
|
+
# just in case we waited a bit too long and
|
|
519
|
+
# if yes: put them in the next buffer, if any
|
|
520
|
+
extra = block_pix - block_size
|
|
521
|
+
block_pix = block_size
|
|
522
|
+
else:
|
|
523
|
+
extra = 0
|
|
524
|
+
n_pix += block_pix
|
|
525
|
+
updt_t = time.perf_counter()
|
|
526
|
+
yield block_pix
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
class Generator(Device):
|
|
530
|
+
""" DS for the data generator. """
|
|
531
|
+
simu_data = device_property(dtype=str, mandatory=False)
|
|
532
|
+
|
|
533
|
+
def __init__(self, *args, **kwargs):
|
|
534
|
+
self._controller = None
|
|
535
|
+
self._mosca = None
|
|
536
|
+
Device.__init__(self, *args, **kwargs)
|
|
537
|
+
|
|
538
|
+
def init_device(self):
|
|
539
|
+
super().init_device()
|
|
540
|
+
logger.info("in init_device()")
|
|
541
|
+
logger.info("simu_data: %s", self.simu_data)
|
|
542
|
+
# for v1 compat
|
|
543
|
+
self.controller
|
|
544
|
+
|
|
545
|
+
@property
|
|
546
|
+
def controller(self):
|
|
547
|
+
if self._controller is None:
|
|
548
|
+
self._controller = get_controller()
|
|
549
|
+
if self.simu_data:
|
|
550
|
+
self._controller.generator().data_file = self.simu_data
|
|
551
|
+
return self._controller
|
|
552
|
+
|
|
553
|
+
@property
|
|
554
|
+
def mosca(self):
|
|
555
|
+
if self._mosca is None:
|
|
556
|
+
self._mosca = get_mosca()
|
|
557
|
+
return self._mosca
|
|
558
|
+
|
|
559
|
+
@property
|
|
560
|
+
def generator(self):
|
|
561
|
+
return self.controller.generator()
|
|
562
|
+
|
|
563
|
+
@command
|
|
564
|
+
def hw_trig(self):
|
|
565
|
+
self.generator.hw_trig()
|
|
566
|
+
|
|
567
|
+
@attribute(dtype=bool)
|
|
568
|
+
def use_external_trigger(self):
|
|
569
|
+
return self.generator.use_external_trigger
|
|
570
|
+
|
|
571
|
+
@use_external_trigger.write
|
|
572
|
+
def use_external_trigger(self, use_external_trigger):
|
|
573
|
+
logger.info("Setting use_external_trigger to %fs.", use_external_trigger)
|
|
574
|
+
self.generator.use_external_trigger = use_external_trigger
|
|
575
|
+
|
|
576
|
+
@attribute(dtype=int)
|
|
577
|
+
def block_size(self):
|
|
578
|
+
return self.generator.block_size
|
|
579
|
+
|
|
580
|
+
@block_size.write
|
|
581
|
+
def block_size(self, block_size):
|
|
582
|
+
self.generator.block_size = block_size
|
|
583
|
+
|
|
584
|
+
@attribute(dtype=TriggerMode)
|
|
585
|
+
def trigger_mode(self):
|
|
586
|
+
return self.generator.trigger_mode
|
|
587
|
+
|
|
588
|
+
@trigger_mode.write
|
|
589
|
+
def trigger_mode(self, trigger_mode):
|
|
590
|
+
self.generator.trigger_mode = trigger_mode
|
|
591
|
+
|
|
592
|
+
@attribute(dtype=float)
|
|
593
|
+
def acq_realtime(self):
|
|
594
|
+
return self.generator.acq_realtime
|
|
595
|
+
|
|
596
|
+
@acq_realtime.write
|
|
597
|
+
def acq_realtime(self, acq_realtime):
|
|
598
|
+
self.generator.acq_realtime = acq_realtime
|
|
599
|
+
|
|
600
|
+
@attribute(dtype=int)
|
|
601
|
+
def acq_nb_points(self):
|
|
602
|
+
return self.generator.acq_nb_points
|
|
603
|
+
|
|
604
|
+
@acq_nb_points.write
|
|
605
|
+
def acq_nb_points(self, acq_nb_points):
|
|
606
|
+
self.generator.acq_nb_points = acq_nb_points
|
|
607
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .simfalconx import SimFalconX, SIMFALCONX_DEVINFO
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
logger = logging.getLogger(__name__)
|
|
3
|
+
|
|
4
|
+
import copy
|
|
5
|
+
from lark import Lark, Transformer
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
ini_grammar = r"""
|
|
9
|
+
?value: (command | CR)*
|
|
10
|
+
|
|
11
|
+
KEY.1: /[^= ]+/
|
|
12
|
+
POLARITY.1: ("+" | "-")
|
|
13
|
+
FLOAT.2: /([+-]{0,1}(\d+\.\d*|\.\d+)(e[-+]?\d+)?|\d+(e[-+]?\d+))/i
|
|
14
|
+
INTEGER.2: INT
|
|
15
|
+
|
|
16
|
+
ANY_STR.1: /.+/
|
|
17
|
+
|
|
18
|
+
section.1: "[" /[^]]+/ "]"
|
|
19
|
+
mapping.1: KEY " = " (FLOAT | INTEGER | POLARITY | ANY_STR)
|
|
20
|
+
start.1: "START " ("#" INTEGER | "module" INTEGER)
|
|
21
|
+
end.1: "END " ("#" INTEGER | "module" INTEGER)
|
|
22
|
+
ignore.1: /\*+.+/
|
|
23
|
+
|
|
24
|
+
command: section
|
|
25
|
+
| mapping
|
|
26
|
+
| start
|
|
27
|
+
| end
|
|
28
|
+
| ignore
|
|
29
|
+
|
|
30
|
+
%import common.ESCAPED_STRING
|
|
31
|
+
%import common.SIGNED_NUMBER
|
|
32
|
+
%import common.INT
|
|
33
|
+
%import common.DIGIT
|
|
34
|
+
%import common.SIGNED_INT
|
|
35
|
+
%import common._STRING_INNER
|
|
36
|
+
%import common.WS
|
|
37
|
+
%import common.HEXDIGIT
|
|
38
|
+
%import common.CR
|
|
39
|
+
%ignore WS
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class IniTransformer(Transformer):
|
|
44
|
+
# TODO: check that END matches the current START
|
|
45
|
+
|
|
46
|
+
INTEGER = int
|
|
47
|
+
SIGNED_INT = int
|
|
48
|
+
SIGNED_NUMBER = float
|
|
49
|
+
FLOAT = float
|
|
50
|
+
KEY = str
|
|
51
|
+
POLARITY = lambda self, value: {"+": 1, "-": -1}[value]
|
|
52
|
+
ANY_STR = str
|
|
53
|
+
|
|
54
|
+
data = property(lambda self: copy.deepcopy(self._data or {}))
|
|
55
|
+
|
|
56
|
+
def __init__(self, *args, **kwargs):
|
|
57
|
+
self._data = {}
|
|
58
|
+
self._section = None
|
|
59
|
+
self._item = None
|
|
60
|
+
|
|
61
|
+
def section(self, value):
|
|
62
|
+
expected = (
|
|
63
|
+
"module channel data",
|
|
64
|
+
"detector definitions",
|
|
65
|
+
"firmware definitions",
|
|
66
|
+
"default definitions",
|
|
67
|
+
"module definitions",
|
|
68
|
+
)
|
|
69
|
+
idx = expected.index(value[0])
|
|
70
|
+
if idx == 0:
|
|
71
|
+
self._section = self._data.setdefault("module_channel_data", {})
|
|
72
|
+
else:
|
|
73
|
+
self._section = self._data.setdefault(value[0].split(" ")[0], {})
|
|
74
|
+
|
|
75
|
+
def mapping(self, value):
|
|
76
|
+
key = value[0]
|
|
77
|
+
self._item[key] = value[1]
|
|
78
|
+
|
|
79
|
+
def start(self, value):
|
|
80
|
+
self._item = self._section.setdefault(value[0], {})
|
|
81
|
+
|
|
82
|
+
def end(self, value):
|
|
83
|
+
self._item = None
|
|
84
|
+
|
|
85
|
+
def ignore(self, value):
|
|
86
|
+
pass
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def load_xia_ini(f_path):
|
|
90
|
+
logger.info("Parsing file [%s]", f_path)
|
|
91
|
+
lark = Lark(ini_grammar, start="value", propagate_positions=True)
|
|
92
|
+
transformer = IniTransformer()
|
|
93
|
+
|
|
94
|
+
with open(f_path) as f_ini:
|
|
95
|
+
for li in f_ini:
|
|
96
|
+
tree = lark.parse(li)
|
|
97
|
+
transformer.transform(tree)
|
|
98
|
+
logger.info("Parsing done.")
|
|
99
|
+
return transformer.data
|