|
3 | 3 | import zarr
|
4 | 4 |
|
5 | 5 | from cubed.types import T_DType, T_RegularChunks, T_Shape, T_Store
|
6 |
| -from cubed.utils import join_path |
7 | 6 |
|
8 | 7 |
|
9 | 8 | class ZarrV3ArrayGroup(dict):
|
@@ -40,41 +39,29 @@ def open_zarr_v3_array(
|
40 | 39 | if isinstance(chunks, int):
|
41 | 40 | chunks = (chunks,)
|
42 | 41 |
|
43 |
| - if mode in ("r", "r+"): |
44 |
| - # TODO: remove when https://github.com/zarr-developers/zarr-python/issues/1978 is fixed |
45 |
| - if mode == "r+": |
46 |
| - mode = "w" |
47 |
| - if dtype is None or not hasattr(dtype, "fields") or dtype.fields is None: |
48 |
| - return zarr.open(store=store, mode=mode, path=path) |
| 42 | + if dtype is None or not hasattr(dtype, "fields") or dtype.fields is None: |
| 43 | + return zarr.open( |
| 44 | + store=store, |
| 45 | + mode=mode, |
| 46 | + shape=shape, |
| 47 | + dtype=dtype, |
| 48 | + chunks=chunks, |
| 49 | + path=path, |
| 50 | + ) |
| 51 | + |
| 52 | + group = zarr.open_group(store=store, mode=mode, path=path) |
| 53 | + |
| 54 | + # create/open all the arrays in the group |
| 55 | + ret = ZarrV3ArrayGroup(shape=shape, dtype=dtype, chunks=chunks) |
| 56 | + for field in dtype.fields: |
| 57 | + field_dtype, _ = dtype.fields[field] |
| 58 | + if mode in ("r", "r+"): |
| 59 | + ret[field] = group[field] |
49 | 60 | else:
|
50 |
| - ret = ZarrV3ArrayGroup(shape=shape, dtype=dtype, chunks=chunks) |
51 |
| - for field in dtype.fields: |
52 |
| - field_dtype, _ = dtype.fields[field] |
53 |
| - field_path = field if path is None else join_path(path, field) |
54 |
| - ret[field] = zarr.open(store=store, mode=mode, path=field_path) |
55 |
| - return ret |
56 |
| - else: |
57 |
| - overwrite = True if mode == "a" else False |
58 |
| - if dtype is None or not hasattr(dtype, "fields") or dtype.fields is None: |
59 |
| - return zarr.create( |
| 61 | + ret[field] = group.create_array( |
| 62 | + field, |
60 | 63 | shape=shape,
|
61 |
| - dtype=dtype, |
| 64 | + dtype=field_dtype, |
62 | 65 | chunk_shape=chunks,
|
63 |
| - store=store, |
64 |
| - overwrite=overwrite, |
65 |
| - path=path, |
66 | 66 | )
|
67 |
| - else: |
68 |
| - ret = ZarrV3ArrayGroup(shape=shape, dtype=dtype, chunks=chunks) |
69 |
| - for field in dtype.fields: |
70 |
| - field_dtype, _ = dtype.fields[field] |
71 |
| - field_path = field if path is None else join_path(path, field) |
72 |
| - ret[field] = zarr.create( |
73 |
| - shape=shape, |
74 |
| - dtype=field_dtype, |
75 |
| - chunk_shape=chunks, |
76 |
| - store=store, |
77 |
| - overwrite=overwrite, |
78 |
| - path=field_path, |
79 |
| - ) |
80 |
| - return ret |
| 67 | + return ret |
0 commit comments