xarray.DataTree.to_netcdf

xarray.DataTree.to_netcdf#

DataTree.to_netcdf(filepath, mode='w', encoding=None, unlimited_dims=None, format=None, engine=None, group=None, write_inherited_coords=False, compute=True, **kwargs)[source]#

Write datatree contents to a netCDF file.

Parameters:
  • filepath (str or Path) – Path to which to save this datatree.

  • mode ({"w", "a"}, default: "w") – Write (‘w’) or append (‘a’) mode. If mode=’w’, any existing file at this location will be overwritten. If mode=’a’, existing variables will be overwritten. Only applies to the root group.

  • encoding (dict, optional) – Nested dictionary with variable names as keys and dictionaries of variable specific encodings as values, e.g., {"root/set1": {"my_variable": {"dtype": "int16", "scale_factor": 0.1, "zlib": True}, ...}, ...}. See xarray.Dataset.to_netcdf for available options.

  • unlimited_dims (dict, optional) – Mapping of unlimited dimensions per group that that should be serialized as unlimited dimensions. By default, no dimensions are treated as unlimited dimensions. Note that unlimited_dims may also be set via dataset.encoding["unlimited_dims"].

  • format ({"NETCDF4", }, optional) – File format for the resulting netCDF file:

    • NETCDF4: Data is stored in an HDF5 file, using netCDF4 API features.

  • engine ({"netcdf4", "h5netcdf"}, optional) – Engine to use when writing netCDF files. If not provided, the default engine is chosen based on available dependencies, with a preference for “netcdf4” if writing to a file on disk.

  • group (str, optional) – Path to the netCDF4 group in the given file to open as the root group of the DataTree. Currently, specifying a group is not supported.

  • write_inherited_coords (bool, default: False) – If true, replicate inherited coordinates on all descendant nodes. Otherwise, only write coordinates at the level at which they are originally defined. This saves disk space, but requires opening the full tree to load inherited coordinates.

  • compute (bool, default: True) – If true compute immediately, otherwise return a dask.delayed.Delayed object that can be computed later. Currently, compute=False is not supported.

  • kwargs – Additional keyword arguments to be passed to xarray.Dataset.to_netcdf

Note

Due to file format specifications the on-disk root group name is always "/" overriding any given DataTree root node name.