fix to_netcdf append bug (GH1215) by jhamman · Pull Request #1609 · pydata/xarray (original) (raw)

This isn't quite enough -- you also need to set the format in this case. Take a look at roundtrip() below.

 # on the base class
 @contextlib.contextmanager
 def roundtrip(self, data, save_kwargs={}, open_kwargs={},
               allow_cleanup_failure=False):
     with create_tmp_file(
             allow_cleanup_failure=allow_cleanup_failure) as path:
         self.save(data, path, **save_kwargs)
         with self.open(path, **open_kwargs) as ds:
             yield ds

 # on subclasses, e.g., for NetCDF3ViaNetCDF4DataTest
 def save(self, dataset, path, **kwargs):
     dataset.to_netcdf(tmp_file, format='NETCDF3_CLASSIC',
                       engine='netcdf4', **kwargs)

 @contextlib.contextmanager
 def open(self, path, **kwargs):
      with open_dataset(tmp_file, engine='netcdf4',
                        autoclose=self.autoclose, **open_kwargs) as ds:
           yield ds

Then you could write roundtrip_append() in terms of save and open.