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.
Files changed (73) hide show
  1. mosca/__init__.py +11 -0
  2. mosca/common/__init__.py +9 -0
  3. mosca/common/enums.py +79 -0
  4. mosca/common/logger.py +139 -0
  5. mosca/common/threads.py +199 -0
  6. mosca/common/utils.py +70 -0
  7. mosca/control/__init__.py +4 -0
  8. mosca/control/buffer.py +526 -0
  9. mosca/control/controller.py +278 -0
  10. mosca/control/datahandler.py +261 -0
  11. mosca/control/detectors.py +116 -0
  12. mosca/control/mosca.py +693 -0
  13. mosca/control/spectra.py +36 -0
  14. mosca/control/stats.py +43 -0
  15. mosca/control/tasks/__init__.py +3 -0
  16. mosca/control/tasks/background.py +118 -0
  17. mosca/control/tasks/rois.py +466 -0
  18. mosca/control/tasks/saving.py +391 -0
  19. mosca/control/tasks/spectrasums.py +185 -0
  20. mosca/control/tasks/task.py +201 -0
  21. mosca/control/tasks/taskmanager.py +133 -0
  22. mosca/devices/__init__.py +28 -0
  23. mosca/devices/hamamatsu/__init__.py +0 -0
  24. mosca/devices/hamamatsu/hamamatsu.py +88 -0
  25. mosca/devices/hamamatsu/hamamatsuhw.py +1298 -0
  26. mosca/devices/oceanoptics/__init__.py +0 -0
  27. mosca/devices/oceanoptics/oceanoptics.py +59 -0
  28. mosca/devices/oceanoptics/oceanopticshw.py +379 -0
  29. mosca/devices/simulation/__init__.py +0 -0
  30. mosca/devices/simulation/dante/__init__.py +1 -0
  31. mosca/devices/simulation/dante/simdante.py +33 -0
  32. mosca/devices/simulation/dante/simdantehw.py +506 -0
  33. mosca/devices/simulation/dummy/__init__.py +1 -0
  34. mosca/devices/simulation/dummy/dummy.py +299 -0
  35. mosca/devices/simulation/generator.py +607 -0
  36. mosca/devices/simulation/xia/__init__.py +1 -0
  37. mosca/devices/simulation/xia/iniparser.py +99 -0
  38. mosca/devices/simulation/xia/simfalconx.py +26 -0
  39. mosca/devices/simulation/xia/simfalconxhw.py +599 -0
  40. mosca/devices/xglab/__init__.py +0 -0
  41. mosca/devices/xglab/dante.py +126 -0
  42. mosca/devices/xglab/dantehw.py +1178 -0
  43. mosca/devices/xia/__init__.py +0 -0
  44. mosca/devices/xia/falconx.py +232 -0
  45. mosca/devices/xia/handel/__init__.py +14 -0
  46. mosca/devices/xia/handel/_cffi.py +62 -0
  47. mosca/devices/xia/handel/error.py +272 -0
  48. mosca/devices/xia/handel/handel_inc/handel_constants.h +127 -0
  49. mosca/devices/xia/handel/handel_inc/handel_errors.h +254 -0
  50. mosca/devices/xia/handel/handel_inc/handel_generic.h +66 -0
  51. mosca/devices/xia/handel/interface.py +1025 -0
  52. mosca/devices/xia/handel/mapping.py +107 -0
  53. mosca/devices/xia/handel/parser.py +62 -0
  54. mosca/devices/xia/handel/stats.py +85 -0
  55. mosca/devices/xia/mercury.py +160 -0
  56. mosca/devices/xia/utils.py +47 -0
  57. mosca/devices/xia/xia.py +575 -0
  58. mosca/devices/xia/xiads.py +161 -0
  59. mosca/mosca.py +0 -0
  60. mosca/tango/__init__.py +2 -0
  61. mosca/tango/controller.py +80 -0
  62. mosca/tango/main.py +163 -0
  63. mosca/tango/mosca.py +513 -0
  64. mosca/tango/moscav1.py +408 -0
  65. mosca/tango/shared.py +23 -0
  66. mosca/tango/tasks/__init__.py +0 -0
  67. mosca/tango/tasks/background.py +55 -0
  68. mosca/tango/utils.py +109 -0
  69. mosca-2.0.2.dist-info/METADATA +32 -0
  70. mosca-2.0.2.dist-info/RECORD +73 -0
  71. mosca-2.0.2.dist-info/WHEEL +4 -0
  72. mosca-2.0.2.dist-info/entry_points.txt +2 -0
  73. mosca-2.0.2.dist-info/licenses/LICENSE +166 -0
@@ -0,0 +1,575 @@
1
+ import logging
2
+ logger = logging.getLogger(__name__)
3
+
4
+ import weakref
5
+ import os
6
+ import sys
7
+ import time
8
+ import enum
9
+ import numpy as np
10
+
11
+ from ...common import GateMode, TriggerMode, sleep, to_enum
12
+ from ...control import (Controller, PollResult, mca_property,
13
+ Status, ready_locked)
14
+
15
+ from .handel import interface as hi
16
+
17
+
18
+ # preset_modes
19
+ # /* Preset Run Types */
20
+ #define XIA_PRESET_NONE 0.0 /**< Run until stopped by user. */
21
+ #define XIA_PRESET_FIXED_REAL 1.0 /**< Run for fixed real time. */
22
+ #define XIA_PRESET_FIXED_LIVE 2.0 /**< Run for fixed live time.
23
+ # * Unimplemented for
24
+ # * FalconXn. */
25
+ #define XIA_PRESET_FIXED_EVENTS 3.0 /**< Run until a given number of
26
+ # * output pulses are counted. */
27
+ #define XIA_PRESET_FIXED_TRIGGERS 4.0 /**< Run until a given number of
28
+ # * input triggers are counted. */
29
+
30
+ class XiaPresets(enum.Enum):
31
+ XIA_PRESET_NONE = 0.0
32
+ XIA_PRESET_FIXED_REAL = 1.0
33
+ XIA_PRESET_FIXED_LIVE = 2.0
34
+ XIA_PRESET_FIXED_EVENTS = 3.0
35
+ XIA_PRESET_FIXED_TRIGGERS = 4.0
36
+
37
+
38
+ # TODO: load headers
39
+ class RunMode2Preset(enum.Enum):
40
+ FREERUN = XiaPresets.XIA_PRESET_NONE.value
41
+ REALTIME = XiaPresets.XIA_PRESET_FIXED_REAL.value
42
+ # TRIGGERS = 4.
43
+ # MAPPING = 0.
44
+
45
+
46
+ # /* Mapping Mode Point Control Types */
47
+ #define XIA_MAPPING_CTL_USER 0.0
48
+ #define XIA_MAPPING_CTL_GATE 1.0
49
+ #define XIA_MAPPING_CTL_SYNC 2.0
50
+
51
+ class XiaAdvanceModes(enum.Enum):
52
+ SOFT = 0.
53
+ GATE = 1.
54
+ SYNC = 2.
55
+
56
+
57
+ # Defined in md_generic.h
58
+ #define MD_ERROR 1
59
+ #define MD_WARNING 2
60
+ #define MD_INFO 3
61
+ #define MD_DEBUG 4
62
+ class XiaDebugLevels(enum.Enum):
63
+ MD_ERROR = 1
64
+ MD_WARNING = 2
65
+ MD_INFO = 3
66
+ MD_DEBUG = 4
67
+
68
+
69
+ # /* GATE polarity */
70
+ class XiaGatePolarity(enum.Enum):
71
+ XIA_GATE_COLLECT_HI = 0
72
+ XIA_GATE_COLLECT_LO = 1
73
+
74
+
75
+ class StepPoller:
76
+ def __init__(self, xia, masters, channels, ini_channels, spectrum_size, stats_size):
77
+ self._handel = weakref.proxy(xia.hi)
78
+ self._channels = channels
79
+ self._masters = masters
80
+ self._ini_channels = ini_channels
81
+ self._spectrum_size = spectrum_size
82
+ self._stats_size = stats_size
83
+ self._stop = False
84
+
85
+ def stop(self):
86
+ self._stop = True
87
+
88
+ def __call__(self):
89
+ for channel in self._channels:
90
+ if hi.is_running(channel):
91
+ return PollResult(self._stop, 0, None, None)
92
+
93
+ n_channels = len(self._channels)
94
+ spectra = np.zeros((n_channels, 1, self._spectrum_size), dtype=np.uint32)
95
+ stats = np.zeros((n_channels, 1, self._stats_size), dtype=np.float32)
96
+ for i_chan, channel in enumerate(self._channels):
97
+ spectra[i_chan] = hi.get_spectrum(channel)
98
+
99
+ stats_dict = hi.get_statistics()
100
+ for i_chan, channel in enumerate(self._channels):
101
+ stats[i_chan, :] = stats_dict[channel]
102
+
103
+ return PollResult(True, 1, spectra, stats)
104
+
105
+
106
+ class TrigPoller:
107
+ def __init__(self, xia, n_points, masters, channels, ini_channels, spectrum_size, stats_size, pixels_per_buffer):
108
+ assert len(masters) > 0
109
+
110
+ if xia is None:
111
+ return
112
+ self._n_points = n_points
113
+ self._handel = weakref.proxy(xia.hi)
114
+ self._channels = channels
115
+ self._ini_channels = ini_channels
116
+ self._masters = masters
117
+ self._buffer_id = "a"
118
+ n_channels = len(channels)
119
+ n_masters = len(masters)
120
+ # TODO: check all buffers are the same
121
+ buffer_len = hi.get_buffer_length()
122
+ self._raw_buffers = {"a": np.ndarray((n_masters, buffer_len,), dtype=np.uint32),
123
+ "b": np.ndarray((n_masters, buffer_len,), dtype=np.uint32)}
124
+ self._spectra = np.zeros((n_channels, pixels_per_buffer, spectrum_size), dtype=np.uint32)
125
+ self._stats = np.zeros((n_channels, pixels_per_buffer, stats_size), dtype=np.float32)
126
+ self._n_read = 0
127
+ self._stop = False
128
+
129
+ def stop(self):
130
+ self._stop = True
131
+
132
+ def __call__(self):
133
+ hi = self._handel
134
+ finished = not hi.is_running()
135
+ buffer_id = self._buffer_id
136
+
137
+ current = hi.get_current_pixel()
138
+
139
+ for chan in self._masters:
140
+ if not hi.is_buffer_full(chan, buffer_id):
141
+ # setting finished to True only if we have all expected points
142
+ # or a manual stop was requested
143
+ if self._n_points > 0:
144
+ finished = self._stop or (finished and self._n_read == self._n_points)
145
+ return PollResult(finished, 0, None, None, current)
146
+
147
+ raw_buffers = self._raw_buffers[buffer_id]
148
+ full_buffer_id = f"buffer_{buffer_id}".encode()
149
+
150
+ #TODO: only read and parse the enabled channels
151
+
152
+ for i_chan, chan in enumerate(self._masters):
153
+ rbuf = raw_buffers[i_chan]
154
+ # why uint16?!
155
+ data = hi.ffi.cast("uint16_t *", rbuf.ctypes.data)
156
+ ret = hi.handel.xiaGetRunData(chan, full_buffer_id, data)
157
+ hi.check_error(ret)
158
+ hi.set_buffer_done(chan, buffer_id)
159
+
160
+ # converting to int, because if _pars_map_buffer returns
161
+ # a numpy uint16, then _n_read will be downcast to uint16!
162
+ # (numpy 2 behaviour).
163
+ n_pixels = {int(self._parse_map_buffer(master,
164
+ self._channels,
165
+ self._ini_channels,
166
+ raw_buffers[i_chan],
167
+ self._spectra,
168
+ self._stats,
169
+ i_chan))
170
+ for i_chan, master in enumerate(self._masters)}
171
+ if len(n_pixels) != 1:
172
+ raise RuntimeError("Buffers don't have the same number of pixels.")
173
+ else:
174
+ n_pixels = list(n_pixels)[0]
175
+ self._buffer_id = "a" if buffer_id == "b" else "b"
176
+
177
+ self._n_read += n_pixels
178
+
179
+ # setting finished to True only if we have all expected points
180
+ # or a manual stop was requested
181
+ if self._n_points > 0:
182
+ finished = self._stop or (finished and self._n_read == self._n_points)
183
+ return PollResult(finished, n_pixels, self._spectra, self._stats, current)
184
+
185
+ def _parse_map_buffer(self, master, channels, ini_channels, bf, spectra, stats, module_index):
186
+ raise NotImplementedError
187
+
188
+
189
+ class Xia(Controller):
190
+ TrigPollerCls = TrigPoller
191
+ StepPollerCls = StepPoller
192
+
193
+ hi = property(lambda self: self._handel)
194
+ lib_path = property(lambda self: self._lib_path)
195
+ pixels_per_buffer = mca_property(lambda self: self.hi.get_acquisition_value("num_map_pixels_per_buffer"))
196
+ gate_ignore = mca_property(lambda self: self.hi.get_acquisition_value("gate_ignore"))
197
+ mapping_mode = mca_property(lambda self: self.hi.get_acquisition_value("mapping_mode"))
198
+ preset_type = mca_property(lambda self: self.hi.get_acquisition_value("preset_type"))
199
+ preset_value = mca_property(lambda self: self.hi.get_acquisition_value("preset_value"))
200
+ num_map_pixels = mca_property(lambda self: self.hi.get_acquisition_value("num_map_pixels"))
201
+ pixel_advance_mode = mca_property(lambda self: self.hi.get_acquisition_value("pixel_advance_mode"))
202
+ number_mca_channels = mca_property(lambda self: self.hi.get_acquisition_value("number_mca_channels"))
203
+ input_logic_polarity = mca_property(lambda self: self.hi.get_acquisition_value("input_logic_polarity"))
204
+ modules = mca_property(lambda self: list(self.hi.get_modules()))
205
+
206
+ def __init__(self, *args, lib_path=None, **kwargs):
207
+ super().__init__(*args, **kwargs)
208
+
209
+ self._ini_file = None
210
+ self._lib_path = None
211
+ self._channels = None
212
+ logger.info("lib_path=%s", lib_path)
213
+ if sys.platform == "linux":
214
+ lib_name = "libhandel.so"
215
+ else:
216
+ lib_name = "handel.dll"
217
+ if isinstance(lib_path, (str,)):
218
+ self._lib_path = os.path.join(lib_path, lib_name)
219
+ os.environ["PATH"] = ";".join([os.environ["PATH"], lib_path])
220
+ else:
221
+ self._lib_path = lib_path
222
+ logger.info("Loading library [%s].", self._lib_path)
223
+ hi.load_handel(self.lib_path)
224
+ self._handel = hi
225
+ self._poller = None # TrigPoller(self)
226
+ self._xia_ini_channels = tuple()
227
+
228
+ def unconfig(self):
229
+ self.set_ini_file(None)
230
+
231
+ def mca_status(self):
232
+ if not self._ini_file:
233
+ return Status.UNINITIALIZED
234
+ if self.hi.is_running():
235
+ return Status.RUNNING
236
+ return Status.READY
237
+
238
+ @property
239
+ def ini_loaded(self):
240
+ return self._ini_file is not None
241
+
242
+ @property
243
+ def ini_file(self):
244
+ return self._ini_file
245
+
246
+ # @ini_file.setter
247
+ def set_ini_file(self, ini_file):
248
+ # self._set_status_uninitialized()
249
+ logger.info("Setting ini file to [%s].", ini_file)
250
+ try:
251
+ if not ini_file:
252
+ logger.info("Stopping system.")
253
+ hi.exit_handel()
254
+ self._ini_file = None
255
+ else:
256
+ if self._ini_file:
257
+ hi.exit_handel()
258
+ hi.init_handel(ini_file)
259
+ hi.start_system()
260
+ self._ini_file = ini_file
261
+ logger.info("File loaded (%s).", ini_file)
262
+ # self._set_status_ready()
263
+ except Exception as ex:
264
+ logger.exception(ex)
265
+ hi.exit_handel()
266
+ self._ini_file = None
267
+ raise
268
+ finally:
269
+ self._post_ini()
270
+ # self._set_status_fault()
271
+
272
+ def xiaSetLogLevel(self, level):
273
+ level = to_enum(level, XiaDebugLevels)
274
+ self.hi.set_log_level(level.value)
275
+
276
+ def xiaSetLogOutput(self, filename):
277
+ # self.hi.close_log()
278
+ # if filename:
279
+ self.hi.set_log_output(filename)
280
+
281
+ def _post_ini(self):
282
+ self._update_channels()
283
+
284
+ # TODO: only one supported mode right now,
285
+ if self.ini_loaded:
286
+ self.pixel_advance_mode = XiaAdvanceModes.GATE.value
287
+
288
+ # FIXME: making sure critical parameters are the same across all
289
+ # channels
290
+ if self.gate_ignore is None:
291
+ logger.warning("gate_ignore is not the same across "
292
+ "all detectors. Setting it to 0.")
293
+ self.gate_ignore = 0
294
+ if self.pixels_per_buffer is None:
295
+ logger.warning("num_map_pixels_per_buffer is not the same across "
296
+ "all detectors. Setting it to 10.")
297
+ self.pixels_per_buffer = 10
298
+ if self.number_mca_channels is None:
299
+ raise RuntimeError(
300
+ "The number of mca channels (number_mca_channels) "
301
+ "is not the same across all detectors. Please fix "
302
+ "your ini file.")
303
+
304
+ self.changed()
305
+
306
+ def _update_channels(self):
307
+ if self.ini_loaded:
308
+ channels = [ch for group in hi.get_grouped_channels() for ch in group]
309
+ logger.info("Detectors declared in the ini: %s.", channels)
310
+ else:
311
+ channels = []
312
+ self._xia_ini_channels = tuple(channels)
313
+ enabled_channels = tuple([ch for ch in channels if ch >= 0])
314
+ self._channels = enabled_channels
315
+
316
+ @pixels_per_buffer.setter
317
+ def pixels_per_buffer(self, n_pixels):
318
+ assert n_pixels > 0
319
+ self.hi.set_acquisition_value("num_map_pixels_per_buffer", n_pixels)
320
+ self.hi.apply_acquisition_values()
321
+
322
+ @gate_ignore.setter
323
+ def gate_ignore(self, gate_ignore):
324
+ self.hi.set_acquisition_value("gate_ignore", gate_ignore)
325
+ self.hi.apply_acquisition_values()
326
+
327
+ @mapping_mode.setter
328
+ def mapping_mode(self, mapping_mode):
329
+ self.hi.set_acquisition_value("mapping_mode", mapping_mode)
330
+ self.hi.apply_acquisition_values()
331
+
332
+ @preset_type.setter
333
+ def preset_type(self, preset_type):
334
+ preset_type = to_enum(preset_type, XiaPresets)
335
+ if preset_type not in (XiaPresets.XIA_PRESET_NONE, XiaPresets.XIA_PRESET_FIXED_REAL):
336
+ raise ValueError(f"Xia preset {preset_type} not implemented yet.")
337
+ self.hi.set_acquisition_value("preset_type", preset_type.value)
338
+ self.hi.apply_acquisition_values()
339
+
340
+ @preset_value.setter
341
+ def preset_value(self, preset_value):
342
+ self.hi.set_acquisition_value("preset_value", preset_value)
343
+ self.hi.apply_acquisition_values()
344
+
345
+ @num_map_pixels.setter
346
+ def num_map_pixels(self, num_map_pixels):
347
+ self.hi.set_acquisition_value("num_map_pixels", num_map_pixels)
348
+ self.hi.apply_acquisition_values()
349
+
350
+ @pixel_advance_mode.setter
351
+ def pixel_advance_mode(self, pixel_advance_mode):
352
+ pixel_advance_mode = to_enum(pixel_advance_mode, XiaAdvanceModes)
353
+ self.hi.set_acquisition_value("pixel_advance_mode", pixel_advance_mode.value)
354
+ self.hi.apply_acquisition_values()
355
+
356
+ @number_mca_channels.setter
357
+ def number_mca_channels(self, number_mca_channels):
358
+ self.hi.set_acquisition_value("number_mca_channels", number_mca_channels)
359
+ self.hi.apply_acquisition_values()
360
+
361
+ @input_logic_polarity.setter
362
+ def input_logic_polarity(self, input_logic_polarity):
363
+ input_logic_polarity = to_enum(input_logic_polarity, XiaGatePolarity)
364
+ self.hi.set_acquisition_value("input_logic_polarity", input_logic_polarity.value)
365
+ self.hi.apply_acquisition_values()
366
+
367
+ def master_channels(self):
368
+ return self.hi.get_master_channels()
369
+
370
+ def _start_run(self):
371
+ logger.info("Starting acq (realtime).")
372
+ channels = self.available_detectors()
373
+ for channel in channels:
374
+ hi.start_run(channel)
375
+ # disabling the following code
376
+ # until we figure out why in some cases
377
+ # is_running returns false when starting a
378
+ # realtime acq, even with long acquisition time
379
+ # for channel in channels:
380
+ # while not self.hi.is_running(channel):
381
+ # sleep(0.01)
382
+
383
+ # ==========================================
384
+ # Inherited from base class
385
+ # ==========================================
386
+ @ready_locked
387
+ def start_acq(self, resume=None):
388
+ try:
389
+ # forcing stop_acq, sometimes HW is still running.
390
+ # TODO: run tests to see if this is still necessary
391
+ self.stop_acq()
392
+ except Exception as ex:
393
+ # ignoring exception
394
+ logger.debug("No a acquisition running (%s).", ex)
395
+ pass
396
+
397
+ mapping_mode = self.mapping_mode
398
+ number_mca_channels = int(self.number_mca_channels)
399
+ pixels_per_buffer = int(self.pixels_per_buffer)
400
+
401
+ if mapping_mode:
402
+ acq_nb_points = int(self.num_map_pixels)
403
+ self._poller = self.TrigPollerCls(self,
404
+ acq_nb_points,
405
+ self.master_channels(),
406
+ self.available_detectors(),
407
+ self._xia_ini_channels,
408
+ number_mca_channels,
409
+ len(self.get_stats_names()),
410
+ pixels_per_buffer)
411
+ else:
412
+ self._poller = self.StepPollerCls(self, self.master_channels(),
413
+ self.available_detectors(),
414
+ self._xia_ini_channels,
415
+ number_mca_channels,
416
+ len(self.get_stats_names()))
417
+
418
+ self._start_run()
419
+ # self._set_status_running()
420
+
421
+ def get_stats_names(self):
422
+ return ("realtime",
423
+ "trigger_livetime",
424
+ "energy_livetime",
425
+ "triggers",
426
+ "events",
427
+ "icr",
428
+ "ocr",
429
+ "deadtime")
430
+ # return ["output", "icr", "ocr", "livetime", "deadtime"]
431
+
432
+ def unprepare(self):
433
+ self._poller = None
434
+
435
+ @ready_locked
436
+ def apply_mosca(self,
437
+ acq_nb_points=None,
438
+ acq_realtime=None,
439
+ spectrum_size=None,
440
+ trigger_mode=None,
441
+ gate_mode=None,
442
+ block_size=None,
443
+ **kwargs):
444
+ # TODO, check that params are correctly set?
445
+ if self.is_running():
446
+ raise RuntimeError('Cannot apply_mosca, an acquisition is already running. Stop it first.')
447
+
448
+ self._poller = None
449
+
450
+ mapping_mode = None
451
+ gate_ignore = None
452
+ preset_value = None
453
+ num_map_pixels = None
454
+ preset_type = None
455
+ input_logic_polarity = None
456
+
457
+ # TODO: only one supported mode right now,
458
+ self.pixel_advance_mode = XiaAdvanceModes.GATE.value
459
+
460
+ if gate_mode is not None:
461
+ # gate_ignore for all but trigger_mode = ExtTrig
462
+ gate_ignore = 1. if gate_mode == GateMode.IGNORE else 0.
463
+
464
+ if trigger_mode == TriggerMode.FREERUN:
465
+ # manual start/stop
466
+ # acquisition is active depending on the gate level (or ignore all together)
467
+ if acq_nb_points != 1:
468
+ raise ValueError("Can't have trigger mode FREERUN and acq_nb_points != 1.")
469
+ mapping_mode = 0.
470
+ preset_type = XiaPresets.XIA_PRESET_NONE.value
471
+
472
+ elif trigger_mode == TriggerMode.INT_TRIG_READOUT:
473
+ # acquisition starts right away, but can still be controlled by the input gate
474
+ # i.e: if gate_mode != ignore, the time runs only when the gate is active
475
+ # active gate level is set or turned off by gate_mode
476
+ if acq_nb_points != 1:
477
+ raise ValueError("Can't have trigger mode INT_TRIG_READOUT and acq_nb_points != 1.")
478
+ mapping_mode = 0.
479
+ preset_type = XiaPresets.XIA_PRESET_FIXED_REAL.value
480
+ preset_value = acq_realtime
481
+ else:
482
+ preset_type = XiaPresets.XIA_PRESET_NONE.value
483
+ num_map_pixels = acq_nb_points
484
+ mapping_mode = 1.
485
+
486
+ if gate_mode == GateMode.IGNORE or gate_mode == None:
487
+ raise ValueError(f"Can't have trigger mode {trigger_mode.name} and gate mode IGNORE or None.")
488
+ elif gate_mode == GateMode.HIGH:
489
+ input_logic_polarity = XiaGatePolarity.XIA_GATE_COLLECT_HI.value
490
+ elif gate_mode == GateMode.LOW:
491
+ input_logic_polarity = XiaGatePolarity.XIA_GATE_COLLECT_LO.value
492
+
493
+ if trigger_mode == TriggerMode.GATE:
494
+ # mapping mode, acquiring when the gate is active
495
+ # pixel advance when the gate becomes inactive
496
+ # active gate level is controlled by gate_mode
497
+ # - acquisition runs on HI level: gate mode -> HIGH (pixel advance on falling edge)
498
+ # - acquisition runs on LO level: gate mode -> LOW (pixel advance on rising edge)
499
+ gate_ignore = 0.
500
+
501
+ elif trigger_mode == TriggerMode.EXT_TRIG:
502
+ # mapping mode, without gate
503
+ # continuous acquisition, pixels are advanced when an edge is detected
504
+ # edge direction is controlled by gate_mode:
505
+ # - advance on rising edge: gate mode -> LOW
506
+ # - advance on falling edge: gate mode -> HIGH
507
+ # (choice made to stay coherent with the GATE trigger mode)
508
+ gate_ignore = 1.
509
+
510
+ else:
511
+ raise ValueError(f"Unsupported trigger mode: {trigger_mode}.")
512
+
513
+ if mapping_mode is not None:
514
+ self.mapping_mode = mapping_mode
515
+
516
+ if gate_ignore is not None:
517
+ self.gate_ignore = gate_ignore
518
+
519
+ if preset_type is not None:
520
+ self.preset_type = preset_type
521
+
522
+ if preset_value is not None:
523
+ self.preset_value = preset_value
524
+
525
+ if spectrum_size is not None:
526
+ self.number_mca_channels = spectrum_size
527
+
528
+ if block_size is not None:
529
+ self.pixels_per_buffer = block_size
530
+
531
+ if input_logic_polarity is not None:
532
+ self.input_logic_polarity = input_logic_polarity
533
+
534
+ if num_map_pixels is not None:
535
+ self.num_map_pixels = num_map_pixels
536
+
537
+ @ready_locked
538
+ def prepare_acq(self, resume=False):
539
+ logger.info("Prepare done.")
540
+
541
+ def stop_acq(self):
542
+ hi.stop_run()
543
+ poller = self._poller
544
+ if poller:
545
+ poller.stop()
546
+ logger.info("Waiting for the MCA to stop...")
547
+ while self.hi.is_running():
548
+ sleep(0.5)
549
+ logger.info("OK, MCA stopped...")
550
+
551
+ def poll_acq(self):
552
+ return self._poller()
553
+
554
+ def is_running(self):
555
+ return self.hi.is_running()
556
+
557
+ def get_supported_spectrum_sizes(self):
558
+ return np.arange(128, 4097)
559
+
560
+ def get_supported_trigger_modes(self):
561
+ return [TriggerMode.FREERUN, # manual start/stop
562
+ TriggerMode.INT_TRIG_READOUT, # preset mode = XIA_PRESET_FIXED_REAL
563
+ TriggerMode.GATE, # mapping / gate
564
+ TriggerMode.EXT_TRIG] # mapping / no gate -> edge advance
565
+
566
+ def get_supported_gate_modes(self):
567
+ return [GateMode.IGNORE,
568
+ GateMode.HIGH,
569
+ GateMode.LOW]
570
+
571
+ def available_detectors(self):
572
+ return self._channels
573
+
574
+ def spectrum_size(self):
575
+ return int(self.number_mca_channels)