From 2b99e4aac74a2771c3b6a3fe592f6e19fc0925ca Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 14 Feb 2022 22:43:49 +0000 Subject: [PATCH] Change test_grdfilter.py to use static_earth_relief (#1678) --- pygmt/tests/test_grdfilter.py | 90 ++++++++++++++--------------------- 1 file changed, 35 insertions(+), 55 deletions(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 129f90a8c7a..881e6fc6fea 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -4,92 +4,72 @@ import os import numpy as np -import numpy.testing as npt import pytest import xarray as xr -from pygmt import grdfilter, grdinfo -from pygmt.datasets import load_earth_relief +from pygmt import grdfilter, 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(registration="pixel") + return load_static_earth_relief() -def test_grdfilter_dataarray_in_dataarray_out(grid): +@pytest.fixture(scope="module", name="expected_grid") +def fixture_grid_result(): """ - grdfilter an input DataArray, and output as DataArray. + Load the expected grdfilter grid result. """ - result = grdfilter(grid=grid, filter="g600", distance="4") - # check information of the output grid - assert isinstance(result, xr.DataArray) - assert result.coords["lat"].data.min() == -89.5 - assert result.coords["lat"].data.max() == 89.5 - assert result.coords["lon"].data.min() == -179.5 - assert result.coords["lon"].data.max() == 179.5 - npt.assert_almost_equal(result.data.min(), -6147.4907, decimal=2) - npt.assert_almost_equal(result.data.max(), 5164.06, decimal=2) - assert result.sizes["lat"] == 180 - assert result.sizes["lon"] == 360 - - -def test_grdfilter_dataarray_in_file_out(grid): - """ - grdfilter an input DataArray, and output to a grid file. - """ - with GMTTempFile(suffix=".nc") as tmpfile: - result = grdfilter(grid, outgrid=tmpfile.name, filter="g600", distance="4") - assert result is None # grdfilter returns None if output to a file - result = grdinfo(tmpfile.name, per_column=True) - assert ( - result == "-180 180 -90 90 -6147.49072266 5164.06005859 1 1 360 180 1 1\n" - ) + return xr.DataArray( + data=[ + [502.61914, 488.27576, 494.10657, 559.06244], + [614.6496, 601.4992, 569.9743, 606.0966], + [661.41003, 656.9681, 625.1668, 664.40204], + ], + coords=dict( + lon=[-52.5, -51.5, -50.5, -49.5], + lat=[-19.5, -18.5, -17.5], + ), + dims=["lat", "lon"], + ) -def test_grdfilter_file_in_dataarray_out(): +def test_grdfilter_dataarray_in_dataarray_out(grid, expected_grid): """ - grdfilter an input grid file, and output as DataArray. + Test grdfilter with an input DataArray, and output as DataArray. """ - outgrid = grdfilter( - "@earth_relief_01d", region="0/180/0/90", filter="g600", distance="4" + result = grdfilter( + grid=grid, filter="g600", distance="4", region=[-53, -49, -20, -17] ) - assert isinstance(outgrid, xr.DataArray) - assert outgrid.gmt.registration == 1 # Pixel registration - assert outgrid.gmt.gtype == 1 # Geographic type - # check information of the output DataArray - # the '@earth_relief_01d' is in pixel registration, so the grid range is - # not exactly 0/180/0/90 - assert outgrid.coords["lat"].data.min() == 0.5 - assert outgrid.coords["lat"].data.max() == 89.5 - assert outgrid.coords["lon"].data.min() == 0.5 - assert outgrid.coords["lon"].data.max() == 179.5 - npt.assert_almost_equal(outgrid.data.min(), -6147.4907, decimal=2) - npt.assert_almost_equal(outgrid.data.max(), 5164.06, decimal=2) - assert outgrid.sizes["lat"] == 90 - assert outgrid.sizes["lon"] == 180 + # 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_grdfilter_file_in_file_out(): +def test_grdfilter_dataarray_in_file_out(grid, expected_grid): """ - grdfilter an input grid file, and output to a grid file. + Test grdfilter with an input DataArray, and output to a grid file. """ with GMTTempFile(suffix=".nc") as tmpfile: result = grdfilter( - "@earth_relief_01d", + grid, outgrid=tmpfile.name, - region=[0, 180, 0, 90], filter="g600", distance="4", + 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(tmpfile.name, per_column=True) - assert result == "0 180 0 90 -6147.49072266 5164.06005859 1 1 180 90 1 1\n" + temp_grid = load_dataarray(tmpfile.name) + xr.testing.assert_allclose(a=temp_grid, b=expected_grid) def test_grdfilter_fails():