Changing values in iris cube based on coordinates instead of index

530 Views Asked by At

I would like to be able to change values in iris based on the coordinate, instead of the index.

For example, consider the following cube and say that I wish to set values from -45N to 45N and 160E to 240E to 1:

import iris
import numpy as np
from iris.coords import DimCoord
from iris.cube import Cube

latitude_vals = np.linspace(-90, 90, 4)
longitude_vals = np.linspace(45, 360, 8)
latitude = DimCoord(latitude_vals, standard_name="latitude", units="degrees")
longitude = DimCoord(longitude_vals, standard_name="longitude", units="degrees")
cube = Cube(
    np.zeros((4, 8), np.float32), dim_coords_and_dims=[(latitude, 0), (longitude, 1)]
)

In this example, what I want can be done by invoking xarray:

import xarray as xr
da = xr.DataArray.from_iris(cube)
da.loc[dict(latitude=slice(-45, 45), longitude=slice(160, 240))] = 1

But can this be done entirely within iris, without having to resort to specifying the indices manually?

Example of specifying the indices manually:

cube.data[1:3, 3:5] = cube.data[1:3, 3:5] + 1

Update (22 Jan 2021): This is a known issue, see this cross-post and links for related discussion.

0

There are 0 best solutions below