Skip to content

Commit

Permalink
fix: Fix encoding in 'to_zarr' to remove 'preferred_chunks' before sa…
Browse files Browse the repository at this point in the history
…ving (#1128)

* Encoding and Chunk matching only if DaskArray

* Removing preffered chunks from encoding dict

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Sohambutala and pre-commit-ci[bot] authored Oct 23, 2023
1 parent 0bc534e commit 206be67
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 3 additions & 0 deletions echopype/utils/coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ def set_zarr_encodings(
chunks = _get_auto_chunk(val, chunk_size=chunk_size)

encoding[name]["chunks"] = chunks
if PREFERRED_CHUNKS in encoding[name]:
# Remove 'preferred_chunks', use chunks only instead
encoding[name].pop(PREFERRED_CHUNKS)

return encoding

Expand Down
4 changes: 3 additions & 1 deletion echopype/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import fsspec
import xarray as xr
from dask.array import Array as DaskArray
from fsspec import FSMap
from fsspec.implementations.local import LocalFileSystem

Expand Down Expand Up @@ -61,7 +62,8 @@ def save_file(ds, path, mode, engine, group=None, compression_settings=None, **k
elif engine == "zarr":
# Ensure that encoding and chunks match
for var, enc in encoding.items():
ds[var] = ds[var].chunk(enc.get("chunks", {}))
if isinstance(ds[var].data, DaskArray):
ds[var] = ds[var].chunk(enc.get("chunks", {}))
ds.to_zarr(store=path, mode=mode, group=group, encoding=encoding, **kwargs)
else:
raise ValueError(f"{engine} is not a supported save format")
Expand Down

0 comments on commit 206be67

Please sign in to comment.