-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
286 lines (219 loc) · 8.54 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
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
"""Initial configuration for tests."""
import shutil
import typing
from pathlib import Path
import netCDF4 as nc
import numpy as np
import pytest
test_path = Path(__file__).parents[0].resolve()
data_path = test_path.joinpath("data")
harmony_path = data_path.joinpath("harmony")
granules_path = harmony_path.joinpath("granules")
class DataDirs(typing.NamedTuple):
test_path: Path
test_data_path: Path
class TempDirs(typing.NamedTuple):
output_path: Path
toy_data_path: Path
def path_str(dir_path: Path, filename: str) -> str:
return str(dir_path.joinpath(filename))
def pytest_addoption(parser):
"""Sets up optional argument to keep temporary testing directory."""
parser.addoption(
"--keep-tmp",
action="store_true",
help="Keep temporary directory after testing. Useful for debugging.",
)
def prep_input_files(input_dir: Path, output_dir: Path) -> list[str]:
"""Prepare input by copying from the original test data directory."""
input_files = []
for filepath in input_dir.iterdir():
if Path(filepath).suffix.lower() in (".nc", ".nc4", ".h5", ".hdf"):
copied_input_new_path = output_dir / Path(filepath).name # type: ignore
shutil.copyfile(filepath, copied_input_new_path)
input_files.append(str(copied_input_new_path))
return input_files
@pytest.fixture(scope="class")
def pass_options(request):
"""Adds optional argument to a test class."""
request.cls.KEEP_TMP = request.config.getoption("--keep-tmp")
@pytest.fixture(scope="function")
def temp_toy_data_dir(tmpdir_factory) -> Path:
return Path(tmpdir_factory.mktemp("toy-"))
@pytest.fixture(scope="function", autouse=True)
def temp_output_dir(tmpdir_factory) -> Path:
return Path(tmpdir_factory.mktemp("tmp-"))
@pytest.fixture(scope="function")
def toy_empty_dataset(temp_toy_data_dir):
"""Creates groups, dimensions, variables; and uses chosen step values in an open dataset"""
filepath = temp_toy_data_dir / "test_empty_dataset.nc"
f = nc.Dataset(filename=filepath, mode="w")
grp1 = f.createGroup("Group1")
# Root-level Dimensions/Variables
f.createDimension("step", 1)
f.createDimension("track", 1)
f.createVariable("step", "f4", ("step",), fill_value=False)
f.createVariable("track", "f4", ("track",), fill_value=False)
f.createVariable("var0", "f4", ("step", "track"))
#
f["step"][:] = [np.nan]
f["track"][:] = [np.nan]
f["var0"][:] = [np.nan]
# Group 1 Dimensions/Variables
grp1.createVariable("var1", "f8", ("step", "track"))
#
grp1["var1"][:] = [np.nan]
f.close()
return filepath
def add_to_ds_3dims_3vars_2coords_nogroup(open_ds: nc.Dataset, step_values: list):
"""Creates groups, dimensions, variables; and uses chosen step values in an open dataset"""
# Root-level Dimensions/Variables
open_ds.createDimension("step", 3)
open_ds.createDimension("track", 7)
open_ds.createVariable("step", "i2", ("step",), fill_value=False)
open_ds.createVariable("track", "i2", ("track",), fill_value=False)
open_ds.createVariable("var0", "f4", ("step", "track"))
#
open_ds["step"][:] = step_values
open_ds["track"][:] = [1, 2, 3, 4, 5, 6, 7]
open_ds["var0"][:] = [
[33, 78, 65, 12, 85, 35, 44],
[64, 24, 87, 12, 54, 82, 24],
[66, 18, 99, 52, 77, 88, 59],
]
open_ds["var0"].coordinates = "var0 track"
return open_ds
def add_to_ds_3dims_3vars_2coords_nogroup_duplicate_dimensions(
open_ds: nc.Dataset, step_values: list
):
"""Creates groups, dimensions, variables; and uses chosen step values in an open dataset"""
# Root-level Dimensions/Variables
open_ds.createDimension("step", 3)
open_ds.createDimension("track", 7)
open_ds.createVariable("step", "i2", ("step",), fill_value=False)
open_ds.createVariable("track", "i2", ("track",), fill_value=False)
open_ds.createVariable("var0", "f4", ("track", "step", "step"), fill_value=-99)
#
open_ds["step"][:] = step_values
open_ds["track"][:] = [1, 2, 3, 4, 5, 6, 7]
open_ds["var0"][:] = [
[[33, 78, 65], [33, 78, 65], [33, 78, 65]],
[[64, 24, 87], [64, 24, 87], [64, 24, 87]],
[[66, 18, 99], [66, 18, 99], [66, 18, 99]],
[[77, 88, 59], [77, 88, 59], [77, 88, 59]],
[[52, 77, 88], [52, 77, 88], [52, 77, 88]],
[[66, 18, 99], [66, 18, 99], [66, 18, 99]],
[[18, 99, 52], [18, 99, 52], [18, 99, 52]],
]
open_ds["var0"].coordinates = "track step step"
return open_ds
def add_to_ds_3dims_3vars_3coords_1group_with_step_values(open_ds: nc.Dataset, step_values: list):
"""Creates groups, dimensions, variables; and uses chosen step values in an open dataset"""
grp1 = open_ds.createGroup("Group1")
# Root-level Dimensions/Variables
open_ds.createDimension("step", 3)
open_ds.createDimension("track", 7)
open_ds.createVariable("step", "i2", ("step",), fill_value=False)
open_ds.createVariable("track", "i2", ("track",), fill_value=False)
open_ds.createVariable("var0", "f4", ("step", "track"))
#
open_ds["step"][:] = step_values
open_ds["track"][:] = [1, 2, 3, 4, 5, 6, 7]
open_ds["var0"][:] = [
[33, 78, 65, 12, 85, 35, 44],
[64, 24, 87, 12, 54, 82, 24],
[66, 18, 99, 52, 77, 88, 59],
]
# Group 1 Dimensions/Variables
grp1.createDimension("level", 2)
grp1.createVariable("var1", "f8", ("step", "track"))
grp1.createVariable("var2", "f4", ("step", "track", "level"))
#
grp1["var1"][:] = [
[200, 300, 400, 500, 600, 700, 800],
[200, 300, 400, 500, 600, 700, 800],
[200, 300, 400, 500, 600, 700, 800],
]
grp1["var2"][:] = [
[
[200, 150],
[300, 150],
[400, 150],
[500, 150],
[600, 150],
[700, 150],
[800, 150],
],
[
[200, 150],
[300, 150],
[400, 150],
[500, 150],
[600, 150],
[700, 150],
[800, 150],
],
[
[200, 150],
[300, 150],
[400, 150],
[500, 150],
[600, 150],
[700, 150],
[800, 150],
],
]
return open_ds
@pytest.fixture(scope="function")
def ds_3dims_3vars_2coords_nogroup(temp_toy_data_dir) -> Path:
filepath = temp_toy_data_dir / "test_3dims_3vars_2coords_nogroup.nc"
f = nc.Dataset(filename=filepath, mode="w")
f = add_to_ds_3dims_3vars_2coords_nogroup(f, step_values=[9, 10, 11])
f.close()
return filepath
@pytest.fixture(scope="function")
def ds_3dims_3vars_2coords_nogroup_duplicate_dimensions(temp_toy_data_dir) -> Path:
filepath = temp_toy_data_dir / "test_3dims_3vars_2coords_nogroup_duplicate_dimensions.nc"
f = nc.Dataset(filename=filepath, mode="w")
f = add_to_ds_3dims_3vars_2coords_nogroup_duplicate_dimensions(f, step_values=[9, 10, 11])
f.close()
return filepath
@pytest.fixture(scope="function")
def ds_3dims_3vars_3coords_1group_part1(temp_toy_data_dir) -> Path:
filepath = temp_toy_data_dir / "test_3dims_3vars_3coords_1group_part1.nc"
f = nc.Dataset(filename=filepath, mode="w")
f = add_to_ds_3dims_3vars_3coords_1group_with_step_values(f, step_values=[9, 10, 11])
f.close()
return filepath
@pytest.fixture(scope="function")
def ds_3dims_3vars_3coords_1group_part2(temp_toy_data_dir):
filepath = temp_toy_data_dir / "test_3dims_3vars_3coords_1group_part2.nc"
f = nc.Dataset(filename=filepath, mode="w")
f = add_to_ds_3dims_3vars_3coords_1group_with_step_values(f, step_values=[12, 13, 14])
f.close()
return filepath
@pytest.fixture(scope="function")
def ds_3dims_3vars_3coords_1group_part3(temp_toy_data_dir):
filepath = temp_toy_data_dir / "test_3dims_3vars_3coords_1group_part3.nc"
f = nc.Dataset(filename=filepath, mode="w")
f = add_to_ds_3dims_3vars_3coords_1group_with_step_values(f, step_values=[6, 7, 8])
f.close()
return filepath
@pytest.fixture(scope="function")
def text_file_with_three_paths(temp_toy_data_dir) -> Path:
filepath = temp_toy_data_dir / "text_file_with_paths.txt"
paths = [
path_str(granules_path, x)
for x in [
"TEMPO_NO2_L2_V03_20240601T210934Z_S012G01_subsetted.nc4",
"TEMPO_NO2_L2_V03_20240601T211614Z_S012G02_subsetted.nc4",
"TEMPO_NO2_L2_V03_20240601T212254Z_S012G03_subsetted.nc4",
]
]
contents = f"""{paths[0]}
{paths[1]}
{paths[2]}
"""
with open(filepath, "w") as f:
f.write(contents)
return filepath