Exemplo n.º 1
0
def _parse_packets_v1_0(row, message_dset, *args, **kwargs):
    if row['type'] == 4:
        return TimestampPacket(row['timestamp'])
    if row['type'] == 5:
        index = row['counter']
        message_row = message_dset[index]
        message = message_row['message'].decode()
        timestamp = row['timestamp']
        return MessagePacket(message, timestamp)
    if row['type'] < 4:
        p = Packet_v1()
        p.chip_key = row['chip_key']
        p.packet_type = row['type']
        p.chipid = row['chipid']
        p.parity_bit_value = row['parity']
        if p.packet_type == Packet_v1.DATA_PACKET:
            p.channel = row['channel']
            p.timestamp = row['timestamp']
            p.dataword = row['adc_counts']
            p.fifo_half_flag = row['fifo_half']
            p.fifo_full_flag = row['fifo_full']
        elif p.packet_type == Packet_v1.TEST_PACKET:
            p.counter = row['counter']
        elif (p.packet_type == Packet_v1.CONFIG_WRITE_PACKET
              or p.packet_type == Packet_v1.CONFIG_READ_PACKET):
            p.register_address = row['register']
            p.register_data = row['value']
        p.direction = row['direction']
        return p
    return None
Exemplo n.º 2
0
def _parse_packets_v2_0(row, message_dset, *args, **kwargs):
    if row['packet_type'] == 4:
        return TimestampPacket(row['timestamp'])
    if row['packet_type'] == 5:
        index = row['counter']
        message_row = message_dset[index]
        message = message_row['message'].decode()
        timestamp = row['timestamp']
        return MessagePacket(message, timestamp)
    if row['packet_type'] < 4:
        p = Packet_v2()
        p.io_group = row['io_group']
        p.io_channel = row['io_channel']
        p.chip_id = row['chip_id']
        p.packet_type = row['packet_type']
        p.downstream_marker = row['downstream_marker']
        p.parity = row['parity']
        p.valid_parity = row['valid_parity']
        p.direction = row['direction']
        if p.packet_type == Packet_v2.DATA_PACKET:
            p.channel_id = row['channel_id']
            p.timestamp = row['timestamp']
            p.dataword = row['dataword']
            p.trigger_type = row['trigger_type']
            p.local_fifo = row['local_fifo']
            p.shared_fifo = row['shared_fifo']
            if row['fifo_diagnostics_enabled'] != 0:
                p.fifo_diagnostics_enabled = True
                p.local_fifo = row['local_fifo_events']
                p.shared_fifo = row['shared_fifo_events']
                p.timestamp = row['timestamp']
        elif p.packet_type in (Packet_v2.CONFIG_READ_PACKET,
                               Packet_v2.CONFIG_WRITE_PACKET):
            p.register_address = row['register_address']
            p.register_data = row['register_data']
        return p
    return None
Exemplo n.º 3
0
def message_packet():
    p = MessagePacket('Hello, World!', 1234567)
    return p
Exemplo n.º 4
0
def from_file(filename, version=None, start=None, end=None):
    '''
    Read the data from the given file into LArPix Packet objects.

    :param filename: the name of the file to read
    :param version: the format version. Specify this parameter to
        enforce a version check. When a specific version such as
        ``'1.5'`` is specified, a ``RuntimeError`` will be raised if the
        stored format version number is not an exact match. If a version
        is prefixed with ``'~'`` such as ``'~1.5'``, a ``RuntimeError``
        will be raised if the stored format version is *incompatible*
        with the specified version. Compatible versions are those with
        the same major version and at least the same minor version. E.g.
        for ``'~1.5'``, versions between v1.5 and v2.0 are compatible.
        If unspecified or ``None``, will use the stored format version.
    :param start: the index of the first row to read
    :param end: the index after the last row to read (same semantics as
        Python ``range``)
    :returns packet_dict: a dict with keys ``'packets'`` containing a
        list of packet objects; and ``'created'``, ``'modified'``, and
        ``'version'``, containing the file metadata.

    '''
    with h5py.File(filename, 'r') as f:
        file_version = f['_header'].attrs['version']
        if version is None:
            version = file_version
        elif version[0] == '~':
            file_major, _, file_minor = file_version.split('.')
            version_major, _, version_minor = version.split('.')
            version_major = version_major[1:]
            if (file_major != version_major or file_minor < version_minor):
                raise RuntimeError('Incompatible versions: existing: %s, '
                                   'specified: %s' % (file_version, version))
            else:
                version = file_version
        elif version == file_version:
            pass
        else:
            raise RuntimeError('Incompatible versions: existing: %s, '
                               'specified: %s' % (file_version, version))
        if version not in dtypes:
            raise RuntimeError('Unknown version: %s' % version)
        if version == '0.0':
            dset_name = 'raw_packet'
        else:
            dset_name = 'packets'
            message_dset_name = 'messages'
            message_props = (
                dtype_property_index_lookup[version][message_dset_name])
            message_dset = f[message_dset_name]
        props = dtype_property_index_lookup[version][dset_name]
        packets = []
        if start is None and end is None:
            dset_iter = f[dset_name]
        else:
            dset_iter = f[dset_name][start:end]
        for row in dset_iter:
            if row[props['type']] == 4:
                packets.append(TimestampPacket(row[props['timestamp']]))
                continue
            if row[props['type']] == 5:
                index = row[props['counter']]
                message_row = message_dset[index]
                message = message_row[message_props['message']].decode()
                timestamp = row[props['timestamp']]
                packets.append(MessagePacket(message, timestamp))
                continue
            p = Packet()
            p.chip_key = row[props['chip_key']]
            p.packet_type = row[props['type']]
            p.chipid = row[props['chipid']]
            p.parity_bit_value = row[props['parity']]
            if p.packet_type == Packet.DATA_PACKET:
                p.channel = row[props['channel']]
                p.timestamp = row[props['timestamp']]
                p.dataword = row[props['adc_counts']]
                p.fifo_half_flag = row[props['fifo_half']]
                p.fifo_full_flag = row[props['fifo_full']]
            elif p.packet_type == Packet.TEST_PACKET:
                p.counter = row[props['counter']]
            elif (p.packet_type == Packet.CONFIG_WRITE_PACKET
                  or p.packet_type == Packet.CONFIG_READ_PACKET):
                p.register_address = row[props['register']]
                p.register_data = row[props['value']]
            if version != '0.0':
                p.direction = row[props['direction']]
            packets.append(p)
        return {
            'packets': packets,
            'created': f['_header'].attrs['created'],
            'modified': f['_header'].attrs['modified'],
            'version': f['_header'].attrs['version'],
        }