Skip to content

Commit ac0b99e

Browse files
Change test_grdgradient.py to use static_earth_relief (#1679)
1 parent 2b99e4a commit ac0b99e

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

pygmt/tests/test_grdgradient.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,68 @@
33
"""
44
import os
55

6-
import numpy.testing as npt
76
import pytest
8-
from pygmt import grdgradient, grdinfo
9-
from pygmt.datasets import load_earth_relief
7+
import xarray as xr
8+
from pygmt import grdgradient, 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_grdgradient_outgrid(grid):
22+
@pytest.fixture(scope="module", name="expected_grid")
23+
def fixture_grid_result():
24+
"""
25+
Load the expected grdgradient grid result.
26+
"""
27+
return xr.DataArray(
28+
data=[
29+
[-1.5974800e-03, -9.9056680e-04, -6.1276241e-04, -3.6172546e-04],
30+
[-1.5880326e-03, -1.6113354e-03, -5.4624723e-04, -5.0047837e-04],
31+
[7.2569086e-04, 2.4801277e-04, 1.8859128e-05, -1.2269041e-03],
32+
],
33+
coords=dict(
34+
lon=[-52.5, -51.5, -50.5, -49.5],
35+
lat=[-19.5, -18.5, -17.5],
36+
),
37+
dims=["lat", "lon"],
38+
)
39+
40+
41+
def test_grdgradient_outgrid(grid, expected_grid):
2342
"""
2443
Test the azimuth and direction parameters for grdgradient with a set
2544
outgrid.
2645
"""
2746
with GMTTempFile(suffix=".nc") as tmpfile:
28-
result = grdgradient(grid=grid, outgrid=tmpfile.name, azimuth=10, direction="c")
47+
result = grdgradient(
48+
grid=grid, outgrid=tmpfile.name, azimuth=10, region=[-53, -49, -20, -17]
49+
)
2950
assert result is None # return value is None
3051
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
31-
result = (
32-
grdinfo(grid=tmpfile.name, force_scan="a", per_column="n").strip().split()
33-
)
34-
npt.assert_allclose(float(result[4]), -0.0045060496) # min
35-
npt.assert_allclose(float(result[5]), 0.0575332976) # max
36-
# Check spherically weighted statistics below
37-
npt.assert_allclose(float(result[10]), 0.000384754501283) # median
38-
npt.assert_allclose(float(result[12]), 0.00285958005568) # mean
52+
temp_grid = load_dataarray(tmpfile.name)
53+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
3954

4055

41-
def test_grdgradient_no_outgrid(grid):
56+
def test_grdgradient_no_outgrid(grid, expected_grid):
4257
"""
4358
Test the azimuth and direction parameters for grdgradient with no set
4459
outgrid.
4560
"""
46-
temp_grid = grdgradient(grid=grid, azimuth=10, direction="c")
47-
assert temp_grid.dims == ("lat", "lon")
48-
assert temp_grid.gmt.gtype == 1 # Geographic grid
49-
assert temp_grid.gmt.registration == 1 # Pixel registration
50-
npt.assert_allclose(temp_grid.min(), -0.0045060496)
51-
npt.assert_allclose(temp_grid.max(), 0.0575332976)
52-
npt.assert_allclose(temp_grid.median(), 0.0004889865522272885)
53-
npt.assert_allclose(temp_grid.mean(), 0.0028633063193410635)
61+
result = grdgradient(grid=grid, azimuth=10, region=[-53, -49, -20, -17])
62+
# check information of the output grid
63+
assert isinstance(result, xr.DataArray)
64+
assert result.gmt.gtype == 1 # Geographic grid
65+
assert result.gmt.registration == 1 # Pixel registration
66+
# check information of the output grid
67+
xr.testing.assert_allclose(a=result, b=expected_grid)
5468

5569

5670
def test_grdgradient_fails(grid):

0 commit comments

Comments
 (0)