Ejemplo n.º 1
0
    def testInstanceCreation(self):
        """
        Instantiate in different ways a SardanaValue object.
            """
        sar_val = SardanaValue()
        self.assertIsInstance(
            sar_val, SardanaValue, 'Instantiation of an ' +
            'object SardanaValue without arguments does not work')

        sar_val1 = SardanaValue(value=9)
        self.assertIsInstance(
            sar_val1, SardanaValue, 'Instantiation of an ' +
            'object SardanaValue with the value argument does not work')

        sar_val2 = SardanaValue(value=8,
                                exc_info=None,
                                timestamp='09:30',
                                dtype='int',
                                dformat='int')

        self.assertIsInstance(
            sar_val2, SardanaValue, 'Instantiation of an ' +
            'object SardanaValue with arguments and exc_info equal None, ' +
            'does not work.')

        sar_val3 = SardanaValue(value=7,
                                exc_info='exception_info',
                                timestamp='09:30',
                                dtype='int',
                                dformat='int')

        self.assertIsInstance(
            sar_val3, SardanaValue, 'Instantiation of an ' +
            'object SardanaValue with arguments and exc_info ' +
            'different of None, does not work.')
Ejemplo n.º 2
0
    def testSardanaValueWithNoExceptionInfo(self):
        """Verify the creation of SardanaValue when exc_info is not specified
            and we give a value as argument of the SardanaValue constructor.
            SardanaValue representation shall contain its value.
            """
        value = 5
        sar_val = SardanaValue(value=value)
        returned_string = sar_val.__repr__()

        self.assertRegexpMatches(returned_string, repr(value),
                                 'The SardanaValue representation does not contain its value')

        self.assertEqual(sar_val.error, False,
                         'The error attribute should be False')
Ejemplo n.º 3
0
    def testSardanaValueWithNoExceptionInfo(self):
        """Verify the creation of SardanaValue when exc_info is not specified
            and we give a value as argument of the SardanaValue constructor.
            SardanaValue representation shall contain its value.
            """
        value = 5
        sar_val = SardanaValue(value=value)
        returned_string = sar_val.__repr__()

        self.assertRegexpMatches(
            returned_string, repr(value),
            'The SardanaValue representation does not contain its value')

        self.assertEqual(sar_val.error, False,
                         'The error attribute should be False')
Ejemplo n.º 4
0
    def _read_axis_value(self, element):

        def is_chunk(type_, obj):
            if not is_non_str_seq(obj):
                return False
            if type_ == ElementType.CTExpChannel:
                return True
            elif type_ == ElementType.OneDExpChannel:
                # empty list is also considered as chunk
                if len(obj) == 0 or not is_number(obj[0]):
                    return True
            elif type_ == ElementType.TwoDExpChannel:
                # empty list is also considered as chunk
                if len(obj) == 0 or not is_number(obj[0][0]):
                    return True
            return False

        try:
            axis = element.get_axis()
            type_ = element.get_type()
            ctrl_value = self.ctrl.ReadOne(axis)
            if ctrl_value is None:
                msg = '%s.ReadOne(%s[%d]) return error: Expected value(s), ' \
                      'got None instead' % (self.name, element.name, axis)
                raise ValueError(msg)

            if is_chunk(type_, ctrl_value):
                value = [translate_ctrl_value(v) for v in ctrl_value]
            else:
                value = translate_ctrl_value(ctrl_value)
        except:
            value = SardanaValue(exc_info=sys.exc_info())
        return value
 def ReadOneSingle(self, axis):
     index = self.index[axis]
     #self._log.debug('ReadOne(%d) index = %d' % (axis, index))
     if axis == 1:
         data = [self._integration_time]
     else:
         data = numpy.array([0])
         self.delay_counter[axis] += 1
         self.delay_counter[axis] %= self.QUERY_FILTER
         if self.delay_counter[axis] == 0:
             try:
                 channel = self.channels[axis]
                 data = channel.read_attribute(self.BUFFER_ATTR).value
                 if data is None:
                     data = numpy.array([0])
             except Exception as e:
                 msg = ('ReadOne(%d): Exception while reading' +
                        ' buffer: %s' % (axis, e))
                 self._log.error(msg)
             if len(data) == 2:
                 index = 1
                 data = self._calculate(axis, data, index)
     # values coming from CountBuffer are of type DevULong cast it to float
     data = float(data[0])
     sardana_value = SardanaValue(data)
     #self._log.debug('ReadOne(%d): data: %s' % (axis, repr(data)))
     return sardana_value
Ejemplo n.º 6
0
    def ReadOne(self, axis):
        # self._log.debug("ReadOne(%d): Entering...", axis)
        if len(self.new_data) == 0:
            #return -1
            return None

        if axis == 1:  # timer axis
            channel_index = 0
        else:
            channel_name = str(self.attributes[axis - 1]['ChannelName'])
            if channel_name not in self.channels_order:
                raise ValueError(
                    'Channel name configured is not enabled in pandabox')
            else:
                channel_index = (self.channels_order.index(channel_name) + 1
                                 )  # +1 because of timer column

        if self._synchronization in [
                AcqSynch.SoftwareTrigger, AcqSynch.SoftwareGate
        ]:
            return SardanaValue(self.new_data[channel_index][0])

        else:
            val = self.new_data[channel_index]
            return val
Ejemplo n.º 7
0
 def ReadOne(self, ind):
     self._log.debug('ReadOne(%d): entering...' % ind)
     channel = self.read_channels[ind]
     ret = None
     if channel.mode in (AcqSynch.HardwareTrigger, AcqSynch.HardwareGate):
         values = copy.deepcopy(channel.buffer_values)
         ret = []
         for v in values:
             ret.append(SardanaValue(v))
         channel.buffer_values.__init__()
         channel._counter = channel._counter + len(values)
     elif channel.mode == AcqSynch.SoftwareTrigger:
         v = channel.value
         ret = SardanaValue(v)
     self._log.debug('ReadOne(%d): returning %s' % (ind, repr(ret)))
     return ret
Ejemplo n.º 8
0
    def raw_read_axis_values(self, axes=None, ctrl_values=None):
        """**Unsafe method**. Reads the value for the given axes. If axes
        is None, reads the value of all active axes.

        :param axes: the list of axis to get the value. Default is None
                       meaning all active axis in this controller
        :type axes: seq<int> or None
        :return: a map containing the controller value information for each axis
        :rtype: dict<PoolElement, SardanaValue>"""
        if axes is None:
            axes = list(self._element_axis.keys())
        if ctrl_values is None:
            ctrl_values = {}

        ctrl = self.ctrl

        try:
            ctrl.PreReadAll()
            for axis in axes:
                ctrl.PreReadOne(axis)
            ctrl.ReadAll()
        except:
            exc_info = sys.exc_info()
            for axis in axes:
                element = self.get_element(axis=axis)
                ctrl_values[element] = SardanaValue(exc_info=exc_info)
            return ctrl_values

        for axis in axes:
            element = self.get_element(axis=axis)
            ctrl_values[element] = self._read_axis_value(element)

        return ctrl_values
Ejemplo n.º 9
0
 def calc_all_pseudo(self, physical_positions=None):
     try:
         obj = self.obj
         if physical_positions is None:
             physical_positions = self.get_physical_positions()
         else:
             l_p, l_u = len(physical_positions), len(
                 obj.get_user_elements())
             if l_p != l_u:
                 raise IndexError("CalcAllPseudo():: must give %d physical "
                                  "positions (you gave %d)" % (l_u, l_p))
         result = obj.controller.calc_all_pseudo(physical_positions, None)
     except SardanaException as se:
         result = SardanaValue(exc_info=se.exc_info)
     except:
         result = SardanaValue(exc_info=sys.exc_info())
     return result
Ejemplo n.º 10
0
 def calc_all(self, physical_values=None):
     try:
         obj = self.obj
         if physical_values is None:
             physical_values = self.get_physical_values()
         else:
             l_v, l_u = len(physical_values), len(obj.get_user_elements())
             if l_v != l_u:
                 raise IndexError("CalcAllPseudo(%s): must give %d physical " \
                                  "values (you gave %d)" % (obj.name, l_u, l_v))
         ctrl, axis = obj.controller, obj.axis
         result = ctrl.calc_all(axis, physical_values)
     except SardanaException as se:
         result = SardanaValue(exc_info=se.exc_info)
     except:
         result = SardanaValue(exc_info=sys.exc_info())
     return result
Ejemplo n.º 11
0
 def _get_value_obj(self):
     '''Override superclass method and compose a SardanaValue object from
     the present values.
     '''
     value = self._accumulation.value
     # use timestamp of the last acquired sample as timestamp of
     # accumulation
     timestamp = self._accumulation.get_time_buffer()[-1]
     value_obj = SardanaValue(value=value, timestamp=timestamp)
     return value_obj
Ejemplo n.º 12
0
    def calc_physical(self, new_position):
        try:
            obj = self.obj
            curr_physical_positions = self.get_physical_positions()
            if isinstance(new_position, collections.Sequence):
                new_positions = new_position
            else:
                positions = obj.get_siblings_positions()
                positions[obj] = new_position
                new_positions = len(positions) * [None]
                for pseudo, position in list(positions.items()):
                    new_positions[pseudo.axis - 1] = position

            result = obj.controller.calc_all_physical(new_positions,
                                                      curr_physical_positions)
        except SardanaException as se:
            result = SardanaValue(exc_info=se.exc_info)
        except:
            result = SardanaValue(exc_info=sys.exc_info())
        return result
Ejemplo n.º 13
0
 def calc_all(self, values):
     ctrl = self.ctrl
     try:
         ctrl_value = ctrl.CalcAll(values)
         if ctrl_value is None:
             msg = '%s.CalcAll() return error: Expected value, ' \
                   'got None instead' % (self.name,)
             raise ValueError(msg)
         value = translate_ctrl_value(ctrl_value)
     except:
         value = SardanaValue(exc_info=sys.exc_info())
     return value
Ejemplo n.º 14
0
 def calc_physical(self, axis, pseudo_pos, curr_physical_pos):
     ctrl = self.ctrl
     try:
         ctrl_value = ctrl.CalcPhysical(axis, pseudo_pos, curr_physical_pos)
         if ctrl_value is None:
             msg = '%s.CalcPhysical() return error: Expected value, ' \
                   'got None instead' % (self.name,)
             raise ValueError(msg)
         value = translate_ctrl_value(ctrl_value)
     except:
         value = SardanaValue(exc_info=sys.exc_info())
     return value
Ejemplo n.º 15
0
def translate_ctrl_value(value):
    if isinstance(value, SardanaValue):
        return value

    for _, translator in list(__CTRL_VALUE_TRANSLATORS.items()):
        try:
            return translator(value)
        except CannotTranslateException:
            continue

    # Fallback translator
    return SardanaValue(value=value)
Ejemplo n.º 16
0
 def _read_axis_value(self, element):
     try:
         axis = element.get_axis()
         ctrl_value = self.ctrl.ReadOne(axis)
         if ctrl_value is None:
             msg = '%s.ReadOne(%s[%d]) return error: Expected value, ' \
                   'got None instead' % (self.name, element.name, axis)
             raise ValueError(msg)
         value = translate_ctrl_value(ctrl_value)
     except:
         value = SardanaValue(exc_info=sys.exc_info())
     return value
Ejemplo n.º 17
0
    def testSardanaValueWithExceptionInfo(self):
        """Verify the creation of SardanaValue when exc_info != None.
            Verify that 'Error' is contained in the returned string.
        """
        val = 4
        sar_val = SardanaValue(value=val, exc_info='exception_info')
        representation = repr(sar_val)

        self.assertEqual(sar_val.error, True,
                         'The error attribute should be True.')

        self.assertRegexpMatches(
            representation, ".*<Error>.*",
            'The SardanaValue representation does not contain <Error>.')
    def ReadOne(self, axis):
        if self._synchronization == AcqSynch.SoftwareTrigger:
            if not self._new_data:
                raise Exception("Acquisition did not finish correctly. Adlink "
                                "State %r" % self._hw_state)
            return SardanaValue(self.dataBuff[axis][0])

        elif self._synchronization == AcqSynch.HardwareTrigger:
            if not self._new_data:
                return []
            else:
                return self.dataBuff[axis]
        else:
            raise Exception("Unknown synchronization mode.")
Ejemplo n.º 19
0
def from_deviceattribute(da):
    if da.has_failed:
        exc_info = DevFailed(*da.get_err_stack())
        value = None
    else:
        exc_info = None
        value = from_deviceattribute_value(da.value)

    dtype, dformat = from_tango_type_format(da.type, da.data_format)

    ret = SardanaValue(value=value, exc_info=exc_info,
                       timestamp=da.time.totime(), dtype=dtype,
                       dformat=dformat)
    return ret
    def ReadOne(self, axis):
        # self._log.debug("ReadOne(%d): Entering...", axis)
        if len(self._new_data) == 0:
            return None
        axis -= 1
        channel = 'CHAN0{0}'.format(axis)
        values = list(self._new_data[channel])

        if self._synchronization in [
                AcqSynch.SoftwareTrigger, AcqSynch.SoftwareGate
        ]:
            return SardanaValue(values[0])
        else:
            self._new_data[channel] = []
            return values
    def ReadOne(self, axis):
        if len(self._new_data) == 0:
            if self._synchronization in [
                    AcqSynch.SoftwareTrigger, AcqSynch.SoftwareGate
            ]:
                return None
            else:
                return []

        axis -= 1
        channel = 'CHAN0{0}'.format(axis)
        values = list(self._new_data[channel])

        if self._synchronization in [
                AcqSynch.SoftwareTrigger, AcqSynch.SoftwareGate
        ]:
            return SardanaValue(values[0])
        else:
            self._new_data[channel] = []
            return values
Ejemplo n.º 22
0
    def _read_axis_value_refs(self, element):
        def is_chunk(obj):
            if is_non_str_seq(obj):
                return True
            return False

        try:
            axis = element.get_axis()
            ctrl_value = self.ctrl.RefOne(axis)
            if ctrl_value is None:
                msg = ('%s.RefOne(%s[%d]) return error: Expected value '
                       'ref(s), got None instead' %
                       (self.name, element.name, axis))
                raise ValueError(msg)

            if is_chunk(ctrl_value):
                value = [translate_ctrl_value(v) for v in ctrl_value]
            else:
                value = translate_ctrl_value(ctrl_value)
        except Exception:
            value = SardanaValue(exc_info=sys.exc_info())
        return value