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

Function _maybe_chunk

xarray/structure/chunks.py:64–115  ·  view source on GitHub ↗
(
    name: Hashable,
    var: Variable,
    chunks: Mapping[Any, T_ChunkDim] | None,
    token=None,
    lock=None,
    name_prefix: str = "xarray-",
    overwrite_encoded_chunks: bool = False,
    inline_array: bool = False,
    chunked_array_type: str | ChunkManagerEntrypoint | None = None,
    from_array_kwargs=None,
    just_use_token=False,
)

Source from the content-addressed store, hash-verified

62
63
64def _maybe_chunk(
65 name: Hashable,
66 var: Variable,
67 chunks: Mapping[Any, T_ChunkDim] | None,
68 token=None,
69 lock=None,
70 name_prefix: str = "xarray-",
71 overwrite_encoded_chunks: bool = False,
72 inline_array: bool = False,
73 chunked_array_type: str | ChunkManagerEntrypoint | None = None,
74 from_array_kwargs=None,
75 just_use_token=False,
76) -> Variable:
77 from xarray.namedarray.daskmanager import DaskManager
78
79 if chunks is not None:
80 chunks = {dim: chunks[dim] for dim in var.dims if dim in chunks}
81
82 if var.ndim:
83 chunked_array_type = guess_chunkmanager(
84 chunked_array_type
85 ) # coerce string to ChunkManagerEntrypoint type
86 if isinstance(chunked_array_type, DaskManager):
87 if not just_use_token:
88 from dask.base import tokenize
89
90 # when rechunking by different amounts, make sure dask names change
91 # by providing chunks as an input to tokenize.
92 # subtle bugs result otherwise. see GH3350
93 # we use str() for speed, and use the name for the final array name on the next line
94 token = tokenize(token or var._data, str(chunks))
95
96 name2 = f"{name_prefix}{name}-{token}"
97
98 from_array_kwargs = utils.consolidate_dask_from_array_kwargs(
99 from_array_kwargs,
100 name=name2,
101 lock=lock,
102 inline_array=inline_array,
103 )
104
105 var = var.chunk(
106 chunks,
107 chunked_array_type=chunked_array_type,
108 from_array_kwargs=from_array_kwargs,
109 )
110
111 if overwrite_encoded_chunks and var.chunks is not None:
112 var.encoding["chunks"] = tuple(x[0] for x in var.chunks)
113 return var
114 else:
115 return var
116
117
118_T = TypeVar("_T", bound=Union["Dataset", "DataArray"])

Callers 2

chunkMethod · 0.90
_chunk_dsFunction · 0.90

Calls 2

guess_chunkmanagerFunction · 0.90
chunkMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…