cf.Field.indices

Field.indices(*arg, **kwargs)[source]

Return the data array indices which correspond to one dimensional coordinate values.

If no coordinate values are specified for an axis then a full slice (slice(None)) is assumed for that axis.

Values for size 1 axes which are not spanned by the field’ data array may be specified, but only indices for axes which span the field’s data array will be returned.

The coordinate value conditions may be given in any order.

Parameters :
arg : str, optional

By default keywords are considered to be an abbreviations of one-dimensional coordinate object identities. If arg is 'exact' then keywords are considered to be unabbreviated one-dimensional coordinate object identities. If arg is 'regex' then keywords are considered to be regular expressions supported by the re module and are compared with one-dimensional coordinate object identities via the re.search function. If, however, a keyword is a domain identifier, then arg is ignored for that keyword.

kwargs : optional

Keyword names identify one-dimensional coordinate object and keyword values specify tests on their data array values. For each item’s axis, an index is created which selects data array elements where the test is passed. Each keyword/value pair is specified as follows:

keyword:

A string which selects a unique, one-dimensional coordinate object by comparison with its identity, as returned by its identity method; or the coordinate object may be selected by its domain identifier (such as 'dim1', 'aux0', etc.).

By default it is assumed to be an abbreviation. See the exact argument.

value:

Any object specifying a condition for the coordinate object’s to be tested against. Field data array elements are selected where the coordinate object’s data array equals the value.

By default, each data array axis without an item specification is assigned the full slice slice(None).

Example: To create indices to a field’s data array which select elements where its latitude coordinate values are greater than 60 degrees north you could set latitude=cf.ge(60, 'degrees_N').

Returns :

out : tuple

See also

setdata, subspace

Examples

These examples use the following field, which includes a dimension coordinate object with no identity (ncvar:model_level_number) and which has a data array which doen’t span all of the domain axes:

>>> print f
air_temperature field summary
-----------------------------
Data            : air_temperature(ncvar:model_level_number(19), latitude(73), longitude(96)) K
Dimensions      : ncvar:model_level_number(19) = [1, ... 19]
                : latitude(73) = [-90.0, ..., 90.0] degrees_north
                : longitude(96) = [0.0, ..., 356.25] degrees_east
                : time(1) = [ 2004-06-01 00:00:00] noleap calendar
>>> f.items()
{'dim0': <CF DimensionCoordinate: time(1) days since 2004-01-01 noleap calendar>,
 'dim1': <CF DimensionCoordinate: ncvar:model_level_number(19)>,
 'dim2': <CF DimensionCoordinate: latitude(73), degree_north>,
 'dim3': <CF DimensionCoordinate: latitude(96), degree_east>}
>>> f.indices(lat=0.0, lon=0.0)
(slice(0, 19, 1), slice(0, 1, 1), slice(0, 1, 1))
>>> f.indices(lon=cf.wi(0, 3.75), lat=cf.lt(0.0))
(slice(0, 19, 1), slice(0, 36, 1), slice(0, 2, 1))
>>> f.indices(lat=cf.lt(0.0), lon=cf.set([0, 356.25]))
(slice(0, 19, 1), slice(0, 36, 1), slice(0, 96, 96))
>>> f.indices(lat=cf.lt(0.0), lon=cf.set([0, 356.25, 3.75]))
(slice(0, 19, 1), slice(0, 32, 1), [0, 1, 95])
>>> f.indices(dim1=cf.wi(6, 18))
(slice(5, 19, 1), slice(0, 73, 1), slice(0, 96, 1))
>>> f.indices(dim1=19, lon=cf.ne(0, 'degrees_east'))
(slice(18, 19, 1), slice(0, 73, 1), slice(1, 96, 1))
>>> f.indices('exact', latitude=cf.lt(0.0), longitude=0)
(slice(0, 19, 1), slice(0, 32, 1), slice(0, 1, 1))
>>> f.indices('exact', lat=cf.lt(0.0), long=0)
ValueError: Can't find indices: Ambiguous indentity: 'lat'
>>> f.indices('regex', lat=cf.lt(0.0), long'=0)
(slice(0, 19, 1), slice(0, 32, 1), slice(0, 1, 1))

Previous topic

cf.Field.identity

Next topic

cf.Field.insert_aux

This Page