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
mosca/control/mosca.py
ADDED
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import copy
|
|
3
|
+
import sys
|
|
4
|
+
import os
|
|
5
|
+
import enum
|
|
6
|
+
import queue
|
|
7
|
+
# import typing
|
|
8
|
+
import datetime
|
|
9
|
+
from typing import Union
|
|
10
|
+
import argparse
|
|
11
|
+
from functools import wraps
|
|
12
|
+
from contextlib import contextmanager
|
|
13
|
+
import numpy as np
|
|
14
|
+
|
|
15
|
+
import pathlib
|
|
16
|
+
|
|
17
|
+
from ..common import to_enum, Status, TriggerMode, GateMode, AsyncTask, DiscardMode
|
|
18
|
+
from .datahandler import DataHandler
|
|
19
|
+
from .detectors import Detectors
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
import logging
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def setparam(func, forbidden=tuple(), allowed=(Status.READY,)):
|
|
27
|
+
""" Decorator preventing the decorated method call when the state is "RUNNING".
|
|
28
|
+
Mostly applied to parameter setters.
|
|
29
|
+
"""
|
|
30
|
+
def inner(self, *args, **kwargs):
|
|
31
|
+
if self.is_running():
|
|
32
|
+
raise RuntimeError("Can't set parameters while an acquisition is running.")
|
|
33
|
+
self.unprepare()
|
|
34
|
+
return func(self, *args, **kwargs)
|
|
35
|
+
return inner
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Acq:
|
|
39
|
+
def __init__(self,
|
|
40
|
+
acq_id,
|
|
41
|
+
acq_nb_points,
|
|
42
|
+
metadata=None,
|
|
43
|
+
discard_mode=DiscardMode.NONE):
|
|
44
|
+
|
|
45
|
+
self.acq_id = acq_id
|
|
46
|
+
self.acq_nb_points = acq_nb_points
|
|
47
|
+
self.discard_mode = discard_mode
|
|
48
|
+
|
|
49
|
+
self.triggered = 0
|
|
50
|
+
self.read = 0
|
|
51
|
+
self.discarded = False
|
|
52
|
+
self.n_starts = 0
|
|
53
|
+
|
|
54
|
+
self._metadata = copy.deepcopy(metadata or {})
|
|
55
|
+
self._metadata.update({"acq_id": acq_id,
|
|
56
|
+
"n_starts": 0})
|
|
57
|
+
|
|
58
|
+
def metadata(self):
|
|
59
|
+
self._metadata["n_starts"] = self.n_starts
|
|
60
|
+
return copy.deepcopy(self._metadata)
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def resumed(self):
|
|
64
|
+
return self.n_starts > 1
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Mosca:
|
|
68
|
+
""" Main class of the Mosca module. """
|
|
69
|
+
|
|
70
|
+
# TODO fix properties for mkdocstrings
|
|
71
|
+
|
|
72
|
+
controller = property(lambda self: self._controller)
|
|
73
|
+
data = property(lambda self: self._data_mgr)
|
|
74
|
+
|
|
75
|
+
stats_names = property(lambda self: self.controller.get_stats_names())
|
|
76
|
+
|
|
77
|
+
# discard_first_point = property(lambda self: self._discard_first_point)
|
|
78
|
+
# """ If set to True the first point of an acquisition will be discarded. """
|
|
79
|
+
|
|
80
|
+
poll_interval = property(lambda self: self._poll_interval)
|
|
81
|
+
|
|
82
|
+
def __init__(self,
|
|
83
|
+
controller,
|
|
84
|
+
data_mgr=None,
|
|
85
|
+
spectrum_size: int=4096, **kwargs):
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
controller: instance of `mosca.control.controller.Controller`
|
|
90
|
+
data_mgr: instance of `mosca.control.datahandler.DataHandler`. If not provided, a new one will be instanciated.
|
|
91
|
+
spectrum_size: deprecated
|
|
92
|
+
spectrum_size is there for those controllers that can't read back anything...
|
|
93
|
+
"""
|
|
94
|
+
super().__init__()
|
|
95
|
+
|
|
96
|
+
self._controller = controller
|
|
97
|
+
if data_mgr is None:
|
|
98
|
+
data_mgr = DataHandler()
|
|
99
|
+
self._data_mgr = data_mgr
|
|
100
|
+
|
|
101
|
+
self._acq_nb_points = 1
|
|
102
|
+
self._acq_nb_triggers = 1
|
|
103
|
+
self._acq_realtime = 1.0
|
|
104
|
+
self._poll_interval = 0.01
|
|
105
|
+
# self._discard_first_point = False
|
|
106
|
+
# self._n_triggered_points = 0
|
|
107
|
+
self._discard_mode = DiscardMode.NONE
|
|
108
|
+
|
|
109
|
+
self._detectors = Detectors()
|
|
110
|
+
self._poll_task = None
|
|
111
|
+
self._block_size = 1
|
|
112
|
+
|
|
113
|
+
self._trigger_mode = TriggerMode.UNKNOWN #self.supported_trigger_modes[0]
|
|
114
|
+
self._gate_mode = GateMode.UNKNOWN #self.supported_gate_modes[0]
|
|
115
|
+
self._spectrum_size = 0 #controller.spectrum_size()
|
|
116
|
+
|
|
117
|
+
self._acquisition = None
|
|
118
|
+
|
|
119
|
+
self.resetup()
|
|
120
|
+
self._controller.callback.register(self._controller_changed)
|
|
121
|
+
|
|
122
|
+
@property
|
|
123
|
+
def discard_mode(self):
|
|
124
|
+
return self._discard_mode
|
|
125
|
+
|
|
126
|
+
@discard_mode.setter
|
|
127
|
+
@setparam
|
|
128
|
+
def discard_mode(self, discard_mode):
|
|
129
|
+
self._discard_mode = to_enum(discard_mode, DiscardMode)
|
|
130
|
+
|
|
131
|
+
def _controller_changed(self, reset=True):
|
|
132
|
+
if reset:
|
|
133
|
+
self.resetup()
|
|
134
|
+
else:
|
|
135
|
+
self.reconfig()
|
|
136
|
+
|
|
137
|
+
def clear(self):
|
|
138
|
+
self._acquisition = None
|
|
139
|
+
self.data.clear()
|
|
140
|
+
|
|
141
|
+
def resetup(self):
|
|
142
|
+
""" Method that should be called every time
|
|
143
|
+
the detector configuration is changed
|
|
144
|
+
(e.g: config file loaded, etc...).
|
|
145
|
+
The detectors list will be updated, and all detectors
|
|
146
|
+
that are not hardware disabled will be considered as
|
|
147
|
+
enabled."""
|
|
148
|
+
controller = self.controller
|
|
149
|
+
|
|
150
|
+
if controller.mca_status() == Status.UNINITIALIZED:
|
|
151
|
+
self._trigger_mode = TriggerMode.UNKNOWN #self.supported_trigger_modes[0]
|
|
152
|
+
self._gate_mode = GateMode.UNKNOWN #self.supported_gate_modes[0]
|
|
153
|
+
self._detectors = Detectors()
|
|
154
|
+
# self._spectrum_size = 0 #controller.spectrum_size()
|
|
155
|
+
else:
|
|
156
|
+
self._trigger_mode = self.supported_trigger_modes[0]
|
|
157
|
+
self._gate_mode = self.supported_gate_modes[0]
|
|
158
|
+
detectors = self.controller.available_detectors()
|
|
159
|
+
self._detectors = Detectors(detectors)
|
|
160
|
+
try:
|
|
161
|
+
disabled = self.controller.disabled_detectors()
|
|
162
|
+
for chan in disabled:
|
|
163
|
+
self._detectors.set_hw_disabled(chan)
|
|
164
|
+
except NotImplementedError:
|
|
165
|
+
pass
|
|
166
|
+
self.reconfig()
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def n_triggered_points(self):
|
|
170
|
+
""" Number of triggers received by the controller. """
|
|
171
|
+
if self._acquisition and self._acquisition.triggered > 0:
|
|
172
|
+
# triggered can be temporarily beT
|
|
173
|
+
# (set in the poll method)
|
|
174
|
+
return self._acquisition.triggered
|
|
175
|
+
return 0
|
|
176
|
+
|
|
177
|
+
def reconfig(self):
|
|
178
|
+
controller = self.controller
|
|
179
|
+
self.data.clear()
|
|
180
|
+
if controller.mca_status() == Status.UNINITIALIZED:
|
|
181
|
+
# self._trigger_mode = TriggerMode.UNKNOWN #self.supported_trigger_modes[0]
|
|
182
|
+
# self._gate_mode = TriggerMode.UNKNOWN #self.supported_gate_modes[0]
|
|
183
|
+
# self._detectors = Detectors()
|
|
184
|
+
self._spectrum_size = 0 #controller.spectrum_size()
|
|
185
|
+
else:
|
|
186
|
+
# detectors = self.controller.available_detectors()
|
|
187
|
+
# self._detectors = Detectors(detectors)
|
|
188
|
+
# try:
|
|
189
|
+
# disabled = self.controller.disabled_detectors()
|
|
190
|
+
# for chan in disabled:
|
|
191
|
+
# self._detectors.set_hw_disabled(chan)
|
|
192
|
+
# except NotImplementedError:
|
|
193
|
+
# pass
|
|
194
|
+
self._spectrum_size = controller.spectrum_size()
|
|
195
|
+
self.data.set_detectors(copy.deepcopy(self.detectors))
|
|
196
|
+
self.data.set_spectrum_size(self.spectrum_size)
|
|
197
|
+
self.data.set_stats_names(self.stats_names[:])
|
|
198
|
+
# self.reconfig()
|
|
199
|
+
|
|
200
|
+
# if controller.mca_status == Status.UNINITIALIZED:
|
|
201
|
+
# else:
|
|
202
|
+
# self._spectrum_size = self.controller.spectrum_size()
|
|
203
|
+
# with self.data.delayed_reconfig:
|
|
204
|
+
# self.data.set_detectors(copy.deepcopy(self.detectors))
|
|
205
|
+
# self.data.set_spectrum_size(self.spectrum_size)
|
|
206
|
+
# self.data.set_stats_names(self.stats_names[:])
|
|
207
|
+
|
|
208
|
+
def metadata(self, acq_id=None):
|
|
209
|
+
ctrl = self.controller.metadata()
|
|
210
|
+
data = self.data.metadata()
|
|
211
|
+
|
|
212
|
+
metadata = {"mosca":
|
|
213
|
+
{"block_size": self.block_size,
|
|
214
|
+
"acq_nb_points": self.acq_nb_points,
|
|
215
|
+
"acq_realtime": self.acq_realtime,
|
|
216
|
+
"acq_nb_triggers": self.acq_nb_triggers,
|
|
217
|
+
"gate_mode": self.gate_mode.name,
|
|
218
|
+
"trigger_mode": self.trigger_mode.name,
|
|
219
|
+
"available_detectors": self.detectors.available_detectors,
|
|
220
|
+
"enabled_detectors": self.detectors.detectors,
|
|
221
|
+
"discard_mode": self.discard_mode.name
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
metadata.update({"controller": ctrl or "na"})
|
|
226
|
+
metadata.update({"data": data or "na"})
|
|
227
|
+
return metadata
|
|
228
|
+
|
|
229
|
+
@property
|
|
230
|
+
def status(self) -> Status:
|
|
231
|
+
""" Returns the acquisition status. """
|
|
232
|
+
# return self.controller.mca_status()
|
|
233
|
+
# TODO: lock/reset PREPARED
|
|
234
|
+
if self.is_running():
|
|
235
|
+
return Status.RUNNING
|
|
236
|
+
else:
|
|
237
|
+
return self.controller.mca_status()
|
|
238
|
+
|
|
239
|
+
# @discard_first_point.setter
|
|
240
|
+
# @setparam
|
|
241
|
+
# def discard_first_point(self, discard: bool):
|
|
242
|
+
# self._discard_first_point = discard
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def block_size(self):
|
|
246
|
+
return self._block_size
|
|
247
|
+
|
|
248
|
+
@block_size.setter
|
|
249
|
+
@setparam
|
|
250
|
+
def block_size(self, block_size):
|
|
251
|
+
self._block_size = block_size
|
|
252
|
+
|
|
253
|
+
@property
|
|
254
|
+
def detectors(self) -> Detectors:
|
|
255
|
+
return self._detectors
|
|
256
|
+
|
|
257
|
+
@setparam
|
|
258
|
+
def disable_detectors(self, *detectors: int):
|
|
259
|
+
logger.info("Disabling detectors %s.", detectors)
|
|
260
|
+
for chan in detectors:
|
|
261
|
+
try:
|
|
262
|
+
self.controller.disable_detector(chan)
|
|
263
|
+
self.detectors.set_hw_disabled(chan)
|
|
264
|
+
except NotImplementedError:
|
|
265
|
+
logger.debug("Hardware doesnt support disabling detector. Using soft disable.")
|
|
266
|
+
self.detectors.set_sw_disabled(chan)
|
|
267
|
+
self.reconfig()
|
|
268
|
+
|
|
269
|
+
@setparam
|
|
270
|
+
def enable_detectors(self, *detectors: int):
|
|
271
|
+
logger.info("Enabling detector %s.", detectors)
|
|
272
|
+
for chan in detectors:
|
|
273
|
+
try:
|
|
274
|
+
self.controller.enable_detector(chan)
|
|
275
|
+
except NotImplementedError:
|
|
276
|
+
# TODO, make sure that detector was not in disabled_detectors
|
|
277
|
+
pass
|
|
278
|
+
self.detectors.set_enabled(chan)
|
|
279
|
+
self.reconfig()
|
|
280
|
+
|
|
281
|
+
# def enable_all_detectors(self):
|
|
282
|
+
# logger.info("Enabling all detectors.")
|
|
283
|
+
# for chan in self.available_detectors():
|
|
284
|
+
# self.enable_detector(chan)
|
|
285
|
+
|
|
286
|
+
# TODO
|
|
287
|
+
# def reset(self):
|
|
288
|
+
# self._reset()
|
|
289
|
+
# self.enable_all_detectors()
|
|
290
|
+
|
|
291
|
+
@poll_interval.setter
|
|
292
|
+
def poll_interval(self, poll_interval):
|
|
293
|
+
self._poll_interval = max(0., poll_interval)
|
|
294
|
+
|
|
295
|
+
# =======================
|
|
296
|
+
# Setting up acquisition
|
|
297
|
+
# =======================
|
|
298
|
+
def unprepare(self):
|
|
299
|
+
""" Flags the controller as being "unprepared". """
|
|
300
|
+
self.controller.unprepare()
|
|
301
|
+
|
|
302
|
+
def apply_mosca(self, resume=False):
|
|
303
|
+
""" Applies MOSCA parameters. """
|
|
304
|
+
try:
|
|
305
|
+
self.clear()
|
|
306
|
+
self._apply_mosca(resume=resume)
|
|
307
|
+
except Exception as ex:
|
|
308
|
+
logger.exception(ex)
|
|
309
|
+
raise
|
|
310
|
+
|
|
311
|
+
def prepare(self):
|
|
312
|
+
"""" Prepares acquisition using the current hardware configuration.
|
|
313
|
+
|
|
314
|
+
danger: applying mosca parameters
|
|
315
|
+
`apply_mosca` should be called before if you want to pass
|
|
316
|
+
the MOSCA parameters onto the Controller. """
|
|
317
|
+
logger.info("Preparing controller.")
|
|
318
|
+
try:
|
|
319
|
+
acq_id = int(datetime.datetime.now().timestamp()*10)
|
|
320
|
+
self._acquisition = Acq(acq_id, self.acq_nb_points, discard_mode=self.discard_mode)
|
|
321
|
+
self.controller.prepare_acq(resume=False)
|
|
322
|
+
self._spectrum_size = self.controller.spectrum_size()
|
|
323
|
+
# self.data.set_detectors(self.detectors)
|
|
324
|
+
self.reconfig()
|
|
325
|
+
# self._n_triggered_points = 0
|
|
326
|
+
self.data.prepare()
|
|
327
|
+
except Exception as ex:
|
|
328
|
+
logger.exception(ex)
|
|
329
|
+
raise
|
|
330
|
+
|
|
331
|
+
def _apply_mosca(self, resume=False):
|
|
332
|
+
""" Sends the current mosca parameters to the controller. """
|
|
333
|
+
|
|
334
|
+
logger.info("Applying MOSCA parameters.\n"
|
|
335
|
+
" - acq_nb_points=%s\n"
|
|
336
|
+
" - acq_realtime=%s\n"
|
|
337
|
+
" - acq_nb_triggers=%s\n"
|
|
338
|
+
" - spectrum_size=%s\n"
|
|
339
|
+
" - trigger_mode=%s\n"
|
|
340
|
+
# " - run_mode=%s\n"
|
|
341
|
+
" - gate_mode=%s\n"
|
|
342
|
+
# " - start_trigger=%s\n"
|
|
343
|
+
# " - stop_trigger=%s\n"
|
|
344
|
+
" - block_size=%s\n"
|
|
345
|
+
" - resume=%s\n",
|
|
346
|
+
self.acq_nb_points,
|
|
347
|
+
self.acq_realtime,
|
|
348
|
+
self.acq_nb_triggers,
|
|
349
|
+
self.spectrum_size,
|
|
350
|
+
TriggerMode(self.trigger_mode).name,
|
|
351
|
+
# self.run_mode,
|
|
352
|
+
GateMode(self.gate_mode).name,
|
|
353
|
+
# self.start_trigger,
|
|
354
|
+
# self.stop_trigger,
|
|
355
|
+
self.block_size,
|
|
356
|
+
resume)
|
|
357
|
+
# do try / except
|
|
358
|
+
self.controller.apply_mosca(
|
|
359
|
+
acq_nb_points=self.acq_nb_points,
|
|
360
|
+
acq_realtime=self.acq_realtime,
|
|
361
|
+
acq_nb_triggers=self.acq_nb_triggers,
|
|
362
|
+
# run_mode=self.run_mode,
|
|
363
|
+
spectrum_size=self.spectrum_size,
|
|
364
|
+
trigger_mode=self.trigger_mode,
|
|
365
|
+
gate_mode=self.gate_mode,
|
|
366
|
+
# start_trigger=self.start_trigger,
|
|
367
|
+
# stop_trigger=self.stop_trigger,
|
|
368
|
+
block_size=self.block_size,
|
|
369
|
+
resume=resume)
|
|
370
|
+
|
|
371
|
+
# =======================
|
|
372
|
+
# Starting/stopping acquisition
|
|
373
|
+
# =======================
|
|
374
|
+
def start_mosca(self, wait: bool=False):
|
|
375
|
+
""" Applies the MOSCA parameters then startw an acquisition. """
|
|
376
|
+
self.apply_mosca()
|
|
377
|
+
self.prepare()
|
|
378
|
+
return self.start_acq(wait=wait)
|
|
379
|
+
|
|
380
|
+
def start_acq(self, resume=False, wait: bool=False):
|
|
381
|
+
""" Starts an acquisition.
|
|
382
|
+
|
|
383
|
+
danger: prerequisite
|
|
384
|
+
`apply_mosca` (optional) and `prepare` should have been called before.
|
|
385
|
+
"""
|
|
386
|
+
# TODO: check mca_status and prepared
|
|
387
|
+
#############################
|
|
388
|
+
# TODO: until there is a state machine checking that
|
|
389
|
+
# prepared has been called
|
|
390
|
+
logger.info("==== Start requested, resume=%s.", resume)
|
|
391
|
+
# self.prepare(resume=resume)
|
|
392
|
+
#############################
|
|
393
|
+
|
|
394
|
+
if not self._acquisition:
|
|
395
|
+
raise RuntimeError("Can't start an acquisition without calling prepare first.")
|
|
396
|
+
|
|
397
|
+
if self._acquisition.n_starts > 0 and not resume:
|
|
398
|
+
# forcing the use of the keyword resume if this
|
|
399
|
+
# isnt the first start_acq since prepare was called.
|
|
400
|
+
raise RuntimeError("The use the prepare keyword is mandatory if you're trying to resume"
|
|
401
|
+
" an acquisition. Otherwise, call prepare before start_acq.")
|
|
402
|
+
|
|
403
|
+
logger.info("Starting acquisition (acq_id=%ss).", self._acquisition.acq_id)
|
|
404
|
+
try:
|
|
405
|
+
self._acquisition.n_starts += 1
|
|
406
|
+
self.data.notify_start()
|
|
407
|
+
self.controller.start_acq(resume=resume)
|
|
408
|
+
self._poll_task = AsyncTask(run_func=self._run_poll,
|
|
409
|
+
cleanup_func=self._cleanup_poll)
|
|
410
|
+
logger.info("Acquisition started.")
|
|
411
|
+
if wait:
|
|
412
|
+
self._poll_task.join()
|
|
413
|
+
except Exception as ex:
|
|
414
|
+
logger.exception(ex)
|
|
415
|
+
self.abort_acq()
|
|
416
|
+
# self.data.notify_end()
|
|
417
|
+
self._acquisition.n_starts -= 1
|
|
418
|
+
raise
|
|
419
|
+
|
|
420
|
+
return self._acquisition.acq_id
|
|
421
|
+
|
|
422
|
+
def abort_acq(self):
|
|
423
|
+
self.stop_acq()
|
|
424
|
+
|
|
425
|
+
def stop_acq(self):#, force=False):
|
|
426
|
+
""" Tells the controller to stop a running acquisition and waits for it to finish. """
|
|
427
|
+
|
|
428
|
+
logger.info("Stop acquisition requested.")
|
|
429
|
+
# commented on 4th Sept. 2026
|
|
430
|
+
# if not self.is_running():
|
|
431
|
+
# logger.warning("No acquisition running.")
|
|
432
|
+
try:
|
|
433
|
+
self.controller.stop_acq()
|
|
434
|
+
except Exception as ex:
|
|
435
|
+
logger.exception(ex)
|
|
436
|
+
raise
|
|
437
|
+
finally:
|
|
438
|
+
self.wait_read()
|
|
439
|
+
self.data.notify_end()
|
|
440
|
+
self.data.wait_done()
|
|
441
|
+
logger.info("Acquisition stopped.")
|
|
442
|
+
|
|
443
|
+
# Attention: this one is run in the polling thread
|
|
444
|
+
def _cleanup_poll(self, task, *args, **kwargs):
|
|
445
|
+
""" Called when the polling thread finishes. """
|
|
446
|
+
logger.warning("Cleanup.")
|
|
447
|
+
self._poll_task = None
|
|
448
|
+
self.controller.stop_acq()
|
|
449
|
+
|
|
450
|
+
# Attention: this one is run in the polling thread
|
|
451
|
+
def _run_poll(self, task, *args, **kwargs):
|
|
452
|
+
""" Polling method, running in the polling thread. """
|
|
453
|
+
# TODO: notify main about polling stopped by error
|
|
454
|
+
|
|
455
|
+
logger.info("Starting poll function.")
|
|
456
|
+
logger.info("Discard mode=%s.", self.discard_mode.name)
|
|
457
|
+
|
|
458
|
+
triggered_offset = 0
|
|
459
|
+
discard_mode = self._acquisition.discard_mode
|
|
460
|
+
if not self._acquisition.resumed:
|
|
461
|
+
if discard_mode == DiscardMode.FIRST:
|
|
462
|
+
triggered_offset = -1
|
|
463
|
+
else:
|
|
464
|
+
triggered_offset = self._acquisition.triggered
|
|
465
|
+
|
|
466
|
+
# seed with the offset now, so a stop before the first poll iteration
|
|
467
|
+
# doesn't silently lose it (the loop overwrites this on its first pass anyway)
|
|
468
|
+
self._acquisition.triggered = triggered_offset
|
|
469
|
+
|
|
470
|
+
# if n_triggered_points is not 0 we assume that start_acq was called with
|
|
471
|
+
# resume=True
|
|
472
|
+
# triggered_offset = 0
|
|
473
|
+
# if self._acquisition.n_triggered_points != 0:
|
|
474
|
+
# logger.warning("Number of triggered points not reset. Assuming scan is resumed.")
|
|
475
|
+
# triggered_offset = self._n_triggered_points
|
|
476
|
+
# self._n_triggered_points = 0
|
|
477
|
+
# discard = True
|
|
478
|
+
# else:
|
|
479
|
+
# resumed = False
|
|
480
|
+
|
|
481
|
+
# Number of read points during this polling round
|
|
482
|
+
n_acquired = 0
|
|
483
|
+
|
|
484
|
+
# discard_mode = self._acquisition.discard_mode
|
|
485
|
+
|
|
486
|
+
try:
|
|
487
|
+
t0 = time.perf_counter()
|
|
488
|
+
start_t = t0
|
|
489
|
+
t_wait = self.poll_interval
|
|
490
|
+
while not task._wait_stop(t_wait) and self.data.is_running():
|
|
491
|
+
if task.stop_set:
|
|
492
|
+
break
|
|
493
|
+
t0 = time.perf_counter()
|
|
494
|
+
poll_result = self.controller.poll_acq()
|
|
495
|
+
t1 = time.perf_counter()
|
|
496
|
+
n_pix = poll_result.n_read
|
|
497
|
+
if n_pix > 0:
|
|
498
|
+
|
|
499
|
+
# <<<<<<
|
|
500
|
+
# disabling until proven necessary
|
|
501
|
+
# and then this will need some work to support resume=True
|
|
502
|
+
|
|
503
|
+
# if the user requested discarding some points
|
|
504
|
+
# parameters: * number of acquired points since the beginning
|
|
505
|
+
# * number of points read in that polling loop
|
|
506
|
+
# discard_mode = self.discard_mode
|
|
507
|
+
# if discard_mode == DiscardMode.NONE:
|
|
508
|
+
# discard_lbd = lambda n_acq, n_spec: np.s_[:, :n_spec]
|
|
509
|
+
# elif discard_mode == DiscardMode.FIRST:
|
|
510
|
+
# # discard first point if n_acq == 1
|
|
511
|
+
# discard_lbd = lambda n_acq, n_spec: np.s_[:, :n_spec] if n_acq >= 1 else np.s_[:, 1:n_spec]
|
|
512
|
+
# elif discard_mode == DiscardMode.ODD:
|
|
513
|
+
# # discard first point (and +2 steps) if n_acq is even (n_acq % 2 == 0)
|
|
514
|
+
# discard_lbd = lambda n_acq, n_spec: np.s_[:, 1:n_spec:2] if n_acq % 2 else np.s_[:, :n_spec:2]
|
|
515
|
+
# else: # discard_mode == DiscardMode.EVEN
|
|
516
|
+
# # discard first point (and +2 steps) if n_acq is even (n_acq % 2 == 0)
|
|
517
|
+
# discard_lbd = lambda n_acq, n_spec: np.s_[:, :n_spec:2] if n_acq % 2 else np.s_[:, 1:n_spec:2]
|
|
518
|
+
|
|
519
|
+
# data_slice = discard_lbd(n_acquired, n_pix)
|
|
520
|
+
|
|
521
|
+
# >>>>>>
|
|
522
|
+
|
|
523
|
+
if self.data.n_read_points == 0 and discard_mode == DiscardMode.FIRST:
|
|
524
|
+
# discarding the first point
|
|
525
|
+
# this is the first time ever we read spectra (resumed acquisition or not)
|
|
526
|
+
# this will be applied only once
|
|
527
|
+
data_slice = np.s_[:, 1:n_pix]
|
|
528
|
+
else:
|
|
529
|
+
data_slice = np.s_[:, :n_pix]
|
|
530
|
+
|
|
531
|
+
spectra = poll_result.spectra[data_slice]
|
|
532
|
+
|
|
533
|
+
if spectra.shape[1] > 0:
|
|
534
|
+
if poll_result.stats is not None:
|
|
535
|
+
stats = poll_result.stats[data_slice]
|
|
536
|
+
else:
|
|
537
|
+
stats = None
|
|
538
|
+
self.data.add_spectra(spectra, stats)
|
|
539
|
+
|
|
540
|
+
# actual number of spectra kept, depending on the discard_mode
|
|
541
|
+
n_acquired += spectra.shape[1]
|
|
542
|
+
|
|
543
|
+
t2 = time.perf_counter()
|
|
544
|
+
logger.info("New point(s)=%s, total=%s (t_poll=%s, t_add=%s).", n_pix, self.data.n_read_points, t1 - t0, t2 - t1)
|
|
545
|
+
|
|
546
|
+
# total triggered is the number of triggers seen since the last call to start_acq
|
|
547
|
+
if poll_result.total_triggered is not None:
|
|
548
|
+
self._acquisition.triggered = poll_result.total_triggered + triggered_offset
|
|
549
|
+
else:
|
|
550
|
+
# n_acquired is already cumulative for this start_acq segment (discard-aware, see
|
|
551
|
+
# slices above); add triggered_offset to make it cumulative across resumes too,
|
|
552
|
+
# matching the total_triggered branch above.
|
|
553
|
+
self._acquisition.triggered = n_acquired + triggered_offset
|
|
554
|
+
|
|
555
|
+
if poll_result.finished:
|
|
556
|
+
logger.info("Hardware polling done! Total time=%ss.", time.perf_counter() - start_t)
|
|
557
|
+
break
|
|
558
|
+
|
|
559
|
+
t_wait = max(0.001, self.poll_interval - (time.perf_counter() - t0))
|
|
560
|
+
|
|
561
|
+
except RuntimeError as ex:
|
|
562
|
+
logger.exception(ex)
|
|
563
|
+
raise
|
|
564
|
+
finally:
|
|
565
|
+
self.data.notify_end()
|
|
566
|
+
logger.info("Polling function done.")
|
|
567
|
+
|
|
568
|
+
def wait_read(self):
|
|
569
|
+
""" Wait for the polling task to finish. """
|
|
570
|
+
if self._poll_task is not None:
|
|
571
|
+
self._poll_task.join()
|
|
572
|
+
|
|
573
|
+
def is_running(self) -> bool:
|
|
574
|
+
""" Returns True if an acquisition is in progress (polling, acquisition and data tasks). """
|
|
575
|
+
# TODO: add roi and saving status
|
|
576
|
+
return self.is_polling() or self.controller.is_running() or self.data.is_running()
|
|
577
|
+
|
|
578
|
+
def is_polling(self):
|
|
579
|
+
""" Returns True if the polling task is running. """
|
|
580
|
+
return self._poll_task is not None
|
|
581
|
+
|
|
582
|
+
@property
|
|
583
|
+
def supported_spectrum_sizes(self):
|
|
584
|
+
""" Supported spectra sizes """
|
|
585
|
+
# SORTED
|
|
586
|
+
try:
|
|
587
|
+
return self.controller.get_supported_spectrum_sizes()
|
|
588
|
+
except NotImplementedError:
|
|
589
|
+
return None
|
|
590
|
+
|
|
591
|
+
@property
|
|
592
|
+
def supported_trigger_modes(self) -> list[TriggerMode]:
|
|
593
|
+
""" Supported start triggers """
|
|
594
|
+
try:
|
|
595
|
+
modes = self.controller.get_supported_trigger_modes()
|
|
596
|
+
except NotImplementedError:
|
|
597
|
+
modes = tuple(TriggerMode)
|
|
598
|
+
return modes
|
|
599
|
+
|
|
600
|
+
@property
|
|
601
|
+
def supported_gate_modes(self):
|
|
602
|
+
""" Supported gate modes """
|
|
603
|
+
try:
|
|
604
|
+
modes = self.controller.get_supported_gate_modes()
|
|
605
|
+
if not modes:
|
|
606
|
+
modes = [GateMode.IGNORE]
|
|
607
|
+
except NotImplementedError:
|
|
608
|
+
modes = list([mode for mode in GateMode if mode != GateMode.UNKNOWN])
|
|
609
|
+
return modes
|
|
610
|
+
|
|
611
|
+
|
|
612
|
+
@property
|
|
613
|
+
def trigger_mode(self):
|
|
614
|
+
""" start trigger. """
|
|
615
|
+
return self._trigger_mode
|
|
616
|
+
|
|
617
|
+
@trigger_mode.setter
|
|
618
|
+
@setparam
|
|
619
|
+
def trigger_mode(self, trigger_mode: Union[TriggerMode, int, str]):
|
|
620
|
+
trigger_mode = to_enum(trigger_mode, TriggerMode)
|
|
621
|
+
if trigger_mode not in self.supported_trigger_modes:
|
|
622
|
+
raise RuntimeError(f"Unsupported trigger_mode [{trigger_mode.name}].")
|
|
623
|
+
self._trigger_mode = trigger_mode
|
|
624
|
+
|
|
625
|
+
@property
|
|
626
|
+
def gate_mode(self) -> GateMode:
|
|
627
|
+
""" gate mode """
|
|
628
|
+
return self._gate_mode
|
|
629
|
+
|
|
630
|
+
@gate_mode.setter
|
|
631
|
+
@setparam
|
|
632
|
+
def gate_mode(self, gate_mode: Union[GateMode, int, str]):
|
|
633
|
+
gate_mode = to_enum(gate_mode, GateMode)
|
|
634
|
+
if gate_mode not in self.supported_gate_modes:
|
|
635
|
+
raise RuntimeError(f"Unsupported gate_mode [{gate_mode.name}].")
|
|
636
|
+
# self._set_gate_mode(gate_mode)
|
|
637
|
+
self._gate_mode = gate_mode
|
|
638
|
+
|
|
639
|
+
@property
|
|
640
|
+
def acq_realtime(self):
|
|
641
|
+
""" Acquisition time, usualy used when run mode is set to REALTIME.
|
|
642
|
+
See your specific controller documentation to see how/where the parameter is used.
|
|
643
|
+
|
|
644
|
+
info:
|
|
645
|
+
Property can be set as an int, str or enum value.
|
|
646
|
+
"""
|
|
647
|
+
return self._acq_realtime
|
|
648
|
+
|
|
649
|
+
@acq_realtime.setter
|
|
650
|
+
@setparam
|
|
651
|
+
def acq_realtime(self, acq_realtime):
|
|
652
|
+
self._acq_realtime = acq_realtime
|
|
653
|
+
|
|
654
|
+
@property
|
|
655
|
+
def acq_nb_points(self):
|
|
656
|
+
""" Number of points to acquire.
|
|
657
|
+
See your specific controller documentation to see how/where the parameter is used.
|
|
658
|
+
"""
|
|
659
|
+
return self._acq_nb_points
|
|
660
|
+
|
|
661
|
+
@acq_nb_points.setter
|
|
662
|
+
@setparam
|
|
663
|
+
def acq_nb_points(self, acq_nb_points):
|
|
664
|
+
logger.info("SET: acq_nb_points to [%s].", acq_nb_points)
|
|
665
|
+
# self._set_acq_nb_points(acq_nb_points)
|
|
666
|
+
self._acq_nb_points = acq_nb_points
|
|
667
|
+
|
|
668
|
+
@property
|
|
669
|
+
def acq_nb_triggers(self) -> int:
|
|
670
|
+
""" Sets the duration of a point to a certain number of triggers.
|
|
671
|
+
See your specific controller documentation to see how/where the parameter is used."""
|
|
672
|
+
return self._acq_nb_triggers
|
|
673
|
+
|
|
674
|
+
@acq_nb_triggers.setter
|
|
675
|
+
@setparam
|
|
676
|
+
def acq_nb_triggers(self, acq_nb_triggers: int):
|
|
677
|
+
self._acq_nb_triggers = acq_nb_triggers
|
|
678
|
+
|
|
679
|
+
@property
|
|
680
|
+
def spectrum_size(self):
|
|
681
|
+
# try:
|
|
682
|
+
# self._spectrum_size = self._get_spectrum_size()
|
|
683
|
+
# except NotImplementedError:
|
|
684
|
+
# pass
|
|
685
|
+
return self._spectrum_size
|
|
686
|
+
|
|
687
|
+
@spectrum_size.setter
|
|
688
|
+
@setparam
|
|
689
|
+
def spectrum_size(self, spectrum_size):
|
|
690
|
+
supported = self.supported_spectrum_sizes
|
|
691
|
+
if supported is not None and spectrum_size not in supported:
|
|
692
|
+
raise ValueError(f"Unsupported spectrum size ({spectrum_size}. [{supported}])")
|
|
693
|
+
self._spectrum_size = spectrum_size
|