@@ -4928,3 +4928,96 @@ def _parse_data_params(
49284928 raise ValueError (msg )
49294929 dtype_out = data .dtype
49304930 return data , shape_out , dtype_out
4931+
4932+
4933+ def iter_chunk_coords (
4934+ array : Array | AsyncArray [Any ],
4935+ * ,
4936+ origin : Sequence [int ] | None = None ,
4937+ selection_shape : Sequence [int ] | None = None ,
4938+ ) -> Iterator [ChunkCoords ]:
4939+ """
4940+ Create an iterator over the coordinates of chunks in chunk grid space. If the `origin`
4941+ keyword is used, iteration will start at the chunk index specified by `origin`.
4942+ The default behavior is to start at the origin of the grid coordinate space.
4943+ If the `selection_shape` keyword is used, iteration will be bounded over a contiguous region
4944+ ranging from `[origin, origin selection_shape]`, where the upper bound is exclusive as
4945+ per python indexing conventions.
4946+
4947+ Parameters
4948+ ----------
4949+ array : Array | AsyncArray
4950+ The array to iterate over.
4951+ origin : Sequence[int] | None, default=None
4952+ The origin of the selection relative to the array's chunk grid.
4953+ selection_shape : Sequence[int] | None, default=None
4954+ The shape of the selection in chunk grid coordinates.
4955+
4956+ Yields
4957+ ------
4958+ chunk_coords: ChunkCoords
4959+ The coordinates of each chunk in the selection.
4960+ """
4961+ return _iter_grid (array .cdata_shape , origin = origin , selection_shape = selection_shape )
4962+
4963+
4964+ def iter_chunk_keys (
4965+ array : Array | AsyncArray [Any ],
4966+ * ,
4967+ origin : Sequence [int ] | None = None ,
4968+ selection_shape : Sequence [int ] | None = None ,
4969+ ) -> Iterator [str ]:
4970+ """
4971+ Iterate over the storage keys of each chunk, relative to an optional origin, and optionally
4972+ limited to a contiguous region in chunk grid coordinates.
4973+
4974+ Parameters
4975+ ----------
4976+ array : Array | AsyncArray
4977+ The array to iterate over.
4978+ origin : Sequence[int] | None, default=None
4979+ The origin of the selection relative to the array's chunk grid.
4980+ selection_shape : Sequence[int] | None, default=None
4981+ The shape of the selection in chunk grid coordinates.
4982+
4983+ Yields
4984+ ------
4985+ key: str
4986+ The storage key of each chunk in the selection.
4987+ """
4988+ # Iterate over the coordinates of chunks in chunk grid space.
4989+ for k in iter_chunk_coords (array , origin = origin , selection_shape = selection_shape ):
4990+ # Encode the chunk key from the chunk coordinates.
4991+ yield array .metadata .encode_chunk_key (k )
4992+
4993+
4994+ def iter_chunk_regions (
4995+ array : Array | AsyncArray [Any ],
4996+ * ,
4997+ origin : Sequence [int ] | None = None ,
4998+ selection_shape : Sequence [int ] | None = None ,
4999+ ) -> Iterator [tuple [slice , ...]]:
5000+ """
5001+ Iterate over the regions spanned by each chunk.
5002+
5003+ Parameters
5004+ ----------
5005+ array : Array | AsyncArray
5006+ The array to iterate over.
5007+ origin : Sequence[int] | None, default=None
5008+ The origin of the selection relative to the array's chunk grid.
5009+ selection_shape : Sequence[int] | None, default=None
5010+ The shape of the selection in chunk grid coordinates.
5011+
5012+ Yields
5013+ ------
5014+ region: tuple[slice, ...]
5015+ A tuple of slice objects representing the region spanned by each chunk in the selection.
5016+ """
5017+ for cgrid_position in iter_chunk_coords (array , origin = origin , selection_shape = selection_shape ):
5018+ out : tuple [slice , ...] = ()
5019+ for c_pos , c_shape in zip (cgrid_position , array .chunks , strict = False ):
5020+ start = c_pos * c_shape
5021+ stop = start + c_shape
5022+ out += (slice (start , stop , 1 ),)
5023+ yield out
0 commit comments