def main(): """Writes a psrdada buffer for test""" vis_temp = np.arange(NBLS * NCHAN * NPOL * 2, dtype=np.float32) # Define the data rate, including the buffer size # and the header size samples_per_frame = 1 header_size = 4096 buffer_size = int(4 * NBLS * NPOL * NCHAN * samples_per_frame * 2) assert buffer_size == vis_temp.nbytes, ( "Sample data size and buffer size do not match.") # Create the buffer os.system(f"dada_db -a {header_size} -b {buffer_size} -k {KEY_STRING}") print("Buffer created") # Start the reader read = ( "python ./meridian_fringestop.py /home/ubuntu/data/ " "/home/ubuntu/proj/dsa110-shell/dsa110-meridian-fs/dsamfs/data/test_parameters.yaml " "/home/ubuntu/proj/dsa110-shell/dsa110-meridian-fs/dsamfs/data/test_header.txt" ) with open("/home/ubuntu/data/tmp/write.log", 'w') as read_log: with subprocess.Popen(read, shell=True, stdout=read_log, stderr=read_log) as _read_proc: print("Reader started") sleep(0.1) # Write to the buffer writer = Writer(KEY) print('Writer created') for i in range(48): page = writer.getNextPage() data = np.asarray(page) data[...] = vis_temp.view(np.int8) if i < 9: writer.markFilled() else: writer.markEndOfData() vis_temp += 1 # Wait to allow reader to clear pages sleep(1) writer.disconnect() os.system(f"dada_db -d -k {KEY_STRING}")
class DadaManager: def __init__(self, size, key=0xdada, n_readers=1): """ :type size: int :type key: hex :type n_readers: int :param size: size of the dada buffers in bytes :param key: hex dada key """ self.size = size self.key = key self.n_readers = n_readers def setup(self): logger.debug( f"Destroying previous buffers using: dada_db -d -k {self.key} 2>/dev/null" ) os.system(f"dada_db -d -k {self.key} 2>/dev/null") logger.info( f"Creating new buffers using dada_db -b {self.size} -k {self.key} -r {self.n_readers}" ) os.system(f"dada_db -b {self.size} -k {self.key} -r {self.n_readers}") self.writer = Writer() self.writer.connect(int(self.key, 16)) def dump_header(self, header): """ :type header: dict """ return self.writer.setHeader(header) def dump_data(self, data_input): page = self.writer.getNextPage() data = np.asarray(page) data.fill(0) data[:len(data_input)] = data_input def mark_filled(self): return self.writer.markFilled() def eod(self): return self.writer.markEndOfData() def teardown(self): self.writer.disconnect() os.system(f"dada_db -d -k {self.key} 2> /dev/null")
class DadaManager: """ A manager class for `psrdada` writer. Args: size (int): size of each buffer (in bytes) key (hex): hexadecimal dada key n_readers (int): Number of dada readers. """ def __init__(self, size, key=hex(0xDADA), n_readers=1): self.size = size self.key = key self.n_readers = n_readers # cast dictionary based on https://docs.python.org/3/library/struct.html#format-characters self.cast_dict = { "uint8": "B", "uint16": "H", "int16": "h", "float32": "f" } def setup(self): """ Kill any previous buffers with the same key. Set up the dada buffers and connect to a writer. """ logger.debug( f"Destroying previous buffers using: dada_db -d -k {self.key} 2>/dev/null" ) os.system(f"dada_db -d -k {self.key} 2>/dev/null") logger.info( f"Creating new buffers using dada_db -b {self.size} -k {self.key} -r {self.n_readers}" ) os.system( f"dada_db -b {self.size} -k {self.key} -r {self.n_readers} -n 8 -l -p" ) self.writer = Writer() self.writer.connect(int(self.key, 16)) return self def dump_header(self, header): """ Set the psrdada header """ return self.writer.setHeader(header) def dump_data(self, data_input): """ Dump the data to the buffer Args: data_input (numpy.ndarray): Numpy array of the data. """ if str(data_input.dtype) not in self.cast_dict: raise TypeError("Unsupported data type: %s" % data_input.dtype) page = self.writer.getNextPage().cast(self.cast_dict[str( data_input.dtype)]) data = np.asarray(page, dtype=data_input.dtype) data[...] = data_input[...] return self def mark_filled(self): """ Mark that data is filled in the buffer page. """ return self.writer.markFilled() def eod(self): """ Mark the end of data. """ return self.writer.markEndOfData() def teardown(self): """ Disconnect the writer and tear down the buffers. """ self.writer.disconnect() os.system(f"dada_db -d -k {self.key} 2> /dev/null") def __enter__(self): return self.setup() def __exit__(self, exc_type, exc_val, exc_tb): return self.teardown()
class DadaManager: """ A manager class for `psrdada` writer. Args: size (int): size of each buffer (in bytes) key (hex): hexadecimal dada key n_readers (int): Number of dada readers. """ def __init__(self, size, key=0xDADA, n_readers=1): self.size = size self.key = key self.n_readers = n_readers def setup(self): """ Kill any previous buffers with the same key. Set up the dada buffers and connect to a writer. """ logger.debug( f"Destroying previous buffers using: dada_db -d -k {self.key} 2>/dev/null" ) os.system(f"dada_db -d -k {self.key} 2>/dev/null") logger.info( f"Creating new buffers using dada_db -b {self.size} -k {self.key} -r {self.n_readers}" ) os.system( f"dada_db -b {self.size} -k {self.key} -r {self.n_readers} -n 8 -l -p" ) self.writer = Writer() self.writer.connect(int(self.key, 16)) def dump_header(self, header): """ Set the psrdada header """ return self.writer.setHeader(header) def dump_data(self, data_input): """ Dump the data to the buffer Args: data_input (numpy.ndarray): Numpy array of the data. """ page = self.writer.getNextPage() data = np.asarray(page) data.fill(0) data[:len(data_input)] = data_input def mark_filled(self): """ Mark that data is filled in the buffer page. """ return self.writer.markFilled() def eod(self): """ Mark the end of data. """ return self.writer.markEndOfData() def teardown(self): """ Disconnect the writer and tear down the buffers. """ self.writer.disconnect() os.system(f"dada_db -d -k {self.key} 2> /dev/null")
writer = Writer(0xdbda) # send 10 datasets, separated by an EOD for ndataset in range(10): npages = randint(1, 10) # setting a new header also resets the buffer: isEndOfData = False writer.setHeader({ 'DATASET': str(ndataset), 'PAGES': str(npages), 'MAGIC': random_string(20) }) print(writer.header) for npage in range(npages): page = writer.getNextPage() data = np.asarray(page) data.fill(npage) # marking a page filled will send it on the ringbuffer, # and then we cant set the 'EndOfData' flag anymore. # so we need to treat the last page differently if npage < npages - 1: writer.markFilled() else: time.sleep(0.5) # mark the last page with EOD flag # this will also mark it filled, and send it writer.markEndOfData()