def data_packet(): p = Packet() p.packet_type = Packet.DATA_PACKET p.chipid = 123 p.channel = 7 p.timestamp = 123456 p.dataword = 120 p.fifo_half_flag = 1 p.assign_parity() p.chip_key = Key('1-2-123') p.direction = 1 return p
def test_to_file_v0_0_data_packet(tmpfile, data_packet): to_file(tmpfile, [data_packet], version='0.0') f = h5py.File(tmpfile, 'r') assert len(f['raw_packet']) == 1 row = f['raw_packet'][0] props = dtype_property_index_lookup['0.0']['raw_packet'] new_packet = Packet() new_packet.chip_key = row[props['chip_key']] new_packet.packet_type = row[props['type']] new_packet.chipid = row[props['chipid']] new_packet.parity_bit_value = row[props['parity']] new_packet.channel = row[props['channel']] new_packet.timestamp = row[props['timestamp']] new_packet.dataword = row[props['adc_counts']] new_packet.fifo_half_flag = row[props['fifo_half']] new_packet.fifo_full_flag = row[props['fifo_full']] assert new_packet == data_packet
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'], }