MCPcopy
hub / github.com/pydata/xarray / extract_zarr_variable_encoding

Function extract_zarr_variable_encoding

xarray/backends/zarr.py:471–539  ·  view source on GitHub ↗

Extract zarr encoding dictionary from xarray Variable Parameters ---------- variable : Variable raise_on_invalid : bool, optional name: str | Hashable, optional zarr_format: Literal[2,3] Returns ------- encoding : dict Zarr encoding for `variable`

(
    variable,
    raise_on_invalid=False,
    name=None,
    *,
    zarr_format: ZarrFormat,
)

Source from the content-addressed store, hash-verified

469
470
471def extract_zarr_variable_encoding(
472 variable,
473 raise_on_invalid=False,
474 name=None,
475 *,
476 zarr_format: ZarrFormat,
477):
478 """
479 Extract zarr encoding dictionary from xarray Variable
480
481 Parameters
482 ----------
483 variable : Variable
484 raise_on_invalid : bool, optional
485 name: str | Hashable, optional
486 zarr_format: Literal[2,3]
487 Returns
488 -------
489 encoding : dict
490 Zarr encoding for `variable`
491 """
492
493 encoding = variable.encoding.copy()
494
495 safe_to_drop = {"source", "original_shape", "preferred_chunks"}
496 valid_encodings = {
497 "chunks",
498 "shards",
499 "compressor", # TODO: delete when min zarr >=3
500 "compressors",
501 "filters",
502 "serializer",
503 "cache_metadata",
504 "write_empty_chunks",
505 "chunk_key_encoding",
506 }
507 if zarr_format == 3:
508 valid_encodings.add("fill_value")
509
510 for k in safe_to_drop:
511 if k in encoding:
512 del encoding[k]
513
514 if raise_on_invalid:
515 invalid = [k for k in encoding if k not in valid_encodings]
516 if "fill_value" in invalid and zarr_format == 2:
517 msg = " Use `_FillValue` to set the Zarr array `fill_value`"
518 else:
519 msg = ""
520
521 if invalid:
522 raise ValueError(
523 f"unexpected encoding parameters for zarr backend: {invalid!r}." + msg
524 )
525 else:
526 for k in list(encoding):
527 if k not in valid_encodings:
528 del encoding[k]

Callers 1

set_variablesMethod · 0.85

Calls 5

_determine_zarr_chunksFunction · 0.85
_zarr_v3Function · 0.85
copyMethod · 0.45
addMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…