-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtest_xarray.py
More file actions
901 lines (806 loc) · 32.5 KB
/
test_xarray.py
File metadata and controls
901 lines (806 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
import functools
from collections.abc import Mapping
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Callable
import numpy as np
import pytest
import xarray as xr
import xarray.testing as xrt
from xarray import Dataset, open_dataset
from xarray.core.indexes import Index
from virtualizarr import open_virtual_dataset, open_virtual_mfdataset
from virtualizarr.manifests import ChunkManifest, ManifestArray
from virtualizarr.parsers import HDFParser
from virtualizarr.registry import ObjectStoreRegistry
from virtualizarr.tests import (
requires_dask,
requires_hdf5plugin,
requires_imagecodecs,
requires_lithops,
requires_network,
slow_test,
)
from virtualizarr.tests.utils import obstore_http, obstore_s3
def test_wrapping(array_v3_metadata):
chunks = (5, 10)
shape = (5, 20)
dtype = np.dtype("int32")
chunks_dict = {
"0.0": {"path": "/foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 200, "length": 100},
}
manifest = ChunkManifest(entries=chunks_dict)
marr = ManifestArray(
metadata=array_v3_metadata(chunks=chunks, shape=shape), chunkmanifest=manifest
)
ds = xr.Dataset({"a": (["x", "y"], marr)})
assert isinstance(ds["a"].data, ManifestArray)
assert ds["a"].shape == shape
assert ds["a"].dtype == dtype
assert ds["a"].chunks == chunks
class TestEquals:
# regression test for GH29 https://github.com/zarr-developers/VirtualiZarr/issues/29
def test_equals(self, array_v3_metadata):
chunks_dict1 = {
"0.0": {"path": "/foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
marr1 = ManifestArray(metadata=array_v3_metadata(), chunkmanifest=manifest1)
ds1 = xr.Dataset({"a": (["x", "y"], marr1)})
marr2 = ManifestArray(metadata=array_v3_metadata(), chunkmanifest=manifest1)
ds2 = xr.Dataset({"a": (["x", "y"], marr2)})
assert ds1.equals(ds2)
chunks_dict3 = {
"0.0": {"path": "/foo.nc", "offset": 300, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 400, "length": 100},
}
manifest3 = ChunkManifest(entries=chunks_dict3)
marr3 = ManifestArray(metadata=array_v3_metadata(), chunkmanifest=manifest3)
ds3 = xr.Dataset({"a": (["x", "y"], marr3)})
assert not ds1.equals(ds3)
# TODO refactor these tests by making some fixtures
class TestConcat:
def test_concat_along_existing_dim(self, array_v3_metadata):
# both manifest arrays in this example have the same metadata properties
metadata = array_v3_metadata(chunks=(1, 10), shape=(1, 20))
chunks_dict1 = {
"0.0": {"path": "/foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
marr1 = ManifestArray(metadata=metadata, chunkmanifest=manifest1)
ds1 = xr.Dataset({"a": (["x", "y"], marr1)})
chunks_dict2 = {
"0.0": {"path": "/foo.nc", "offset": 300, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 400, "length": 100},
}
manifest2 = ChunkManifest(entries=chunks_dict2)
marr2 = ManifestArray(metadata=metadata, chunkmanifest=manifest2)
ds2 = xr.Dataset({"a": (["x", "y"], marr2)})
result = xr.concat([ds1, ds2], dim="x")["a"]
assert result.indexes == {}
assert result.shape == (2, 20)
assert result.chunks == (1, 10)
assert result.data.manifest.dict() == {
"0.0": {"path": "file:///foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "file:///foo.nc", "offset": 200, "length": 100},
"1.0": {"path": "file:///foo.nc", "offset": 300, "length": 100},
"1.1": {"path": "file:///foo.nc", "offset": 400, "length": 100},
}
metadata_copy = metadata.to_dict().copy()
metadata_copy["shape"] = (2, 20)
assert result.data.metadata.to_dict() == metadata_copy
def test_concat_along_new_dim(self, array_v3_metadata):
# this calls np.stack internally
# both manifest arrays in this example have the same metadata properties
chunks_dict1 = {
"0.0": {"path": "/foo.nc", "offset": 100, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
metadata = array_v3_metadata(chunks=(5, 10), shape=(5, 20))
marr1 = ManifestArray(metadata=metadata, chunkmanifest=manifest1)
ds1 = xr.Dataset({"a": (["x", "y"], marr1)})
chunks_dict2 = {
"0.0": {"path": "/foo.nc", "offset": 300, "length": 100},
"0.1": {"path": "/foo.nc", "offset": 400, "length": 100},
}
manifest2 = ChunkManifest(entries=chunks_dict2)
marr2 = ManifestArray(metadata=metadata, chunkmanifest=manifest2)
ds2 = xr.Dataset({"a": (["x", "y"], marr2)})
result = xr.concat([ds1, ds2], dim="z")["a"]
assert result.indexes == {}
# xarray.concat adds new dimensions along axis=0
assert result.shape == (2, 5, 20)
assert result.chunks == (1, 5, 10)
assert result.data.manifest.dict() == {
"0.0.0": {"path": "file:///foo.nc", "offset": 100, "length": 100},
"0.0.1": {"path": "file:///foo.nc", "offset": 200, "length": 100},
"1.0.0": {"path": "file:///foo.nc", "offset": 300, "length": 100},
"1.0.1": {"path": "file:///foo.nc", "offset": 400, "length": 100},
}
metadata_copy = metadata.to_dict().copy()
metadata_copy["shape"] = (2, 5, 20)
metadata_copy["chunk_grid"]["configuration"]["chunk_shape"] = (1, 5, 10)
assert result.data.metadata.to_dict() == metadata_copy
def test_concat_dim_coords_along_existing_dim(self, array_v3_metadata):
# Tests that dimension coordinates don't automatically get new indexes on concat
# See https://github.com/pydata/xarray/issues/8871
# both manifest arrays in this example have the same metadata properties
chunks_dict1 = {
"0": {"path": "/foo.nc", "offset": 100, "length": 100},
"1": {"path": "/foo.nc", "offset": 200, "length": 100},
}
manifest1 = ChunkManifest(entries=chunks_dict1)
metadata = array_v3_metadata(chunks=(10,), shape=(20,))
marr1 = ManifestArray(metadata=metadata, chunkmanifest=manifest1)
coords = xr.Coordinates({"t": (["t"], marr1)}, indexes={})
ds1 = xr.Dataset(coords=coords)
chunks_dict2 = {
"0": {"path": "/foo.nc", "offset": 300, "length": 100},
"1": {"path": "/foo.nc", "offset": 400, "length": 100},
}
manifest2 = ChunkManifest(entries=chunks_dict2)
marr2 = ManifestArray(metadata=metadata, chunkmanifest=manifest2)
coords = xr.Coordinates({"t": (["t"], marr2)}, indexes={})
ds2 = xr.Dataset(coords=coords)
result = xr.concat([ds1, ds2], dim="t")["t"]
assert result.indexes == {}
assert result.shape == (40,)
assert result.chunks == (10,)
assert result.data.manifest.dict() == {
"0": {"path": "file:///foo.nc", "offset": 100, "length": 100},
"1": {"path": "file:///foo.nc", "offset": 200, "length": 100},
"2": {"path": "file:///foo.nc", "offset": 300, "length": 100},
"3": {"path": "file:///foo.nc", "offset": 400, "length": 100},
}
metadata_copy = metadata.to_dict().copy()
metadata_copy["shape"] = (40,)
metadata_copy["chunk_grid"]["configuration"]["chunk_shape"] = (10,)
assert result.data.metadata.to_dict() == metadata_copy
@requires_hdf5plugin
@requires_imagecodecs
class TestCombine:
def test_combine_by_coords(
self, netcdf4_files_factory: Callable[[], tuple[str, str]], local_registry
):
filepath1, filepath2 = netcdf4_files_factory()
parser = HDFParser()
with (
open_virtual_dataset(
url=filepath1,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds1,
open_virtual_dataset(
url=filepath2,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds2,
):
combined_vds = xr.combine_by_coords(
[vds2, vds1],
)
assert (
combined_vds.xindexes["time"].to_pandas_index().is_monotonic_increasing
)
def test_2d_combine_by_coords(
self,
netcdf4_files_factory_2d: Callable[[], tuple[str, str, str, str]],
local_registry,
):
filepath1, filepath2, filepath3, filepath4 = netcdf4_files_factory_2d()
parser = HDFParser()
with (
open_virtual_dataset(
url=filepath1,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds1,
open_virtual_dataset(
url=filepath2,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds2,
open_virtual_dataset(
url=filepath3,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds3,
open_virtual_dataset(
url=filepath4,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds4,
):
combined_vds = xr.combine_by_coords(
[vds2, vds1, vds4, vds3],
coords="minimal",
compat="override",
join="override",
)
assert combined_vds.sizes == {"lat": 20, "time": 2920, "lon": 53}
assert (
combined_vds.xindexes["time"].to_pandas_index().is_monotonic_increasing
)
assert (
combined_vds.xindexes["lat"].to_pandas_index().is_monotonic_decreasing
)
def test_2d_combine_nested(
self,
netcdf4_files_factory_2d: Callable[[], tuple[str, str, str, str]],
local_registry,
):
filepath1, filepath2, filepath3, filepath4 = netcdf4_files_factory_2d()
parser = HDFParser()
with (
open_virtual_dataset(
url=filepath1,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds1,
open_virtual_dataset(
url=filepath2,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds2,
open_virtual_dataset(
url=filepath3,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds3,
open_virtual_dataset(
url=filepath4,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds4,
):
combined_vds = xr.combine_nested(
[
[vds1, vds3],
[vds2, vds4],
],
concat_dim=["time", "lat"],
coords="minimal",
compat="override",
join="override",
)
assert combined_vds.sizes == {"lat": 20, "time": 2920, "lon": 53}
assert (
combined_vds.xindexes["time"].to_pandas_index().is_monotonic_increasing
)
assert (
combined_vds.xindexes["lat"].to_pandas_index().is_monotonic_decreasing
)
@pytest.mark.xfail(reason="Not yet implemented, see issue #18")
def test_combine_by_coords_keeping_manifestarrays(
self, netcdf4_files_factory: Callable[[], tuple[str, str]], local_registry
):
filepath1, filepath2 = netcdf4_files_factory()
parser = HDFParser()
with (
open_virtual_dataset(
url=filepath1, registry=local_registry, parser=parser
) as vds1,
open_virtual_dataset(
url=filepath2, registry=local_registry, parser=parser
) as vds2,
):
combined_vds = xr.combine_by_coords([vds2, vds1])
assert isinstance(combined_vds["time"].data, ManifestArray)
assert isinstance(combined_vds["lat"].data, ManifestArray)
assert isinstance(combined_vds["lon"].data, ManifestArray)
class TestRenamePaths:
def test_old_accessor(self, netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
) as vds:
with pytest.warns(DeprecationWarning):
renamed_vds = vds.virtualize.rename_paths("s3://bucket/air.nc")
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
def test_rename_to_str(self, netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
) as vds:
renamed_vds = vds.vz.rename_paths("s3://bucket/air.nc")
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
def test_rename_using_function(self, netcdf4_file, local_registry):
def local_to_s3_url(old_local_path: str) -> str:
from pathlib import Path
new_s3_bucket_url = "s3://bucket/"
filename = Path(old_local_path).name
return str(new_s3_bucket_url + filename)
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
) as vds:
renamed_vds = vds.vz.rename_paths(local_to_s3_url)
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
def test_invalid_type(self, netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file, registry=local_registry, parser=parser
) as vds:
with pytest.raises(TypeError):
vds.vz.rename_paths(["file1.nc", "file2.nc"])
@requires_hdf5plugin
@requires_imagecodecs
def test_mixture_of_manifestarrays_and_numpy_arrays(
self, netcdf4_file, local_registry
):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
loadable_variables=["lat", "lon"],
) as vds:
renamed_vds = vds.vz.rename_paths("s3://bucket/air.nc")
assert (
renamed_vds["air"].data.manifest.dict()["0.0.0"]["path"]
== "s3://bucket/air.nc"
)
assert isinstance(renamed_vds["lat"].data, np.ndarray)
@requires_hdf5plugin
@requires_imagecodecs
def test_nbytes(simple_netcdf4, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=simple_netcdf4,
registry=local_registry,
parser=parser,
) as vds:
assert vds.vz.nbytes == 32
assert vds.nbytes == 48
with open_virtual_dataset(
url=simple_netcdf4,
registry=local_registry,
parser=parser,
loadable_variables=["foo"],
) as vds:
assert vds.vz.nbytes == 48
with open_dataset(simple_netcdf4) as ds:
assert ds.vz.nbytes == ds.nbytes
class TestOpenVirtualDatasetIndexes:
@pytest.mark.xfail(reason="not yet implemented")
def test_specify_no_indexes(self, netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file, registry=local_registry, parser=parser, indexes={}
) as vds:
assert vds.indexes == {}
@requires_hdf5plugin
@requires_imagecodecs
def test_create_default_indexes_for_loadable_variables(
self, netcdf4_file, local_registry
):
loadable_variables = ["time", "lat"]
parser = HDFParser()
with (
open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
indexes=None,
loadable_variables=loadable_variables,
) as vds,
open_dataset(netcdf4_file, decode_times=True) as ds,
):
# TODO use xr.testing.assert_identical(vds.indexes, ds.indexes) instead once class supported by assertion comparison, see https://github.com/pydata/xarray/issues/5812
assert index_mappings_equal(vds.xindexes, ds[loadable_variables].xindexes)
def index_mappings_equal(indexes1: Mapping[str, Index], indexes2: Mapping[str, Index]):
# Check if the mappings have the same keys
if set(indexes1.keys()) != set(indexes2.keys()):
return False
# Check if the values for each key are identical
for key in indexes1.keys():
index1 = indexes1[key]
index2 = indexes2[key]
if not index1.equals(index2):
return False
return True
@requires_hdf5plugin
@requires_imagecodecs
def test_cftime_index(tmp_path: Path, local_registry):
"""Ensure a virtual dataset contains the same indexes as an Xarray dataset"""
# Note: Test was created to debug: https://github.com/zarr-developers/VirtualiZarr/issues/168
filepath = str(tmp_path / "tmp.nc")
ds = xr.Dataset(
data_vars={
"tasmax": (["time", "lat", "lon"], np.random.rand(2, 18, 36)),
},
coords={
"time": np.array(["2023-01-01", "2023-01-02"], dtype="datetime64[ns]"),
"lat": np.arange(-90, 90, 10),
"lon": np.arange(-180, 180, 10),
},
attrs={"attr1_key": "attr1_val"},
)
ds.to_netcdf(filepath)
parser = HDFParser()
with open_virtual_dataset(
url=filepath,
registry=local_registry,
parser=parser,
loadable_variables=["time", "lat", "lon"],
) as vds:
# TODO use xr.testing.assert_identical(vds.indexes, ds.indexes) instead once class supported by assertion comparison, see https://github.com/pydata/xarray/issues/5812
assert index_mappings_equal(vds.xindexes, ds.xindexes)
assert list(ds.coords) == list(vds.coords)
assert vds.dims == ds.dims
assert vds.attrs == ds.attrs
class TestOpenVirtualDatasetAttrs:
def test_drop_array_dimensions(self, netcdf4_file, local_registry):
parser = HDFParser()
# regression test for GH issue #150
vds = open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
)
assert "_ARRAY_DIMENSIONS" not in vds["air"].attrs
def test_coordinate_variable_attrs_preserved(self, netcdf4_file, local_registry):
# regression test for GH issue #155
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
) as vds:
assert vds["lat"].attrs == {
"standard_name": "latitude",
"long_name": "Latitude",
"units": "degrees_north",
"axis": "Y",
}
class TestDetermineCoords:
def test_infer_one_dimensional_coords(self, netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
) as vds:
assert set(vds.coords) == {"time", "lat", "lon"}
def test_var_attr_coords(self, netcdf4_file_with_2d_coords, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file_with_2d_coords,
registry=local_registry,
parser=parser,
) as vds:
expected_dimension_coords = ["ocean_time", "s_rho"]
expected_2d_coords = ["lon_rho", "lat_rho", "h"]
expected_1d_non_dimension_coords = ["Cs_r"]
expected_scalar_coords = ["hc", "Vtransform"]
expected_coords = (
expected_dimension_coords
+ expected_2d_coords
+ expected_1d_non_dimension_coords
+ expected_scalar_coords
)
assert set(vds.coords) == set(expected_coords)
@requires_network
class TestReadRemote:
@slow_test
@pytest.mark.parametrize(
"indexes",
[
None,
pytest.param({}, marks=pytest.mark.xfail(reason="not implemented")),
],
ids=["None index", "empty dict index"],
)
def test_anon_read_s3(self, indexes):
"""Parameterized tests for empty vs supplied indexes and filetypes."""
# TODO: Switch away from this s3 url after minIO is implemented.
filepath = "s3://carbonplan-share/virtualizarr/local.nc"
object_store = obstore_s3(url=filepath, region="us-west-2")
registry = ObjectStoreRegistry()
registry.register(filepath, object_store)
parser = HDFParser()
with open_virtual_dataset(
url=filepath,
registry=registry,
indexes=indexes,
parser=parser,
) as vds:
assert vds.dims == {"time": 2920, "lat": 25, "lon": 53}
assert isinstance(vds["air"].data, ManifestArray)
for name in ["time", "lat", "lon"]:
assert isinstance(vds[name].data, np.ndarray)
@slow_test
def test_virtualizarr_vs_local_nisar(self):
# Open group directly from locally cached file with xarray
url = "https://nisar.asf.earthdatacloud.nasa.gov/NISAR-SAMPLE-DATA/GCOV/ALOS1_Rosamond_20081012/NISAR_L2_PR_GCOV_001_005_A_219_4020_SHNA_A_20081012T060910_20081012T060926_P01101_F_N_J_001.h5"
hdf_group = "science/LSAR/GCOV/grids/frequencyA"
store = obstore_http(url=url)
registry = ObjectStoreRegistry()
registry.register(url, store)
drop_variables = ["listOfCovarianceTerms", "listOfPolarizations"]
parser = HDFParser(group=hdf_group, drop_variables=drop_variables)
with (
xr.open_dataset(
url,
engine="h5netcdf",
group=hdf_group,
drop_variables=drop_variables,
phony_dims="access",
) as dsXR,
# save group reference file via virtualizarr, then open with engine="kerchunk"
open_virtual_dataset(
url=url,
registry=registry,
parser=parser,
) as vds,
):
tmpref = "/tmp/cmip6.json"
vds.vz.to_kerchunk(tmpref, format="json")
with xr.open_dataset(tmpref, engine="kerchunk") as dsV:
# xrt.assert_identical(dsXR, dsV) #Attribute order changes
xrt.assert_equal(dsXR, dsV)
class TestOpenVirtualDatasetHDFGroup:
def test_open_empty_group(self, empty_netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=empty_netcdf4_file,
registry=local_registry,
parser=parser,
) as vds:
assert isinstance(vds, xr.Dataset)
expected = Dataset()
xrt.assert_identical(vds, expected)
def test_open_subgroup(
self, netcdf4_file_with_data_in_multiple_groups, local_registry
):
parser = HDFParser(group="subgroup")
with open_virtual_dataset(
url=netcdf4_file_with_data_in_multiple_groups,
registry=local_registry,
parser=parser,
) as vds:
assert list(vds.variables) == ["bar"]
assert isinstance(vds["bar"].data, ManifestArray)
assert vds["bar"].shape == (2,)
@pytest.mark.parametrize("group", ["", None])
def test_open_root_group(
self, netcdf4_file_with_data_in_multiple_groups, group, local_registry
):
parser = HDFParser(group=group)
with open_virtual_dataset(
url=netcdf4_file_with_data_in_multiple_groups,
registry=local_registry,
parser=parser,
) as vds:
assert list(vds.variables) == ["foo"]
assert isinstance(vds["foo"].data, ManifestArray)
assert vds["foo"].shape == (3,)
@requires_hdf5plugin
@requires_imagecodecs
class TestLoadVirtualDataset:
@pytest.mark.parametrize(
"loadable_variables, expected_loadable_variables",
[
([], []),
(["time"], ["time"]),
(["air", "time"], ["air", "time"]),
(None, ["lat", "lon", "time"]),
],
)
def test_loadable_variables(
self,
netcdf4_file,
loadable_variables,
expected_loadable_variables,
local_registry,
):
parser = HDFParser()
with (
open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
loadable_variables=loadable_variables,
parser=parser,
) as vds,
xr.open_dataset(netcdf4_file, decode_times=True) as ds,
):
assert set(vds.variables) == set(ds.variables)
assert set(vds.coords) == set(ds.coords)
virtual_variables = {
name: var
for name, var in vds.variables.items()
if isinstance(var.data, ManifestArray)
}
actual_loadable_variables = {
name: var
for name, var in vds.variables.items()
if not isinstance(var.data, ManifestArray)
}
assert set(actual_loadable_variables) == set(expected_loadable_variables)
for var in virtual_variables.values():
assert isinstance(var.data, ManifestArray)
for name, var in ds.variables.items():
if name in actual_loadable_variables:
xrt.assert_identical(vds.variables[name], ds.variables[name])
def test_group_kwarg_not_a_group(self, hdf5_groups_file, local_registry):
parser = HDFParser(group="doesnt_exist")
with pytest.raises(ValueError, match="not an HDF Group"):
with open_virtual_dataset(
url=hdf5_groups_file,
registry=local_registry,
parser=parser,
):
pass
def test_group_kwarg(self, hdf5_groups_file, local_registry):
parser = HDFParser(group="test/group")
vars_to_load = ["air", "time"]
with (
open_virtual_dataset(
url=hdf5_groups_file,
registry=local_registry,
loadable_variables=vars_to_load,
parser=parser,
) as vds,
xr.open_dataset(hdf5_groups_file, group="test/group") as full_ds,
):
for name in full_ds.variables:
if name in vars_to_load:
xrt.assert_identical(vds.variables[name], full_ds.variables[name])
def test_open_dataset_with_empty(self, hdf5_empty, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=hdf5_empty, registry=local_registry, parser=parser
) as vds:
assert vds.empty.dims == ()
assert vds.empty.attrs == {"empty": "true"}
def test_open_dataset_with_scalar(self, hdf5_scalar, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=f"file://{hdf5_scalar}", registry=local_registry, parser=parser
) as vds:
assert vds.scalar.dims == ()
assert vds.scalar.attrs == {"scalar": "true"}
assert isinstance(vds.scalar.data, ManifestArray)
ms = parser(registry=local_registry, url=f"file://{hdf5_scalar}")
with (
xr.open_dataset(hdf5_scalar, engine="h5netcdf") as expected,
xr.open_zarr(ms, consolidated=False, zarr_format=3) as observed,
):
xr.testing.assert_allclose(expected, observed)
preprocess_func = functools.partial(
xr.Dataset.rename_vars,
air="nair",
)
@requires_hdf5plugin
@requires_imagecodecs
class TestOpenVirtualMFDataset:
@pytest.mark.parametrize("invalid_parallel_kwarg", ["ray", Dataset])
def test_invalid_parallel_kwarg(
self, netcdf4_files_factory, invalid_parallel_kwarg, local_registry
):
filepath1, filepath2 = netcdf4_files_factory()
parser = HDFParser()
with pytest.raises(ValueError, match="Unrecognized argument"):
open_virtual_mfdataset(
[filepath1, filepath2],
registry=local_registry,
parser=parser,
combine="nested",
concat_dim="time",
parallel=invalid_parallel_kwarg,
)
@pytest.mark.parametrize(
"parallel",
[
False,
ThreadPoolExecutor,
pytest.param("dask", marks=requires_dask),
pytest.param("lithops", marks=requires_lithops),
],
)
@pytest.mark.parametrize(
"preprocess",
[
None,
preprocess_func,
],
)
def test_parallel_open(
self, netcdf4_files_factory, parallel, preprocess, local_registry
):
if parallel == "lithops":
pytest.xfail(
"TODO - investigate intermittent test failures with lithops executor"
)
filepath1, filepath2 = netcdf4_files_factory()
parser = HDFParser()
with (
open_virtual_dataset(
url=filepath1, registry=local_registry, parser=parser
) as vds1,
open_virtual_dataset(
url=filepath2,
registry=local_registry,
parser=parser,
) as vds2,
):
expected_vds = xr.concat([vds1, vds2], dim="time")
if preprocess:
expected_vds = preprocess_func(expected_vds)
# test combine nested, which doesn't use in-memory indexes
combined_vds = open_virtual_mfdataset(
[filepath1, filepath2],
registry=local_registry,
parser=parser,
combine="nested",
concat_dim="time",
parallel=parallel,
preprocess=preprocess,
)
xrt.assert_identical(combined_vds, expected_vds)
# test combine by coords using in-memory indexes
combined_vds = open_virtual_mfdataset(
[filepath1, filepath2],
registry=local_registry,
parser=parser,
combine="by_coords",
parallel=parallel,
preprocess=preprocess,
)
xrt.assert_identical(combined_vds, expected_vds)
# test combine by coords again using in-memory indexes but for a glob
file_glob = Path(filepath1).parent.glob("air*.nc")
combined_vds = open_virtual_mfdataset(
file_glob,
registry=local_registry,
parser=parser,
combine="by_coords",
parallel=parallel,
preprocess=preprocess,
)
xrt.assert_identical(combined_vds, expected_vds)
def test_drop_variables(netcdf4_file, local_registry):
parser = HDFParser()
with open_virtual_dataset(
url=netcdf4_file,
registry=local_registry,
parser=parser,
drop_variables=["air"],
) as vds:
assert "air" not in vds.variables
def test_concat_zero_dimensional_var(manifest_array):
# regression test for https://github.com/zarr-developers/VirtualiZarr/pull/641
marr = manifest_array(shape=(), chunks=())
vds1 = xr.Dataset({"a": marr})
vds2 = xr.Dataset({"a": marr})
result = xr.concat([vds1, vds2], dim="time", coords="minimal", compat="override")
assert result["a"].sizes == {"time": 2}