pycontrails.core.coordinates

Coordinates utilities.

Functions

intersect_domain(domain, request)

Return boolean mask of request that are within the bounds of domain.

slice_domain(domain, request[, buffer])

Return slice of domain containing coordinates overlapping request.

pycontrails.core.coordinates.intersect_domain(domain, request)

Return boolean mask of request that are within the bounds of domain.

Parameters:
Returns:

numpy.ndarray – Boolean array of request values within the bounds of domain

Raises:

ValueError – Raises a ValueError when domain has all nan values.

Examples

>>> domain = np.array([3.0, 4.0, 2.0])
>>> request = np.arange(1.0, 6.0)
>>> intersect_domain(domain, request)
array([False,  True,  True,  True, False])
>>> domain = np.array([3.0, np.nan, np.nan])
>>> request = np.arange(1.0, 6.0)
>>> intersect_domain(domain, request)
array([False, False,  True, False, False])
pycontrails.core.coordinates.slice_domain(domain, request, buffer=(0.0, 0.0))

Return slice of domain containing coordinates overlapping request.

Computes index-based slice (to be used with xarray.Dataset.isel() method) as opposed to value-based slice.

Returns slice(None, None) when domain has a length <= 2 or the request has all nan values.

Changed in version 0.24.1: The returned slice is the minimum index-based slice that contains the requested coordinates. In other words, it’s now possible for domain[sl][0] to equal min(request) where sl is the output of this function. Previously, we were guaranteed that domain[sl][0] would be less than min(request).

Parameters:
  • domain (numpy.ndarray) – Full set of domain values

  • request (npt.ArrayLike) – Requested values. Only the nanmin and nanmax values are considered.

  • buffer (tuple[float | np.timedelta64, float | np.timedelta64], optional) – Extend the domain past the requested coordinates by buffer[0] on the low side and buffer[1] on the high side. Units of buffer must be the same as domain.

Returns:

slice – Slice object for slicing out encompassing, or nearest, domain values

Raises:

ValueError – Raises a ValueError when domain has all nan values.

Examples

>>> domain = np.arange(-180, 180, 0.25)
>>> # Call with request as np.array
>>> request = np.linspace(-20, 20, 100)
>>> slice_domain(domain, request)
slice(np.int64(640), np.int64(801), None)
>>> # Call with request as tuple
>>> request = -20, 20
>>> slice_domain(domain, request)
slice(np.int64(640), np.int64(801), None)
>>> # Call with a buffer
>>> request = -16, 13
>>> buffer = 4, 7
>>> slice_domain(domain, request, buffer)
slice(np.int64(640), np.int64(801), None)
>>> # Call with request as a single number
>>> request = -20
>>> slice_domain(domain, request)
slice(np.int64(640), np.int64(641), None)
>>> request = -19.9
>>> slice_domain(domain, request)
slice(np.int64(640), np.int64(642), None)