-
Notifications
You must be signed in to change notification settings - Fork 24
/
conftest.py
122 lines (88 loc) · 3.07 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
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 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_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 netcdf4_files(tmpdir):
# Set up example xarray dataset
ds = xr.tutorial.open_dataset("air_temperature")
# split inrto equal chunks so we can concatenate them back together later
ds1 = ds.isel(time=slice(None, 1460))
ds2 = ds.isel(time=slice(1460, None))
# Save it to disk as netCDF (in temporary directory)
filepath1 = f"{tmpdir}/air1.nc"
filepath2 = f"{tmpdir}/air2.nc"
ds1.to_netcdf(filepath1)
ds2.to_netcdf(filepath2)
ds1.close()
ds2.close()
return filepath1, filepath2
@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