cf.Domain.indices

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

Create indices that define a subspace of the domain construct.

The indices returned by this method 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.

New in version 3.11.0.

See also

subspace, where, __getitem__, __setitem__

Parameters
mode: str, optional

There are two modes of operation, each of which provides indices for a different type of subspace:

mode

Description

'compress'

Return indices that identify only the requested locations.

This is the default mode.

Note that if a multi-dimensional metadata construct is being used to define the indices then some unrequested locations may also be selected.

'envelope'

The returned subspace is the smallest that contains all of the requested 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]