@@ -115,8 +115,9 @@ def test_to_netcdf(self, tmpdir, simple_datatree):
115
115
original_dt = simple_datatree
116
116
original_dt .to_netcdf (filepath , engine = self .engine )
117
117
118
- roundtrip_dt = open_datatree (filepath , engine = self .engine )
119
- assert_equal (original_dt , roundtrip_dt )
118
+ with open_datatree (filepath , engine = self .engine ) as roundtrip_dt :
119
+ assert roundtrip_dt ._close is not None
120
+ assert_equal (original_dt , roundtrip_dt )
120
121
121
122
def test_to_netcdf_inherited_coords (self , tmpdir ):
122
123
filepath = tmpdir / "test.nc"
@@ -128,10 +129,10 @@ def test_to_netcdf_inherited_coords(self, tmpdir):
128
129
)
129
130
original_dt .to_netcdf (filepath , engine = self .engine )
130
131
131
- roundtrip_dt = open_datatree (filepath , engine = self .engine )
132
- assert_equal (original_dt , roundtrip_dt )
133
- subtree = cast (DataTree , roundtrip_dt ["/sub" ])
134
- assert "x" not in subtree .to_dataset (inherit = False ).coords
132
+ with open_datatree (filepath , engine = self .engine ) as roundtrip_dt :
133
+ assert_equal (original_dt , roundtrip_dt )
134
+ subtree = cast (DataTree , roundtrip_dt ["/sub" ])
135
+ assert "x" not in subtree .to_dataset (inherit = False ).coords
135
136
136
137
def test_netcdf_encoding (self , tmpdir , simple_datatree ):
137
138
filepath = tmpdir / "test.nc"
@@ -142,14 +143,13 @@ def test_netcdf_encoding(self, tmpdir, simple_datatree):
142
143
enc = {"/set2" : {var : comp for var in original_dt ["/set2" ].dataset .data_vars }}
143
144
144
145
original_dt .to_netcdf (filepath , encoding = enc , engine = self .engine )
145
- roundtrip_dt = open_datatree (filepath , engine = self .engine )
146
+ with open_datatree (filepath , engine = self .engine ) as roundtrip_dt :
147
+ assert roundtrip_dt ["/set2/a" ].encoding ["zlib" ] == comp ["zlib" ]
148
+ assert roundtrip_dt ["/set2/a" ].encoding ["complevel" ] == comp ["complevel" ]
146
149
147
- assert roundtrip_dt ["/set2/a" ].encoding ["zlib" ] == comp ["zlib" ]
148
- assert roundtrip_dt ["/set2/a" ].encoding ["complevel" ] == comp ["complevel" ]
149
-
150
- enc ["/not/a/group" ] = {"foo" : "bar" } # type: ignore[dict-item]
151
- with pytest .raises (ValueError , match = "unexpected encoding group.*" ):
152
- original_dt .to_netcdf (filepath , encoding = enc , engine = self .engine )
150
+ enc ["/not/a/group" ] = {"foo" : "bar" } # type: ignore[dict-item]
151
+ with pytest .raises (ValueError , match = "unexpected encoding group.*" ):
152
+ original_dt .to_netcdf (filepath , encoding = enc , engine = self .engine )
153
153
154
154
155
155
@requires_netCDF4
@@ -179,18 +179,17 @@ def test_open_groups(self, unaligned_datatree_nc) -> None:
179
179
assert "/Group1" in unaligned_dict_of_datasets .keys ()
180
180
assert "/Group1/subgroup1" in unaligned_dict_of_datasets .keys ()
181
181
# Check that group name returns the correct datasets
182
- assert_identical (
183
- unaligned_dict_of_datasets ["/" ],
184
- xr .open_dataset (unaligned_datatree_nc , group = "/" ),
185
- )
186
- assert_identical (
187
- unaligned_dict_of_datasets ["/Group1" ],
188
- xr .open_dataset (unaligned_datatree_nc , group = "Group1" ),
189
- )
190
- assert_identical (
191
- unaligned_dict_of_datasets ["/Group1/subgroup1" ],
192
- xr .open_dataset (unaligned_datatree_nc , group = "/Group1/subgroup1" ),
193
- )
182
+ with xr .open_dataset (unaligned_datatree_nc , group = "/" ) as expected :
183
+ assert_identical (unaligned_dict_of_datasets ["/" ], expected )
184
+ with xr .open_dataset (unaligned_datatree_nc , group = "Group1" ) as expected :
185
+ assert_identical (unaligned_dict_of_datasets ["/Group1" ], expected )
186
+ with xr .open_dataset (
187
+ unaligned_datatree_nc , group = "/Group1/subgroup1"
188
+ ) as expected :
189
+ assert_identical (unaligned_dict_of_datasets ["/Group1/subgroup1" ], expected )
190
+
191
+ for ds in unaligned_dict_of_datasets .values ():
192
+ ds .close ()
194
193
195
194
def test_open_groups_to_dict (self , tmpdir ) -> None :
196
195
"""Create an aligned netCDF4 with the following structure to test `open_groups`
@@ -234,8 +233,10 @@ def test_open_groups_to_dict(self, tmpdir) -> None:
234
233
235
234
aligned_dict_of_datasets = open_groups (filepath )
236
235
aligned_dt = DataTree .from_dict (aligned_dict_of_datasets )
237
-
238
- assert open_datatree (filepath ).identical (aligned_dt )
236
+ with open_datatree (filepath ) as opened_tree :
237
+ assert opened_tree .identical (aligned_dt )
238
+ for ds in aligned_dict_of_datasets .values ():
239
+ ds .close ()
239
240
240
241
241
242
@requires_h5netcdf
@@ -252,8 +253,8 @@ def test_to_zarr(self, tmpdir, simple_datatree):
252
253
original_dt = simple_datatree
253
254
original_dt .to_zarr (filepath )
254
255
255
- roundtrip_dt = open_datatree (filepath , engine = "zarr" )
256
- assert_equal (original_dt , roundtrip_dt )
256
+ with open_datatree (filepath , engine = "zarr" ) as roundtrip_dt :
257
+ assert_equal (original_dt , roundtrip_dt )
257
258
258
259
def test_zarr_encoding (self , tmpdir , simple_datatree ):
259
260
import zarr
@@ -264,14 +265,14 @@ def test_zarr_encoding(self, tmpdir, simple_datatree):
264
265
comp = {"compressor" : zarr .Blosc (cname = "zstd" , clevel = 3 , shuffle = 2 )}
265
266
enc = {"/set2" : {var : comp for var in original_dt ["/set2" ].dataset .data_vars }}
266
267
original_dt .to_zarr (filepath , encoding = enc )
267
- roundtrip_dt = open_datatree (filepath , engine = "zarr" )
268
268
269
- print (roundtrip_dt ["/set2/a" ].encoding )
270
- assert roundtrip_dt ["/set2/a" ].encoding ["compressor" ] == comp ["compressor" ]
269
+ with open_datatree (filepath , engine = "zarr" ) as roundtrip_dt :
270
+ print (roundtrip_dt ["/set2/a" ].encoding )
271
+ assert roundtrip_dt ["/set2/a" ].encoding ["compressor" ] == comp ["compressor" ]
271
272
272
- enc ["/not/a/group" ] = {"foo" : "bar" } # type: ignore[dict-item]
273
- with pytest .raises (ValueError , match = "unexpected encoding group.*" ):
274
- original_dt .to_zarr (filepath , encoding = enc , engine = "zarr" )
273
+ enc ["/not/a/group" ] = {"foo" : "bar" } # type: ignore[dict-item]
274
+ with pytest .raises (ValueError , match = "unexpected encoding group.*" ):
275
+ original_dt .to_zarr (filepath , encoding = enc , engine = "zarr" )
275
276
276
277
def test_to_zarr_zip_store (self , tmpdir , simple_datatree ):
277
278
from zarr .storage import ZipStore
@@ -281,8 +282,8 @@ def test_to_zarr_zip_store(self, tmpdir, simple_datatree):
281
282
store = ZipStore (filepath )
282
283
original_dt .to_zarr (store )
283
284
284
- roundtrip_dt = open_datatree (store , engine = "zarr" )
285
- assert_equal (original_dt , roundtrip_dt )
285
+ with open_datatree (store , engine = "zarr" ) as roundtrip_dt :
286
+ assert_equal (original_dt , roundtrip_dt )
286
287
287
288
def test_to_zarr_not_consolidated (self , tmpdir , simple_datatree ):
288
289
filepath = tmpdir / "test.zarr"
@@ -295,8 +296,8 @@ def test_to_zarr_not_consolidated(self, tmpdir, simple_datatree):
295
296
assert not s1zmetadata .exists ()
296
297
297
298
with pytest .warns (RuntimeWarning , match = "consolidated" ):
298
- roundtrip_dt = open_datatree (filepath , engine = "zarr" )
299
- assert_equal (original_dt , roundtrip_dt )
299
+ with open_datatree (filepath , engine = "zarr" ) as roundtrip_dt :
300
+ assert_equal (original_dt , roundtrip_dt )
300
301
301
302
def test_to_zarr_default_write_mode (self , tmpdir , simple_datatree ):
302
303
import zarr
@@ -317,10 +318,10 @@ def test_to_zarr_inherited_coords(self, tmpdir):
317
318
filepath = tmpdir / "test.zarr"
318
319
original_dt .to_zarr (filepath )
319
320
320
- roundtrip_dt = open_datatree (filepath , engine = "zarr" )
321
- assert_equal (original_dt , roundtrip_dt )
322
- subtree = cast (DataTree , roundtrip_dt ["/sub" ])
323
- assert "x" not in subtree .to_dataset (inherit = False ).coords
321
+ with open_datatree (filepath , engine = "zarr" ) as roundtrip_dt :
322
+ assert_equal (original_dt , roundtrip_dt )
323
+ subtree = cast (DataTree , roundtrip_dt ["/sub" ])
324
+ assert "x" not in subtree .to_dataset (inherit = False ).coords
324
325
325
326
def test_open_groups_round_trip (self , tmpdir , simple_datatree ) -> None :
326
327
"""Test `open_groups` opens a zarr store with the `simple_datatree` structure."""
@@ -331,7 +332,11 @@ def test_open_groups_round_trip(self, tmpdir, simple_datatree) -> None:
331
332
roundtrip_dict = open_groups (filepath , engine = "zarr" )
332
333
roundtrip_dt = DataTree .from_dict (roundtrip_dict )
333
334
334
- assert open_datatree (filepath , engine = "zarr" ).identical (roundtrip_dt )
335
+ with open_datatree (filepath , engine = "zarr" ) as opened_tree :
336
+ assert opened_tree .identical (roundtrip_dt )
337
+
338
+ for ds in roundtrip_dict .values ():
339
+ ds .close ()
335
340
336
341
def test_open_datatree (self , unaligned_datatree_zarr ) -> None :
337
342
"""Test if `open_datatree` fails to open a zarr store with an unaligned group hierarchy."""
@@ -353,21 +358,22 @@ def test_open_groups(self, unaligned_datatree_zarr) -> None:
353
358
assert "/Group1/subgroup1" in unaligned_dict_of_datasets .keys ()
354
359
assert "/Group2" in unaligned_dict_of_datasets .keys ()
355
360
# Check that group name returns the correct datasets
356
- assert_identical (
357
- unaligned_dict_of_datasets ["/" ],
358
- xr .open_dataset (unaligned_datatree_zarr , group = "/" , engine = "zarr" ),
359
- )
360
- assert_identical (
361
- unaligned_dict_of_datasets ["/Group1" ],
362
- xr .open_dataset (unaligned_datatree_zarr , group = "Group1" , engine = "zarr" ),
363
- )
364
- assert_identical (
365
- unaligned_dict_of_datasets ["/Group1/subgroup1" ],
366
- xr .open_dataset (
367
- unaligned_datatree_zarr , group = "/Group1/subgroup1" , engine = "zarr"
368
- ),
369
- )
370
- assert_identical (
371
- unaligned_dict_of_datasets ["/Group2" ],
372
- xr .open_dataset (unaligned_datatree_zarr , group = "/Group2" , engine = "zarr" ),
373
- )
361
+ with xr .open_dataset (
362
+ unaligned_datatree_zarr , group = "/" , engine = "zarr"
363
+ ) as expected :
364
+ assert_identical (unaligned_dict_of_datasets ["/" ], expected )
365
+ with xr .open_dataset (
366
+ unaligned_datatree_zarr , group = "Group1" , engine = "zarr"
367
+ ) as expected :
368
+ assert_identical (unaligned_dict_of_datasets ["/Group1" ], expected )
369
+ with xr .open_dataset (
370
+ unaligned_datatree_zarr , group = "/Group1/subgroup1" , engine = "zarr"
371
+ ) as expected :
372
+ assert_identical (unaligned_dict_of_datasets ["/Group1/subgroup1" ], expected )
373
+ with xr .open_dataset (
374
+ unaligned_datatree_zarr , group = "/Group2" , engine = "zarr"
375
+ ) as expected :
376
+ assert_identical (unaligned_dict_of_datasets ["/Group2" ], expected )
377
+
378
+ for ds in unaligned_dict_of_datasets .values ():
379
+ ds .close ()
0 commit comments