What happened?
Dimensions from the root group are re-defined in subgroups when saving a DataTree to netCDF.
What did you expect to happen?
Subgroups to inherit dimensions from the root group, as explained in CF conventions
Minimal Complete Verifiable Example
import xarray as xr
import netCDF4
# Create sample tree
only_coords = xr.Dataset(
coords={"y": [1, 2], "x": [1, 2]}
)
only_data = xr.Dataset(
{"myvar": (("y", "x"), [[1, 2], [3, 4]])}
)
datasets = {
"/": only_coords,
"/data": only_data
}
tree = xr.DataTree.from_dict(datasets)
# Save to netCDF
tree.to_netcdf("test.nc")
# Display file contents
ds = netCDF4.Dataset("test.nc")
print(ds.groups["data"])
MVCE confirmation
Relevant log output
<class 'netCDF4.Group'>
group /data:
dimensions(sizes): y(2), x(2)
variables(dimensions): int64 myvar(y, x)
Anything else we need to know?
Using netCDF4 it is possible to create a group with only the variable and inherited dimensions
import netCDF4
import numpy as np
root_group = netCDF4.Dataset("test2.nc", mode="w")
root_group.createDimension("y", 2)
root_group.createDimension("x", 2)
root_group.createVariable("y", "i8", ("y",))
root_group.createVariable("x", "i8", ("x",))
data_group = root_group.createGroup("data")
data_var = data_group.createVariable("data", "i8", ("y", "x"))
data_var[:] = np.array([[1, 2], [3, 4]])
root_group.close()
ncdump:
netcdf test2 {
dimensions:
y = 2 ;
x = 2 ;
variables:
int64 y(y) ;
int64 x(x) ;
group: data {
variables:
int64 data(y, x) ;
} // group data
}
Environment
Details
INSTALLED VERSIONS
commit: None
python: 3.12.9 | packaged by conda-forge | (main, Feb 14 2025, 08:00:06) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 4.18.0-513.5.1.el8_9.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.utf-8
LANG: en_US
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2
xarray: 2025.3.1
pandas: 2.2.3
numpy: 2.1.3
scipy: 1.15.2
netCDF4: 1.7.2
pydap: None
h5netcdf: 1.5.0
h5py: 3.13.0
zarr: 2.18.4
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.2.0
distributed: 2025.2.0
matplotlib: 3.10.0
cartopy: 0.24.0
seaborn: 0.13.2
numbagg: None
fsspec: 2025.2.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.8.2
pip: 25.0.1
conda: None
pytest: 8.3.4
mypy: 1.15.0
IPython: 8.17.2
sphinx: 8.2.1
What happened?
Dimensions from the root group are re-defined in subgroups when saving a DataTree to netCDF.
What did you expect to happen?
Subgroups to inherit dimensions from the root group, as explained in CF conventions
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
Using netCDF4 it is possible to create a group with only the variable and inherited dimensions
ncdump:
Environment
Details
INSTALLED VERSIONS
commit: None
python: 3.12.9 | packaged by conda-forge | (main, Feb 14 2025, 08:00:06) [GCC 13.3.0]
python-bits: 64
OS: Linux
OS-release: 4.18.0-513.5.1.el8_9.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.utf-8
LANG: en_US
LOCALE: ('en_US', 'UTF-8')
libhdf5: 1.14.3
libnetcdf: 4.9.2
xarray: 2025.3.1
pandas: 2.2.3
numpy: 2.1.3
scipy: 1.15.2
netCDF4: 1.7.2
pydap: None
h5netcdf: 1.5.0
h5py: 3.13.0
zarr: 2.18.4
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.2.0
distributed: 2025.2.0
matplotlib: 3.10.0
cartopy: 0.24.0
seaborn: 0.13.2
numbagg: None
fsspec: 2025.2.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 75.8.2
pip: 25.0.1
conda: None
pytest: 8.3.4
mypy: 1.15.0
IPython: 8.17.2
sphinx: 8.2.1