scippneutron.io.cif.Block#

class scippneutron.io.cif.Block(name, content=None, *, comment='', schema=None)#

A CIF data block.

A block contains an ordered sequence of loops and chunks (groups of key-value-pairs). The contents are written to file in the order specified in the block.

__init__(name, content=None, *, comment='', schema=None)#

Create a new CIF data block.

Parameters:
  • name (str) – Name of the block. Can contain any non-whitespace characters. Can be at most 75 characters long.

  • content (Optional[Iterable[Union[Mapping[str, Any], Loop, Chunk]]], default: None) – Initial loops and chunks. dicts are converted to scippneutron.io.cif.Chunk.

  • comment (str, default: '') – Optional comment that can be written above the block in the file.

  • schema (Union[CIFSchema, Iterable[CIFSchema], None], default: None) – CIF Schema used for the block. Content is not checked against the schema, but the schema is written to the file.

Methods

__init__(name[, content, comment, schema])

Create a new CIF data block.

add(content, /[, comment])

Add a chunk or loop to the end of the block.

add_powder_calibration(cal, *[, comment])

Add a powder calibration table.

add_reduced_powder_data(data, *[, comment])

Add a loop with reduced powder data.

write(f)

Write this block to a file.

Attributes

comment

Optional comment that can be written above the block in the file.

name

Name of the block.

schema

CIF Schema used for the block.

add(content, /, comment='')#

Add a chunk or loop to the end of the block.

Parameters:
  • content (Union[Mapping[str, Any], Iterable[tuple[str, Any]], Chunk, Loop]) – A loop, chunk, or mapping to add. Mappings get converted to chunks.

  • comment (str, default: '') – Optional comment that can be written above the chunk or loop in the file.

Return type:

None

add_powder_calibration(cal, *, comment='')#

Add a powder calibration table.

The calibration data encode the following transformation from d-spacing to time-of-flight:

\[t = \sum_{i=0}^N\, c_i d^{p_i}\]

where \(c_i\) is the i-th element of cal and \(p^{p_i}\) is the i-th element of cal.coords['power'].

Parameters:
  • cal (DataArray) – The data are the calibration coefficients (possibly with variances). Must have a coordinate called 'power' defining \(p\) in the equation above.

  • comment (str, default: '') – Optional comment that can be written above the data in the file.

Return type:

None

Examples

Add a mockup calibration table:

>>> import scipp as sc
>>> from scippneutron.io import cif
>>> cal = sc.DataArray(
...     sc.array(dims=['cal'], values=[3.4, 0.2]),
...     coords={'power': sc.array(dims=['cal'], values=[0, 1])},
... )
>>> block = cif.Block('powder-calibration')
>>> block.add_powder_calibration(cal)
add_reduced_powder_data(data, *, comment='')#

Add a loop with reduced powder data.

The input must be 1-dimensional with a dimension name in ('tof', 'dspacing'). The data array may also have a name in ('intensity_net', 'intensity_norm', 'intensity_total'). If the name is not set, it defaults to 'intensity_net'.

The data gets written as intensity along a single coord whose name matches the dimension name. Standard uncertainties are also written if present.

The unit of the coordinate must match the requirement of pdCIF.

Parameters:
  • data (DataArray) – 1-dimensional data array with a recognized dimension name

  • comment (str, default: '') – Optional comment that can be written above the data in the file.

Return type:

None

Examples

Make mockup powder diffraction data:

>>> import scipp as sc
>>> tof = sc.array(dims=['tof'], values=[1.2, 1.4, 2.3], unit='us')
>>> intensity = sc.array(
...     dims=['tof'],
...     values=[13.6, 26.0, 9.7],
...     variances=[0.7, 1.1, 0.5],
... )

Add to a block:

>>> from scippneutron.io import cif
>>> block = cif.Block('reduced-data')
>>> da = sc.DataArray(intensity, coords={'tof': tof})
>>> block.add_reduced_powder_data(da)
property comment: str#

Optional comment that can be written above the block in the file.

property name: str#

Name of the block.

property schema: set[CIFSchema]#

CIF Schema used for the block.

write(f)#

Write this block to a file.

Used mainly internally, use scippneutron.io.cif.save_cif() instead.

Parameters:

f (TextIOBase) – File handle.

Return type:

None