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
andget_frame_rate
.- Parameters
- fh_rawfilehandle
Filehandle of the raw binary data file.
- ntrackint or None, optional.
Number of Mark 4 bitstreams. Can be determined automatically as part of locating the first frame.
- decadeint 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
(self)determine_ntrack
(self[, maximum])Determines the number of tracks, by seeking the next frame.
find_header
(self[, forward, maximum])Find the nearest header from the current position.
get_frame_rate
(self)Determine the number of frames per second.
locate_frame
(self[, forward, maximum])Locate the frame nearest the current position.
read_frame
(self[, verify])Read a single frame (header plus payload).
read_header
(self)Read a single header from the file.
temporary_offset
(self)Context manager for temporarily seeking to another file position.
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. This class has two additional attributes specific to Mark 4 files (ntrack
andoffset0
, 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 readable = True offset0 = 2696 missing: decade, ref_time: needed to infer full times. errors: start_time: unsupported operand type(s) for //: 'NoneType' and 'int' >>> 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 readable = True offset0 = 2696 >>> fh.close()
- Attributes
- formatstr 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_frameint
Number of complete samples in each frame.
- sample_shapetuple
Dimensions of each complete sample (e.g.,
(nchan,)
).- bpsint
Number of bits used to encode each elementary sample.
- complex_databool
Whether the data are complex.
- start_time
Time
Time of the first complete sample.
- ntrackint
Number of “tape tracks” simulated in the disk file.
- offset0int
Offset in bytes from the start of the file to the location of the first header.
- readablebool
Whether the first sample could be read and decoded.
- missingdict
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
andref_time
.- errorsdict
Any exceptions raised while trying to determine attributes. Keyed by the attributes.
- formatstr or
Methods Documentation
-
close
(self) [edit on github]¶
-
determine_ntrack
(self, maximum=None)[source] [edit on github]¶ Determines the number of tracks, by seeking the next frame.
Uses
locate_frame
to look for the first occurrence of a frame from the current position for all supportedntrack
values. Returns the firstntrack
for whichlocate_frame
is successful, setting the file’sntrack
property appropriately, and leaving the file pointer at the start of the frame.- Parameters
- maximumint, optional
Maximum number of bytes forward to search through. Default: twice the frame size (
20000 * ntrack // 8
).
- Returns
- ntrackint or None
Number of Mark 4 bitstreams.
None
if no frame was found.
-
find_header
(self, 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
- Returns
- header
Mark4Header
or None Retrieved Mark 4 header, or
None
if nothing found.
- header
-
get_frame_rate
(self)[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.
- frame_rate
-
locate_frame
(self, 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
- forwardbool, optional
Whether to search forwards or backwards. Default:
True
.- maximumint, optional
Maximum number of bytes forward to search through. Default: twice the frame size (
20000 * ntrack // 8
).
- Returns
-
read_frame
(self, verify=True)[source] [edit on github]¶ Read a single frame (header plus payload).
- Returns
- frame
Mark4Frame
With
.header
and.data
properties that return theMark4Header
and data encoded in the frame, respectively.- verifybool, optional
Whether to do basic checks of frame integrity. Default:
True
.
- frame
-
read_header
(self)[source] [edit on github]¶ Read a single header from the file.
- Returns
- header
Mark4Header
- header
-
temporary_offset
(self) [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.