Skip to content

Commit 34814c4

Browse files
author
Meghan Jones
committed
Check output values in surface tests
1 parent 25b35d5 commit 34814c4

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

pygmt/tests/test_surface.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pandas as pd
77
import pytest
88
import xarray as xr
9+
from numpy import testing as npt
910
from pygmt import surface, which
1011
from pygmt.exceptions import GMTInvalidInput
1112
from pygmt.helpers import GMTTempFile, data_kind
@@ -22,27 +23,41 @@ def fixture_fname():
2223
@pytest.fixture(scope="module", name="data")
2324
def fixture_data(fname):
2425
"""
25-
Load Table 5.11 in Davis: Statistics and Data Analysis in Geology, J.
26-
Wiley
26+
Load Table 5.11 in Davis: Statistics and Data Analysis in Geology.
2727
"""
2828
return pd.read_csv(fname, sep=r"\s+", header=None, names=["x", "y", "z"])
2929

30+
3031
@pytest.fixture(scope="module", name="region")
3132
def fixture_region():
3233
"""
3334
Define the data region.
3435
"""
35-
return [0, 6.5, -0.2, 6.5]
36+
return [-0.2, 6.6, -0.2, 6.2]
37+
38+
39+
def check_values(grid):
40+
"""
41+
Check the attributes and values of the DataArray returned by surface.
42+
"""
43+
assert isinstance(grid, xr.DataArray)
44+
assert grid.gmt.registration == 0 # Gridline registration
45+
assert grid.gmt.gtype == 0 # Cartesian type
46+
assert grid.coords["y"].data.min() == -0.2
47+
assert grid.coords["y"].data.max() == 6.2
48+
assert grid.coords["x"].data.min() == -0.2
49+
assert grid.coords["x"].data.max() == 6.6
50+
npt.assert_allclose(grid[0, 0].values, 1011.1221)
51+
assert grid.sizes["y"] == 33
52+
assert grid.sizes["x"] == 35
3653

3754

3855
def test_surface_input_file(fname, region):
3956
"""
4057
Run surface by passing in a filename.
4158
"""
4259
output = surface(data=fname, spacing="0.2", region=region)
43-
assert isinstance(output, xr.DataArray)
44-
assert output.gmt.registration == 0 # Gridline registration
45-
assert output.gmt.gtype == 0 # Cartesian type
60+
check_values(output)
4661

4762

4863
def test_surface_input_data_array(data, region):
@@ -51,7 +66,7 @@ def test_surface_input_data_array(data, region):
5166
"""
5267
data = data.values # convert pandas.DataFrame to numpy.ndarray
5368
output = surface(data=data, spacing="0.2", region=region)
54-
assert isinstance(output, xr.DataArray)
69+
check_values(output)
5570

5671

5772
def test_surface_input_xyz(data, region):
@@ -65,7 +80,7 @@ def test_surface_input_xyz(data, region):
6580
spacing="0.2",
6681
region=region,
6782
)
68-
assert isinstance(output, xr.DataArray)
83+
check_values(output)
6984

7085

7186
def test_surface_wrong_kind_of_input(data, region):
@@ -84,13 +99,11 @@ def test_surface_with_outgrid_param(data, region):
8499
"""
85100
data = data.values # convert pandas.DataFrame to numpy.ndarray
86101
with GMTTempFile(suffix=".nc") as tmpfile:
87-
output = surface(
88-
data=data, spacing="0.2", region=region, outgrid=tmpfile.name
89-
)
102+
output = surface(data=data, spacing="0.2", region=region, outgrid=tmpfile.name)
90103
assert output is None # check that output is None since outgrid is set
91104
assert os.path.exists(path=tmpfile.name) # check that outgrid exists at path
92105
with xr.open_dataarray(tmpfile.name) as grid:
93-
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
106+
check_values(grid)
94107

95108

96109
def test_surface_deprecate_outfile_to_outgrid(data, region):
@@ -106,9 +119,8 @@ def test_surface_deprecate_outfile_to_outgrid(data, region):
106119
)
107120
assert output is None # check that output is None since outfile is set
108121
assert os.path.exists(path=tmpfile.name) # check that file exists at path
109-
110122
with xr.open_dataarray(tmpfile.name) as grid:
111-
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
123+
check_values(grid)
112124
assert len(record) == 1 # check that only one warning was raised
113125

114126

@@ -122,9 +134,7 @@ def test_surface_short_aliases(data, region):
122134
with GMTTempFile(suffix=".nc") as tmpfile:
123135
output = surface(data=data, I="0.2", R=region, G=tmpfile.name)
124136
assert output is None # check that output is None since outgrid is set
125-
assert os.path.exists(
126-
path=tmpfile.name
127-
) # check that outgrid exists at path
137+
assert os.path.exists(path=tmpfile.name) # check that file exists at path
128138
with xr.open_dataarray(tmpfile.name) as grid:
129-
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
139+
check_values(grid)
130140
assert len(record) == 3

0 commit comments

Comments
 (0)