VLBIFileReaderBase

class baseband.base.base.VLBIFileReaderBase(fh_raw)[source] [edit on github]

Bases: FileBase

VLBI wrapped file reader base class.

Parameters:
fh_rawfilehandle

Filehandle of the raw binary data file.

Notes

A subclass should define read_header and read_frame methods.

This baseclass includes a base locate_frames method that can search the file for a header patter. It can be overridden by a version that just passes in the relevant pattern.

Also defined is are a find_header method which combines the above with read_header, and a basic get_frame_rate methods which scans the file for headers and determines the maximum frame number that occurs before the jump down (which is taken to be for the next second, as in all standard VLBI formats). This method assumes headers have a ‘frame_nr’ item, and define a payload_nbytes property.

Attributes Summary

fh_raw

info

Standardized information on file readers.

Methods Summary

close()

find_header(*args, **kwargs)

Find the nearest header from the current position.

get_frame_rate([offset])

Determine the number of frames per second.

locate_frames(pattern, *[, mask, ...])

Use a pattern to locate frame starts near the current position.

temporary_offset([offset, whence])

Context manager for temporarily seeking to another file position.

Attributes Documentation

fh_raw = None
info

Standardized information on file readers.

The info descriptor has a number of standard attributes, which are determined from arguments passed in opening the file, from the first header (info.header0) and from possibly scanning the file to determine the duration of frames.

Examples

The most common use is simply to print information:

>>> from baseband.data import SAMPLE_MARK5B
>>> from baseband import mark5b
>>> fh = mark5b.open(SAMPLE_MARK5B, 'rb')
>>> fh.info
Mark5BFile information:
format = mark5b
number_of_frames = 4
frame_rate = 6400.0 Hz
bps = 2
complex_data = False
readable = False

missing:  nchan: needed to determine sample shape, frame rate, ...
          kday, ref_time: needed to infer full times.

>>> fh.close()

>>> fh = mark5b.open(SAMPLE_MARK5B, 'rb', kday=56000, nchan=8)
>>> fh.info
Mark5BFile information:
format = mark5b
number_of_frames = 4
frame_rate = 6400.0 Hz
sample_rate = 32.0 MHz
samples_per_frame = 5000
sample_shape = (8,)
bps = 2
complex_data = False
start_time = 2014-06-13T05:30:01.000000000
readable = True

checks:  decodable: True
>>> fh.close()

Methods Documentation

close() [edit on github]
find_header(*args, **kwargs)[source] [edit on github]

Find the nearest header from the current position.

If successful, the file pointer is left at the start of the header.

Parameters are as for locate_frames.

Returns:
header

Retrieved header.

Raises:
~baseband.base.base.HeaderNotFoundError

If no header could be located.

AssertionError

If the header did not pass verification.

get_frame_rate(offset=0)[source] [edit on github]

Determine the number of frames per second.

The method cycles through headers, starting from the start of the file, finding the largest frame number before it jumps back to 0 for a new second.

Parameters:
offsetint or None, optional

Offset at which to start searching. Default is 0, i.e., the start of the file. Use None to start at the current position.

Returns:
frame_rateQuantity

Frames per second.

Raises:
EOFError

If the file contains less than one second of data.

locate_frames(pattern, *, mask=None, frame_nbytes=None, offset=0, forward=True, maximum=None, check=1)[source] [edit on github]

Use a pattern to locate frame starts near the current position.

Note that the current position is always included.

Parameters:
patternheader, ~numpy.ndaray, bytes, int, or iterable of int

Synchronization pattern to look for. If a header or header class, invariant_pattern() is used to create a masked pattern, using invariant keys from invariants(). If an ndarray or bytes instance, a byte array view is taken. If an (iterable of) int, the integers need to be unsigned 32 bit and will be interpreted as little-endian.

mask~numpy.ndarray, bytes, int, or iterable of int.

Bit mask for the pattern, with 1 indicating a given bit will be used the comparison.

frame_nbytesint, optional

Frame size in bytes. Defaults to the frame size in any header passed in.

offsetint, optional

Offset from the frame start that the pattern occurs. Any offsets inferred from masked entries are added to this (hence, no offset needed when a header is passed in as pattern).

forwardbool, optional

Seek forward if True (default), backward if False.

maximumint, optional

Maximum number of bytes to search away from the present location. Default: search twice the frame size if given, otherwise 1 million (extra bytes to avoid partial patterns will be added). Use 0 to check only at the current position.

checkint or tuple of int, optional

Frame offsets where another sync pattern should be present (if inside the file). Ignored if frame_nbytes is not given. Default: 1, i.e., a sync pattern should be present one frame after the one found (independent of forward), thus helping to guarantee the frame is not corrupted.

Returns:
locationslist of int

Locations of sync patterns within the range scanned, in order of proximity to the starting position.

temporary_offset(offset=None, whence=0) [edit on github]

Context manager for temporarily seeking to another file position.

To be used as part of a with statement:

with fh_raw.temporary_offset() [as fh_raw]:
    with-block

On exiting the with-block, the file pointer is moved back to its original position. As a convenience, one can pass on the offset to seek to when entering the context manager. Parameters are as for io.IOBase.seek().