@@ -142,10 +142,20 @@ def test_some_slices_local_output_to_existing_dir_force_new(self):
142142 zappend (slices , target_dir = target_dir , force_new = True )
143143 self .assertEqual (False , lock_file .exists ())
144144
145- def test_some_slices_with_class_slice_source (self ):
145+ def test_some_slices_with_slice_source_class (self ):
146+ class DropTsm (SliceSource ):
147+ def __init__ (self , slice_ds ):
148+ self .slice_ds = slice_ds
149+
150+ def get_dataset (self ) -> xr .Dataset :
151+ return self .slice_ds .drop_vars (["tsm" ])
152+
153+ def dispose (self ):
154+ pass
155+
146156 target_dir = "memory://target.zarr"
147157 slices = [make_test_dataset (index = 3 * i ) for i in range (3 )]
148- zappend (slices , target_dir = target_dir , slice_source = MySliceSource )
158+ zappend (slices , target_dir = target_dir , slice_source = DropTsm )
149159 ds = xr .open_zarr (target_dir )
150160 self .assertEqual ({"time" : 9 , "y" : 50 , "x" : 100 }, ds .sizes )
151161 self .assertEqual ({"chl" }, set (ds .data_vars ))
@@ -158,13 +168,13 @@ def test_some_slices_with_class_slice_source(self):
158168 ds .attrs ,
159169 )
160170
161- def test_some_slices_with_func_slice_source (self ):
162- def process_slice (slice_ds : xr .Dataset ) -> SliceSource :
163- return MySliceSource ( slice_ds )
171+ def test_some_slices_with_slice_source_func (self ):
172+ def drop_tsm (slice_ds : xr .Dataset ) -> xr . Dataset :
173+ return slice_ds . drop_vars ([ "tsm" ] )
164174
165175 target_dir = "memory://target.zarr"
166176 slices = [make_test_dataset (index = 3 * i ) for i in range (3 )]
167- zappend (slices , target_dir = target_dir , slice_source = process_slice )
177+ zappend (slices , target_dir = target_dir , slice_source = drop_tsm )
168178 ds = xr .open_zarr (target_dir )
169179 self .assertEqual ({"time" : 9 , "y" : 50 , "x" : 100 }, ds .sizes )
170180 self .assertEqual ({"chl" }, set (ds .data_vars ))
@@ -177,9 +187,67 @@ def process_slice(slice_ds: xr.Dataset) -> SliceSource:
177187 ds .attrs ,
178188 )
179189
180- def test_some_slices_with_cropping_slice_source (self ):
181- # TODO: implement me after #78
182- pass
190+ # See https://github.com/bcdev/zappend/issues/77
191+ def test_some_slices_with_cropping_slice_source_no_chunks_spec (self ):
192+ def crop_ds (slice_ds : xr .Dataset ) -> xr .Dataset :
193+ w = slice_ds .x .size
194+ h = slice_ds .y .size
195+ return slice_ds .isel (x = slice (5 , w - 5 ), y = slice (5 , h - 5 ))
196+
197+ target_dir = "memory://target.zarr"
198+ slices = [make_test_dataset (index = 3 * i ) for i in range (3 )]
199+ zappend (slices , target_dir = target_dir , slice_source = crop_ds )
200+ ds = xr .open_zarr (target_dir )
201+ self .assertEqual ({"time" : 9 , "y" : 40 , "x" : 90 }, ds .sizes )
202+ self .assertEqual ({"chl" , "tsm" }, set (ds .data_vars ))
203+ self .assertEqual ({"time" , "y" , "x" }, set (ds .coords ))
204+ self .assertEqual ((90 ,), ds .x .encoding .get ("chunks" ))
205+ self .assertEqual ((40 ,), ds .y .encoding .get ("chunks" ))
206+ self .assertEqual ((3 ,), ds .time .encoding .get ("chunks" ))
207+ # Chunk sizes are the ones of the original array, because we have not
208+ # specified chunks in encoding.
209+ self .assertEqual ((1 , 25 , 45 ), ds .chl .encoding .get ("chunks" ))
210+ self .assertEqual ((1 , 25 , 45 ), ds .tsm .encoding .get ("chunks" ))
211+
212+ # See https://github.com/bcdev/zappend/issues/77
213+ def test_some_slices_with_cropping_slice_source_with_chunks_spec (self ):
214+ def crop_ds (slice_ds : xr .Dataset ) -> xr .Dataset :
215+ w = slice_ds .x .size
216+ h = slice_ds .y .size
217+ return slice_ds .isel (x = slice (5 , w - 5 ), y = slice (5 , h - 5 ))
218+
219+ variables = {
220+ "*" : {
221+ "encoding" : {
222+ "chunks" : None ,
223+ }
224+ },
225+ "chl" : {
226+ "encoding" : {
227+ "chunks" : [1 , None , None ],
228+ }
229+ },
230+ "tsm" : {
231+ "encoding" : {
232+ "chunks" : [None , 25 , 50 ],
233+ }
234+ },
235+ }
236+
237+ target_dir = "memory://target.zarr"
238+ slices = [make_test_dataset (index = 3 * i ) for i in range (3 )]
239+ zappend (
240+ slices , target_dir = target_dir , slice_source = crop_ds , variables = variables
241+ )
242+ ds = xr .open_zarr (target_dir )
243+ self .assertEqual ({"time" : 9 , "y" : 40 , "x" : 90 }, ds .sizes )
244+ self .assertEqual ({"chl" , "tsm" }, set (ds .data_vars ))
245+ self .assertEqual ({"time" , "y" , "x" }, set (ds .coords ))
246+ self .assertEqual ((90 ,), ds .x .encoding .get ("chunks" ))
247+ self .assertEqual ((40 ,), ds .y .encoding .get ("chunks" ))
248+ self .assertEqual ((3 ,), ds .time .encoding .get ("chunks" ))
249+ self .assertEqual ((1 , 40 , 90 ), ds .chl .encoding .get ("chunks" ))
250+ self .assertEqual ((3 , 25 , 50 ), ds .tsm .encoding .get ("chunks" ))
183251
184252 def test_some_slices_with_inc_append_step (self ):
185253 target_dir = "memory://target.zarr"
@@ -395,14 +463,3 @@ def test_some_slices_with_profiling(self):
395463 finally :
396464 if os .path .exists ("prof.out" ):
397465 os .remove ("prof.out" )
398-
399-
400- class MySliceSource (SliceSource ):
401- def __init__ (self , slice_ds ):
402- self .slice_ds = slice_ds
403-
404- def get_dataset (self ) -> xr .Dataset :
405- return self .slice_ds .drop_vars (["tsm" ])
406-
407- def dispose (self ):
408- pass
0 commit comments