When I open a netCDF file with xarray in Python, I open it as a Dataset
object:
ds = xr.open_dataset(file_path)
How do I get the nth time slice of this dataset as a NumPy array?
I know that I can get that if I know the NetCDF variable name, like so:
xvar = ds.data_vars[var_name]
array = xvar.isel(time=n).values
but that requires knowing var_name
, i.e., the NetCDF variable name, which I may not know for all netCDF files.
With iris, this name is available as the attribute var_name
in the resulting Cube
object after loading the netCDF file with iris.load_cube
. How can I get the same variable name in xarray
after loading the netCDF file into an xarray dataset?
Or is there any even simpler way to get the nth time slice of the netCDF file as a NumPy array with xarray?