@@ -308,6 +308,18 @@ def _determine_zarr_chunks(
308308
309309
310310def _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+
311323 # Zarr arrays do not have dimensions. To get around this problem, we add
312324 # an attribute that specifies the dimension. We have to hide this attribute
313325 # when we send the attributes to the user.
@@ -919,6 +931,7 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
919931 import zarr
920932
921933 existing_keys = tuple (self .zarr_group .array_keys ())
934+ is_zarr_v3_format = _zarr_v3 () and self .zarr_group .metadata .zarr_format == 3
922935
923936 for vn , v in variables .items ():
924937 name = _encode_variable_name (vn )
@@ -1022,7 +1035,10 @@ def set_variables(self, variables, check_encoding_set, writer, unlimited_dims=No
10221035 # new variable
10231036 encoded_attrs = {}
10241037 # 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
10261042 for k2 , v2 in attrs .items ():
10271043 encoded_attrs [k2 ] = self .encode_attribute (v2 )
10281044
0 commit comments