@@ -308,6 +308,18 @@ def _determine_zarr_chunks(
308
308
309
309
310
310
def _get_zarr_dims_and_attrs (zarr_obj , dimension_key , try_nczarr ):
311
+ # Zarr V3 explicitly stores the dimension names in the metadata
312
+ try :
313
+ # if this exists, we are looking at a Zarr V3 array
314
+ # convert None to empty tuple
315
+ dimensions = zarr_obj .metadata .dimension_names or ()
316
+ except AttributeError :
317
+ # continue to old code path
318
+ pass
319
+ else :
320
+ attributes = dict (zarr_obj .attrs )
321
+ return dimensions , attributes
322
+
311
323
# Zarr arrays do not have dimensions. To get around this problem, we add
312
324
# an attribute that specifies the dimension. We have to hide this attribute
313
325
# when we send the attributes to the user.
@@ -919,6 +931,7 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
919
931
import zarr
920
932
921
933
existing_keys = tuple (self .zarr_group .array_keys ())
934
+ is_zarr_v3_format = _zarr_v3 () and self .zarr_group .metadata .zarr_format == 3
922
935
923
936
for vn , v in variables .items ():
924
937
name = _encode_variable_name (vn )
@@ -1022,7 +1035,10 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
1022
1035
# new variable
1023
1036
encoded_attrs = {}
1024
1037
# the magic for storing the hidden dimension data
1025
- encoded_attrs [DIMENSION_KEY ] = dims
1038
+ if is_zarr_v3_format :
1039
+ encoding ["dimension_names" ] = dims
1040
+ else :
1041
+ encoded_attrs [DIMENSION_KEY ] = dims
1026
1042
for k2 , v2 in attrs .items ():
1027
1043
encoded_attrs [k2 ] = self .encode_attribute (v2 )
1028
1044
0 commit comments