0

I want to use xr.where to get index:(lat,lon) which contain y<1 from this xarray dataset:

import xarray as xr
y = xr.DataArray(
    0.1 * np.arange(12).reshape(3, 4),
    dims=["lat", "lon"],
    coords={"lat": np.arange(3), "lon": 10 + np.arange(4)},
    name="sst",
)

When I tried

xr.where(y<1,y.lat,y.lon)

output is:

array([[ 0,  0,  0,  0],
       [ 1,  1,  1,  1],
       [ 2,  2, 12, 13]])

That is difficult for me to interpret.

Any suggestions?

Many thanks

Tugiyo
  • 31
  • 6

1 Answers1

1

There are a number of ways to do this in Xarray. Assuming you want a list of (lat, lon) pairs, here is a simple solution that uses a mix of Xarray and Numpy:

lat_i, lon_i = np.nonzero(xr.where(y<1, 1, 0).data)
lats = y.lat.isel(lat=lat_i).data
lons = y.lon.isel(lon=lon_i).data
pairs = list(zip(lats, lons))
pairs

Output:

[(0, 10),
 (0, 11),
 (0, 12),
 (0, 13),
 (1, 10),
 (1, 11),
 (1, 12),
 (1, 13),
 (2, 10),
 (2, 11)]
jhamman
  • 5,867
  • 19
  • 39