Saving multiple xarray.Datasets to groups in a single HDF5 file #6564
-
I'd like to write multiple For example, when I do this: ds1.to_netcdf("test.nc", "a", group="apple", format="NETCDF4")
ds2.to_netcdf("test.nc", "a", group="banana", format="NETCDF4") I get an error:
But when I change the first save to write instead of append, it works: ds1.to_netcdf("test.nc", "w", group="apple", format="NETCDF4")
ds2.to_netcdf("test.nc", "a", group="banana", format="NETCDF4") This is akward when writing multiple groups in a for-loop since the first group written has to be written with |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
I don't have a solution. I'd just like to point out that this behavior is surprising because it's not consistent with the meaning of For now, consider using try-except if that makes sense within the context. |
Beta Was this translation helpful? Give feedback.
-
Support for multiple groups is one of the reasons why we are building xarray-datatree. It's still just an early prototype, but it should already allow you to save multiple groups like this In [1]: import xarray as xr
In [2]: ds1 = xr.Dataset({'foo': 0})
In [3]: ds2 = xr.Dataset({'bar': 1})
In [4]: from datatree import DataTree
In [5]: dt = DataTree.from_dict({'apple': ds1, 'banana': ds2})
In [6]: dt
Out[6]:
DataTree('None', parent=None)
├── DataTree('apple')
│ Dimensions: ()
│ Data variables:
│ foo int64 0
└── DataTree('banana')
Dimensions: ()
Data variables:
bar int64 1
In [7]: dt.to_netcdf("test.nc", format="NETCDF4")
I don't know of a neater way to do this using only |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
Support for multiple groups is one of the reasons why we are building xarray-datatree.
It's still just an early prototype, but it should already allow you to save multiple groups like this