shnitsel.core.xrhelpers¶
Functions¶
|
Replaces each occurence of to_replace in da with the corresponding element of value. |
|
|
|
|
|
|
|
|
|
Assign new values to levels of MultiIndexes in |
|
Opens a NetCDF4 file saved by shnitsel-tools, specially interpreting certain attributes. |
|
Save a |
|
|
|
|
|
Group a Dataset or DataArray by several levels of a MultiIndex it contains. |
|
|
|
Select trajectories using a list of trajectories IDs or a boolean mask |
|
Will not generally return trajectories in order given |
|
Unstack the |
|
Stack the |
Module Contents¶
- replace_total(da, to_replace, value)¶
Replaces each occurence of to_replace in da with the corresponding element of value. Replacement must be total, i.e. every element of da must be in to_replace. This permits a change of dtype between to_replace and value. This function is based on the snippets at https://github.com/pydata/xarray/issues/6377
- Parameters:
da (xarray.DataArray) – An xr.DataArray
to_replace (numpy.ndarray | list) – Values to replace
value (numpy.ndarray | list) – Values with which to replace them
- Return type:
An xr.DataArray with dtype matching value.
- midx_combs(values, name=None)¶
- flatten_midx(obj, idx_name, renamer=None)¶
- Parameters:
obj (xarray.Dataset | xarray.DataArray)
idx_name (str)
renamer (callable | None)
- Return type:
- flatten_levels(obj, idx_name, levels, new_name=None, position=0, renamer=None)¶
- Parameters:
obj (xarray.Dataset | xarray.DataArray)
idx_name (str)
levels (Sequence[str])
new_name (str | None)
position (int)
renamer (Callable | None)
- Return type:
- expand_midx(obj, midx_name, level_name, value)¶
- Parameters:
obj (xarray.Dataset | xarray.DataArray)
- Return type:
- assign_levels(obj, levels=None, **levels_kwargs)¶
Assign new values to levels of MultiIndexes in
obj- Parameters:
obj (xarray.Dataset | xarray.DataArray) – An
xarrayobject with at least one MultiIndexlevels (dict[str, numpy.typing.ArrayLike] | None) – A mapping whose keys are the names of the levels and whose values are the levels to assign. The mapping will be passed to
xarray.DataArray.assign_coords()(or thexarray.Datasetequivalent).optional – A mapping whose keys are the names of the levels and whose values are the levels to assign. The mapping will be passed to
xarray.DataArray.assign_coords()(or thexarray.Datasetequivalent).levels_kwargs (numpy.typing.ArrayLike)
- Return type:
A new object with the new level values replacing the old level values.
- Raises:
ValueError – If levels are provided in both keyword and dictionary form.
- open_frames(path)¶
Opens a NetCDF4 file saved by shnitsel-tools, specially interpreting certain attributes.
- Parameters:
path – The path of the file to open.
- Return type:
An
xarray.Datasetwith any MultiIndex restored.- Raises:
FileNotFoundError – If there is is nothing at
path, orpathis not a file.ValueError (or other exception) – Raised by the underlying h5netcdf engine if the file is corrupted.
- save_frames(frames, path, complevel=9)¶
Save a
Dataset, presumably (but not necessarily) consisting of frames of trajectories, to a file atpath.- Parameters:
accessor) (frames (omit if using) – The
Datasetto savepath – The path at which to save it
complevel – The level of
gzipcompression which will be applied to all variables in theDataset, by default 9optional – The level of
gzipcompression which will be applied to all variables in theDataset, by default 9
Notes
This function/accessor method wraps
xarray.Dataset.to_netcdf()but notnumpy.any().
- split_for_saving(frames, bytes_per_chunk=50000000.0)¶
- save_split(frames, path_template, bytes_per_chunk=50000000.0, complevel=9, ignore_errors=False)¶
- mgroupby(obj, levels)¶
Group a Dataset or DataArray by several levels of a MultiIndex it contains.
- Parameters:
obj (xarray.Dataset | xarray.DataArray) – The
xrobject to grouplevels (Sequence[str]) – Names of MultiIndex levels all belonging to the same MultiIndex
- Returns:
The grouped object, which behaves as documented at
xr.Dataset.groupby()and xr.DataArray.groupby with the caveat that the specified levels have been
”flattened” into a single Multiindex level of tuples.
- Raises:
ValueError – If no MultiIndex is found, or if the named levels belong to different MultiIndexes.
- Return type:
xarray.core.groupby.DataArrayGroupBy | xarray.core.groupby.DatasetGroupBy
Warning
The function does not currently check whether the levels specified are really levels of a MultiIndex, as opposed to names of non-MultiIndex indexes.
- msel(obj, **kwargs)¶
- Parameters:
obj (xarray.Dataset | xarray.DataArray)
- Return type:
- sel_trajs(frames, trajids_or_mask, invert=False)¶
Select trajectories using a list of trajectories IDs or a boolean mask
- Parameters:
frames (xarray.Dataset | xarray.DataArray) – The
xr.Datasetfrom which a selection is to be drawntrajids_or_mask (Sequence[int] | Sequence[bool]) –
- Either
A sequences of integers representing trajectory IDs to be included, in which case the trajectories may not be returned in the order specified.
Or a sequence of booleans, each indicating whether the trajectory with an ID in the corresponding entry in the
Dataset’strajid_coordinate should be included
invert – Whether to invert the selection, i.e. return those trajectories not specified, by default False
optional – Whether to invert the selection, i.e. return those trajectories not specified, by default False
- Return type:
A new
xr.Datasetcontaining only the specified trajectories- Raises:
NotImplementedError – when an attempt is made to index an
xr.Datsetwithout atrajid_dimension/coordinate using a boolean maskTypeError – If
trajids_or_maskhas a dtype other than integer or boolean
- sel_trajids(frames, trajids, invert=False)¶
Will not generally return trajectories in order given
- Parameters:
frames (xarray.Dataset)
trajids (numpy.typing.ArrayLike)
- Return type:
- unstack_trajs(frames)¶
Unstack the
frameMultiIndex so thattrajidandtimebecome separate dims. Wraps thexarray.Dataset.unstack()method.- Parameters:
frames (xarray.Dataset | xarray.DataArray) – An
xarray.Datasetwith aframedimension associated with a MultiIndex coordinate with levels namedtrajidandtime. The Dataset may also have atrajid_dimension used for variables and coordinates that store information pertaining to each trajectory in aggregate; this will be aligned along thetrajiddimension of the unstacked Dataset.- Returns:
An
xarray.Datasetwith independenttrajidandtimedimensions.
- Return type:
- stack_trajs(unstacked)¶
Stack the
trajidandtimedims of an unstacked Dataset into a MultiIndex along a new dimension calledframe. Wraps thexarray.Dataset.stack()method.- Parameters:
frames – An
xarray.Datasetwith independenttrajidandtimedimensions.unstacked (xarray.Dataset | xarray.DataArray)
- Returns:
An
xarray.Datasetwith aframedimension associated witha MultiIndex coordinate with levels named
trajidandtime. Those variablesand coordinates which only depended on one of
trajidor
timebut not the other in the unstacked Dataset, will be aligned along newdimensions named
trajid_andtime_. The new dimensionstrajid_andtime_will be independent of theframedimension and itstrajidandtimelevels.
- Return type: