Skip to content
Advertisement

rioaxrray open netcdf file result is a list not an xarray

I am trying to open a netcdf file using rioarray:

import rioxarray
import xarray
import raster

xds = rioxarray.open_rasterio(file, crs=’+proj=latlong’, masked=True)

but:

type(xds)

list

and xds has none of the attributes or methods of an xarray.

xds_lonlat = xds.rio.reproject(“epsg:4326”)


AttributeError Traceback (most recent call last) in —-> 1 xds_lonlat = xds.rio.reproject(“epsg:4326”)

AttributeError: ‘list’ object has no attribute ‘rio’

clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)


AttributeError Traceback (most recent call last) in —-> 1 clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)

AttributeError: ‘list’ object has no attribute ‘rio’

Can anyone advise?

Advertisement

Answer

I recently encountered this when I was opening a netCDF (with rioxarray) that had multiple variables. Since it returns a list, you would not expect it to have any of the rioxarray attributes or methods.

The documentation for the function is here: https://corteva.github.io/rioxarray/stable/rioxarray.html

One of the return types is List[xarray.Dataset], so I think this behavior is expected.

My guess is that you want one of the entries in the list like xds=xds[0], though it’s hard to know without having more information about the file that you are opening.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement