You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Dataset.to_zarr with the region keyword provided, if the keyword write_empty_chunks is set to False (or True), the operation will fail with a ReadOnlyError: object is read-only. This occurs when keyword consolidated is not provided (and thus defaults to None) or if it is set to True. It does not happen when consolidated=False is provided.
From what I can find, while the error stems from the zarr library, the root cause of this bug occurs in line 677 of xarray/backends/zarr.py. Basically, a new zarr Array is created with store=self.zarr_group.store (which is a read-only zarr.storage.ConsolidatedMetadataStore instance when using consolidated metadata), instead of store=self.zarr_group.chunk_store (which is a normal zarr.storage.DirectoryStore instance), which is effectively the instance that is used in all other cases.
Replacing store=self.zarr_group.store with store=self.zarr_group.chunk_store seems to solve the issue.
What did you expect to happen?
I expected not to get an error and instead to write the data to the given zarr region, whilst respecting the write_empty_chunks=False behavior.
Minimal Complete Verifiable Example
importxarrayasxrimportnumpyasnpstore_path="test.zarr"# First, create an empty Zarr store using compute=Falseds=xr.Dataset(coords={"x": range(1000), "y": range(2000)}, data_vars={"foo": (("x", "y"), np.zeros((1000, 2000), dtype="float32"))})
ds.to_zarr(store_path, compute=False)
# Attempts to write to the zarr using 'region' and 'write_empty_chunks=False'region= {"x": slice(0, 1000), "y": slice(0, 2000)}
# Without using consolidated metadata works:ds.to_zarr(store_path, region=region, write_empty_chunks=False, consolidated=False)
# But when using consolidated metadata, it raises a ReadOnlyErrords.to_zarr(store_path, region=region, write_empty_chunks=False)
MVCE confirmation
Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
Complete example — the example is self-contained, including all data and the text of any traceback.
Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
New issue — a search of GitHub Issues suggests this is not a duplicate.
Recent environment — the issue occurs with the latest version of xarray and its dependencies.
What happened?
When using
Dataset.to_zarr
with theregion
keyword provided, if the keywordwrite_empty_chunks
is set to False (or True), the operation will fail with aReadOnlyError: object is read-only
. This occurs when keywordconsolidated
is not provided (and thus defaults to None) or if it is set to True. It does not happen whenconsolidated=False
is provided.From what I can find, while the error stems from the zarr library, the root cause of this bug occurs in line 677 of xarray/backends/zarr.py. Basically, a new zarr Array is created with
store=self.zarr_group.store
(which is a read-onlyzarr.storage.ConsolidatedMetadataStore
instance when using consolidated metadata), instead ofstore=self.zarr_group.chunk_store
(which is a normalzarr.storage.DirectoryStore
instance), which is effectively the instance that is used in all other cases.Replacing
store=self.zarr_group.store
withstore=self.zarr_group.chunk_store
seems to solve the issue.What did you expect to happen?
I expected not to get an error and instead to write the data to the given zarr region, whilst respecting the
write_empty_chunks=False
behavior.Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
python-bits: 64
OS: Linux
OS-release: 5.15.90.1-microsoft-standard-WSL2
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('en_US', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2023.9.0
pandas: 2.1.1
numpy: 1.25.2
scipy: 1.11.2
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.16.1
cftime: None
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: 1.3.7
dask: 2023.9.3
distributed: None
matplotlib: 3.5.1
cartopy: None
seaborn: None
numbagg: None
fsspec: 2023.6.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 59.6.0
pip: 23.3
conda: None
pytest: 7.4.2
mypy: 1.5.1
IPython: 7.31.1
sphinx: None
The text was updated successfully, but these errors were encountered: