def empty_queue(self):
        '''
        Fetch and parse waiting packets on pacman data socket

        returns tuple of list of packets, full bytestream of all messages

        '''
        packets = []
        address_list = list()
        bytestream_list = list()
        bytestream = b''
        n_recv = 0
        while self.poller.poll(0) and n_recv < self.hwm:
            events = dict(self.poller.poll(0))
            for socket, n_events in events.items():
                for _ in range(n_events):
                    message = socket.recv()
                    n_recv += 1
                    bytestream_list += [message]
                    address_list += [self.receivers.inv[socket]]
        if not self.disable_packet_parsing:
            for message, address in zip(bytestream_list, address_list):
                packets += pacman_msg_format.parse(message, io_group=self._io_group_table.inv[address])
            bytestream = b''.join(bytestream_list)
        if self.enable_raw_file_writing:
            self._raw_file_queue.put((bytestream_list, [self._io_group_table.inv[address] for address in address_list]))
            if not self._raw_file_worker.is_alive():
                self._launch_raw_file_worker()

        return packets,bytestream
Beispiel #2
0
def readout():
    try:
        commander = zmq.Context().socket(zmq.REQ)
        commander.connect(cmd)

        # Using SUB socket to collect data
        reader = zmq.Context().socket(zmq.SUB)
        reader.connect(data)
        reader.setsockopt(zmq.SUBSCRIBE, b"")

        print("Press ENTER to start listening...")
        input()
        commander.send(b'')
        print("Signal sent to PACMAN card.")
        commander.recv()

        messages = 0
        while True:
            message = reader.recv()
            print("Message received:", message)
            messages += 1
            print("Total messages received:", messages)

            print("Converting to a packet...")
            packet = pacman_msg_format.parse(message)
            print("Writing to HDF5 file:", datafile)
            larpix.format.hdf5format.to_file(datafile, packet)
            print("Message written to file.")

    except:
        raise
    finally:
        reader.close()
Beispiel #3
0
    def empty_queue(self):
        '''
        Fetch and parse waiting packets on pacman data socket

        returns tuple of list of packets, full bytestream of all messages

        '''
        packets = []
        address_list = list()
        bytestream_list = list()
        bytestream = b''
        n_recv = 0
        while self.poller.poll(0) and n_recv < self.hwm:
            events = dict(self.poller.poll(0))
            for socket, n_events in events.items():
                for _ in range(n_events):
                    message = socket.recv()
                    n_recv += 1
                    bytestream_list += [message]
                    address_list += [self.receivers.inv[socket]]
        for message, address in zip(bytestream_list, address_list):
            packets += pacman_msg_format.parse(message, io_group=self._io_group_table.inv[address])
        bytestream = b''.join(bytestream_list)
        #print(bytestream)
        #for packet in packets:
        #    if isinstance(packet, Packet_v2): print(packet)
        return packets,bytestream
Beispiel #4
0
 def next_chunk(self):
     """Read and parse the next chunk of raw data, return formatted data"""
     # Check if the file has been 'opened'
     if not self._is_open:
         print("You should open the file before reading!")
         return None
     # Check if at end of file
     if self._current_read_index == self._raw_file_length:
         # End of file
         return None
     # Set the chunk start and end indices
     start = self._current_read_index
     end = start + self._chunksize
     if end > self._raw_file_length: end = self._raw_file_length
     # Try to read the chunk
     try:
         rd = rh5.from_rawfile(self._filename, start=start, end=end)
         self._current_read_index = end
     except Exception as e:
         print("Couldn't read file: ", self._filename)
         return None
     # Associate io group to packets, following Peter's example
     pkts = list()
     for io_group, msg in zip(rd['msg_headers']['io_groups'], rd['msgs']):
         pkts.extend(parse(msg, io_group=io_group))
         #pkts.extend(msg)
     # Return the packets
     return pkts
def main(input_filename, output_filename, block_size):
    total_messages = len_rawfile(input_filename)
    total_blocks = total_messages // block_size + 1
    last = time.time()
    for i_block in range(total_blocks):
        start = i_block * block_size
        end = min(start + block_size, total_messages)
        if start == end: return

        if time.time() > last + 1:
            print('reading block {} of {}...\r'.format(i_block + 1,
                                                       total_blocks),
                  end='')
            last = time.time()
        rd = from_rawfile(input_filename, start=start, end=end)
        pkts = list()
        for i_msg, data in enumerate(
                zip(rd['msg_headers']['io_groups'], rd['msgs'])):
            io_group, msg = data
            pkts.extend(parse(msg, io_group=io_group))
        to_file(output_filename, packet_list=pkts)
    print()