Skip to content

Commit

Permalink
Change test_grdgradient.py to use static_earth_relief (GenericMapping…
Browse files Browse the repository at this point in the history
  • Loading branch information
willschlitzer authored and Josh Sixsmith committed Dec 21, 2022
1 parent 9cf49dd commit 8d0f566
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions pygmt/tests/test_grdgradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,68 @@
"""
import os

import numpy.testing as npt
import pytest
from pygmt import grdgradient, grdinfo
from pygmt.datasets import load_earth_relief
import xarray as xr
from pygmt import grdgradient, load_dataarray
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
from pygmt.helpers.testing import load_static_earth_relief


@pytest.fixture(scope="module", name="grid")
def fixture_grid():
"""
Load the grid data from the sample earth_relief file.
Load the grid data from the static_earth_relief file.
"""
return load_earth_relief(resolution="01d", region=[-5, 5, -5, 5])
return load_static_earth_relief()


def test_grdgradient_outgrid(grid):
@pytest.fixture(scope="module", name="expected_grid")
def fixture_grid_result():
"""
Load the expected grdgradient grid result.
"""
return xr.DataArray(
data=[
[-1.5974800e-03, -9.9056680e-04, -6.1276241e-04, -3.6172546e-04],
[-1.5880326e-03, -1.6113354e-03, -5.4624723e-04, -5.0047837e-04],
[7.2569086e-04, 2.4801277e-04, 1.8859128e-05, -1.2269041e-03],
],
coords=dict(
lon=[-52.5, -51.5, -50.5, -49.5],
lat=[-19.5, -18.5, -17.5],
),
dims=["lat", "lon"],
)


def test_grdgradient_outgrid(grid, expected_grid):
"""
Test the azimuth and direction parameters for grdgradient with a set
outgrid.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
result = grdgradient(grid=grid, outgrid=tmpfile.name, azimuth=10, direction="c")
result = grdgradient(
grid=grid, outgrid=tmpfile.name, azimuth=10, region=[-53, -49, -20, -17]
)
assert result is None # return value is None
assert os.path.exists(path=tmpfile.name) # check that outgrid exists
result = (
grdinfo(grid=tmpfile.name, force_scan="a", per_column="n").strip().split()
)
npt.assert_allclose(float(result[4]), -0.0045060496) # min
npt.assert_allclose(float(result[5]), 0.0575332976) # max
# Check spherically weighted statistics below
npt.assert_allclose(float(result[10]), 0.000384754501283) # median
npt.assert_allclose(float(result[12]), 0.00285958005568) # mean
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)


def test_grdgradient_no_outgrid(grid):
def test_grdgradient_no_outgrid(grid, expected_grid):
"""
Test the azimuth and direction parameters for grdgradient with no set
outgrid.
"""
temp_grid = grdgradient(grid=grid, azimuth=10, direction="c")
assert temp_grid.dims == ("lat", "lon")
assert temp_grid.gmt.gtype == 1 # Geographic grid
assert temp_grid.gmt.registration == 1 # Pixel registration
npt.assert_allclose(temp_grid.min(), -0.0045060496)
npt.assert_allclose(temp_grid.max(), 0.0575332976)
npt.assert_allclose(temp_grid.median(), 0.0004889865522272885)
npt.assert_allclose(temp_grid.mean(), 0.0028633063193410635)
result = grdgradient(grid=grid, azimuth=10, region=[-53, -49, -20, -17])
# check information of the output grid
assert isinstance(result, xr.DataArray)
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_grdgradient_fails(grid):
Expand Down

0 comments on commit 8d0f566

Please sign in to comment.