Skip to content

Create a conventional netcdf using to_netcdf() #7329

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

You must be logged in to vote

I'm having trouble understanding how to write a netcdf4 file with 2 groups (two sets with different dimensions) using the xarray.to_netcdf() in Python.

You should be able to do this using the group and mode="a" arguments to to_netcdf(), like this

import xarray as xr

ds1 = xr.Dataset({"a": 0})

ds2 = xr.Dataset({"b": 1})

ds1.to_netcdf("file.nc", group="A")
ds2.to_netcdf("file.nc", group="B", mode="a")
$ ncdump file.nc 
netcdf file {

group: A {
  variables:
  	int64 a ;
  data:

   a = 0 ;
  } // group A

group: B {
  variables:
  	int64 b ;
  data:

   b = 1 ;
  } // group B
}

Another way would be to use xarray-datatree.

The reason for this is because I have only 5GB to work with.

D…

Replies: 2 comments 6 replies

Comment options

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

@dcherian
Comment options

@Marston
Comment options

@TomNicholas
Comment options

Answer selected by TomNicholas
Comment options

You must be logged in to vote
2 replies
@TomNicholas
Comment options

@Marston
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants