Exemplo n.º 1
0
    def setUp(self):
        """Test setup."""
        os.system("dada_db -d 2> /dev/null ; dada_db -k dada")

        self.writer = Writer()
        self.writer.connect(0xdada)

        self.reader = Reader()
        self.reader.connect(0xdada)
Exemplo n.º 2
0
 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}")
     self.writer = Writer()
     self.writer.connect(int(self.key, 16))
Exemplo n.º 3
0
 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))
Exemplo n.º 4
0
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}")
Exemplo n.º 5
0
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")
Exemplo n.º 6
0
class TestReadWriteHeader(unittest.TestCase):
    """
    Test for reading and writing header data.

    Start a ringbuffer instance and write some data to the header block,
    then read it back.
    """
    def setUp(self):
        """Test setup."""
        os.system("dada_db -d 2> /dev/null ; dada_db -k dada")

        self.writer = Writer()
        self.writer.connect(0xdada)

        self.reader = Reader()
        self.reader.connect(0xdada)

    def tearDown(self):
        """Test teardown."""
        self.writer.disconnect()
        self.reader.disconnect()

        os.system("dada_db -d -k dada 2> /dev/null")

    def test_writing_header(self):
        """
        Header reading and writing test.

        Read a previously written header from the ringbuffer,
        and test if the headers are equal.
        """
        self.writer.setHeader(HEADER_TEST_DATA)
        header = self.reader.getHeader()

        del header['__RAW_HEADER__']
        self.assertDictEqual(header, HEADER_TEST_DATA)
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
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")
Version without iterors
"""
import time
from random import randint, choice
from string import ascii_lowercase
import numpy as np

from psrdada import Writer


def random_string(length=10):
    """Generate a random string of given length."""
    return ''.join(choice(ascii_lowercase) for i in range(length))


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()