Skip to content

Commit 3b65997

Browse files
willschlitzerseismanMeghan Jones
authored
Use xarray.testing rather than pygmt.grdinfo in xyz2grd tests (#1682)
Update xyz2grd tests to use xarray.testing and new syntax for loading data Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Meghan Jones <[email protected]>
1 parent 757407e commit 3b65997

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

pygmt/tests/test_xyz2grd.py

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import numpy as np
77
import pytest
88
import xarray as xr
9-
from pygmt import grdinfo, xyz2grd
10-
from pygmt.datasets import load_sample_bathymetry
9+
from pygmt import load_dataarray, xyz2grd
10+
from pygmt.datasets import load_sample_data
1111
from pygmt.helpers import GMTTempFile
1212

1313

@@ -16,54 +16,52 @@ def fixture_ship_data():
1616
"""
1717
Load the data from the sample bathymetry dataset.
1818
"""
19-
return load_sample_bathymetry()
19+
return load_sample_data(name="bathymetry")
2020

2121

22-
def test_xyz2grd_input_file():
22+
@pytest.fixture(scope="module", name="expected_grid")
23+
def fixture_grid_result():
2324
"""
24-
Run xyz2grd by passing in a filename.
25+
Load the expected xyz2grd grid result.
2526
"""
26-
output = xyz2grd(data="@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30])
27-
assert isinstance(output, xr.DataArray)
28-
assert output.gmt.registration == 0 # Gridline registration
29-
assert output.gmt.gtype == 0 # Cartesian type
30-
return output
31-
32-
33-
def test_xyz2grd_input_array(ship_data):
34-
"""
35-
Run xyz2grd by passing in a numpy array.
36-
"""
37-
output = xyz2grd(data=np.array(ship_data), spacing=5, region=[245, 255, 20, 30])
38-
assert isinstance(output, xr.DataArray)
39-
assert output.gmt.registration == 0 # Gridline registration
40-
assert output.gmt.gtype == 0 # Cartesian type
41-
return output
27+
return xr.DataArray(
28+
data=[
29+
[-3651.0608, -3015.214, -2320.1033],
30+
[-2546.2512, -1977.8754, -963.23303],
31+
[-352.3795, -1025.4508, np.nan],
32+
],
33+
coords=dict(
34+
x=[245.0, 250.0, 255.0],
35+
y=[20.0, 25.0, 30.0],
36+
),
37+
dims=["y", "x"],
38+
)
4239

4340

44-
def test_xyz2grd_input_df(ship_data):
41+
@pytest.mark.parametrize("array_func", [np.array, xr.Dataset])
42+
def test_xyz2grd_input_array(array_func, ship_data, expected_grid):
4543
"""
46-
Run xyz2grd by passing in a data frame.
44+
Run xyz2grd by passing in an xarray datset or numpy array.
4745
"""
48-
output = xyz2grd(data=ship_data, spacing=5, region=[245, 255, 20, 30])
46+
output = xyz2grd(data=array_func(ship_data), spacing=5, region=[245, 255, 20, 30])
4947
assert isinstance(output, xr.DataArray)
5048
assert output.gmt.registration == 0 # Gridline registration
5149
assert output.gmt.gtype == 0 # Cartesian type
52-
return output
50+
xr.testing.assert_allclose(a=output, b=expected_grid)
5351

5452

55-
def test_xyz2grd_input_array_file_out(ship_data):
53+
def test_xyz2grd_input_array_file_out(ship_data, expected_grid):
5654
"""
5755
Run xyz2grd by passing in a numpy array and set an outgrid file.
5856
"""
5957
with GMTTempFile(suffix=".nc") as tmpfile:
6058
result = xyz2grd(
61-
data=np.array(ship_data),
59+
data=ship_data,
6260
spacing=5,
6361
region=[245, 255, 20, 30],
6462
outgrid=tmpfile.name,
6563
)
6664
assert result is None # return value is None
6765
assert os.path.exists(path=tmpfile.name)
68-
result = grdinfo(tmpfile.name, per_column=True).strip()
69-
assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0"
66+
temp_grid = load_dataarray(tmpfile.name)
67+
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)

0 commit comments

Comments
 (0)