Skip to content

Use xarray.testing rather than pygmt.grdinfo in grdfill tests #1677

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 4 commits into from
Dec 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions pygmt/tests/test_grdfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pytest
import xarray as xr
from pygmt import grdfill, grdinfo
from pygmt import grdfill, load_dataarray
from pygmt.datasets import load_earth_relief
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
Expand All @@ -18,36 +18,56 @@ def fixture_grid():
Load the grid data from the sample earth_relief file and set value(s) to
NaN.
"""
grid = load_earth_relief(registration="pixel", region=[-5, 5, -5, 5])
grid[3:5, 3:5] = np.nan
grid[5:7, 5:7] = np.inf
grid = load_earth_relief(registration="pixel", region=[125, 130, -25, -20])
grid[2:4, 1:3] = np.nan
grid[0:2, 2:4] = np.inf
return grid


def test_grdfill_dataarray_out(grid):
@pytest.fixture(scope="module", name="expected_grid")
def fixture_grid_result():
"""
grdfill with a DataArray output.
Load the expected grdfill grid result.
"""
return xr.DataArray(
data=[
[442.5, 439.0, np.inf, np.inf, 508.0],
[393.0, 364.5, np.inf, np.inf, 506.5],
[362.0, 20.0, 20.0, 373.5, 402.5],
[321.5, 20.0, 20.0, 356.0, 422.5],
[282.5, 318.0, 326.5, 379.5, 383.5],
],
coords=dict(
lon=[125.5, 126.5, 127.5, 128.5, 129.5],
lat=[-24.5, -23.5, -22.5, -21.5, -20.5],
),
dims=["lat", "lon"],
)


def test_grdfill_dataarray_out(grid, expected_grid):
"""
Test grdfill with a DataArray output.
"""
result = grdfill(grid=grid, mode="c20")
# check information of the output grid
assert isinstance(result, xr.DataArray)
assert result[4, 4] == 20
assert result[5, 5] == np.inf
assert not result.isnull().all() # check that no NaN values exists
assert result.gmt.gtype == 1 # Geographic grid
assert result.gmt.registration == 1 # Pixel registration
# check information of the output grid
xr.testing.assert_allclose(a=result, b=expected_grid)


def test_grdfill_file_out(grid):
def test_grdfill_file_out(grid, expected_grid):
"""
grdfill with an outgrid set.
Test grdfill with an outgrid set.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdfill(grid=grid, mode="c20", outgrid=tmpfile.name)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
result = grdinfo(tmpfile.name, per_column=True).strip()
assert result == "-5 5 -5 5 -5130.5 inf 1 1 10 10 1 1"
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)


def test_grdfill_required_args(grid):
Expand Down