Skip to content

Commit

Permalink
Use xarray.testing rather than pygmt.grdinfo in xyz2grd tests (#1682)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
3 people authored Feb 18, 2022
1 parent 757407e commit 3b65997
Showing 1 changed file with 27 additions and 29 deletions.
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)

0 comments on commit 3b65997

Please sign in to comment.