Simulator.py 10.6 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
108

        self.init_device()

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

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

        # 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
119
120
121
122
123
124

    @Core.DEB_MEMBER_FUNCT
    def getAttrStringValueList(self, attr_name):
        return AttrHelper.get_attr_string_value_list(self, attr_name)

    def __getattr__(self,name) :
Samuel Debionne's avatar
Samuel Debionne committed
125
126
127
128
        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
129
130
131
132
133
134
135
136

    @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
137
        gauss_peaks = _SimuCamera.getFrameGetter().getPeaks()
Samuel Debionne's avatar
Samuel Debionne committed
138
139
        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
140
        attr.set_value(peak_list_flat)
Samuel Debionne's avatar
Samuel Debionne committed
141
142
143
144

    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
145
        _SimuCamera.getFrameGetter().setPeaks(gauss_peaks)
Samuel Debionne's avatar
Samuel Debionne committed
146
147

    def read_peak_angles(self,attr) :
Samuel Debionne's avatar
Samuel Debionne committed
148
        peak_angle_list = _SimuCamera.getFrameGetter().getPeakAngles()
Samuel Debionne's avatar
Samuel Debionne committed
149
150
151
152
        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
153
        _SimuCamera.getFrameGetter().setPeakAngles(map(float, peak_angle_list))
Samuel Debionne's avatar
Samuel Debionne committed
154
155

    def read_diffraction_pos(self,attr) :
Samuel Debionne's avatar
Samuel Debionne committed
156
        x, y = _SimuCamera.getFrameGetter().getDiffractionPos()
Samuel Debionne's avatar
Samuel Debionne committed
157
158
159
160
        attr.set_value((x, y))

    def write_diffraction_pos(self,attr) :
        x, y = attr.get_write_value()
Samuel Debionne's avatar
Samuel Debionne committed
161
        _SimuCamera.getFrameGetter().setDiffractionPos(x, y)
Samuel Debionne's avatar
Samuel Debionne committed
162
163

    def read_diffraction_speed(self,attr) :
Samuel Debionne's avatar
Samuel Debionne committed
164
        sx, sy = _SimuCamera.getFrameGetter().getDiffractionSpeed()
Samuel Debionne's avatar
Samuel Debionne committed
165
166
167
168
        attr.set_value((sx, sy))

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

Samuel Debionne's avatar
Samuel Debionne committed
171
172
173
174
175
176
177
178
    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)

Samuel Debionne's avatar
Samuel Debionne committed
179
180
181
182
183
184
185
186
    # def read_mode(self,attr) :
    #     invMode = {v: k for k, v in self.__Mode.items()}
    #     mode = _SimuCamera.getMode()
    #     attr.set_value(invMode[mode])
    #
    # def write_mode(self,attr) :
    #     mode = attr.get_write_value()
    #     _SimuCamera.setMode(self.__Mode[mode])
Samuel Debionne's avatar
Samuel Debionne committed
187
188
189
190
191
192

class SimulatorClass(PyTango.DeviceClass):

    class_property_list = {}

    device_property_list = {
Samuel Debionne's avatar
Samuel Debionne committed
193
194
195
196
197
198
        '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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
        '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"]],
    }

    attr_list = {
Samuel Debionne's avatar
Samuel Debionne committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
        # Simulator mode
        '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
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
        '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
285
    global _SimuCamera, _SimuInterface
Samuel Debionne's avatar
Samuel Debionne committed
286
    if _SimuInterface is None:
Samuel Debionne's avatar
Samuel Debionne committed
287
288
289
290
291
292
293
294

        # 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
295
296
297
298
299
300
301
302
303
        _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
304
            _SimuCamera.getFrameGetter().setPeaks(gauss_peaks)
Samuel Debionne's avatar
Samuel Debionne committed
305
306
307
308
309
        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
310
            _SimuCamera.getFrameGetter().setPeakAngles(peak_angles)
Samuel Debionne's avatar
Samuel Debionne committed
311
312
313
314
315

    return Core.CtControl(_SimuInterface)

def get_tango_specific_class_n_device():
    return SimulatorClass,Simulator