SlsDetectorJungfrau.py 17.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
############################################################################
# This file is part of LImA, a Library for Image Acquisition
#
# Copyright (C) : 2009-2011
# European Synchrotron Radiation Facility
# BP 220, Grenoble 38043
# FRANCE
#
# This is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
############################################################################
#=============================================================================
#
# file :        SlsDetectorJungfrau.py
#
# description : Python source for the PSI/SlsDetector and its commands.
#                The class is derived from Device. It represents the
#                CORBA servant object which will be accessed from the
#                network. All commands which can be executed on the
#                SlsDetector are implemented in this file.
#
# project :     TANGO Device Server
#
# copyleft :    European Synchrotron Radiation Facility
#               BP 220, Grenoble 38043
#               FRANCE
#
#=============================================================================
#         (c) - Bliss - ESRF
#=============================================================================
#

from .SlsDetector import *
44
45
46
from threading import Thread

import contextlib
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#------------------------------------------------------------------
#    SlsDetectorJungfrau device class
#------------------------------------------------------------------

class SlsDetectorJungfrau(SlsDetector):

    Core.DEB_CLASS(Core.DebModApplication, 'LimaSlsDetectorJungfrau')


#------------------------------------------------------------------
#    Device constructor
#------------------------------------------------------------------

    ModelAttrs = ['img_proc_config',
62
63
                  'img_src',
                  'gain_ped_map_type',
64
65
66
67
68
69
70
71
72
    ]

    GainPedCalibAttrRe = re.compile('((?P<action>read|write)_)?'
                                    '(?P<select>gain|ped)_'
                                    '(?P<gain>[0-2])_calib_map')

    GainPedProcAttrRe = re.compile('((?P<action>read|write)_)?'
                                   'gain_ped_proc_map(?P<map_type>16|32)')

73
74
75
76
77
78
    Tango2NpType = {PyTango.DevUChar: 'uint8',
                    PyTango.DevUShort: 'uint16',
                    PyTango.DevULong: 'uint32',
                    PyTango.DevFloat: 'float32',
                    PyTango.DevDouble: 'float64'}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    def __init__(self,*args) :
        SlsDetector.__init__(self,*args)

#------------------------------------------------------------------
#    Device destructor
#------------------------------------------------------------------
    def delete_device(self):
        pass

#------------------------------------------------------------------
#    Device initialization
#------------------------------------------------------------------
    @Core.DEB_MEMBER_FUNCT
    def init_device(self):
        SlsDetector.init_device(self)

        self.gain_ped_bckgnd = None
96
        self.gain_ped_seq = None
97
98
99
100
101
102
103

    def init_list_attr(self):
        SlsDetector.init_list_attr(self)

        nl = ['Map16', 'Map32']
        klass = SlsDetectorHw.Jungfrau.GainPed
        self.__GainPedMapType = ConstListAttr(nl, namespc=klass)
104
105
106
107

        nl = ['Raw', 'GainPedCorr']
        klass = SlsDetectorHw.Jungfrau
        self.__ImgSrc = ConstListAttr(nl, namespc=klass)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

    def __getattr__(self, name):
        m = self.GainPedCalibAttrRe.match(name)
        if m:
            name = f'{m.group("action")}_gain_ped_calib_map'
            return partial(getattr(self, name), map_select=m.group("select"),
                           gain=int(m.group("gain")))
        m = self.GainPedProcAttrRe.match(name)
        if m:
            name = f'{m.group("action")}_gain_ped_proc_map'
            return partial(getattr(self, name), map_type=m.group("map_type"))
        obj = None
        for attr in self.ModelAttrs:
            if attr in name:
                obj = self.model
        return self.getDevAttr(name, obj)

    @Core.DEB_MEMBER_FUNCT
    def read_det_map(self, attr):
        det_map = self.model.getDetMap();
        attr.set_value(det_map.buffer)

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    @Core.DEB_MEMBER_FUNCT
    def getFrameSize(self):
        raw = self.cam.getRawMode()
        size = self.cam.getFrameDim(raw).getSize()
        res = size.getWidth(), size.getHeight()
        deb.Return('width=%d, height=%d' % res)
        return res

    @Core.DEB_MEMBER_FUNCT
    def readMapCheckGainPedSeq(self, attr):
        if not self.gain_ped_seq:
            return True
        deb.Trace('Pedestal sequence active. Skipping!')
        if attr:
            width, height = self.getFrameSize()
            dtype = self.Tango2NpType[attr.get_data_type()]
            d = np.zeros((height, width), dtype)
            attr.set_value(d)
        return False
    
150
151
    @Core.DEB_MEMBER_FUNCT
    def read_gain_map(self, attr):
152
153
        if not self.readMapCheckGainPedSeq(attr):
            return
154
155
156
157
158
159
160
        jungfrau = _SlsDetectorJungfrau
        gain_data, adc_data, frame = jungfrau.readGainADCMaps(-1)
        deb.Return("frame=%s, gain_data=%s" % (frame, gain_data))
        attr.set_value(gain_data.buffer)

    @Core.DEB_MEMBER_FUNCT
    def read_adc_map(self, attr):
161
162
        if not self.readMapCheckGainPedSeq(attr):
            return
163
164
165
166
167
168
169
170
171
        jungfrau = _SlsDetectorJungfrau
        gain_data, adc_data, frame = jungfrau.readGainADCMaps(-1)
        deb.Return("frame=%s, adc_data=%s" % (frame, adc_data))
        attr.set_value(adc_data.buffer)

    @Core.DEB_MEMBER_FUNCT
    def getGainPedProcMap(self, map_type):
        jungfrau = _SlsDetectorJungfrau
        map_type_nb = self.__GainPedMapType[f'MAP{map_type}']
172
        if jungfrau.getGainPedMapType() != map_type_nb:
173
174
175
176
177
            raise ValueError('Invalid map_type %s' % map_type);
        return jungfrau.readGainPedProcMap(-1)

    @Core.DEB_MEMBER_FUNCT
    def read_gain_ped_proc_map(self, attr, map_type):
178
179
        if not self.readMapCheckGainPedSeq(attr):
            return
180
181
182
183
184
185
186
        proc_data, frame = self.getGainPedProcMap(map_type)
        data = proc_data.buffer
        if self.gain_ped_bckgnd is not None:
            data = data - self.gain_ped_bckgnd
        deb.Return("frame=%s, data=%s" % (frame, data))
        attr.set_value(data)

187
188
189
190
191
192
193
194
195
196
    @Core.DEB_MEMBER_FUNCT
    def read_ave_map(self, attr):
        if not self.readMapCheckGainPedSeq(attr):
            return
        jungfrau = _SlsDetectorJungfrau
        ave_data, nb_frames, frame = jungfrau.readAveMap(-1)
        deb.Return("frame=%s, nb_frames=%s, ave_data=%s" % (frame, nb_frames,
                                                            ave_data))
        attr.set_value(ave_data.buffer)

197
198
199
200
201
202
203
204
205
    @Core.DEB_MEMBER_FUNCT
    def setGainPedBackground(self, level=0):
        proc_data, frame = self.getGainPedProcMap('32')
        self.gain_ped_bckgnd = proc_data.buffer.copy() - level

    @Core.DEB_MEMBER_FUNCT
    def clearGainPedBackground(self):
        self.gain_ped_bckgnd = None

206
207
208
209
210
211
212
213
214
215
216
217
218
219
    @Core.DEB_MEMBER_FUNCT
    def write_gain_ped_calib_map(self, attr, map_select, gain):
        d = attr.get_write_value()
        attr_name = "%s_%d" % (map_select, gain)
        deb.Param("%s=%s" % (attr_name, d))
        jungfrau = _SlsDetectorJungfrau
        c = jungfrau.getGainPedCalib()
        l_name = '%s_map' % map_select
        l = getattr(c, l_name)
        l[gain].buffer = d
        setattr(c, l_name, l)
        deb.Trace("%s=%s" % (attr_name, getattr(c, l_name)[gain].buffer))
        jungfrau.setGainPedCalib(c)

220
221
    @Core.DEB_MEMBER_FUNCT
    def read_gain_ped_calib_map(self, attr, map_select, gain):
222
223
        jungfrau = _SlsDetectorJungfrau
        c = jungfrau.getGainPedCalib()
224
225
226
227
        d = c.gain_map[gain] if map_select == 'gain' else c.ped_map[gain]
        deb.Return("%s_%d=%s" % (map_select, gain, d))
        attr.set_value(d.buffer)

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
    @Core.DEB_MEMBER_FUNCT
    def setSettings(self, settings):
        deb.Param('settings=%s' % settings)
        self.putCmd('settings ' + settings)

    @Core.DEB_MEMBER_FUNCT
    def getSettings(self):
        settings = self.getCmd('settings')
        deb.Return('settings=%s' % settings)
        return settings

    @Core.DEB_MEMBER_FUNCT
    def acqNbFrames(self, nb_frames):
        @contextlib.contextmanager
        def setFrames():
            from .SlsDetector import _SlsDetectorControl as control
            acq = control.acquisition()
            prev_nb_frames = acq.getAcqNbFrames()
            deb.Trace('Changing nb_frames=%s' % nb_frames)
            acq.setAcqNbFrames(nb_frames)
            try:
                yield True
            finally:
                if nb_frames != prev_nb_frames:
                    deb.Trace('Restoring nb_frames=%s' % prev_nb_frames)
                    acq.setAcqNbFrames(prev_nb_frames)
        return setFrames()

    @Core.DEB_MEMBER_FUNCT
    def detSafeSettings(self):
        @contextlib.contextmanager
        def safeSettings():
            prev_settings = self.getSettings()
            try:
                yield True
            finally:
                if self.getSettings() != prev_settings:
                    deb.Trace('Restoring settings=%s' % prev_settings)
                    self.setSettings(prev_settings)
        return safeSettings()

    @Core.DEB_MEMBER_FUNCT
    def detSettings(self, settings):
        @contextlib.contextmanager
        def setSettings():
            with self.detSafeSettings():
                deb.Trace('Changing settings=%s' % settings)
                self.setSettings(settings)
                yield True
        return setSettings()

    @Core.DEB_MEMBER_FUNCT
    def jungfrauImgProcConfig(self, config):
        @contextlib.contextmanager
        def setImgProcConfig():
            jungfrau = _SlsDetectorJungfrau
            prev_config = jungfrau.getImgProcConfig()
            deb.Trace('Setting img_proc_config=%s' % config)
            jungfrau.setImgProcConfig(config)
            try:
                yield True
            finally:
                if config != prev_config:
                    deb.Trace('Restoring img_proc_config=%s' % prev_config)
                    jungfrau.setImgProcConfig(prev_config)
        return setImgProcConfig()

    @Core.DEB_MEMBER_FUNCT
    def measurePedestal(self, nb_frames):
        with self.acqNbFrames(nb_frames):
            with self.jungfrauImgProcConfig('gain_adc_map,ave'):
                from .SlsDetector import _SlsDetectorControl as control
                jungfrau = _SlsDetectorJungfrau
                deb.Trace('Starting pedestal acq')
                control.prepareAcq()
                control.startAcq()
                def running():
                    acq_status = control.getStatus().AcquisitionStatus
                    return acq_status not in [Core.AcqReady, Core.AcqFault]
                while running():
                    time.sleep(0.01)
                deb.Trace('Finished pedestal acq')
                pedestal, nb_frames, frame = jungfrau.readAveMap(-1)
                return pedestal, nb_frames, frame

    @Core.DEB_MEMBER_FUNCT
    def measureGainPedestal(self, gain, nb_frames):
        gain_settings = {0: 'dynamicgain',
                         1: 'forceswitchg1',
                         2: 'forceswitchg2'}
        with self.detSettings(gain_settings[gain]):
            pedestal_data = self.measurePedestal(nb_frames)
            return pedestal_data

    @Core.DEB_MEMBER_FUNCT
    def startGainPedestalSeq(self, gain_nb_frames):
        gain, nb_frames = map(int, gain_nb_frames.split(','))
        deb.Param("gain=%d, nb_frames=%d" % (gain, nb_frames))
        if self.cam.getExpTime() > 0.1:
            raise ValueError('Jungfrau exposure time too high')
        elif self.gain_ped_seq:
            raise RuntimeError('Another sequence is active')

        self.gain_ped_seq = {'task': None, 'res': None}
        def task(gain, nb_frames):
            pedestal_data = self.measureGainPedestal(gain, nb_frames)
            data, acq_nb_frames, frame = pedestal_data
            print(f'nb_frames={nb_frames}, acq_nb_frames={acq_nb_frames}, '
                  f'frame={frame}')
            self.gain_ped_seq['res'] = data.buffer.copy(), frame
        t = Thread(target=task, args=(gain, nb_frames))
        self.gain_ped_seq['task'] = t
        t.start()
        res = '%d,%d' % self.getFrameSize()
        deb.Return("res=%s" % res)
        return res

    @Core.DEB_MEMBER_FUNCT
    def getGainPedestalSeqStatus(self):
        if not self.gain_ped_seq:
            raise RuntimeError('No sequence active')
        is_alive = self.gain_ped_seq['task'].is_alive()
        deb.Return("is_alive=%s" % is_alive)
        return is_alive

    @Core.DEB_MEMBER_FUNCT
    def getGainPedestalSeqResult(self):
        if self.getGainPedestalSeqStatus():
            raise RuntimeError('Sequence still running')
        self.gain_ped_seq['task'].join()
        res = self.gain_ped_seq['res']
        self.gain_ped_seq = None
        deb.Return('res=%s' % (res,))
        return res[0].flatten()

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383

#------------------------------------------------------------------
#    SlsDetectorJungfrau class
#------------------------------------------------------------------

class SlsDetectorJungfrauClass(SlsDetectorClass):

    class_property_list = {}
    class_property_list.update(SlsDetectorClass.class_property_list)

    device_property_list = {
        }
    device_property_list.update(SlsDetectorClass.device_property_list)

    cmd_list = {
        'setGainPedBackground':
        [[PyTango.DevULong, "level"],
         [PyTango.DevVoid, ""]],
        'clearGainPedBackground':
        [[PyTango.DevVoid, ""],
         [PyTango.DevVoid, ""]],
384
385
386
387
388
389
390
391
392
393
394
        'startGainPedestalSeq':
        [[PyTango.DevString,
          "2-value comma-separated tuple: gain,nb-of-frames"],
         [PyTango.DevString,
          "result: 2-value comma-separated tuple: width,height"]],
        'getGainPedestalSeqStatus':
        [[PyTango.DevVoid, ""],
         [PyTango.DevBoolean, "Sequence is running"]],
        'getGainPedestalSeqResult':
        [[PyTango.DevVoid, ""],
         [PyTango.DevVarDoubleArray, "Pedestal seq result"]],
395
396
397
398
399
        }
    cmd_list.update(SlsDetectorClass.cmd_list)

    attr_list = {
        'img_proc_config':
400
401
402
403
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'img_src':
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'det_map':
        [[PyTango.DevULong,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'gain_map':
        [[PyTango.DevUChar,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'adc_map':
        [[PyTango.DevUShort,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'gain_ped_map_type':
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'gain_ped_proc_map16':
        [[PyTango.DevUShort,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'gain_ped_proc_map32':
428
429
430
431
        [[PyTango.DevULong,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'ave_map':
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
        [[PyTango.DevULong,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'gain_0_calib_map':
        [[PyTango.DevDouble,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'gain_1_calib_map':
        [[PyTango.DevDouble,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'gain_2_calib_map':
        [[PyTango.DevDouble,
          PyTango.IMAGE,
          PyTango.READ, 8192, 8192]],
        'ped_0_calib_map':
        [[PyTango.DevDouble,
          PyTango.IMAGE,
450
          PyTango.READ_WRITE, 8192, 8192]],
451
452
453
        'ped_1_calib_map':
        [[PyTango.DevDouble,
          PyTango.IMAGE,
454
          PyTango.READ_WRITE, 8192, 8192]],
455
456
457
        'ped_2_calib_map':
        [[PyTango.DevDouble,
          PyTango.IMAGE,
458
          PyTango.READ_WRITE, 8192, 8192]],
459
460
461
462
463
464
465
466
467
468
469
470
471
        }
    attr_list.update(SlsDetectorClass.attr_list)

    def __init__(self,name) :
        SlsDetectorClass.__init__(self,name)


#----------------------------------------------------------------------------
# Plugin
#----------------------------------------------------------------------------
_SlsDetectorJungfrau = None

def get_control(config_fname, **keys) :
472
    global _SlsDetectorJungfrau
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487

    _Cam, _HwInter, _Control = get_slsdetector_objs()
    if _Control is not None:
        return _Control 

    det_id = 0
    _Cam = SlsDetectorHw.Camera(config_fname, det_id)
    det_type = _Cam.getType()

    _HwInter = SlsDetectorHw.Interface(_Cam)

    if det_type != SlsDetectorHw.JungfrauDet:
        raise ValueError("Bad detector type: %s" % det_type)

    _SlsDetectorJungfrau = SlsDetectorHw.Jungfrau(_Cam)
488
489
    _Reconstruction = _SlsDetectorJungfrau.getReconstruction();
    _HwInter.setReconstruction(_Reconstruction)
490
491
492

    _Control = Core.CtControl(_HwInter)

493
494
    _ImgProc = _SlsDetectorJungfrau.createImgProcTask()
    if _ImgProc:
495
496
497
498
        ext_op_mgr = _Control.externalOperation()
        alias = 'SlsDetectorImgProc'
        stage_level = 10
        soft_op = ext_op_mgr.addOp(Core.USER_SINK_TASK, alias, stage_level)
499
        soft_op.setSinkTask(_ImgProc)
500
501
502
503
504
505
506
507

    set_slsdetector_objs(_Cam, _HwInter, _Control)
    return _Control 

def get_tango_specific_class_n_device():
    return SlsDetectorJungfrauClass, SlsDetectorJungfrau