forked from zarr-developers/VirtualiZarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
152 lines (103 loc) · 4.16 KB
/
conftest.py
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
from pathlib import Path
from typing import Any, Callable, Mapping, Optional
import h5py
import numpy as np
import pytest
import xarray as xr
from xarray.core.variable import Variable
def pytest_addoption(parser):
"""Add command-line flags for pytest."""
parser.addoption(
"--run-network-tests",
action="store_true",
help="runs tests requiring a network connection",
)
def pytest_runtest_setup(item):
# based on https://stackoverflow.com/questions/47559524
if "network" in item.keywords and not item.config.getoption("--run-network-tests"):
pytest.skip(
"set --run-network-tests to run tests requiring an internet connection"
)
@pytest.fixture
def empty_netcdf4_file(tmp_path: Path) -> str:
filepath = tmp_path / "empty.nc"
# Set up example xarray dataset
with xr.Dataset() as ds: # Save it to disk as netCDF (in temporary directory)
ds.to_netcdf(filepath, format="NETCDF4")
return str(filepath)
@pytest.fixture
def netcdf4_file(tmp_path: Path) -> str:
filepath = tmp_path / "air.nc"
# Set up example xarray dataset
with xr.tutorial.open_dataset("air_temperature") as ds:
# Save it to disk as netCDF (in temporary directory)
ds.to_netcdf(filepath, format="NETCDF4")
return str(filepath)
@pytest.fixture
def netcdf4_file_with_data_in_multiple_groups(tmp_path: Path) -> str:
filepath = tmp_path / "test.nc"
ds1 = xr.DataArray([1, 2, 3], name="foo").to_dataset()
ds1.to_netcdf(filepath)
ds2 = xr.DataArray([4, 5], name="bar").to_dataset()
ds2.to_netcdf(filepath, group="subgroup", mode="a")
return str(filepath)
@pytest.fixture
def netcdf4_files_factory(tmp_path: Path) -> Callable:
def create_netcdf4_files(
encoding: Optional[Mapping[str, Mapping[str, Any]]] = None,
) -> tuple[str, str]:
filepath1 = tmp_path / "air1.nc"
filepath2 = tmp_path / "air2.nc"
with xr.tutorial.open_dataset("air_temperature") as ds:
# Split dataset into two parts
ds1 = ds.isel(time=slice(None, 1460))
ds2 = ds.isel(time=slice(1460, None))
# Save datasets to disk as NetCDF in the temporary directory with the provided encoding
ds1.to_netcdf(filepath1, encoding=encoding)
ds2.to_netcdf(filepath2, encoding=encoding)
return str(filepath1), str(filepath2)
return create_netcdf4_files
@pytest.fixture
def netcdf4_file_with_2d_coords(tmp_path: Path) -> str:
filepath = tmp_path / "ROMS_example.nc"
with xr.tutorial.open_dataset("ROMS_example") as ds:
ds.to_netcdf(filepath, format="NETCDF4")
return str(filepath)
@pytest.fixture
def netcdf4_virtual_dataset(netcdf4_file):
from virtualizarr import open_virtual_dataset
return open_virtual_dataset(netcdf4_file, indexes={})
@pytest.fixture
def netcdf4_inlined_ref(netcdf4_file):
from kerchunk.hdf import SingleHdf5ToZarr
return SingleHdf5ToZarr(netcdf4_file, inline_threshold=1000).translate()
@pytest.fixture
def hdf5_groups_file(tmp_path: Path) -> str:
filepath = tmp_path / "air.nc"
# Set up example xarray dataset
with xr.tutorial.open_dataset("air_temperature") as ds:
# Save it to disk as netCDF (in temporary directory)
ds.to_netcdf(filepath, format="NETCDF4", group="test/group")
return str(filepath)
@pytest.fixture
def hdf5_empty(tmp_path: Path) -> str:
filepath = tmp_path / "empty.nc"
with h5py.File(filepath, "w") as f:
dataset = f.create_dataset("empty", shape=(), dtype="float32")
dataset.attrs["empty"] = "true"
return str(filepath)
@pytest.fixture
def hdf5_scalar(tmp_path: Path) -> str:
filepath = tmp_path / "scalar.nc"
with h5py.File(filepath, "w") as f:
dataset = f.create_dataset("scalar", data=0.1, dtype="float32")
dataset.attrs["scalar"] = "true"
return str(filepath)
@pytest.fixture
def simple_netcdf4(tmp_path: Path) -> str:
filepath = tmp_path / "simple.nc"
arr = np.arange(12, dtype=np.dtype("int32")).reshape(3, 4)
var = Variable(data=arr, dims=["x", "y"])
ds = xr.Dataset({"foo": var})
ds.to_netcdf(filepath)
return str(filepath)