MCPcopy Index your code
hub / github.com/pydata/xarray / _determine_zarr_chunks

Function _determine_zarr_chunks

xarray/backends/zarr.py:336–408  ·  view source on GitHub ↗

Given encoding chunks (possibly None or []) and variable chunks (possibly None or []).

(enc_chunks, var_chunks, ndim, name)

Source from the content-addressed store, hash-verified

334
335
336def _determine_zarr_chunks(enc_chunks, var_chunks, ndim, name):
337 """
338 Given encoding chunks (possibly None or []) and variable chunks
339 (possibly None or []).
340 """
341
342 # zarr chunk spec:
343 # chunks : int or tuple of ints, optional
344 # Chunk shape. If not provided, will be guessed from shape and dtype.
345
346 # if there are no chunks in encoding and the variable data is a numpy
347 # array, then we let zarr use its own heuristics to pick the chunks
348 if not var_chunks and not enc_chunks:
349 return None
350
351 # if there are no chunks in encoding but there are dask chunks, we try to
352 # use the same chunks in zarr
353 # However, zarr chunks needs to be uniform for each array
354 # https://zarr-specs.readthedocs.io/en/latest/v2/v2.0.html#chunks
355 # while dask chunks can be variable sized
356 # https://dask.pydata.org/en/latest/array-design.html#chunks
357 if var_chunks and not enc_chunks:
358 if any(len(set(chunks[:-1])) > 1 for chunks in var_chunks):
359 raise ValueError(
360 "Zarr requires uniform chunk sizes except for final chunk. "
361 f"Variable named {name!r} has incompatible dask chunks: {var_chunks!r}. "
362 "Consider rechunking using `chunk()`."
363 )
364 if any((chunks[0] < chunks[-1]) for chunks in var_chunks):
365 raise ValueError(
366 "Final chunk of Zarr array must be the same size or smaller "
367 f"than the first. Variable named {name!r} has incompatible Dask chunks {var_chunks!r}."
368 "Consider either rechunking using `chunk()` or instead deleting "
369 "or modifying `encoding['chunks']`."
370 )
371 # return the first chunk for each dimension
372 return tuple(chunk[0] for chunk in var_chunks)
373
374 # From here on, we are dealing with user-specified chunks in encoding
375 # zarr allows chunks to be an integer, in which case it uses the same chunk
376 # size on each dimension.
377 # Here we re-implement this expansion ourselves. That makes the logic of
378 # checking chunk compatibility easier
379
380 if isinstance(enc_chunks, integer_types):
381 enc_chunks_tuple = ndim * (enc_chunks,)
382 else:
383 enc_chunks_tuple = tuple(enc_chunks)
384
385 if len(enc_chunks_tuple) != ndim:
386 # throw away encoding chunks, start over
387 return _determine_zarr_chunks(
388 None,
389 var_chunks,
390 ndim,
391 name,
392 )
393

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…