I have a xarray dataset covering longitudes from 9 to 30 and 54 to 66. How to set all the variables in that dataset from certain coordinate range to -1?
As soon as I do df.isel or df.iloc or df.sel or df.loc, I get a subset of data, which is much smaller than the original dataset. I would like to obtain dataset with original resolution.
Some small example:
#!/usr/bin/env ipython
import xarray as xr
import numpy as np
# --------------------------
xvals = np.arange(9,30,1);nx = np.size(xvals);
yvals = np.arange(53,66,0.5);ny = np.size(yvals);
# -----------------------------------------------
data_a = np.random.random((ny,nx));
data_b = np.random.random((ny,nx));
somedata = xr.Dataset(data_vars={'data_a':(('latc','lonc'),data_a),'data_b':(('latc','lonc'),data_b)},coords={'lonc':('lonc',xvals),'latc':('latc',yvals)})
# -----------------------------------------------
# How to set all data variables in somedata at coordinate range lonc = 15-20 and latc=59-61 to -1e0 using xarray?
I can easily use NumPy:
# -----------------------------------------------
# NumPy:
xm, ym = np.meshgrid(xvals,yvals);
kk = np.where((xm>15) & (ym>59) & (xm<20) &(ym<61))
data_a[kk] = -1.e0;
data_b[kk] = -1.e0;
somedata['data_a'][:] = data_a;
somedata['data_b'][:] = data_b;
somedata.to_netcdf('test.nc')
# -----------------------------------------------
but perhaps there is also a nice way doing this with xarray for all variables?
You've got a couple options. You could use xr.where
or the where
method of the dataset itself, if you want a copy with the replaced data:
newdata = somedata.where((somedata.lonc > 15) & (somedata.lonc < 20) & (somedata.latc > 59) & (somedata.latc < 61), -1.0)
Or if you want to do it in place, you can set data through .loc
directly (using np.s_
to make the slices cleaner):
somedata.loc[{'lonc': np.s_[15:20], 'latc': np.s_[59:61]}] = -1.0