Return map from each dim to chunk sizes, accounting for backend's preferred chunks.
( # type: ignore[no-untyped-def]
data: DuckArray[Any],
chunks,
chunkmanager: ChunkManagerEntrypoint[Any],
*,
preferred_chunks,
dims=None,
)
| 201 | |
| 202 | |
| 203 | def _get_chunk( # type: ignore[no-untyped-def] |
| 204 | data: DuckArray[Any], |
| 205 | chunks, |
| 206 | chunkmanager: ChunkManagerEntrypoint[Any], |
| 207 | *, |
| 208 | preferred_chunks, |
| 209 | dims=None, |
| 210 | ) -> Mapping[Any, T_ChunkDim]: |
| 211 | """ |
| 212 | Return map from each dim to chunk sizes, accounting for backend's preferred chunks. |
| 213 | """ |
| 214 | from xarray.core.common import _contains_cftime_datetimes |
| 215 | from xarray.core.utils import emit_user_level_warning |
| 216 | from xarray.structure.chunks import _get_breaks_cached |
| 217 | |
| 218 | dims = chunks.keys() if dims is None else dims |
| 219 | shape = data.shape |
| 220 | |
| 221 | # Determine the explicit requested chunks. |
| 222 | preferred_chunk_shape = tuple( |
| 223 | itertools.starmap(preferred_chunks.get, zip(dims, shape, strict=True)) |
| 224 | ) |
| 225 | if isinstance(chunks, Number) or (chunks == "auto"): |
| 226 | chunks = dict.fromkeys(dims, chunks) |
| 227 | chunk_shape = tuple( |
| 228 | chunks.get(dim, None) or preferred_chunk_sizes |
| 229 | for dim, preferred_chunk_sizes in zip(dims, preferred_chunk_shape, strict=True) |
| 230 | ) |
| 231 | |
| 232 | limit: int | None |
| 233 | if _contains_cftime_datetimes(data): |
| 234 | limit, dtype = fake_target_chunksize(data, chunkmanager.get_auto_chunk_size()) |
| 235 | else: |
| 236 | limit = None |
| 237 | dtype = data.dtype |
| 238 | |
| 239 | chunk_shape = chunkmanager.normalize_chunks( |
| 240 | chunk_shape, |
| 241 | shape=shape, |
| 242 | dtype=dtype, |
| 243 | limit=limit, |
| 244 | previous_chunks=preferred_chunk_shape, |
| 245 | ) |
| 246 | |
| 247 | # Warn where requested chunks break preferred chunks, provided that the variable |
| 248 | # contains data. |
| 249 | if data.size: # type: ignore[unused-ignore,attr-defined] # DuckArray protocol doesn't include 'size' - should it? |
| 250 | for dim, size, chunk_sizes in zip(dims, shape, chunk_shape, strict=True): |
| 251 | if preferred_chunk_sizes := preferred_chunks.get(dim): |
| 252 | disagreement = _get_breaks_cached( |
| 253 | size=size, |
| 254 | chunk_sizes=chunk_sizes, |
| 255 | preferred_chunk_sizes=preferred_chunk_sizes, |
| 256 | ) |
| 257 | if disagreement: |
| 258 | emit_user_level_warning( |
| 259 | "The specified chunks separate the stored chunks along " |
| 260 | f'dimension "{dim}" starting at index {disagreement}. This could ' |
no test coverage detected
searching dependent graphs…