Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing values in iris cube based on coordinates instead of index #3948

Closed
chuaxr opened this issue Jan 13, 2021 · 2 comments
Closed

Changing values in iris cube based on coordinates instead of index #3948

chuaxr opened this issue Jan 13, 2021 · 2 comments

Comments

@chuaxr
Copy link

chuaxr commented Jan 13, 2021

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

Cross-posted on Stack Overflow.

@trexfeathers
Copy link
Contributor

Hi @chuaxr , we definitely agree with you! There has been much discussion on this topic - see IEP 1 (and the discussion: #1988). There are plans in the near future to focus more attention on IEP's, so hopefully we will see some movement soon.

@rcomer
Copy link
Member

rcomer commented Jan 20, 2021

I think #3172 is also relevant here: however we currently slice our cube, we get a copy. So assigning values to the slice’s data will not affect the original cube. We have to slice the numpy array (i.e. cube.data) instead, to get get a view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants