scippneutron.io.xye.save_xye#
- scippneutron.io.xye.save_xye(fname, da, *, coord=None, header=GenerateHeader, metadata=None, comment='')[source]#
Write a data array to an XYE file.
The input must be 1-dimensional, have variances, and at least one coordinate. It is possible to select which coordinate gets written with the
coordargument.The data are written as an ASCII table with columns X, Y, E, where
X: coordinate of the data array,
Y: data values of the data array,
E: standard deviations corresponding to Y.
This format is lossy, coordinates other than X, attributes, and masks are not written and the coordinate name, dimension name, and units of the input are lost. To improve the situation slightly,
save_xyecan write some description to the top of the file. All lines in the description are prefixed with#.The description is structured like this:
# ScippNeutron XYE file # <comment> # <metadata> # <header>
- Parameters:
fname (
str|Path|TextIO) – Name or file handle of the output file.da (
DataArray) – 1-dimensional data to write.coord (
str|None, default:None) – Coordinate name ofdato write. If omitted anddahas only one coordinate, that coordinate is used. If omitted anddahas multiple coordinates, attempts to use a coordinate with the same name asda.dim. If that does not exist, raise an error.header (
str|GenerateHeaderType, default:GenerateHeader) – String to write at the beginning of the file. A simple table header gets generated automatically by default. Setheader=''to prevent this.metadata (
dict[str,BaseModel|str|list[Any] |dict[str,Any]] |None, default:None) –Optional dictionary of metadata to serialize into the header. The metadata is serialized as YAML. Each entry can be any data that pydantic can serialize.
Note that there is no fixed schema for metadata, so
save_xyewill accept any serializable metadata.comment (
str, default:'') – Optional string to show at the top of the file.
- Return type:
Examples
>>> from scippneutron.io.xye import save_xye >>> from scippneutron.metadata import Beamline >>> # Make test data >>> data = sc.DataArray(sc.arange('x', 4.0), coords={'x': sc.arange('x', 4)}) >>> data.variances = 0.1 * data.values >>> # Serialize >>> save_xye("data.xye", data, metadata={"beamline": Beamline(name="test")})
See also
scippneutron.io.xye.load_xyeFunction to load XYE files.