-
Notifications
You must be signed in to change notification settings - Fork 5
/
conftest.py
469 lines (370 loc) · 11.5 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
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
import pytest
from datetime import datetime
from roms_tools import (
Grid,
TidalForcing,
InitialConditions,
BoundaryForcing,
SurfaceForcing,
)
from roms_tools.setup.datasets import (
GLORYSDataset,
ERA5Dataset,
CESMBGCDataset,
CESMBGCSurfaceForcingDataset,
TPXODataset,
)
from roms_tools.setup.download import download_test_data
import hashlib
def pytest_addoption(parser):
parser.addoption(
"--overwrite",
action="append",
default=[],
help="Specify which fixtures to overwrite. Use 'all' to overwrite all fixtures.",
)
parser.addoption(
"--use_dask", action="store_true", default=False, help="Run tests with Dask"
)
def pytest_configure(config):
if "all" in config.getoption("--overwrite"):
# If 'all' is specified, overwrite everything
config.option.overwrite = ["all"]
@pytest.fixture(scope="session")
def use_dask(request):
return request.config.getoption("--use_dask")
@pytest.fixture(scope="session")
def grid():
grid = Grid(nx=1, ny=1, size_x=100, size_y=100, center_lon=-20, center_lat=0, rot=0)
return grid
@pytest.fixture(scope="session")
def grid_that_straddles_dateline():
grid = Grid(nx=1, ny=1, size_x=100, size_y=100, center_lon=0, center_lat=0, rot=20)
return grid
@pytest.fixture(scope="session")
def tidal_forcing(request, use_dask):
grid = Grid(
nx=3, ny=3, size_x=1500, size_y=1500, center_lon=235, center_lat=25, rot=-20
)
fname = download_test_data("TPXO_regional_test_data.nc")
return TidalForcing(
grid=grid, source={"name": "TPXO", "path": fname}, ntides=1, use_dask=use_dask
)
@pytest.fixture(scope="session")
def initial_conditions(request, use_dask):
"""Fixture for creating an InitialConditions object."""
grid = Grid(
nx=2,
ny=2,
size_x=500,
size_y=1000,
center_lon=0,
center_lat=55,
rot=10,
N=3, # number of vertical levels
theta_s=5.0, # surface control parameter
theta_b=2.0, # bottom control parameter
hc=250.0, # critical depth
)
fname = download_test_data("GLORYS_coarse_test_data.nc")
return InitialConditions(
grid=grid,
ini_time=datetime(2021, 6, 29),
source={"path": fname, "name": "GLORYS"},
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def initial_conditions_with_bgc(request, use_dask):
"""Fixture for creating an InitialConditions object."""
grid = Grid(
nx=2,
ny=2,
size_x=500,
size_y=1000,
center_lon=0,
center_lat=55,
rot=10,
N=3, # number of vertical levels
theta_s=5.0, # surface control parameter
theta_b=2.0, # bottom control parameter
hc=250.0, # critical depth
)
fname = download_test_data("GLORYS_coarse_test_data.nc")
fname_bgc = download_test_data("CESM_regional_test_data_one_time_slice.nc")
return InitialConditions(
grid=grid,
ini_time=datetime(2021, 6, 29),
source={"path": fname, "name": "GLORYS"},
bgc_source={"path": fname_bgc, "name": "CESM_REGRIDDED"},
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def initial_conditions_with_bgc_from_climatology(request, use_dask):
"""Fixture for creating an InitialConditions object."""
grid = Grid(
nx=2,
ny=2,
size_x=500,
size_y=1000,
center_lon=0,
center_lat=55,
rot=10,
N=3, # number of vertical levels
theta_s=5.0, # surface control parameter
theta_b=2.0, # bottom control parameter
hc=250.0, # critical depth
)
fname = download_test_data("GLORYS_coarse_test_data.nc")
fname_bgc = download_test_data("CESM_regional_test_data_climatology.nc")
return InitialConditions(
grid=grid,
ini_time=datetime(2021, 6, 29),
source={"path": fname, "name": "GLORYS"},
bgc_source={
"path": fname_bgc,
"name": "CESM_REGRIDDED",
"climatology": True,
},
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def boundary_forcing(request, use_dask):
"""Fixture for creating a BoundaryForcing object."""
grid = Grid(
nx=2,
ny=2,
size_x=500,
size_y=1000,
center_lon=0,
center_lat=55,
rot=10,
N=3, # number of vertical levels
theta_s=5.0, # surface control parameter
theta_b=2.0, # bottom control parameter
hc=250.0, # critical depth
)
fname = download_test_data("GLORYS_coarse_test_data.nc")
return BoundaryForcing(
grid=grid,
start_time=datetime(2021, 6, 29),
end_time=datetime(2021, 6, 30),
source={"name": "GLORYS", "path": fname},
apply_2d_horizontal_fill=True,
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def bgc_boundary_forcing_from_climatology(request, use_dask):
"""Fixture for creating a BoundaryForcing object."""
grid = Grid(
nx=2,
ny=2,
size_x=500,
size_y=1000,
center_lon=0,
center_lat=55,
rot=10,
N=3, # number of vertical levels
theta_s=5.0, # surface control parameter
theta_b=2.0, # bottom control parameter
hc=250.0, # critical depth
)
fname_bgc = download_test_data("CESM_regional_coarse_test_data_climatology.nc")
return BoundaryForcing(
grid=grid,
start_time=datetime(2021, 6, 29),
end_time=datetime(2021, 6, 30),
source={"path": fname_bgc, "name": "CESM_REGRIDDED", "climatology": True},
type="bgc",
apply_2d_horizontal_fill=True,
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def surface_forcing(request, use_dask):
"""Fixture for creating a SurfaceForcing object."""
grid = Grid(
nx=5,
ny=5,
size_x=1800,
size_y=2400,
center_lon=180,
center_lat=61,
rot=20,
)
start_time = datetime(2020, 1, 31)
end_time = datetime(2020, 2, 2)
fname = download_test_data("ERA5_global_test_data.nc")
return SurfaceForcing(
grid=grid,
start_time=start_time,
end_time=end_time,
source={"name": "ERA5", "path": fname},
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def coarse_surface_forcing(request, use_dask):
"""Fixture for creating a SurfaceForcing object."""
grid = Grid(
nx=5,
ny=5,
size_x=1800,
size_y=2400,
center_lon=180,
center_lat=61,
rot=20,
)
start_time = datetime(2020, 1, 31)
end_time = datetime(2020, 2, 2)
fname = download_test_data("ERA5_global_test_data.nc")
return SurfaceForcing(
grid=grid,
start_time=start_time,
end_time=end_time,
use_coarse_grid=True,
source={"name": "ERA5", "path": fname},
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def corrected_surface_forcing(request, use_dask):
"""Fixture for creating a SurfaceForcing object with shortwave radiation
correction."""
grid = Grid(
nx=5,
ny=5,
size_x=1800,
size_y=2400,
center_lon=180,
center_lat=61,
rot=20,
)
start_time = datetime(2020, 1, 31)
end_time = datetime(2020, 2, 2)
fname = download_test_data("ERA5_global_test_data.nc")
return SurfaceForcing(
grid=grid,
start_time=start_time,
end_time=end_time,
source={"name": "ERA5", "path": fname},
correct_radiation=True,
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def bgc_surface_forcing(request, use_dask):
"""Fixture for creating a SurfaceForcing object with BGC."""
grid = Grid(
nx=5,
ny=5,
size_x=1800,
size_y=2400,
center_lon=180,
center_lat=61,
rot=20,
)
start_time = datetime(2020, 2, 1)
end_time = datetime(2020, 2, 1)
fname_bgc = download_test_data("CESM_surface_global_test_data.nc")
return SurfaceForcing(
grid=grid,
start_time=start_time,
end_time=end_time,
source={"name": "CESM_REGRIDDED", "path": fname_bgc},
type="bgc",
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def bgc_surface_forcing_from_climatology(request, use_dask):
"""Fixture for creating a SurfaceForcing object with BGC from climatology."""
grid = Grid(
nx=5,
ny=5,
size_x=1800,
size_y=2400,
center_lon=180,
center_lat=61,
rot=20,
)
start_time = datetime(2020, 2, 1)
end_time = datetime(2020, 2, 1)
fname_bgc = download_test_data("CESM_surface_global_test_data_climatology.nc")
return SurfaceForcing(
grid=grid,
start_time=start_time,
end_time=end_time,
source={"name": "CESM_REGRIDDED", "path": fname_bgc, "climatology": True},
type="bgc",
use_dask=use_dask,
)
@pytest.fixture(scope="session")
def era5_data(request, use_dask):
fname = download_test_data("ERA5_regional_test_data.nc")
data = ERA5Dataset(
filename=fname,
start_time=datetime(2020, 1, 31),
end_time=datetime(2020, 2, 2),
use_dask=use_dask,
)
return data
@pytest.fixture(scope="session")
def glorys_data(request, use_dask):
# the following GLORYS data has a wide enough domain
# to have different masks for tracers vs. velocities
fname = download_test_data("GLORYS_test_data.nc")
data = GLORYSDataset(
filename=fname,
start_time=datetime(2012, 1, 1),
end_time=datetime(2013, 1, 1),
use_dask=use_dask,
)
ds = data.ds.isel(depth=[0, 10, 30])
object.__setattr__(data, "ds", ds)
data.extrapolate_deepest_to_bottom()
return data
@pytest.fixture(scope="session")
def tpxo_data(request, use_dask):
fname = download_test_data("TPXO_regional_test_data.nc")
data = TPXODataset(
filename=fname,
use_dask=use_dask,
)
return data
@pytest.fixture(scope="session")
def cesm_bgc_data(request, use_dask):
fname = download_test_data("CESM_regional_test_data_one_time_slice.nc")
data = CESMBGCDataset(
filename=fname,
start_time=datetime(2012, 1, 1),
end_time=datetime(2013, 1, 1),
climatology=False,
use_dask=use_dask,
)
return data
@pytest.fixture(scope="session")
def coarsened_cesm_bgc_data(request, use_dask):
fname = download_test_data("CESM_BGC_2012.nc")
data = CESMBGCDataset(
filename=fname,
start_time=datetime(2012, 1, 1),
end_time=datetime(2013, 1, 1),
climatology=False,
use_dask=use_dask,
)
data.extrapolate_deepest_to_bottom()
return data
@pytest.fixture(scope="session")
def cesm_surface_bgc_data(request, use_dask):
fname = download_test_data("CESM_BGC_SURFACE_2012.nc")
data = CESMBGCSurfaceForcingDataset(
filename=fname,
start_time=datetime(2012, 1, 1),
end_time=datetime(2013, 1, 1),
climatology=False,
use_dask=use_dask,
)
data.post_process()
return data
def calculate_file_hash(filepath, hash_algorithm="sha256"):
"""Calculate the hash of a file using the specified hash algorithm."""
hash_func = hashlib.new(hash_algorithm)
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_func.update(chunk)
return hash_func.hexdigest()