Skip to content

Saving multiple xarray.Datasets to groups in a single HDF5 file #6564

Answered by TomNicholas
jrmagers asked this question in Q&A
Discussion options

You must be logged in to vote

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")
>…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@jrmagers
Comment options

@mronda
Comment options

@TomNicholas
Comment options

@mronda
Comment options

Answer selected by andersy005
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants