Skip to content

Commit eb35d68

Browse files
Meghan Jonesseisman
Meghan Jones
andauthored
Use xarray.testing rather than pygmt.grdinfo in grdcut tests (#1574)
* Use xarray.testing rather than pygmt.grdinfo in grdcut tests Co-authored-by: Dongdong Tian <[email protected]>
1 parent dd2b374 commit eb35d68

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

pygmt/tests/test_grdcut.py

+31-34
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 grdcut, grdinfo
9+
from pygmt import grdcut, load_dataarray
1010
from pygmt.datasets import load_earth_relief
1111
from pygmt.exceptions import GMTInvalidInput
1212
from pygmt.helpers import GMTTempFile
@@ -20,67 +20,64 @@ def fixture_grid():
2020
return load_earth_relief(registration="pixel")
2121

2222

23-
def test_grdcut_file_in_file_out():
23+
@pytest.fixture(scope="module", name="expected_grid")
24+
def fixture_grid_result():
25+
"""
26+
Load the expected grdcut grid result.
27+
"""
28+
return xr.DataArray(
29+
data=[
30+
[-5069.5, -5105.0, -4937.0, -4708.0],
31+
[-4115.5, -4996.0, -4762.0, -4599.0],
32+
[-656.0, -160.0, -3484.5, -3897.5],
33+
],
34+
coords=dict(lon=[-2.5, -1.5, -0.5, 0.5], lat=[2.5, 3.5, 4.5]),
35+
dims=["lat", "lon"],
36+
)
37+
38+
39+
def test_grdcut_file_in_file_out(expected_grid):
2440
"""
2541
grdcut an input grid file, and output to a grid file.
2642
"""
2743
with GMTTempFile(suffix=".nc") as tmpfile:
28-
result = grdcut("@earth_relief_01d", outgrid=tmpfile.name, region="0/180/0/90")
44+
result = grdcut("@earth_relief_01d", outgrid=tmpfile.name, region=[-3, 1, 2, 5])
2945
assert result is None # return value is None
3046
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
31-
result = grdinfo(tmpfile.name, per_column=True)
32-
assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1 1\n"
47+
temp_grid = load_dataarray(tmpfile.name)
48+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
3349

3450

35-
def test_grdcut_file_in_dataarray_out():
51+
def test_grdcut_file_in_dataarray_out(expected_grid):
3652
"""
3753
grdcut an input grid file, and output as DataArray.
3854
"""
39-
outgrid = grdcut("@earth_relief_01d", region="0/180/0/90")
55+
outgrid = grdcut("@earth_relief_01d", region=[-3, 1, 2, 5])
4056
assert isinstance(outgrid, xr.DataArray)
4157
assert outgrid.gmt.registration == 1 # Pixel registration
4258
assert outgrid.gmt.gtype == 1 # Geographic type
4359
# check information of the output grid
44-
# the '@earth_relief_01d' is in pixel registration, so the grid range is
45-
# not exactly 0/180/0/90
46-
assert outgrid.coords["lat"].data.min() == 0.5
47-
assert outgrid.coords["lat"].data.max() == 89.5
48-
assert outgrid.coords["lon"].data.min() == 0.5
49-
assert outgrid.coords["lon"].data.max() == 179.5
50-
assert outgrid.data.min() == -8182.0
51-
assert outgrid.data.max() == 5651.5
52-
assert outgrid.sizes["lat"] == 90
53-
assert outgrid.sizes["lon"] == 180
60+
xr.testing.assert_allclose(a=outgrid, b=expected_grid)
5461

5562

56-
def test_grdcut_dataarray_in_file_out(grid):
63+
def test_grdcut_dataarray_in_file_out(grid, expected_grid):
5764
"""
5865
grdcut an input DataArray, and output to a grid file.
5966
"""
6067
with GMTTempFile(suffix=".nc") as tmpfile:
61-
result = grdcut(grid, outgrid=tmpfile.name, region="0/180/0/90")
68+
result = grdcut(grid, outgrid=tmpfile.name, region=[-3, 1, 2, 5])
6269
assert result is None # grdcut returns None if output to a file
63-
result = grdinfo(tmpfile.name, per_column=True)
64-
assert result == "0 180 0 90 -8182 5651.5 1 1 180 90 1 1\n"
70+
temp_grid = load_dataarray(tmpfile.name)
71+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
6572

6673

67-
def test_grdcut_dataarray_in_dataarray_out(grid):
74+
def test_grdcut_dataarray_in_dataarray_out(grid, expected_grid):
6875
"""
6976
grdcut an input DataArray, and output as DataArray.
7077
"""
71-
outgrid = grdcut(grid, region="0/180/0/90")
78+
outgrid = grdcut(grid, region=[-3, 1, 2, 5])
7279
assert isinstance(outgrid, xr.DataArray)
73-
# check information of the output grid
74-
# the '@earth_relief_01d' is in pixel registration, so the grid range is
75-
# not exactly 0/180/0/90
76-
assert outgrid.coords["lat"].data.min() == 0.5
77-
assert outgrid.coords["lat"].data.max() == 89.5
78-
assert outgrid.coords["lon"].data.min() == 0.5
79-
assert outgrid.coords["lon"].data.max() == 179.5
80-
assert outgrid.data.min() == -8182.0
81-
assert outgrid.data.max() == 5651.5
82-
assert outgrid.sizes["lat"] == 90
83-
assert outgrid.sizes["lon"] == 180
80+
xr.testing.assert_allclose(a=outgrid, b=expected_grid)
8481

8582

8683
def test_grdcut_fails():

0 commit comments

Comments
 (0)