xarray icon indicating copy to clipboard operation
xarray copied to clipboard

dataset encodings 'source' and 'original_shape' are not dropped in `zarr` backend

Open observingClouds opened this issue 3 years ago • 0 comments

What happened?

When opening a dataset, like one from the tutorial, and writing it as zarr file, an error is raised due to encodings that are invalid for the zarr driver, when the encoding is given in to_zarr. In this particular case, the encodings source and original_shape are added by xarray itself, so that I expect that it can handle these encodings without raising an error.

What did you expect to happen?

I expect that the encodings source and original_shape being dropped similar to the netCDF4 backend.

Minimal Complete Verifiable Example

import xarray as xr
ds = xr.tutorial.load_dataset("eraint_uvz")
ds.to_zarr("test.zarr", encoding={"z":{**ds.z.encoding}})

MVCE confirmation

  • [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • [X] Complete example — the example is self-contained, including all data and the text of any traceback.
  • [X] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • [X] New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

>>> ds.to_zarr("test_w_offset.zarr01", encoding={"z":{**ds.z.encoding}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/core/dataset.py", line 2068, in to_zarr
    return to_zarr(  # type: ignore
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/api.py", line 1653, in to_zarr
    dump_to_store(dataset, zstore, writer, encoding=encoding)
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/api.py", line 1273, in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/zarr.py", line 574, in store
    self.set_variables(
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/zarr.py", line 621, in set_variables
    encoding = extract_zarr_variable_encoding(
  File ".../.conda/envs/xarray2022090/lib/python3.10/site-packages/xarray/backends/zarr.py", line 247, in extract_zarr_variable_encoding
    raise ValueError(
ValueError: unexpected encoding parameters for zarr backend:  ['source', 'original_shape']

Anything else we need to know?

The respective lines in the netCDF4 backend are: https://github.com/pydata/xarray/blob/13c52b27b777709fc3316cf4334157f50904c02b/xarray/backends/netCDF4_.py#L235 and

https://github.com/pydata/xarray/blob/13c52b27b777709fc3316cf4334157f50904c02b/xarray/backends/netCDF4_.py#L272-L274

Environment

INSTALLED VERSIONS ------------------ commit: None python: 3.10.6 | packaged by conda-forge | (main, Aug 22 2022, 20:35:26) [GCC 10.4.0] python-bits: 64 OS: Linux OS-release: 4.18.0-305.25.1.el8_4.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.8.1

xarray: 2022.9.0 pandas: 1.5.0 numpy: 1.23.3 scipy: None netCDF4: 1.6.1 pydap: None h5netcdf: None h5py: None Nio: None zarr: 2.13.2 cftime: 1.6.2 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: None iris: None bottleneck: None dask: None distributed: None matplotlib: None cartopy: None seaborn: None numbagg: None fsspec: None cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 65.4.1 pip: 22.2.2 conda: None pytest: None IPython: 8.3.0 sphinx: None

observingClouds avatar Oct 05 '22 00:10 observingClouds