docs: def to_netcdf(dataset, path_or_file=None, mode='w', format=None, group=None,
                engine=None, encoding=None, unlimited_dims=None, compute=True,
                multifile=False):
      """This function creates an appropriate datastore for writing a dataset to
      disk as a netCDF file
  
      See `Dataset.to_netcdf` for full API docs.
  
      The ``multifile`` argument is only for the private use of save_mfdataset.
      """
      if isinstance(path_or_file, Path):
          path_or_file = str(path_or_file)
  
      if encoding is None:
          encoding = {}
  
      if path_or_file is None:
          if engine is None:
              engine = 'scipy'
          elif engine != 'scipy':
              raise ValueError('invalid engine for creating bytes with '
                               'to_netcdf: %r. Only the default engine '
                               "or engine='scipy' is supported" % engine)
          if not compute:
              raise NotImplementedError(
                  'to_netcdf() with compute=False is not yet implemented when '
                  'returning bytes')
      elif isinstance(path_or_file, str):
          if engine is None:
              engine = _get_default_engine(path_or_file)
          path_or_file = _normalize_path(path_or_file)
      else:  # file-like object
          engine = 'scipy'
  
      # validate Dataset keys, DataArray names, and attr keys/values
      _validate_dataset_names(dataset)
      _validate_attrs(dataset)
  
      try:
          store_open = WRITEABLE_STORES[engine]
      except KeyError:
          raise ValueError('unrecognized engine for to_netcdf: %r' % engine)
  
      if format is not None:
          format = format.upper()
  
      # handle scheduler specific logic
      scheduler = _get_scheduler()
      have_chunks = any(v.chunks for v in dataset.variables.values())
  
      autoclose = have_chunks and scheduler in ['distributed', 'multiprocessing']
      if autoclose and engine == 'scipy':
          raise NotImplementedError("Writing netCDF files with the %s backend "
                                    "is not currently supported with dask's %s "
                                    "scheduler" % (engine, scheduler))
  
      target = path_or_file if path_or_file is not None else BytesIO()
      kwargs = dict(autoclose=True) if autoclose else {}
      store = store_open(target, mode, format, group, **kwargs)
  
      if unlimited_dims is None:
          unlimited_dims = dataset.encoding.get('unlimited_dims', None)
      if isinstance(unlimited_dims, str):
          unlimited_dims = [unlimited_dims]
  
      writer = ArrayWriter()
  
      # TODO: figure out how to refactor this logic (here and in save_mfdataset)
      # to avoid this mess of conditionals
      try:
          # TODO: allow this work (setting up the file for writing array data)
          # to be parallelized with dask
          dump_to_store(dataset, store, writer, encoding=encoding,
                        unlimited_dims=unlimited_dims)
          if autoclose:
              store.close()
  
          if multifile:
              return writer, store
  
          writes = writer.sync(compute=compute)
  
          if path_or_file is None:
              store.sync()
              return target.getvalue()
      finally:
          if not multifile and compute:
              store.close()
  
      if not compute:
          import dask
          return dask.delayed(_finalize_store)(writes, store)