class_docstr: Container for inference data storage using xarray.
  
  For a detailed introduction to ``InferenceData`` objects and their usage, see
  :ref:`xarray_for_arviz`. This page provides help and documentation
  on ``InferenceData`` methods and their low level implementation.
class_docstr_tok: ["Container","for","inference","data","storage","using","xarray",".","For","a","detailed","introduction","to","``","InferenceData","``","objects","and","their","usage",",","see",":","ref",":","`","xarray_for_arviz","`",".","This","page","provides","help","and","documentation","on","``","InferenceData","``","methods","and","their","low","level","implementation","."]
docstr: Write InferenceData to netcdf4 file.
  
  Parameters
  ----------
  filename : str
      Location to write to
  compress : bool, optional
      Whether to compress result. Note this saves disk space, but may make
      saving and loading somewhat slower (default: True).
  groups : list, optional
      Write only these groups to netcdf file.
  engine : {"h5netcdf", "netcdf4"}, default "h5netcdf"
      Library used to read the netcdf file.
  base_group : str, default "/"
      The group in the netCDF file where the InferenceData is will be stored.
      By default, will write to the root of the netCDF file
  overwrite_existing : bool, default True
      Whether to overwrite the existing file or append to it.
  
  Returns
  -------
  str
      Location of netcdf file
docstr_tok: ["Write","InferenceData","to","netcdf4","file",".","Parameters","--","--","--","--","--","filename",":","str","Location","to","write","to","compress",":","bool",",","optional","Whether","to","compress","result",".","Note","this","saves","disk","space",",","but","may","make","saving","and","loading","somewhat","slower","(","default",":","True",")",".","groups",":","list",",","optional","Write","only","these","groups","to","netcdf","file",".","engine",":","{","``","h5netcdf","''",",","``","netcdf4","''","}",",","default","``","h5netcdf","''","Library","used","to","read","the","netcdf","file",".","base_group",":","str",",","default","``","\/","''","The","group","in","the","netCDF","file","where","the","InferenceData","is","will","be","stored",".","By","default",",","will","write","to","the","root","of","the","netCDF","file","overwrite_existing",":","bool",",","default","True","Whether","to","overwrite","the","existing","file","or","append","to","it",".","Returns","--","--","--","-","str","Location","of","netcdf","file"]
code: def to_netcdf(
      self,
      filename: str,
      compress: bool = True,
      groups: Optional[List[str]] = None,
      engine: str = "h5netcdf",
      base_group: str = "/",
      overwrite_existing: bool = True,
  ) -> str:
      """Write InferenceData to netcdf4 file.
  
      Parameters
      ----------
      filename : str
          Location to write to
      compress : bool, optional
          Whether to compress result. Note this saves disk space, but may make
          saving and loading somewhat slower (default: True).
      groups : list, optional
          Write only these groups to netcdf file.
      engine : {"h5netcdf", "netcdf4"}, default "h5netcdf"
          Library used to read the netcdf file.
      base_group : str, default "/"
          The group in the netCDF file where the InferenceData is will be stored.
          By default, will write to the root of the netCDF file
      overwrite_existing : bool, default True
          Whether to overwrite the existing file or append to it.
  
      Returns
      -------
      str
          Location of netcdf file
      """
      if base_group is None:
          base_group = "/"
  
      if os.path.exists(filename) and not overwrite_existing:
          mode = "a"
      else:
          mode = "w"  # overwrite first, then append
  
      if self._attrs:
          xr.Dataset(attrs=self._attrs).to_netcdf(
              filename, mode=mode, engine=engine, group=base_group
          )
          mode = "a"
  
      if self._groups_all:  # check's whether a group is present or not.
          if groups is None:
              groups = self._groups_all
          else:
              groups = [
                  group for group in self._groups_all if group in groups
              ]
  
          for group in groups:
              data = getattr(self, group)
              kwargs = {"engine": engine}
              if compress:
                  kwargs["encoding"] = {
                      var_name: {"zlib": True}
                      for var_name, values in data.variables.items()
                      if _compressible_dtype(values.dtype)
                  }
              data.to_netcdf(
                  filename,
                  mode=mode,
                  group=f"{base_group}/{group}",
                  **kwargs,
              )
              data.close()
              mode = "a"
      elif (
          not self._attrs
      ):  # creates a netcdf file for an empty InferenceData object.
          if engine == "h5netcdf":
              import h5netcdf
  
              empty_netcdf_file = h5netcdf.File(filename, mode="w")
          elif engine == "netcdf4":
              import netCDF4 as nc
  
              empty_netcdf_file = nc.Dataset(
                  filename, mode="w", format="NETCDF4"
              )
          empty_netcdf_file.close()
      return filename
  
imports: import os
  import re
  import sys
  import uuid
  import warnings
  from collections import OrderedDict, defaultdict
  from collections.abc import MutableMapping, Sequence
  from copy import copy
  from copy import deepcopy
  from datetime import datetime
  from html import escape
  from typing import TYPE_CHECKING, Any, Iterable, Iterator, List, Mapping, Optional, Tuple, TypeVar, Union, overload
  import numpy
  import xarray
  from packaging import version
  from ..rcparams import rcParams
  from ..utils import HtmlTemplate, _subset_list, _var_names, either_dict_or_kwargs
  from .base import _extend_xr_method, _make_json_serializable, dict_to_dataset
label_desc: Use image node_id 17 for calling the InferenceData obj's underlying member method code with example usage: obj.to_netcdf(filename, compress, groups, engine, base_group, overwrite_existing) and returns: filename