forked from rcaneill/xnemogcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nemo.py
177 lines (159 loc) · 5.19 KB
/
test_nemo.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
import pytest
from xnemogcm import open_domain_cfg, open_nemo, process_nemo
from xnemogcm.nemo import nemo_preprocess
from xnemogcm.arakawa_points import ALL_POINTS
import xarray as xr
@pytest.mark.parametrize("parallel", [True, False])
@pytest.mark.parametrize("option", [0, 1, 2, 3])
def test_options_for_files(parallel, option, data_path):
"""Test options to provide files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
datadir = data_path / "nemo"
if option == 0:
# 0. Provide datadir and no files
open_nemo(datadir=datadir, files=None, domcfg=domcfg, parallel=parallel)
open_nemo(datadir=datadir, files="", domcfg=domcfg, parallel=parallel)
open_nemo(datadir=datadir, files=[], domcfg=domcfg, parallel=parallel)
elif option == 1:
# 1. Provide datadir and files
files = [
"GYRE_1y_00010101_00011230_grid_T.nc",
"GYRE_1y_00010101_00011230_grid_U.nc",
]
open_nemo(datadir=datadir, files=files, domcfg=domcfg, parallel=parallel)
elif option == 2:
# 2. Don't provide datadir but files
open_nemo(
datadir=None,
files=datadir.glob("*grid*.nc"),
domcfg=domcfg,
parallel=parallel,
)
open_nemo(
datadir="",
files=datadir.glob("*grid*.nc"),
domcfg=domcfg,
parallel=parallel,
)
open_nemo(
datadir=[],
files=datadir.glob("*grid*.nc"),
domcfg=domcfg,
parallel=parallel,
)
elif option == 3:
# 3. Don't provide anything => error
try:
open_nemo(datadir=None, files=None, domcfg=domcfg, parallel=parallel)
except FileNotFoundError:
pass
def test_no_file_provided_or_wrong_name(data_path):
"""Test exception raised if no file is found"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
with pytest.raises(FileNotFoundError):
open_nemo(datadir=data_path, domcfg=domcfg)
with pytest.raises(ValueError):
open_nemo(
files=(data_path / "mesh_mask_1_file").glob("mesh_mask*"), domcfg=domcfg
)
def test_open_nemo(data_path):
"""Test opening of nemo files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
def test_open_nemo_no_grid_in_filename(data_path):
"""Test opening of nemo files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
nemo_ds2 = open_nemo(
files=(data_path / "nemo_no_grid_in_filename").glob("*.nc"),
domcfg=domcfg,
)
xr.testing.assert_identical(nemo_ds, nemo_ds2)
def test_process_nemo(data_path):
"""Test processing of nemo files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
positions = [
(xr.open_dataset(data_path / f"nemo_no_grid_in_filename/{i}.nc"), i)
for i in ["T", "U", "V", "W"]
]
nemo_ds2 = process_nemo(
positions=positions,
domcfg=domcfg,
)
xr.testing.assert_identical(nemo_ds, nemo_ds2)
def test_process_nemo_from_desc(data_path):
"""Test processing of nemo files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
positions = [
(
xr.open_dataset(data_path / f"nemo_no_grid_in_filename/{i}.nc"),
None,
)
for i in ["T", "U", "V", "W"]
]
nemo_ds2 = process_nemo(
positions=positions,
domcfg=domcfg,
)
xr.testing.assert_identical(nemo_ds, nemo_ds2)
def test_use_preprocess(data_path):
"""Test opening of one nemo file and preprocess it by hand"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
ds_raw = xr.open_dataset(data_path / "nemo/GYRE_1y_00010101_00011230_grid_T.nc")
ds_raw.encoding["source"] = "GYRE_1y_00010101_00011230_grid_T.nc"
ds = nemo_preprocess(ds_raw, domcfg)
assert "x_c" in ds
assert "t" in ds
def test_coordinates_horizontal(data_path):
"""Test that coordinates are added to nemo files"""
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
assert "glamt" in nemo_ds.toce.coords
assert "glamu" in nemo_ds.uoce.coords
def test_coordinates_vertical(data_path, request):
"""Test that coordinates are added to nemo files"""
if request.node.callspec.id == "3.6":
pytest.xfail(
"Failing for nemo <= 3.6 as gdept_0 and gdepw_0 are not in mesh mask"
)
domcfg = open_domain_cfg(
datadir=data_path / "mesh_mask_1_file",
)
nemo_ds = open_nemo(
datadir=data_path / "nemo",
domcfg=domcfg,
)
assert "gdept_0" in nemo_ds.toce.coords