Basic example

Using iterators

Connect to and read from a PSRDada ringbuffer in a pythonic way, using the Iterator interface:

#!/usr/bin/env python3
"""Example program showing how to read from a ringbuffer using an iterator."""
import numpy as np
from psrdada import Reader


def read_untill_end():
    """Connect, read all pages, and disconnect from a ringbuffer."""
    # Create a reader instace and connect to a running ringbuffer
    reader = Reader(0xdada)

    # loop over the pages
    for page in reader:
        # read the page as numpy array
        data = np.asarray(page)
        print(np.sum(data))

    reader.disconnect()


if __name__ == '__main__':
    read_untill_end()

Explicit ringbuffer control

For usecases that require a more finegrained flow control, there is a more C-like API:

#!/usr/bin/env python3
"""Example program showing how to read from a ringbuffer."""
import numpy as np
from psrdada import Reader


def read_untill_end():
    """Connect, read all pages, and disconnect from a ringbuffer."""
    # Create a reader instace and connect to a running ringbuffer
    reader = Reader(0xdada)

    # loop over the pages until EOD is encountered
    while not reader.isEndOfData:
        # read the page as numpy array
        page = reader.getNextPage()

        data = np.asarray(page)
        print(np.sum(data))

        reader.markCleared()

    reader.disconnect()


if __name__ == '__main__':
    read_untill_end()

More examples

For more examples, see the examples and tests directories in the repo. Examples cover writing to the ringbuffer, reading and writing header data, and dealing with EndOfData. EndOfData is used by dada_dbevent to send independent datasets through a ringbuffer, instead of a single continuous stream.