4
4
from collections import Mapping
5
5
import io
6
6
import re
7
+ import itertools
8
+
9
+
10
+ import numpy as np
7
11
8
12
9
13
from zarr .core import Array
@@ -611,6 +615,7 @@ def _copy(log, source, dest, name, root, shallow, without_attrs, overwrite, **cr
611
615
else :
612
616
if dest_h5py :
613
617
# zarr -> h5py; use some vaguely sensible defaults
618
+ kws .setdefault ('chunks' , True )
614
619
kws .setdefault ('compression' , 'gzip' )
615
620
kws .setdefault ('compression_opts' , 1 )
616
621
kws .setdefault ('shuffle' , False )
@@ -621,9 +626,14 @@ def _copy(log, source, dest, name, root, shallow, without_attrs, overwrite, **cr
621
626
# create new dataset in destination
622
627
ds = dest .create_dataset (name , shape = source .shape , dtype = source .dtype , ** kws )
623
628
624
- # copy data - N.B., if dest is h5py this will load all data into memory
629
+ # copy data - N.B., go chunk by chunk to avoid loading everything into memory
625
630
log ('{} -> {}' .format (source .name , ds .name ))
626
- ds [:] = source
631
+ shape = ds .shape
632
+ chunks = ds .chunks
633
+ chunk_offsets = [range (0 , s , c ) for s , c in zip (shape , chunks )]
634
+ for offset in itertools .product (* chunk_offsets ):
635
+ sel = tuple (slice (o , min (s , o + c )) for o , s , c in zip (offset , shape , chunks ))
636
+ ds [sel ] = source [sel ]
627
637
628
638
# copy attributes
629
639
if not without_attrs :
0 commit comments