cf.Field.__getitem__¶
-
Field.
__getitem__
(indices)[source]¶ Return a subspace of the field defined by metadata values
Subspacing by axis indices uses an extended Python slicing syntax, which is similar to numpy array indexing:
>>> f.shape (12, 73, 96) >>> f[...].shape (12, 73, 96) >>> f[slice(0, 12), :, 10:0:-2].shape (12, 73, 5) >>> f[..., f.coord('longitude')<180].shape (12, 73, 48)
There are three extensions to the numpy indexing functionality:
Size 1 axes are never removed.
An integer index i takes the i-th element but does not reduce the rank of the output array by one:
>>> f.shape (12, 73, 96) >>> f[0].shape (1, 73, 96) >>> f[3, slice(10, 0, -2), 95:93:-1].shape (1, 5, 2)
The indices for each axis work independently.
When more than one axis’s slice is a 1-d boolean sequence or 1-d sequence of integers, then these indices work independently along each axis (similar to the way vector subscripts work in Fortran), rather than by their elements:
>>> f.shape (12, 73, 96) >>> f[:, [0, 72], [5, 4, 3]].shape (12, 2, 3)
Note that the indices of the last example would raise an error when given to a numpy array.
Boolean indices may be any object which exposes the numpy array interface, such as the field’s coordinate objects:
>>> f[:, f.coord('latitude')<0].shape (12, 36, 96)
f.__getitem__(indices) <==> f[indices]
New in version 2.0.
Examples 1: >>> g = f[..., 0, :6, 9:1:-2, [1, 3, 4]]
Returns: out: cf.{+Variable}