Skip to content

Commit 4b5611b

Browse files
authored
Merge pull request MOM6-community#44 from hdrake/raise-errors
Test `get_geo_corners` and its ValueError if (lon, lat) missing
2 parents f262d4d + 212a5ee commit 4b5611b

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

sectionate/gridutils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ def get_geo_corners(grid):
2525
vorticity coordinates at 'outer' and 'right' positions, respectively.")
2626

2727
coords = grid._ds.coords
28-
return {
28+
29+
geo_coord_dict = {
2930
axis: [
3031
coords[c] for c in coords
3132
if (
32-
(geoc in c) and
33+
(geoc in c.lower()) and
3334
(dims["X"] in coords[c].dims) and
3435
(dims["Y"] in coords[c].dims)
3536
)
36-
][0]
37+
]
3738
for axis, geoc in zip(["X", "Y"], ["lon", "lat"])
3839
}
40+
if any([len(v) == 0 for (k,v) in geo_coord_dict.items()]):
41+
raise ValueError("""grid._ds must contain two-dimensional ("X", "Y") coordinates including the strings "lon" and "lat", consistent with grid.coords.""")
42+
return {k:v[0] for (k,v) in geo_coord_dict.items()}
3943

4044
def coord_dict(grid):
4145
"""

sectionate/tests/test_utils.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1+
import pytest
2+
13
def test_load_section():
24
from sectionate.utils import get_all_section_names, load_section
35
section_names = get_all_section_names()
4-
load_section(section_names[0])
6+
load_section(section_names[0])
7+
8+
def test_get_geo_corners():
9+
import numpy as np
10+
import xarray as xr
11+
import xgcm
12+
coords = {
13+
"xh": np.arange(0, 10),
14+
"yh": np.arange(0, 10),
15+
"xq": np.arange(0, 11),
16+
"yq": np.arange(0, 11),
17+
}
18+
19+
# Without coordinates
20+
ds = xr.Dataset(coords=coords)
21+
grid = xgcm.Grid(
22+
ds = ds,
23+
coords={
24+
"X":{"center":"xh", "outer":"xq"},
25+
"Y":{"center":"yh", "outer":"yq"}
26+
},
27+
boundary={"X":"periodic", "Y":"extend"},
28+
autoparse_metadata=False
29+
)
30+
31+
from sectionate.gridutils import get_geo_corners
32+
33+
# Fail when (lon, lat) coordinates are missing
34+
with pytest.raises(ValueError):
35+
get_geo_corners(grid)
36+
37+
# Pass when coordinates are there
38+
grid._ds = grid._ds.assign_coords({
39+
"lon_c": xr.DataArray(xr.broadcast(grid._ds.xq, grid._ds.yq)[0]),
40+
"lat_c": xr.DataArray(xr.broadcast(grid._ds.xq, grid._ds.yq)[1])
41+
})
42+
get_geo_corners(grid)

0 commit comments

Comments
 (0)