Skip to content

Commit a598df9

Browse files
Use xarray.testing rather than pygmt.grdinfo in grdfill tests (#1677)
* remove grdinfo from grdfill tests * convert testing to use xarray testing * change region in sample grid to use land coordinates over smaller region
1 parent 7061dac commit a598df9

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

pygmt/tests/test_grdfill.py

+33-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77
import pytest
88
import xarray as xr
9-
from pygmt import grdfill, grdinfo
9+
from pygmt import grdfill, load_dataarray
1010
from pygmt.datasets import load_earth_relief
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile
@@ -18,36 +18,56 @@ def fixture_grid():
1818
Load the grid data from the sample earth_relief file and set value(s) to
1919
NaN.
2020
"""
21-
grid = load_earth_relief(registration="pixel", region=[-5, 5, -5, 5])
22-
grid[3:5, 3:5] = np.nan
23-
grid[5:7, 5:7] = np.inf
21+
grid = load_earth_relief(registration="pixel", region=[125, 130, -25, -20])
22+
grid[2:4, 1:3] = np.nan
23+
grid[0:2, 2:4] = np.inf
2424
return grid
2525

2626

27-
def test_grdfill_dataarray_out(grid):
27+
@pytest.fixture(scope="module", name="expected_grid")
28+
def fixture_grid_result():
2829
"""
29-
grdfill with a DataArray output.
30+
Load the expected grdfill grid result.
31+
"""
32+
return xr.DataArray(
33+
data=[
34+
[442.5, 439.0, np.inf, np.inf, 508.0],
35+
[393.0, 364.5, np.inf, np.inf, 506.5],
36+
[362.0, 20.0, 20.0, 373.5, 402.5],
37+
[321.5, 20.0, 20.0, 356.0, 422.5],
38+
[282.5, 318.0, 326.5, 379.5, 383.5],
39+
],
40+
coords=dict(
41+
lon=[125.5, 126.5, 127.5, 128.5, 129.5],
42+
lat=[-24.5, -23.5, -22.5, -21.5, -20.5],
43+
),
44+
dims=["lat", "lon"],
45+
)
46+
47+
48+
def test_grdfill_dataarray_out(grid, expected_grid):
49+
"""
50+
Test grdfill with a DataArray output.
3051
"""
3152
result = grdfill(grid=grid, mode="c20")
3253
# check information of the output grid
3354
assert isinstance(result, xr.DataArray)
34-
assert result[4, 4] == 20
35-
assert result[5, 5] == np.inf
36-
assert not result.isnull().all() # check that no NaN values exists
3755
assert result.gmt.gtype == 1 # Geographic grid
3856
assert result.gmt.registration == 1 # Pixel registration
57+
# check information of the output grid
58+
xr.testing.assert_allclose(a=result, b=expected_grid)
3959

4060

41-
def test_grdfill_file_out(grid):
61+
def test_grdfill_file_out(grid, expected_grid):
4262
"""
43-
grdfill with an outgrid set.
63+
Test grdfill with an outgrid set.
4464
"""
4565
with GMTTempFile(suffix=".nc") as tmpfile:
4666
result = grdfill(grid=grid, mode="c20", outgrid=tmpfile.name)
4767
assert result is None # return value is None
4868
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
49-
result = grdinfo(tmpfile.name, per_column=True).strip()
50-
assert result == "-5 5 -5 5 -5130.5 inf 1 1 10 10 1 1"
69+
temp_grid = load_dataarray(tmpfile.name)
70+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
5171

5272

5373
def test_grdfill_required_args(grid):

0 commit comments

Comments
 (0)