slice data python xarray code example
Example 1: how to select subset of data in a dataset using xarray
import xarray as xr
ds = xr.open_dataset(file_name.nc)
ds.sel(dim=slice())
ds.loc[{'dim': slice()}]
ds.where(bool array)
ds[var_name].loc[{'latitude': slice(60,48),
'longitude': slice(-12,5)}]
ds.where(ds[var_name] > 0.1)
Example 2: how to select variables in a dataset using xarray
import xarray as xr
ds = xr.open_dataset(file_name.nc)
selection | syntax | returns
--------------------------------------------------------------
single variable | ds[var_name] | DataArray
--------------------------------------------------------------
single variable | ds[[var_name]] | Dataset
--------------------------------------------------------------
multiple variable | ds[[var_name1, var_name2...]] | Dataset