-
Notifications
You must be signed in to change notification settings - Fork 25
/
conftest.py
153 lines (109 loc) · 3.94 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
153
from typing import Any, Dict, 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(tmpdir):
# Set up example xarray dataset
ds = xr.Dataset() # Save it to disk as netCDF (in temporary directory)
filepath = f"{tmpdir}/empty.nc"
ds.to_netcdf(filepath, format="NETCDF4")
ds.close()
return filepath
@pytest.fixture
def netcdf4_file(tmpdir):
# Set up example xarray dataset
ds = xr.tutorial.open_dataset("air_temperature")
# Save it to disk as netCDF (in temporary directory)
filepath = f"{tmpdir}/air.nc"
ds.to_netcdf(filepath, format="NETCDF4")
ds.close()
return filepath
@pytest.fixture
def netcdf4_file_with_data_in_multiple_groups(tmpdir):
filepath = str(tmpdir / "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 filepath
@pytest.fixture
def netcdf4_files_factory(tmpdir) -> callable:
def create_netcdf4_files(
encoding: Optional[Dict[str, Dict[str, Any]]] = None,
) -> tuple[str, str]:
ds = xr.tutorial.open_dataset("air_temperature")
# 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
filepath1 = f"{tmpdir}/air1.nc"
filepath2 = f"{tmpdir}/air2.nc"
ds1.to_netcdf(filepath1, encoding=encoding)
ds2.to_netcdf(filepath2, encoding=encoding)
# Close datasets
ds1.close()
ds2.close()
return filepath1, filepath2
return create_netcdf4_files
@pytest.fixture
def netcdf4_file_with_2d_coords(tmpdir):
ds = xr.tutorial.open_dataset("ROMS_example")
filepath = f"{tmpdir}/ROMS_example.nc"
ds.to_netcdf(filepath, format="NETCDF4")
ds.close()
return 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(tmpdir):
# Set up example xarray dataset
ds = xr.tutorial.open_dataset("air_temperature")
# Save it to disk as netCDF (in temporary directory)
filepath = f"{tmpdir}/air.nc"
ds.to_netcdf(filepath, format="NETCDF4", group="test/group")
ds.close()
return filepath
@pytest.fixture
def hdf5_empty(tmpdir):
filepath = f"{tmpdir}/empty.nc"
f = h5py.File(filepath, "w")
dataset = f.create_dataset("empty", shape=(), dtype="float32")
dataset.attrs["empty"] = "true"
return filepath
@pytest.fixture
def hdf5_scalar(tmpdir):
filepath = f"{tmpdir}/scalar.nc"
f = h5py.File(filepath, "w")
dataset = f.create_dataset("scalar", data=0.1, dtype="float32")
dataset.attrs["scalar"] = "true"
return filepath
@pytest.fixture
def simple_netcdf4(tmpdir):
filepath = f"{tmpdir}/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 filepath