sarkit.cphd.Writer

class Writer(file, metadata)

Write a CPHD file

A Writer object can be used as a context manager in a with statement.

Parameters:
filefile object

CPHD file to write

metadataMetadata

CPHD metadata to write (copied on construction)

Methods

done()

Warn about unwritten arrays declared in the XML

write_pvp(channel_identifier, pvp_array)

Write pvp data to a CPHD file

write_signal(channel_identifier, signal_array)

Write signal data to a CPHD file

write_support_array(...)

Write support array data to a CPHD file

See also

Reader

Examples

Generate some metadata and data

>>> import lxml.etree

>>> xmltree = lxml.etree.parse("data/example-cphd-1.1.0.xml")
>>> first_channel = xmltree.find("{*}Data/{*}Channel")
>>> ch_id = first_channel.findtext("{*}Identifier")
>>> num_v = int(first_channel.findtext("{*}NumVectors"))
>>> num_s = int(first_channel.findtext("{*}NumSamples"))
>>> sig_format = xmltree.findtext("{*}Data/{*}SignalArrayFormat")

>>> import sarkit.cphd as skcphd

>>> meta = skcphd.Metadata(
...     xmltree=xmltree,
...     file_header_part=skcphd.FileHeaderPart(additional_kvps={"K": "V"}),
... )

>>> import numpy as np

>>> sig = np.zeros((num_v, num_s), dtype=skcphd.binary_format_string_to_dtype(sig_format))
>>> pvps = np.zeros(num_v, dtype=skcphd.get_pvp_dtype(xmltree))

Write a channel’s signal and PVP arrays to a file.

>>> with (tmppath / "written.cphd").open("wb") as f, skcphd.Writer(f, meta) as w:
...     w.write_signal(ch_id, sig)
...     w.write_pvp(ch_id, pvps)