diff --git a/xarray/backends/h5netcdf_.py b/xarray/backends/h5netcdf_.py index f2e3c556ce4..feadfcb4601 100644 --- a/xarray/backends/h5netcdf_.py +++ b/xarray/backends/h5netcdf_.py @@ -46,12 +46,6 @@ def _read_attributes(h5netcdf_var): _extract_h5nc_encoding = functools.partial(_extract_nc4_variable_encoding, lsd_okay=False, backend='h5netcdf') -def _close_ds(ds): - # netCDF4 only allows closing the root group - ds = find_root(ds) - if not ds._closed: - ds.close() - def _open_h5netcdf_group(filename, mode, group): import h5netcdf.legacyapi ds = h5netcdf.legacyapi.Dataset(filename, mode=mode) @@ -162,5 +156,8 @@ def sync(self): def close(self): if self._isopen: - _close_ds(self.ds) + # netCDF4 only allows closing the root group + ds = find_root(self.ds) + if not ds._closed: + ds.close() self._isopen = False diff --git a/xarray/backends/netCDF4_.py b/xarray/backends/netCDF4_.py index b25e325f2aa..6c74f6981dc 100644 --- a/xarray/backends/netCDF4_.py +++ b/xarray/backends/netCDF4_.py @@ -177,12 +177,6 @@ def _extract_nc4_variable_encoding(variable, raise_on_invalid=False, return encoding -def _close_ds(ds): - # netCDF4 only allows closing the root group - ds = find_root(ds) - if ds._isopen: - ds.close() - def _open_netcdf4_group(filename, mode, group=None, **kwargs): import netCDF4 as nc4 @@ -342,5 +336,8 @@ def sync(self): def close(self): if self._isopen: - _close_ds(self.ds) + # netCDF4 only allows closing the root group + ds = find_root(self.ds) + if ds._isopen: + ds.close() self._isopen = False