@@ -224,7 +224,8 @@ class ZarrStore(AbstractWritableDataStore):
224
224
"""
225
225
226
226
@classmethod
227
- def open_group (cls , store , mode = 'r' , synchronizer = None , group = None ):
227
+ def open_group (cls , store , mode = 'r' , synchronizer = None , group = None ,
228
+ consolidated = False , consolidate_on_close = False ):
228
229
import zarr
229
230
min_zarr = '2.2'
230
231
@@ -234,15 +235,27 @@ def open_group(cls, store, mode='r', synchronizer=None, group=None):
234
235
"installation "
235
236
"http://zarr.readthedocs.io/en/stable/"
236
237
"#installation" % min_zarr )
237
- zarr_group = zarr .open_group (store = store , mode = mode ,
238
- synchronizer = synchronizer , path = group )
239
- return cls (zarr_group )
240
238
241
- def __init__ (self , zarr_group ):
239
+ if consolidated or consolidate_on_close :
240
+ if LooseVersion (zarr .__version__ ) <= '2.2.1.dev2' : # pragma: no cover
241
+ raise NotImplementedError ("Zarr version 2.2.1.dev2 or greater "
242
+ "is required by for consolidated "
243
+ "metadata." )
244
+
245
+ open_kwargs = dict (mode = mode , synchronizer = synchronizer , path = group )
246
+ if consolidated :
247
+ # TODO: an option to pass the metadata_key keyword
248
+ zarr_group = zarr .open_consolidated (store , ** open_kwargs )
249
+ else :
250
+ zarr_group = zarr .open_group (store , ** open_kwargs )
251
+ return cls (zarr_group , consolidate_on_close )
252
+
253
+ def __init__ (self , zarr_group , consolidate_on_close = False ):
242
254
self .ds = zarr_group
243
255
self ._read_only = self .ds .read_only
244
256
self ._synchronizer = self .ds .synchronizer
245
257
self ._group = self .ds .path
258
+ self ._consolidate_on_close = consolidate_on_close
246
259
247
260
def open_store_variable (self , name , zarr_array ):
248
261
data = indexing .LazilyOuterIndexedArray (ZarrArrayWrapper (name , self ))
@@ -333,11 +346,16 @@ def store(self, variables, attributes, *args, **kwargs):
333
346
def sync (self ):
334
347
pass
335
348
349
+ def close (self ):
350
+ if self ._consolidate_on_close :
351
+ import zarr
352
+ zarr .consolidate_metadata (self .ds .store )
353
+
336
354
337
355
def open_zarr (store , group = None , synchronizer = None , auto_chunk = True ,
338
356
decode_cf = True , mask_and_scale = True , decode_times = True ,
339
357
concat_characters = True , decode_coords = True ,
340
- drop_variables = None ):
358
+ drop_variables = None , consolidated = False ):
341
359
"""Load and decode a dataset from a Zarr store.
342
360
343
361
.. note:: Experimental
@@ -383,10 +401,13 @@ def open_zarr(store, group=None, synchronizer=None, auto_chunk=True,
383
401
decode_coords : bool, optional
384
402
If True, decode the 'coordinates' attribute to identify coordinates in
385
403
the resulting dataset.
386
- drop_variables: string or iterable, optional
404
+ drop_variables : string or iterable, optional
387
405
A variable or list of variables to exclude from being parsed from the
388
406
dataset. This may be useful to drop variables with problems or
389
407
inconsistent values.
408
+ consolidated : bool, optional
409
+ Whether to open the store using zarr's consolidated metadata
410
+ capability. Only works for stores that have already been consolidated.
390
411
391
412
Returns
392
413
-------
@@ -423,7 +444,7 @@ def maybe_decode_store(store, lock=False):
423
444
mode = 'r'
424
445
zarr_store = ZarrStore .open_group (store , mode = mode ,
425
446
synchronizer = synchronizer ,
426
- group = group )
447
+ group = group , consolidated = consolidated )
427
448
ds = maybe_decode_store (zarr_store )
428
449
429
450
# auto chunking needs to be here and not in ZarrStore because variable
0 commit comments