# xarray.DataTree.to_netcdf

#### DataTree.to_netcdf(filepath=None, mode='w', encoding=None, unlimited_dims=None, format=None, engine=None, group=None, write_inherited_coords=False, compute=True, \*\*kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/datatree.py#L2065-L2150)

Write datatree contents to a netCDF file.

* **Parameters:**
  * **filepath** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or `PathLike` or `file-like object` or [`None`](https://docs.python.org/3/builtins/constants.html#None)) – Path to which to save this datatree, or a file-like object to write
    it to (which must support read and write and be seekable) or None
    to return in-memory bytes as a memoryview.
  * **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`](https://docs.python.org/3/builtins/stdtypes.html#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`](https://docs.python.org/3/builtins/stdtypes.html#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, by default
    preferring “h5netcdf” over “netcdf4” (customizable via
    `netcdf_engine_order` in `xarray.set_options()`).
  * **group** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#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`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`False`](https://docs.python.org/3/builtins/constants.html#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`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – If true compute immediately, otherwise return a
    `dask.delayed.Delayed` object that can be computed later.
  * **kwargs** – Additional keyword arguments to be passed to `xarray.Dataset.to_netcdf`
* **Returns:**
  * `* ``memoryview`` if path is None\`
  * `* ``dask.delayed.Delayed`` if compute is False\`
  * `* ``None`` otherwise\`

### Notes

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