Skip to content

Commit ccaa8bf

Browse files
committed
* make write_array_direct more DRY
* update pixi lock
1 parent 6329f0f commit ccaa8bf

File tree

2 files changed

+33
-98
lines changed

2 files changed

+33
-98
lines changed

ngff_zarr/to_ngff_zarr.py

Lines changed: 31 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -377,100 +377,38 @@ def _write_array_direct(
377377
"""Write an array directly using dask.array.to_zarr."""
378378
arr = _prep_for_to_zarr(store, arr)
379379

380-
if format_kwargs.get("zarr_format") == 3:
381-
if region is not None and zarr_array is not None:
382-
dask.array.to_zarr(
383-
arr,
384-
zarr_array,
385-
region=region,
386-
component=path,
387-
overwrite=False,
388-
compute=True,
389-
return_stored=False,
390-
**sharding_kwargs,
391-
**zarr_kwargs,
392-
**format_kwargs,
393-
**dimension_names_kwargs,
394-
**kwargs,
395-
)
396-
397-
else:
398-
array = zarr.create_array(
399-
store=store,
400-
name=path,
401-
shape=arr.shape,
402-
dtype=arr.dtype,
403-
**sharding_kwargs,
404-
**zarr_kwargs,
405-
**format_kwargs,
406-
**dimension_names_kwargs,
407-
**kwargs,
408-
)
409-
410-
array[:] = arr.compute()
411-
412-
elif format_kwargs.get("zarr_format") == 2:
413-
if region is not None and zarr_array is not None:
414-
dask.array.to_zarr(
415-
arr,
416-
zarr_array,
417-
region=region,
418-
component=path,
419-
overwrite=False,
420-
compute=True,
421-
return_stored=False,
422-
**sharding_kwargs,
423-
**zarr_kwargs,
424-
**format_kwargs,
425-
**dimension_names_kwargs,
426-
**kwargs,
427-
)
380+
zarr_fmt = format_kwargs.get("zarr_format")
381+
to_zarr_kwargs = {
382+
**sharding_kwargs,
383+
**zarr_kwargs,
384+
**format_kwargs,
385+
**dimension_names_kwargs,
386+
**kwargs,
387+
}
428388

429-
else:
430-
dask.array.to_zarr(
431-
arr,
432-
store,
433-
component=path,
434-
overwrite=False,
435-
compute=True,
436-
return_stored=False,
437-
**sharding_kwargs,
438-
**zarr_kwargs,
439-
**format_kwargs,
440-
**dimension_names_kwargs,
441-
**kwargs,
442-
)
389+
if zarr_fmt == 3 and region is None and zarr_array is None:
390+
# Zarr v3, no region/zarr_array: use zarr.create_array and assign
391+
array = zarr.create_array(
392+
store=store,
393+
name=path,
394+
shape=arr.shape,
395+
dtype=arr.dtype,
396+
**to_zarr_kwargs,
397+
)
398+
array[:] = arr.compute()
443399
else:
444-
if region is not None and zarr_array is not None:
445-
dask.array.to_zarr(
446-
arr,
447-
zarr_array,
448-
region=region,
449-
component=path,
450-
overwrite=False,
451-
compute=True,
452-
return_stored=False,
453-
**sharding_kwargs,
454-
**zarr_kwargs,
455-
**format_kwargs,
456-
**dimension_names_kwargs,
457-
**kwargs,
458-
)
459-
460-
else:
461-
dask.array.to_zarr(
462-
arr,
463-
store,
464-
component=path,
465-
overwrite=False,
466-
compute=True,
467-
return_stored=False,
468-
**sharding_kwargs,
469-
**zarr_kwargs,
470-
**format_kwargs,
471-
**dimension_names_kwargs,
472-
**kwargs,
473-
)
400+
# All other cases: use dask.array.to_zarr
401+
target = zarr_array if (region is not None and zarr_array is not None) else store
402+
dask.array.to_zarr(
403+
arr,
404+
target,
405+
region=region if (region is not None and zarr_array is not None) else None,
406+
component=path,
407+
overwrite=False,
408+
compute=True,
409+
return_stored=False,
410+
**to_zarr_kwargs,
411+
)
474412

475413

476414

@@ -844,10 +782,7 @@ def to_ngff_zarr(
844782
format_kwargs = {"zarr_format": zarr_format} if zarr_version_major >= 3 else {}
845783
_zarr_kwargs = zarr_kwargs.copy()
846784

847-
# Force dimension_separator only for Zarr V2.
848-
# Zarr V3 defaults to '/' for dimension names,
849-
# and dimension_separator is currently not yet ported.
850-
if zarr_format == 2 and zarr_version_major < 3:
785+
if zarr_format == 2 and zarr_version_major >= 3:
851786
_zarr_kwargs["dimension_separator"] = "/"
852787

853788
# Process each scale level

pixi.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)