Mark4FileReader

class baseband.mark4.base.Mark4FileReader(fh_raw, ntrack=None, decade=None, ref_time=None)[source] [edit on github]

Bases: baseband.vlbi_base.base.VLBIFileReaderBase

Simple reader for Mark 4 files.

Wraps a binary filehandle, providing methods to help interpret the data, such as locate_frame, `read_frame and get_frame_rate.

Parameters:

fh_raw : filehandle

Filehandle of the raw binary data file.

ntrack : int or None, optional.

Number of Mark 4 bitstreams. Can be determined automatically as part of locating the first frame.

decade : int or None

Decade in which the observations were taken. Can instead pass an approximate ref_time.

ref_time : Time or None

Reference time within 4 years of the observation time. Used only if decade is not given.

Attributes Summary

info Standardized information on Mark 4 file readers.

Methods Summary

close()
determine_ntrack([maximum]) Determines the number of tracks, by seeking the next frame.
find_header([forward, maximum]) Find the nearest header from the current position.
get_frame_rate() Determine the number of frames per second.
locate_frame([forward, maximum]) Locate the frame nearest the current position.
read_frame([verify]) Read a single frame (header plus payload).
read_header() Read a single header from the file.

Attributes Documentation

info

Standardized information on Mark 4 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. Mark4FileReaderInfo has two additional attributes specific to Mark 4 files (ntrack and offset0, see below).

Examples

The most common use is simply to print information:

>>> from baseband.data import SAMPLE_MARK4
>>> from baseband import mark4
>>> fh = mark4.open(SAMPLE_MARK4, 'rb')
>>> fh.info
File information:
format = mark4
frame_rate = 400.0 Hz
sample_rate = 32.0 MHz
samples_per_frame = 80000
sample_shape = (8,)
bps = 2
complex_data = False
offset0 = 2696

missing:  decade, ref_time: needed to infer full times.

>>> fh.close()

>>> fh = mark4.open(SAMPLE_MARK4, 'rb', decade=2010)
>>> fh.info
File information:
format = mark4
frame_rate = 400.0 Hz
sample_rate = 32.0 MHz
samples_per_frame = 80000
sample_shape = (8,)
bps = 2
complex_data = False
start_time = 2014-06-16T07:38:12.475000000
offset0 = 2696
>>> fh.close()

Attributes

format (str or None) File format, or None if the underlying file cannot be parsed.
frame_rate (Quantity) Number of data frames per unit of time.
sample_rate (Quantity) Complete samples per unit of time.
samples_per_frame (int) Number of complete samples in each frame.
sample_shape (tuple) Dimensions of each complete sample (e.g., (nchan,)).
bps (int) Number of bits used to encode each elementary sample.
complex_data (bool) Whether the data are complex.
start_time (Time) Time of the first complete sample.
ntrack (int) Number of “tape tracks” simulated in the disk file.
offset0 (int) Offset in bytes from the start of the file to the location of the first header.
missing (dict) Entries are keyed by names of arguments that should be passed to the file reader to obtain full information. The associated entries explain why these arguments are needed. For Mark 4, the possible entries are decade and ref_time.

Methods Documentation

close() [edit on github]
determine_ntrack(maximum=None)[source] [edit on github]

Determines the number of tracks, by seeking the next frame.

Uses find_frame to look for the first occurrence of a frame from the current position for all supported ntrack values. Returns the first ntrack for which find_frame is successful, setting the file’s ntrack property appropriately, and leaving the file pointer at the start of the frame.

Parameters:

maximum : int, optional

Maximum number of bytes forward to search through. Default: twice the frame size (20000 * ntrack // 8).

Returns:

ntrack : int or None

Number of Mark 4 bitstreams. None if no frame was found.

find_header(forward=True, maximum=None)[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:

forward : bool, optional

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

maximum : int, optional

Maximum number of bytes forward to search through. Default: twice the frame size (20000 * ntrack // 8).

Returns:

header : Mark4Header or None

Retrieved Mark 4 header, or None if nothing found.

get_frame_rate()[source] [edit on github]

Determine the number of frames per second.

The frame rate is calculated from the time elapsed between the first two frames, as inferred from their time stamps.

Returns:

frame_rate : Quantity

Frames per second.

locate_frame(forward=True, maximum=None)[source] [edit on github]

Locate the frame nearest the current position.

The search is for the following pattern:

  • 32*tracks bits set at offset bytes
  • 1*tracks bits unset before offset
  • 32*tracks bits set at offset+2500*tracks bytes

This reflects ‘sync_pattern’ of 0xffffffff for a given header and one a frame ahead, which is in word 2, plus the lsb of word 1, which is ‘system_id’.

If the file does not have ntrack is set, it will be auto-determined.

Parameters:

forward : bool, optional

Whether to search forwards or backwards. Default: True.

maximum : int, optional

Maximum number of bytes forward to search through. Default: twice the frame size (20000 * ntrack // 8).

Returns:

offset : int or None

Byte offset of the next frame. None if the search was not successful.

read_frame(verify=True)[source] [edit on github]

Read a single frame (header plus payload).

Returns:

frame : Mark4Frame

With .header and .data properties that return the Mark4Header and data encoded in the frame, respectively.

verify : bool, optional

Whether to do basic checks of frame integrity. Default: True.

read_header()[source] [edit on github]

Read a single header from the file.

Returns:header : Mark4Header