erlab.io.nexusutils¶
Utilities for reading NeXus files into xarray objects.
This module provides functions that can be used to extract coordinates and data from NeXus files and convert them into xarray DataArrays conveniently. All functions in this module require the nexusformat package to be installed.
Functions
|
Generate a dictionary of coordinates from a NeXus group. |
|
Get an NXentry object from a NeXus file. |
|
Get all non-primary coordinates in a group. |
|
Generate a dictionary of primary coordinates from a list of NXfields. |
|
Get all primary coordinates in a group. |
|
Convert a NeXus group to a dictionary. |
|
Convert a coord-like 1D NeXus field to a single |
|
Convert a NeXus group to an xarray DataArray. |
Classes
|
|
|
|
|
|
|
A Mapping is a generic container for associating key/value pairs. |
|
All the operations on a read-only sequence. |
- erlab.io.nexusutils.get_primary_coords(group)[source]¶
Get all primary coordinates in a group.
Retrieves all fields with the attribute
primary=1in the group and its subgroups recursively. The output list is sorted by theaxisattribute of the fields.
- erlab.io.nexusutils.get_non_primary_coords(group)[source]¶
Get all non-primary coordinates in a group.
Retrieves all fields with the attribute
axisin the group and its subgroups recursively. The output list is sorted by the order of traversal.
- erlab.io.nexusutils.get_primary_coord_dict(fields)[source]¶
Generate a dictionary of primary coordinates from a list of NXfields.
The output dictionary contains the absolute NXpath of the field as the key and the value is either a
xarray.DataArrayor a tuple of the associated dimensions, data, and attributes.The output arguments can be directly used as arguments for the
xarray.DataArrayconstructor.If there are multiple fields with the same axis, one of the fields will depend on the other field.
- Parameters:
fields (
listofNXfield) – The list of primary coordinates to be converted. The list can be obtained from a NXgroup usingget_primary_coords().- Returns:
- Return type:
- erlab.io.nexusutils.get_coord_dict(group)[source]¶
Generate a dictionary of coordinates from a NeXus group.
- Parameters:
group (
NXgroup) – The NeXus group from which to extract coordinates.- Returns:
- Return type:
- erlab.io.nexusutils.nexus_group_to_dict(group, exclude, relative=True, replace_slash=True, parse=False)[source]¶
Convert a NeXus group to a dictionary.
This function takes a NeXus group and converts it into a dictionary where the keys are the paths of the group’s items relative to the group’s root path, and the values are the corresponding items.
- Parameters:
group (
NXgroup) – The NeXus group to be converted.exclude (
Sequence[str] |None) – List of paths to exclude from the output.relative (
bool, default:True) – Whether to use the relative or absolute paths of the items. IfTrue, the keys are the paths of the items relative to the path of the group. IfFalse, the keys are the absolute paths of the items relative to the root of the NeXus file.replace_slash (
bool, default:True) – Whether to replace the slashes in the paths with dots.parse (
bool, default:False) – Whether to coerce the values of NXfields to native Python types.
- erlab.io.nexusutils.nxfield_to_xarray(field, no_dims=False)[source]¶
Convert a coord-like 1D NeXus field to a single
xarray.DataArray.
- erlab.io.nexusutils.nxgroup_to_xarray(group, data, without_values=False)[source]¶
Convert a NeXus group to an xarray DataArray.
- Parameters:
group (
NXgroup) – The NeXus group to be converted.data (
strorcallable) –The location of the data values which can be specified in two ways:
without_values (
bool, default:False) – IfTrue, the returned DataArray values will be filled with zeros. Use this to check the coords or attrs quickly without loading in the full data.
- Returns:
DataArray– The DataArray containing the data. Dimension and coordinate names are the relative paths of the corresponding NXfields, with the slashes replaced by dots.- Return type: