cf.Domain.indices

Domain.indices(*config, **kwargs)[source]

Create indices that define a subspace of the domain construct.

The indices returned by this method may be used to create the subspace by passing them to the subspace method of the original domain construct.

The subspace is defined by identifying indices based on the metadata constructs.

Metadata constructs are selected conditions are specified on their data. Indices for subspacing are then automatically inferred from where the conditions are met.

Metadata constructs and the conditions on their data are defined by keyword parameters.

  • Any domain axes that have not been identified remain unchanged.

  • Multiple domain axes may be subspaced simultaneously, and it doesn’t matter which order they are specified in.

  • Explicit indices may also be assigned to a domain axis identified by a metadata construct, with either a Python slice object, or a sequence of integers or booleans.

  • For a dimension that is cyclic, a subspace defined by a slice or by a Query instance is assumed to “wrap” around the edges of the data.

  • Conditions may also be applied to multi-dimensional metadata constructs. The “compress” mode is still the default mode (see the positional arguments), but because the indices may not be acting along orthogonal dimensions, some missing data may still need to be inserted into the field construct’s data.

Halos

If a halo is defined via a positional argument, then each subspaced axis will be extended to include that many extra elements at each “side” of the axis. The number of extra elements will be automatically reduced if including the full amount defined by the halo would extend the subspace beyond the axis limits.

New in version 3.11.0.

See also

subspace, where, __getitem__, __setitem__, cf.Field.indices

Parameters
config: optional

Configure the subspace by specifying the mode of operation (mode) and any halo to be added to the subspaced axes (halo), with positional arguments in the format mode, or halo, or mode, halo, or with no positional arguments at all.

A mode of operation is given as a str, and a halo as a non-negative int (or any object that can be converted to one):

mode

Description

Not provided

If no positional arguments are provided then assume the 'compress' mode of operation with no halo added to the subspaced axes.

mode

Define the mode of operation with no halo added to the subspaced axes.

mode, halo

Define a mode of operation, as well as a halo to be added to the subspaced axes.

halo

Assume the 'compress' mode of operation and define a halo to be added to the subspaced axes.

Valid modes are:

  • 'compress' This is the default.

    Unselected locations are removed to create the subspace. If the result is not hyperrectangular then the minimum amount of unselected locations required to make it so will also be specially selected.

  • 'envelope'

    The subspace is the smallest hyperrectangular subspace that contains all of the selected locations.

kwargs: optional

A keyword name is an identity of a metadata construct, and the keyword value provides a condition for inferring indices that apply to the dimension (or dimensions) spanned by the metadata construct’s data. Indices are created that select every location for which the metadata construct’s data satisfies the condition.

Returns
dict

A dictionary of indices, keyed by the domain axis construct identifiers to which they apply.

Examples

>>> d = cf.example_field(0).domain
>>> print(d)
Dimension coords: latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(8) = [22.5, ..., 337.5] degrees_east
                : time(1) = [2019-01-01 00:00:00]
>>> indices = d.indices(X=112.5)
>>> indices
{'domainaxis0': slice(0, 5, 1),
 'domainaxis1': slice(2, 3, 1),
 'domainaxis2': slice(0, 1, 1)}
>>> print(d.subspace(**indices))
Dimension coords: latitude(5) = [-75.0, ..., 75.0] degrees_north
                : longitude(1) = [112.5] degrees_east
                : time(1) = [2019-01-01 00:00:00]
>>> indices = d.indices(X=112.5, Y=cf.wi(-60, 30))
>>> indices
{'domainaxis0': slice(1, 3, 1),
 'domainaxis1': slice(2, 3, 1),
 'domainaxis2': slice(0, 1, 1)}
>>> print(d.subspace(**indices))
Dimension coords: latitude(2) = [-45.0, 0.0] degrees_north
                : longitude(1) = [112.5] degrees_east
                : time(1) = [2019-01-01 00:00:00]
>>> d.indices(X=[-1, 0], Y=slice(1, -1))
{'domainaxis0': slice(1, 4, 1),
 'domainaxis1': slice(7, None, -7),
 'domainaxis2': slice(0, 1, 1)}
>>> print(print(d.subspace(**indices)))
Dimension coords: latitude(3) = [-45.0, 0.0, 45.0] degrees_north
                : longitude(2) = [337.5, 22.5] degrees_east
                : time(1) = [2019-01-01 00:00:00]