Skip to content

Use xarray.testing rather than pygmt.grdinfo in xyz2grd tests #1682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 18, 2022
56 changes: 27 additions & 29 deletions pygmt/tests/test_xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import numpy as np
import pytest
import xarray as xr
from pygmt import grdinfo, xyz2grd
from pygmt.datasets import load_sample_bathymetry
from pygmt import load_dataarray, xyz2grd
from pygmt.datasets import load_sample_data
from pygmt.helpers import GMTTempFile


Expand All @@ -16,54 +16,52 @@ def fixture_ship_data():
"""
Load the data from the sample bathymetry dataset.
"""
return load_sample_bathymetry()
return load_sample_data(name="bathymetry")


def test_xyz2grd_input_file():
@pytest.fixture(scope="module", name="expected_grid")
def fixture_grid_result():
"""
Run xyz2grd by passing in a filename.
Load the expected xyz2grd grid result.
"""
output = xyz2grd(data="@tut_ship.xyz", spacing=5, region=[245, 255, 20, 30])
assert isinstance(output, xr.DataArray)
assert output.gmt.registration == 0 # Gridline registration
assert output.gmt.gtype == 0 # Cartesian type
return output


def test_xyz2grd_input_array(ship_data):
"""
Run xyz2grd by passing in a numpy array.
"""
output = xyz2grd(data=np.array(ship_data), spacing=5, region=[245, 255, 20, 30])
assert isinstance(output, xr.DataArray)
assert output.gmt.registration == 0 # Gridline registration
assert output.gmt.gtype == 0 # Cartesian type
return output
return xr.DataArray(
data=[
[-3651.0608, -3015.214, -2320.1033],
[-2546.2512, -1977.8754, -963.23303],
[-352.3795, -1025.4508, np.nan],
],
coords=dict(
x=[245.0, 250.0, 255.0],
y=[20.0, 25.0, 30.0],
),
dims=["y", "x"],
)


def test_xyz2grd_input_df(ship_data):
@pytest.mark.parametrize("array_func", [np.array, xr.Dataset])
def test_xyz2grd_input_array(array_func, ship_data, expected_grid):
"""
Run xyz2grd by passing in a data frame.
Run xyz2grd by passing in an xarray datset or numpy array.
"""
output = xyz2grd(data=ship_data, spacing=5, region=[245, 255, 20, 30])
output = xyz2grd(data=array_func(ship_data), spacing=5, region=[245, 255, 20, 30])
assert isinstance(output, xr.DataArray)
assert output.gmt.registration == 0 # Gridline registration
assert output.gmt.gtype == 0 # Cartesian type
return output
xr.testing.assert_allclose(a=output, b=expected_grid)


def test_xyz2grd_input_array_file_out(ship_data):
def test_xyz2grd_input_array_file_out(ship_data, expected_grid):
"""
Run xyz2grd by passing in a numpy array and set an outgrid file.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = xyz2grd(
data=np.array(ship_data),
data=ship_data,
spacing=5,
region=[245, 255, 20, 30],
outgrid=tmpfile.name,
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name)
result = grdinfo(tmpfile.name, per_column=True).strip()
assert result == "245 255 20 30 -3651.06079102 -352.379486084 5 5 3 3 0 0"
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)