Skip to content

Commit ab029b8

Browse files
willschlitzerseismanMeghan Jones
authored
Change test_grdproject.py to use static_earth_relief (#1683)
Use xarray.testing and static_earth_relief in grdproject tests. Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Meghan Jones <[email protected]>
1 parent 2acfe4d commit ab029b8

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

pygmt/tests/test_grdproject.py

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,73 @@
33
"""
44
import os
55

6-
import numpy.testing as npt
76
import pytest
8-
from pygmt import grdinfo, grdproject
9-
from pygmt.datasets import load_earth_relief
7+
import xarray as xr
8+
from pygmt import grdproject, load_dataarray
109
from pygmt.exceptions import GMTInvalidInput
1110
from pygmt.helpers import GMTTempFile
11+
from pygmt.helpers.testing import load_static_earth_relief
1212

1313

1414
@pytest.fixture(scope="module", name="grid")
1515
def fixture_grid():
1616
"""
17-
Load the grid data from the sample earth_relief file.
17+
Load the grid data from the static_earth_relief file.
1818
"""
19-
return load_earth_relief(resolution="01d", region=[-5, 5, -5, 5])
19+
return load_static_earth_relief()
2020

2121

22-
def test_grdproject_file_out(grid):
22+
@pytest.fixture(scope="module", name="expected_grid")
23+
def fixture_grid_result():
24+
"""
25+
Load the expected grdproject grid result.
26+
"""
27+
return xr.DataArray(
28+
data=[
29+
[427.85062, 431.05698, 452.34268],
30+
[563.92957, 540.5212, 501.46896],
31+
[740.80133, 679.1116, 554.78534],
32+
[794.233, 829.4449, 764.12225],
33+
[749.37445, 834.55994, 831.2627],
34+
],
35+
coords=dict(
36+
x=[1.666667, 5.0, 8.333333],
37+
y=[1.572432, 4.717295, 7.862158, 11.007022, 14.151885],
38+
),
39+
dims=["y", "x"],
40+
)
41+
42+
43+
def test_grdproject_file_out(grid, expected_grid):
2344
"""
2445
grdproject with an outgrid set.
2546
"""
2647
with GMTTempFile(suffix=".nc") as tmpfile:
27-
result = grdproject(grid=grid, projection="M10c", outgrid=tmpfile.name)
48+
result = grdproject(
49+
grid=grid,
50+
projection="M10c",
51+
outgrid=tmpfile.name,
52+
spacing=3,
53+
region=[-53, -51, -20, -17],
54+
)
2855
assert result is None # return value is None
2956
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
30-
result = grdinfo(tmpfile.name, per_column=True).strip().split()
31-
npt.assert_allclose(float(result[0]), 0) # x min
32-
npt.assert_allclose(float(result[1]), 10) # x max
33-
npt.assert_allclose(float(result[2]), 0, atol=1.0e-10) # y min
34-
npt.assert_allclose(float(result[3]), 9.94585661273) # y max
35-
npt.assert_allclose(float(result[4]), -5130.48193359) # min
36-
npt.assert_allclose(float(result[5]), -152.585281372) # max
57+
temp_grid = load_dataarray(tmpfile.name)
58+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
3759

3860

39-
def test_grdproject_no_outgrid(grid):
61+
def test_grdproject_no_outgrid(grid, expected_grid):
4062
"""
4163
Test grdproject with no set outgrid.
4264
"""
4365
assert grid.gmt.gtype == 1 # Geographic grid
44-
temp_grid = grdproject(grid=grid, projection="M10c")
45-
assert temp_grid.dims == ("y", "x")
46-
assert temp_grid.gmt.gtype == 0 # Rectangular grid
47-
assert temp_grid.gmt.registration == 1 # Pixel registration
48-
npt.assert_allclose(temp_grid.min(), -5130.482)
49-
npt.assert_allclose(temp_grid.max(), -152.58528)
50-
npt.assert_allclose(temp_grid.median(), -4578.288)
51-
npt.assert_allclose(temp_grid.mean(), -4354.3296)
66+
result = grdproject(
67+
grid=grid, projection="M10c", spacing=3, region=[-53, -51, -20, -17]
68+
)
69+
assert result.gmt.gtype == 0 # Rectangular grid
70+
assert result.gmt.registration == 1 # Pixel registration
71+
# check information of the output grid
72+
xr.testing.assert_allclose(a=result, b=expected_grid)
5273

5374

5475
def test_grdproject_fails(grid):

0 commit comments

Comments
 (0)