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
mosca/tango/mosca.py ADDED
@@ -0,0 +1,513 @@
1
+ import io
2
+ import sys
3
+ import time
4
+ import enum
5
+ import struct
6
+ import logging
7
+ logger = logging.getLogger(__name__)
8
+
9
+ from ruamel.yaml import YAML
10
+
11
+ import tango
12
+ from tango import DevState, DevEncoded
13
+ from tango.server import Device, command, attribute, device_property
14
+
15
+ from ..common.logger import set_mosca_log_level
16
+ from .utils import get_device_instance, get_server_name
17
+ from ..control.datahandler import SPECTRA_DTYPE, STATS_DTYPE
18
+ from ..control import Mosca as MoscaCtrl
19
+ from .shared import get_controller, _set_mosca
20
+ from ..common.enums import GateMode, TriggerMode, Status, DiscardMode
21
+
22
+
23
+ # TODO:
24
+ STRUCT_VERSION = 0x1234
25
+
26
+ SERVER_VERSION = 0x02
27
+
28
+
29
+ # keeping it, in case it's reused some day.
30
+ def pack_spectra(spectra):
31
+ np_order = spectra.dtype.byteorder
32
+ if np_order not in ("<", ">"):
33
+ np_order = "<" if sys.byteorder == "little" else ">"
34
+
35
+ n_dets, n_ptrs, sp_size = spectra.shape
36
+
37
+ # - server version (short)
38
+ # - struct version (short)
39
+ # - nb detectors (dim 0, int)
40
+ # - nb points (dim 1, int)
41
+ # - spectrum size (dim 2, int)
42
+ # - dtype (10 chars)
43
+ str_format = f"{np_order}hhiii10sL"
44
+
45
+ # dtype = [ord(c) for c in spectra.dtype.name]
46
+ # dtype += [ord(' ') for i in range(10-len(dtype))]
47
+ dtype = f"{spectra.dtype.name:<10s}".encode()
48
+
49
+ data = struct.pack(str_format,
50
+ SERVER_VERSION,
51
+ STRUCT_VERSION,
52
+ n_dets, n_ptrs, sp_size,
53
+ dtype,
54
+ spectra.nbytes)
55
+ data += spectra.tobytes()
56
+ return data
57
+
58
+
59
+ class Mosca(Device):
60
+ """ MOSCA Tango device server. Wraps the Mosca instance. """
61
+
62
+ mca_type = device_property(dtype=str, mandatory=True)
63
+ detector_name = device_property(dtype=str, default_value="")
64
+ log_file = device_property(dtype=str, )
65
+
66
+ _lazy_init = False
67
+
68
+ server_version = attribute(lambda self: SERVER_VERSION, dtype=int)
69
+
70
+ timeout_start = attribute(lambda self: self.controller_ds.timeout_start)
71
+ timeout_stop = attribute(lambda self: self.controller_ds.timeout_stop)
72
+ timeout_setup = attribute(lambda self: self.controller_ds.timeout_setup)
73
+ timeout_prepare = attribute(lambda self: self.controller_ds.timeout_prepare)
74
+
75
+ data = property(lambda self: self.mosca.data)
76
+
77
+ def __init__(self, *args, **kwargs):
78
+ logger.info("Mosca __init__")
79
+ self._controller_ds = None
80
+ self._mosca = None
81
+ self._controller = None
82
+ self._start_epoch = 0
83
+ Device.__init__(self, *args, **kwargs)
84
+ # self.set_state(DevState.UNKNOWN)
85
+
86
+ def init_device(self):
87
+ logger.info("in init_device() (lazy=%s)", self._lazy_init)
88
+ try:
89
+ Device.init_device(self)
90
+ if not self._lazy_init:
91
+ _ = self.mosca
92
+ _ = self.controller_ds
93
+ _ = self.controller
94
+ self.set_state(DevState.ON)
95
+ self._start_epoch = int(time.time())
96
+ except:
97
+ self.set_state(DevState.FAULT)
98
+ raise
99
+
100
+ @command(dtype_in=str)
101
+ def set_mosca_log_level(self, level):
102
+ levels = {
103
+ "DEBUG": logging.DEBUG,
104
+ "INFO": logging.INFO,
105
+ "WARNING": logging.WARNING,
106
+ "ERROR": logging.ERROR,
107
+ "CRITICAL": logging.CRITICAL
108
+ }
109
+ try:
110
+ level = levels[level]
111
+ set_mosca_log_level(level=level)
112
+ except KeyError:
113
+ logger.error("Unknown log level: %s.", level)
114
+
115
+ @attribute(dtype=int)
116
+ def start_epoch(self):
117
+ return self._start_epoch
118
+
119
+ @attribute(dtype=str)
120
+ def controller_name(self):
121
+ """
122
+ :return: the controller's DS name
123
+ """
124
+ return self.controller_ds.name()
125
+
126
+ @property
127
+ def mosca(self):
128
+ if self._mosca is None:
129
+ self._mosca = MoscaCtrl(self.controller)
130
+ _set_mosca(self._mosca)
131
+ return self._mosca
132
+
133
+ @property
134
+ def controller_ds(self):
135
+ if self._controller_ds is None:
136
+ self._controller_ds = get_device_instance(self.mca_type)
137
+ return self._controller_ds
138
+
139
+ @property
140
+ def controller(self):
141
+ if self._controller is None:
142
+ self._controller = get_controller()
143
+ return self._controller
144
+
145
+ # =================================
146
+ # Commands
147
+ # =================================
148
+ @command
149
+ def apply_mosca(self):
150
+ self.mosca.apply_mosca()
151
+
152
+ @command
153
+ def prepare_acq(self):
154
+ self.mosca.prepare()
155
+
156
+ @command(dtype_out=int)
157
+ def start_acq(self):
158
+ return self.mosca.start_acq()
159
+
160
+ @command
161
+ def start_mosca(self):
162
+ # self.mosca.apply_mosca()
163
+ # self.mosca.prepare()
164
+ self.mosca.start_mosca()
165
+
166
+ @command
167
+ def resume_acq(self):
168
+ self.mosca.start_acq(resume=True)
169
+
170
+ @command
171
+ def stop_acq(self):
172
+ # TODO: put state in always executed_hook
173
+ self.mosca.stop_acq()
174
+
175
+ @command
176
+ def abort_acq(self):
177
+ self.mosca.stop_acq()
178
+
179
+ @command(dtype_out=str)
180
+ def metadata(self):
181
+ mdata = self.mosca.metadata()
182
+ mdata["tango"] = {"mca_type": self.mca_type,
183
+ "name": self.detector_name}
184
+ stream = io.StringIO()
185
+ YAML().dump(mdata, stream=stream)
186
+ return stream.getvalue()
187
+
188
+ @command(dtype_in=[int])
189
+ def disable_detectors(self, detectors):
190
+ self.mosca.disable_detectors(*detectors)
191
+
192
+ @command(dtype_in=[int])
193
+ def enable_detectors(self, detectors):
194
+ self.mosca.enable_detectors(*detectors)
195
+
196
+ # =================================
197
+ # Attributes
198
+ # =================================
199
+
200
+ @attribute(dtype=(TriggerMode,), max_dim_x=10)
201
+ def trigger_mode_enum(self):
202
+ return list(TriggerMode)
203
+
204
+ @attribute(dtype=(GateMode,), max_dim_x=10)
205
+ def gate_mode_enum(self):
206
+ return list(GateMode)
207
+
208
+ @attribute(dtype=(Status,), max_dim_x=10)
209
+ def mca_status_enum(self):
210
+ return list(Status)
211
+
212
+ @attribute(dtype=(DiscardMode,), max_dim_x=10)
213
+ def discard_mode_enum(self):
214
+ return list(DiscardMode)
215
+
216
+ # =====
217
+ # Hardware
218
+ # =====
219
+
220
+ @attribute(dtype=[int], max_dim_x=100)
221
+ def detectors(self):
222
+ return list(self.mosca.detectors.detectors)
223
+
224
+ @attribute(dtype=[int], max_dim_x=100)
225
+ def available_detectors(self):
226
+ return list(self.mosca.detectors.available_detectors)
227
+
228
+ @attribute(dtype=[int], max_dim_x=100)
229
+ def disabled_detectors(self):
230
+ detectors = self.mosca.detectors
231
+ return list(detectors.disabled_detectors)
232
+
233
+ # =====
234
+ # Data
235
+ # =====
236
+
237
+ @command(dtype_in=[int], dtype_out=[SPECTRA_DTYPE])
238
+ def read_spectra(self, args):
239
+ data = self.data.get_spectra(*args, flatten=True)
240
+ return data
241
+
242
+ @command(dtype_in=[int], dtype_out=[STATS_DTYPE])
243
+ def read_stats(self, args):
244
+ return self.data.get_stats(*args, flatten=True)
245
+
246
+ @command(dtype_in=[int], dtype_out=[int])
247
+ def read_rois(self, args):
248
+ return self.data.rois.get_data(*args, flatten=True)
249
+
250
+ @attribute(dtype=[str], max_dim_x=20)
251
+ def stats_names(self):
252
+ return self.mosca.stats_names
253
+
254
+ # @command(dtype_in=[int], dtype_out=DevVarCharArray)
255
+ # def read_spectra_h5(self, args):
256
+ # t8 = time.perf_counter()
257
+ # # data = self.data.get_spectra(*args, flatten=False)
258
+ # t9 = time.perf_counter()
259
+ # import numpy as np
260
+ # data = np.arange(args[0], dtype=np.uint32)
261
+ # t10 = time.perf_counter()
262
+ # f = h5py.File.in_memory()
263
+ # t0 = time.perf_counter()
264
+ # dset = f.create_dataset("spectra", data.shape, compression="lzf")
265
+ # t1 = time.perf_counter()
266
+ # dset[:] = data
267
+ # t2 = time.perf_counter()
268
+ # f.flush()
269
+ # t3 = time.perf_counter()
270
+ # h5_bytes = f.id.get_file_image()
271
+ # t4 = time.perf_counter()
272
+ # print("====", h5_bytes.__class__, len(h5_bytes), t10 - t9, t1 - t0, t2 - t1, t3 - t2, t4 - t3, t4 - t9)
273
+ # # print(t10 - t9, t0 - t10)
274
+ # return h5_bytes
275
+
276
+ # =====
277
+ # Acquistion
278
+ # =====
279
+
280
+ @attribute(dtype=float)
281
+ def poll_interval(self):
282
+ return self.mosca.poll_interval
283
+
284
+ @poll_interval.write
285
+ def poll_interval(self, poll_interval):
286
+ self.mosca.poll_interval = poll_interval
287
+
288
+ @attribute(dtype=DiscardMode)
289
+ def discard_mode(self):
290
+ return self.mosca.discard_mode
291
+
292
+ @discard_mode.write
293
+ def discard_mode(self, discard_mode):
294
+ self.mosca.discard_mode = discard_mode
295
+
296
+ # @attribute(dtype=bool)
297
+ # def discard_first_point(self):
298
+ # return self.mosca.discard_first_point
299
+
300
+ # @discard_first_point.write
301
+ # def discard_first_point(self, discard):
302
+ # self.mosca.discard_first_point = discard
303
+
304
+ @attribute(dtype=int)
305
+ def n_read_points(self):
306
+ return self.data.n_read_points
307
+
308
+ @attribute(dtype=int)
309
+ def n_available_points(self):
310
+ return self.data.n_available_points
311
+
312
+ @attribute(dtype=Status)
313
+ def acq_status(self):
314
+ # TODO: READY only if saving is done
315
+ return self.mosca.status
316
+
317
+ @attribute(dtype=int)
318
+ def n_triggered_points(self):
319
+ return self.mosca.n_triggered_points
320
+
321
+ @attribute(dtype=int)
322
+ def acq_nb_points(self):
323
+ return self.mosca.acq_nb_points
324
+
325
+ @acq_nb_points.write
326
+ def acq_nb_points(self, acq_nb_points):
327
+ self.mosca.acq_nb_points = acq_nb_points
328
+
329
+ @attribute(dtype=float)
330
+ def acq_realtime(self):
331
+ return self.mosca.acq_realtime
332
+
333
+ @acq_realtime.write
334
+ def acq_realtime(self, acq_realtime):
335
+ self.mosca.acq_realtime = acq_realtime
336
+
337
+ @attribute(dtype=float)
338
+ def acq_nb_triggers(self):
339
+ return self.mosca.acq_nb_triggers
340
+
341
+ @acq_nb_triggers.write
342
+ def acq_nb_triggers(self, acq_nb_triggers):
343
+ self.mosca.acq_nb_triggers = acq_nb_triggers
344
+
345
+ @attribute(dtype=int)
346
+ def spectrum_size(self):
347
+ return self.mosca.spectrum_size
348
+ # return self.get_spectrum_size()
349
+
350
+ @spectrum_size.write
351
+ def spectrum_size(self, spectrum_size):
352
+ self.mosca.spectrum_size = spectrum_size
353
+
354
+ @attribute(dtype=int)
355
+ def block_size(self):
356
+ return self.mosca.block_size
357
+
358
+ @block_size.write
359
+ def block_size(self, block_size):
360
+ self.mosca.block_size = block_size
361
+
362
+ @attribute(dtype=TriggerMode)
363
+ def trigger_mode(self):
364
+ return self.mosca.trigger_mode
365
+
366
+ @trigger_mode.write
367
+ def trigger_mode(self, trigger_mode):
368
+ self.mosca.trigger_mode = trigger_mode
369
+
370
+ @attribute(dtype=GateMode)
371
+ def gate_mode(self):
372
+ return self.mosca.gate_mode
373
+
374
+ @gate_mode.write
375
+ def gate_mode(self, gate_mode):
376
+ self.mosca.gate_mode = gate_mode
377
+
378
+ @attribute(dtype=(GateMode,), max_dim_x=10)
379
+ def supported_gate_modes(self):
380
+ return self.mosca.supported_gate_modes
381
+
382
+ @attribute(dtype=(TriggerMode,), max_dim_x=10)
383
+ def supported_trigger_modes(self):
384
+ return self.mosca.supported_trigger_modes
385
+
386
+ # ====
387
+ # Rois
388
+ # ====
389
+
390
+ @command
391
+ def clear_rois(self):
392
+ self.data.rois.clear_definitions()
393
+
394
+ @command(dtype_in=[str])
395
+ def add_roi(self, args):
396
+ self._add_roi(args)
397
+
398
+ @command(dtype_in=[str])
399
+ def add_roi_sum(self, args):
400
+ self._add_roi(args, is_sum=True)
401
+
402
+ def _add_roi(self, args, is_sum=False):
403
+ name = args[0]
404
+ iargs = [int(arg) for arg in args[1:]]
405
+ start, stop = iargs[0:2]
406
+ detectors = iargs[2:]
407
+ if is_sum:
408
+ self.data.rois.add_roi_sum(name, start, stop, *detectors)
409
+ else:
410
+ self.data.rois.add_roi(name, start, stop, *detectors)
411
+
412
+ @command(dtype_in=[str])
413
+ def remove_roi(self, args):
414
+ self._remove_roi(args)
415
+
416
+ @command(dtype_in=[str])
417
+ def remove_roi_sum(self, args):
418
+ self._remove_roi(args, is_sum=True)
419
+
420
+ def _remove_roi(self, args, is_sum=False):
421
+ name = args[0]
422
+ detectors = [int(arg) for arg in args[1:]]
423
+ if is_sum:
424
+ rc = self.data.rois.remove_roi_sum(name, *detectors)
425
+ else:
426
+ rc = self.data.rois.remove_roi(name, *detectors)
427
+ return rc
428
+
429
+ @command(dtype_in=int, dtype_out=bool)
430
+ def remove_roi_by_index(self, roi_index):
431
+ return self.data.rois.remove_roi_by_index(roi_index)
432
+
433
+ @command(dtype_out=str)
434
+ def roi_counters(self):
435
+ counters_dict = self.data.rois.rois_dict()
436
+ stream = io.StringIO()
437
+ YAML().dump(counters_dict, stream=stream)
438
+ return stream.getvalue()
439
+
440
+ @command(dtype_out=str)
441
+ def roi_definitions(self):
442
+ defs_dict = self.data.rois.definitions_dict()
443
+ stream = io.StringIO()
444
+ YAML().dump(defs_dict, stream=stream)
445
+ return stream.getvalue()
446
+
447
+ # ====
448
+ # Saving
449
+ # ====
450
+
451
+ @attribute(dtype=int)
452
+ def n_saved_points(self):
453
+ return self.data.saving.n_points
454
+
455
+ # @attribute(dtype=str)
456
+ # def saving_format(self):
457
+ # return self.data.saving.file_format.name
458
+
459
+ # @saving_format.write
460
+ # def saving_format(self, saving_format):
461
+ # self.data.saving.file_format = saving_format
462
+
463
+ @attribute(dtype=str)
464
+ def saving_directory(self):
465
+ return self.data.saving.directory
466
+
467
+ @saving_directory.write
468
+ def saving_directory(self, saving_directory):
469
+ self.data.saving.directory = saving_directory
470
+
471
+ @attribute(dtype=str)
472
+ def saving_file_prefix(self):
473
+ return self.data.saving.prefix
474
+
475
+ @saving_file_prefix.write
476
+ def saving_file_prefix(self, saving_file_prefix):
477
+ self.data.saving.prefix = saving_file_prefix
478
+
479
+ @attribute(dtype=str)
480
+ def saving_hdf5_path(self):
481
+ return self.data.saving.hdf5_path
482
+
483
+ @saving_hdf5_path.write
484
+ def saving_hdf5_path(self, saving_hdf5_path):
485
+ self.data.saving.hdf5_path = saving_hdf5_path
486
+
487
+ @attribute(dtype=bool)
488
+ def saving_enabled(self):
489
+ return self.data.is_active()
490
+
491
+ @saving_enabled.write
492
+ def saving_enabled(self, saving_enabled):
493
+ if saving_enabled:
494
+ self.data.saving.saving_on()
495
+ else:
496
+ self.data.saving.saving_off()
497
+
498
+
499
+ # @attribute(dtype=str)
500
+ # def saving_mode(self):
501
+ # return self.data.saving.mode.name
502
+
503
+ # @saving_mode.write
504
+ # def saving_mode(self, mode):
505
+ # self.data.saving.mode = mode
506
+
507
+ @attribute(dtype=int)
508
+ def saving_points_per_file(self):
509
+ return self.data.saving.points_per_file
510
+
511
+ @saving_points_per_file.write
512
+ def saving_points_per_file(self, saving_points_per_file):
513
+ self.data.saving.points_per_file = saving_points_per_file