Skip to content

Commit 7061dac

Browse files
Use xarray.testing rather than pygmt.grdinfo in grdlandmask tests (#1680)
1 parent 1e10b03 commit 7061dac

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

pygmt/tests/test_grdlandmask.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,57 @@
44
import os
55

66
import pytest
7-
from pygmt import grdinfo, grdlandmask
7+
import xarray as xr
8+
from pygmt import grdlandmask, load_dataarray
89
from pygmt.exceptions import GMTInvalidInput
910
from pygmt.helpers import GMTTempFile
1011

1112

12-
def test_grdlandmask_outgrid():
13+
@pytest.fixture(scope="module", name="expected_grid")
14+
def fixture_grid_result():
15+
"""
16+
Load the expected grdlandmask grid result.
17+
"""
18+
return xr.DataArray(
19+
data=[
20+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
21+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
22+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
23+
[0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
24+
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
25+
[0.0, 0.0, 1.0, 1.0, 0.0, 0.0],
26+
],
27+
coords=dict(
28+
lon=[125.0, 126.0, 127.0, 128.0, 129.0, 130.0],
29+
lat=[30.0, 31.0, 32.0, 33.0, 34.0, 35.0],
30+
),
31+
dims=["lat", "lon"],
32+
)
33+
34+
35+
def test_grdlandmask_outgrid(expected_grid):
1336
"""
1437
Creates a grid land mask with an outgrid argument.
1538
"""
1639
with GMTTempFile(suffix=".nc") as tmpfile:
17-
result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[-5, 5, -5, 5])
40+
result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35])
1841
assert result is None # return value is None
1942
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
20-
result = (
21-
grdinfo(grid=tmpfile.name, force_scan=0, per_column="n").strip().split()
22-
)
23-
assert result == ["-5", "5", "-5", "5", "0", "1", "1", "1", "11", "11", "0", "1"]
43+
temp_grid = load_dataarray(tmpfile.name)
44+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
2445

2546

26-
def test_grdlandmask_no_outgrid():
47+
def test_grdlandmask_no_outgrid(expected_grid):
2748
"""
2849
Test grdlandmask with no set outgrid.
2950
"""
30-
temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5])
31-
assert temp_grid.dims == ("lat", "lon")
32-
assert temp_grid.gmt.gtype == 1 # Geographic grid
33-
assert temp_grid.gmt.registration == 0 # Pixel registration
34-
assert temp_grid.min() == 0
35-
assert temp_grid.max() == 1
51+
result = grdlandmask(spacing=1, region=[125, 130, 30, 35])
52+
# check information of the output grid
53+
assert isinstance(result, xr.DataArray)
54+
assert result.gmt.gtype == 1 # Geographic grid
55+
assert result.gmt.registration == 0 # Gridline registration
56+
# check information of the output grid
57+
xr.testing.assert_allclose(a=result, b=expected_grid)
3658

3759

3860
def test_grdlandmask_fails():

0 commit comments

Comments
 (0)