andreas-h
andreas-h

Reputation: 11110

Access to a Dataset's netCDF4.Dataset

After I load a netCDF4 file using the xr.open_dataset (or xr.open_mfdataset) function, is there any way to access the underlying netCDF4.Dataset objects of the xarray.Dataset?

The problem is that I want to use the wrf-python library, but it only works properly with netCDF4.Dataset objects. And I would prefer only having to touch each file once, i.e., create a xarray.Dataset or a netCDF4.Dataset.

Upvotes: 0

Views: 716

Answers (1)

shoyer
shoyer

Reputation: 9623

This is Python, so of course it's possible to pull out this object :

In [30]: import xarray

In [31]: xarray.Dataset({'foo': 1}).to_netcdf('foo.nc')

In [32]: ds = xarray.open_dataset('foo.nc')

In [37]: ds._file_obj.ds
Out[37]:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    dimensions(sizes):
    variables(dimensions): int64 foo()
    groups:

In [38]: xarray.__version__
Out[38]: '0.9.1'

Do I recommend it? No, this is not a public API, so you should expect it to break without warning in any future release.

I agree that this could be useful, though, so I encourage you to discuss potential APIs on GitHub.

Upvotes: 1

Related Questions