Simulator.py 12 KB
Newer Older
Samuel Debionne's avatar
Samuel Debionne committed
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
############################################################################
# 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/>.
############################################################################
#----------------------------------------------------------------------------
# The Simulator camera plugin TANGO interface
#----------------------------------------------------------------------------

import itertools
import PyTango

Laurent Claustre's avatar
Laurent Claustre committed
29
30
31
32
33
34
# python3 compat
try:
    from itertools import izip as zip
except ImportError:
    pass

Samuel Debionne's avatar
Samuel Debionne committed
35
36
37
38
39
40
from Lima.Server import AttrHelper

from Lima import Core
from Lima import Simulator as SimuMod

def grouper(n, iterable, padvalue=None):
Laurent Claustre's avatar
Laurent Claustre committed
41
    return zip(*[itertools.chain(iterable, itertools.repeat(padvalue, n-1))]*n)
Samuel Debionne's avatar
Samuel Debionne committed
42
43
44
45


class Simulator(PyTango.Device_4Impl):

Samuel Debionne's avatar
Samuel Debionne committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#------------------------------------------------------------------
#    Static properties
#------------------------------------------------------------------
    _Mode = {
        'GENERATOR': SimuMod.Camera.MODE_GENERATOR,
        'GENERATOR_PREFETCH': SimuMod.Camera.MODE_GENERATOR_PREFETCH,
        'LOADER': SimuMod.Camera.MODE_LOADER,
        'LOADER_PREFETCH': SimuMod.Camera.MODE_LOADER_PREFETCH,
	}

    _invMode = {v: k for k, v in _Mode.items()}

    _RotationAxis = {
        'ROTATIONX': SimuMod.FrameBuilder.RotationX,
        'ROTATIONY': SimuMod.FrameBuilder.RotationY,
	}

    _FillType = {
        'GAUSS':       SimuMod.FrameBuilder.Gauss,
        'DIFFRACTION': SimuMod.FrameBuilder.Diffraction,
	}

Samuel Debionne's avatar
Samuel Debionne committed
68
69
70
71
72
73
74
75
    Core.DEB_CLASS(Core.DebModApplication, 'LimaSimulator')

#------------------------------------------------------------------
#    Device constructor
#------------------------------------------------------------------
    def __init__(self,*args) :
        PyTango.Device_4Impl.__init__(self,*args)

Samuel Debionne's avatar
Samuel Debionne committed
76
77
78
79
80
81
82
83
84
        # Leagcy code used by AttrHelper
        # switch to the new pyTango please
        self.__Mode = {
            'GENERATOR': SimuMod.Camera.MODE_GENERATOR,
            'GENERATOR_PREFETCH': SimuMod.Camera.MODE_GENERATOR_PREFETCH,
            'LOADER': SimuMod.Camera.MODE_LOADER,
            'LOADER_PREFETCH': SimuMod.Camera.MODE_LOADER_PREFETCH,
    	}

Samuel Debionne's avatar
Samuel Debionne committed
85
86
87
        self.__RotationAxis = {
            'ROTATIONX': SimuMod.FrameBuilder.RotationX,
            'ROTATIONY': SimuMod.FrameBuilder.RotationY,
Samuel Debionne's avatar
Samuel Debionne committed
88
    	}
Samuel Debionne's avatar
Samuel Debionne committed
89
90
91
92

        self.__FillType = {
            'GAUSS':       SimuMod.FrameBuilder.Gauss,
            'DIFFRACTION': SimuMod.FrameBuilder.Diffraction,
Samuel Debionne's avatar
Samuel Debionne committed
93
    	}
Samuel Debionne's avatar
Samuel Debionne committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107

        self.init_device()

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

#------------------------------------------------------------------
#    Device initialization
#------------------------------------------------------------------
    @Core.DEB_MEMBER_FUNCT
    def init_device(self):
Samuel Debionne's avatar
Samuel Debionne committed
108
109
110
111
112
113
114
115
116
117

        # Load the properties
        self.get_device_properties(self.get_device_class())

        # Apply properties if any
        if self.mode and (Simulator._Mode.get(self.mode) != None):
            _SimuCamera.setMode(Simulator._Mode[self.mode])

        if 'PREFETCH' in self.mode and self.nb_prefetched_frames:
            _SimuCamera.getFrameGetter().setNbPrefetchedFrames(self.nb_prefetched_frames)
Samuel Debionne's avatar
Samuel Debionne committed
118

119
120
121
122
        if self.frame_dim:
            frame_dim = self.getFrameDimFromLongArray(self.frame_dim)
            _SimuCamera.setFrameDim(frame_dim)

123
124
125
126
        if self.pixel_size:
            pixel_size = self.pixel_size
            _SimuCamera.setPixelSize(pixel_size[0], pixel_size[1])

127
128
        self.set_state(PyTango.DevState.ON)

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    @Core.DEB_MEMBER_FUNCT
    def getFrameDimFromLongArray(self, dim_arr):
        width, height, depth = dim_arr
        if depth == 1:
            image_type = Core.Bpp8
        elif depth == 2:
            image_type = Core.Bpp16
        elif depth == 4:
            image_type = Core.Bpp32
        else:
            raise ValueError('Unknown pixel depth: %d' % depth)
        return Core.FrameDim(width, height, image_type)

    @Core.DEB_MEMBER_FUNCT
    def getLongArrayFromFrameDim(self, frame_dim):
        size = frame_dim.getSize()
        return [size.getWidth(), size.getHeight(), frame_dim.getDepth()]

Samuel Debionne's avatar
Samuel Debionne committed
147
148
149
150
    @Core.DEB_MEMBER_FUNCT
    def getAttrStringValueList(self, attr_name):
        return AttrHelper.get_attr_string_value_list(self, attr_name)

151
152
153
    def trigExternal(self):
        _SimuCamera.extTrigAcq()

Valentin Valls's avatar
Typo    
Valentin Valls committed
154
    def __getattr__(self, name):
Samuel Debionne's avatar
Samuel Debionne committed
155
156
157
158
        try:
            return AttrHelper.get_attr_4u(self, name, _SimuCamera.getFrameGetter(), False)
        except:
            return AttrHelper.get_attr_4u(self, name, _SimuCamera)
Samuel Debionne's avatar
Samuel Debionne committed
159
160
161
162
163
164
165
166

    @staticmethod
    def getGaussPeaksFromFloatArray(peak_list_flat):
        peak_list = grouper(4, map(float, peak_list_flat))
        gauss_peaks = [SimuMod.GaussPeak(*p) for p in peak_list]
        return gauss_peaks

    def read_peaks(self,attr):
Samuel Debionne's avatar
Samuel Debionne committed
167
        gauss_peaks = _SimuCamera.getFrameGetter().getPeaks()
Samuel Debionne's avatar
Samuel Debionne committed
168
169
        peak_list = [(p.x0, p.y0, p.fwhm, p.max) for p in gauss_peaks]
        peak_list_flat = list(itertools.chain(*peak_list))
Samuel Debionne's avatar
Samuel Debionne committed
170
        attr.set_value(peak_list_flat)
Samuel Debionne's avatar
Samuel Debionne committed
171
172
173
174

    def write_peaks(self,attr) :
        peak_list_flat = attr.get_write_value()
        gauss_peaks = self.getGaussPeaksFromFloatArray(peak_list_flat)
Samuel Debionne's avatar
Samuel Debionne committed
175
        _SimuCamera.getFrameGetter().setPeaks(gauss_peaks)
Samuel Debionne's avatar
Samuel Debionne committed
176
177

    def read_peak_angles(self,attr) :
Samuel Debionne's avatar
Samuel Debionne committed
178
        peak_angle_list = _SimuCamera.getFrameGetter().getPeakAngles()
Samuel Debionne's avatar
Samuel Debionne committed
179
180
181
182
        attr.set_value(peak_angle_list)

    def write_peak_angles(self,attr) :
        peak_angle_list = attr.get_write_value()
Samuel Debionne's avatar
Samuel Debionne committed
183
        _SimuCamera.getFrameGetter().setPeakAngles(map(float, peak_angle_list))
Samuel Debionne's avatar
Samuel Debionne committed
184
185

    def read_diffraction_pos(self,attr) :
Samuel Debionne's avatar
Samuel Debionne committed
186
        x, y = _SimuCamera.getFrameGetter().getDiffractionPos()
Samuel Debionne's avatar
Samuel Debionne committed
187
188
189
190
        attr.set_value((x, y))

    def write_diffraction_pos(self,attr) :
        x, y = attr.get_write_value()
Samuel Debionne's avatar
Samuel Debionne committed
191
        _SimuCamera.getFrameGetter().setDiffractionPos(x, y)
Samuel Debionne's avatar
Samuel Debionne committed
192
193

    def read_diffraction_speed(self,attr) :
Samuel Debionne's avatar
Samuel Debionne committed
194
        sx, sy = _SimuCamera.getFrameGetter().getDiffractionSpeed()
Samuel Debionne's avatar
Samuel Debionne committed
195
196
197
198
        attr.set_value((sx, sy))

    def write_diffraction_speed(self,attr) :
        sx, sy = attr.get_write_value()
Samuel Debionne's avatar
Samuel Debionne committed
199
        _SimuCamera.getFrameGetter().setDiffractionSpeed(sx, sy)
Samuel Debionne's avatar
Samuel Debionne committed
200

Samuel Debionne's avatar
Samuel Debionne committed
201
202
203
204
205
206
207
208
    def read_nb_prefetched_frames(self,attr) :
        if (_SimuCamera.getMode() == SimuMod.Camera.MODE_GENERATOR_PREFETCH) or \
           (_SimuCamera.getMode() == SimuMod.Camera.MODE_LOADER_PREFETCH) :
            nb_prefetched_frames = _SimuCamera.getFrameGetter().getNbPrefetchedFrames()
        else :
            nb_prefetched_frames = 0
        attr.set_value(nb_prefetched_frames)

209
210
211
212
213
214
215
216
217
    def read_frame_dim(self,attr) :
        frame_dim = _SimuCamera.getFrameDim()
        dim_arr = self.getLongArrayFromFrameDim(frame_dim)
        attr.set_value(dim_arr)

    def write_frame_dim(self,attr) :
        dim_arr = attr.get_write_value()
        frame_dim = self.getFrameDimFromLongArray(dim_arr)
        _SimuCamera.setFrameDim(frame_dim)
Samuel Debionne's avatar
Samuel Debionne committed
218
219
220
221
222
223

class SimulatorClass(PyTango.DeviceClass):

    class_property_list = {}

    device_property_list = {
224
225
226
        'frame_dim':
        [PyTango.DevVarLongArray,
         "Frame dimension in the form: width, height, depth", []],
227
228
229
        'pixel_size':
        [PyTango.DevVarDoubleArray,
         "Pixel size in the form: width, height (in meter)", []],
Samuel Debionne's avatar
Samuel Debionne committed
230
231
232
233
234
235
        'mode':
        [PyTango.DevString,
         "Simulator mode: GENERATOR, GENERATOR_PREFETCH, LOADER, LOADER_PREFETCH",[]],
        'nb_prefetched_frames':
        [PyTango.DevLong,
         "Number of prefetched frames",[]],
Samuel Debionne's avatar
Samuel Debionne committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        'peaks':
        [PyTango.DevVarDoubleArray,
         "Gauss peak list [x0,y0,w0,A0,x1,y1,w1,A1...]",[]],
        'peak_angles':
        [PyTango.DevVarDoubleArray,
         "Base rotation angle for each peak",[]],
        'fill_type':
        [PyTango.DevString,
         "Image fill type: GAUSS, DIFFRACTION",[]],
        'rotation_axis':
        [PyTango.DevString,
         "Peak move policy: STATIC, ROTATIONX, ROTATIONY",[]],
        }

    cmd_list = {
        'getAttrStringValueList':
        [[PyTango.DevString, "Attribute name"],
         [PyTango.DevVarStringArray, "Authorized String value list"]],
254
        "trigExternal": [[PyTango.DevVoid, ""], [PyTango.DevVoid, ""]],
Samuel Debionne's avatar
Samuel Debionne committed
255
256
257
    }

    attr_list = {
Samuel Debionne's avatar
Samuel Debionne committed
258
        # Simulator mode
259
260
261
262
        'frame_dim':
        [[PyTango.DevLong,
          PyTango.SPECTRUM,
          PyTango.READ_WRITE, 3]],
Samuel Debionne's avatar
Samuel Debionne committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
        'mode':
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        # Simulator with prefetch
        'nb_prefetched_frames':
        [[PyTango.DevLong,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        # Simulator in loader mode
        'file_pattern':
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.WRITE]],
        # Simulator in generator mode
Samuel Debionne's avatar
Samuel Debionne committed
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
        'peaks':
        [[PyTango.DevDouble,
          PyTango.SPECTRUM,
          PyTango.READ_WRITE, 4000]],
        'peak_angles':
        [[PyTango.DevDouble,
          PyTango.SPECTRUM,
          PyTango.READ_WRITE, 1000]],
        'grow_factor':
        [[PyTango.DevDouble,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'fill_type':
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'rotation_axis':
        [[PyTango.DevString,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'diffraction_pos':
        [[PyTango.DevDouble,
          PyTango.SPECTRUM,
          PyTango.READ_WRITE, 2]],
        'diffraction_speed':
        [[PyTango.DevDouble,
          PyTango.SPECTRUM,
          PyTango.READ_WRITE, 2]],
        'rotation_angle':
        [[PyTango.DevDouble,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        'rotation_speed':
        [[PyTango.DevDouble,
          PyTango.SCALAR,
          PyTango.READ_WRITE]],
        }

    def __init__(self,name) :
        PyTango.DeviceClass.__init__(self,name)
        self.set_type(name)

#----------------------------------------------------------------------------
# Plugins
#----------------------------------------------------------------------------
_SimuCamera = None
_SimuInterface = None

def get_control(peaks=[], peak_angles=[], **keys) :
Samuel Debionne's avatar
Samuel Debionne committed
327
    global _SimuCamera, _SimuInterface
Samuel Debionne's avatar
Samuel Debionne committed
328
    if _SimuInterface is None:
Samuel Debionne's avatar
Samuel Debionne committed
329
330
331
332
333
334
335
336

        # If initial mode is specified, pass it to the constructor
        if 'mode' in keys:
            mode = Simulator._Mode[keys['mode']]
            _SimuCamera = SimuMod.Camera(mode)
        else:
            _SimuCamera = SimuMod.Camera()

Samuel Debionne's avatar
Samuel Debionne committed
337
338
339
340
341
342
343
344
345
        _SimuInterface = SimuMod.Interface(_SimuCamera)

        if peaks:
            if (type(peaks) != str and getattr(peaks, '__getitem__', None) and
                type(peaks[0]) == str):
                peaks = ','.join(peaks)
            if type(peaks) == str:
                peaks = map(float, peaks.split(','))
            gauss_peaks = Simulator.getGaussPeaksFromFloatArray(peaks)
Samuel Debionne's avatar
Samuel Debionne committed
346
            _SimuCamera.getFrameGetter().setPeaks(gauss_peaks)
Samuel Debionne's avatar
Samuel Debionne committed
347
348
349
350
351
        if peak_angles:
            if type(peak_angles) == str:
                peak_angles = peak_angles.split(',')
            if type(peak_angles[0]) == str:
                peak_angles = map(float, peak_angles)
Samuel Debionne's avatar
Samuel Debionne committed
352
            _SimuCamera.getFrameGetter().setPeakAngles(peak_angles)
Samuel Debionne's avatar
Samuel Debionne committed
353
354
355
356
357

    return Core.CtControl(_SimuInterface)

def get_tango_specific_class_n_device():
    return SimulatorClass,Simulator