capefile: Reader for CAPE-specific binary files¶
- class cape.dkit.capefile.CapeFile(a: str | dict | IOBase | None = None, meta: bool = False)¶
Interface to simple binary format specific to CAPE
- Call:
>>> cdb = CapeFile(fname=None, meta=False) >>> cdb = CapeFile(fp, meta=False) >>> cdb = CapeFile(data, meta=False)
- Inputs:
- fname:
str Name of file to read
- fp:
io.IOBase File handle open in
rbmode- meta:
True| {False} Option to only read variable names and sizes
- fname:
- Outputs:
- exception cape.dkit.capefile.CapeFileError¶
Base for all exceptions raised by
capefile
- exception cape.dkit.capefile.CapeFileTypeError¶
Exception class for incorrect type in
capefile
- exception cape.dkit.capefile.CapeFileValueError¶
Exception class for incorrect value in
capefile
- cape.dkit.capefile.assert_isinstance(obj, cls_or_tuple, desc=None)¶
Conveniently check types
Applies
isinstance(obj, cls_or_tuple)but also constructs aTypeErrorand appropriate message if test fails.If cls is
None, no checks are performed.- Call:
>>> assert_isinstance(obj, cls, desc=None) >>> assert_isinstance(obj, cls_tuple, desc=None)
- Inputs:
- Raises:
- cape.dkit.capefile.assert_size(v: ndarray, n: int, fp: IOBase, desc=None)¶
Check if array has expected size
- Call:
>>> assert_size(v, n, fp, desc=None)
- Inputs:
- v:
np.ndarray An array
- n:
int Expected size
- fp:
io.IOBase File handle to report position
- desc: {
None} |str Optional text describing v for including in error msg
- v:
- Raises:
- cape.dkit.capefile.assert_value(obj, val_or_tuple, desc=None)¶
Conveniently check values
If val is
None, no checks are performed.- Call:
>>> assert_value(obj, val, desc=None) >>> assert_value(obj, val_tuple, desc=None)
- Inputs:
- Raises:
- cape.dkit.capefile.check_header(fp: IOBase)¶
Check that file has the correct header for file type
- Call:
>>> check_header(fp)
- Inputs:
- fp:
io.IOBase File open for reading bytes (
'rb'or similar)
- fp:
- Raises:
CapeFileValueErrorif file does not have correct header
- cape.dkit.capefile.genr8_recordtype(v, name: str | None = None) uint32¶
Get capefile record type code based on Python type
- Call:
>>> rt = genr8_recordtype(v, name=None)
- Inputs:
- v:
object Any value to analyze type
- name: {
None} |str Title for v, used to determine “title” bit in rt
- v:
- Outputs:
- rt:
np.uint32 Record type code
- rt:
- cape.dkit.capefile.read_nrecord(fp: IOBase) uint64¶
Read number of records in file
- Call:
>>> nr = read_nrecord(fp)
- Inputs:
- fp:
io.IOBase File handle
- fp:
- Outputs:
- nr:
np.uint64 Number of records
- nr:
- cape.dkit.capefile.write_record(fp: IOBase, v, name: str | None = None) uint64¶
Write a record to file
- Call:
>>> size = write_record(fp, v, name=None)
- Inputs:
- fp:
io.IOBase A file open for writing bytes
- v:
object Object to be written to file
- name: {
None} |str Optional title to save v as
- fp:
- Outputs:
- size:
np.uint64 Number of bytes written
- size: