From 2e2e73d9538ad1aacf4808081811589579090059 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Fri, 11 Feb 2022 15:51:53 +0000 Subject: [PATCH 1/3] change test_grdcut.py to use static earth relief --- pygmt/tests/test_grdcut.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index 95eb5562655..4ae850b0ceb 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -7,9 +7,9 @@ import pytest import xarray as xr from pygmt import grdcut, load_dataarray -from pygmt.datasets import load_earth_relief 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") @@ -17,7 +17,7 @@ def fixture_grid(): """ Load the grid data from the sample earth_relief file. """ - return load_earth_relief(registration="pixel") + return load_static_earth_relief() @pytest.fixture(scope="module", name="expected_grid") @@ -27,11 +27,11 @@ def fixture_grid_result(): """ return xr.DataArray( data=[ - [-5069.5, -5105.0, -4937.0, -4708.0], - [-4115.5, -4996.0, -4762.0, -4599.0], - [-656.0, -160.0, -3484.5, -3897.5], + [446.5, 481.5, 439.5, 553.0], + [757.0, 570.5, 538.5, 524.0], + [796.0, 886.0, 571.5, 638.5], ], - coords=dict(lon=[-2.5, -1.5, -0.5, 0.5], lat=[2.5, 3.5, 4.5]), + coords=dict(lon=[-52.5, -51.5, -50.5, -49.5], lat=[-19.5, -18.5, -17.5]), dims=["lat", "lon"], ) @@ -41,7 +41,9 @@ def test_grdcut_file_in_file_out(expected_grid): grdcut an input grid file, and output to a grid file. """ with GMTTempFile(suffix=".nc") as tmpfile: - result = grdcut("@earth_relief_01d", outgrid=tmpfile.name, region=[-3, 1, 2, 5]) + result = grdcut( + "@static_earth_relief.nc", outgrid=tmpfile.name, region=[-53, -49, -20, -17] + ) assert result is None # return value is None assert os.path.exists(path=tmpfile.name) # check that outgrid exists temp_grid = load_dataarray(tmpfile.name) @@ -52,7 +54,7 @@ def test_grdcut_file_in_dataarray_out(expected_grid): """ grdcut an input grid file, and output as DataArray. """ - outgrid = grdcut("@earth_relief_01d", region=[-3, 1, 2, 5]) + outgrid = grdcut("@static_earth_relief.nc", region=[-53, -49, -20, -17]) assert isinstance(outgrid, xr.DataArray) assert outgrid.gmt.registration == 1 # Pixel registration assert outgrid.gmt.gtype == 1 # Geographic type @@ -65,7 +67,7 @@ def test_grdcut_dataarray_in_file_out(grid, expected_grid): grdcut an input DataArray, and output to a grid file. """ with GMTTempFile(suffix=".nc") as tmpfile: - result = grdcut(grid, outgrid=tmpfile.name, region=[-3, 1, 2, 5]) + result = grdcut(grid, outgrid=tmpfile.name, region=[-53, -49, -20, -17]) assert result is None # grdcut returns None if output to a file temp_grid = load_dataarray(tmpfile.name) xr.testing.assert_allclose(a=temp_grid, b=expected_grid) @@ -75,7 +77,7 @@ def test_grdcut_dataarray_in_dataarray_out(grid, expected_grid): """ grdcut an input DataArray, and output as DataArray. """ - outgrid = grdcut(grid, region=[-3, 1, 2, 5]) + outgrid = grdcut(grid, region=[-53, -49, -20, -17]) assert isinstance(outgrid, xr.DataArray) xr.testing.assert_allclose(a=outgrid, b=expected_grid) From 9f0050d587c915bec18d6b0d6bb710cfef889e93 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 12 Feb 2022 09:25:33 +0000 Subject: [PATCH 2/3] remove grdcut tests that directly call remote files --- pygmt/tests/test_grdcut.py | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index 4ae850b0ceb..54bd5e7aafe 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -36,32 +36,6 @@ def fixture_grid_result(): ) -def test_grdcut_file_in_file_out(expected_grid): - """ - grdcut an input grid file, and output to a grid file. - """ - with GMTTempFile(suffix=".nc") as tmpfile: - result = grdcut( - "@static_earth_relief.nc", outgrid=tmpfile.name, region=[-53, -49, -20, -17] - ) - assert result is None # return value is None - assert os.path.exists(path=tmpfile.name) # check that outgrid exists - temp_grid = load_dataarray(tmpfile.name) - xr.testing.assert_allclose(a=temp_grid, b=expected_grid) - - -def test_grdcut_file_in_dataarray_out(expected_grid): - """ - grdcut an input grid file, and output as DataArray. - """ - outgrid = grdcut("@static_earth_relief.nc", 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 grid - xr.testing.assert_allclose(a=outgrid, b=expected_grid) - - def test_grdcut_dataarray_in_file_out(grid, expected_grid): """ grdcut an input DataArray, and output to a grid file. From ea34218f1db9d85b8dd58d1563f2ef868fe05ed1 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 12 Feb 2022 10:02:33 +0000 Subject: [PATCH 3/3] add region fixture for multiple test functions --- pygmt/tests/test_grdcut.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_grdcut.py b/pygmt/tests/test_grdcut.py index 54bd5e7aafe..e7139df0dea 100644 --- a/pygmt/tests/test_grdcut.py +++ b/pygmt/tests/test_grdcut.py @@ -20,6 +20,14 @@ def fixture_grid(): return load_static_earth_relief() +@pytest.fixture(scope="module", name="region") +def fixture_region(): + """ + Set the data region. + """ + return [-53, -49, -20, -17] + + @pytest.fixture(scope="module", name="expected_grid") def fixture_grid_result(): """ @@ -36,22 +44,22 @@ def fixture_grid_result(): ) -def test_grdcut_dataarray_in_file_out(grid, expected_grid): +def test_grdcut_dataarray_in_file_out(grid, expected_grid, region): """ grdcut an input DataArray, and output to a grid file. """ with GMTTempFile(suffix=".nc") as tmpfile: - result = grdcut(grid, outgrid=tmpfile.name, region=[-53, -49, -20, -17]) + result = grdcut(grid, outgrid=tmpfile.name, region=region) assert result is None # grdcut returns None if output to a file temp_grid = load_dataarray(tmpfile.name) xr.testing.assert_allclose(a=temp_grid, b=expected_grid) -def test_grdcut_dataarray_in_dataarray_out(grid, expected_grid): +def test_grdcut_dataarray_in_dataarray_out(grid, expected_grid, region): """ grdcut an input DataArray, and output as DataArray. """ - outgrid = grdcut(grid, region=[-53, -49, -20, -17]) + outgrid = grdcut(grid, region=region) assert isinstance(outgrid, xr.DataArray) xr.testing.assert_allclose(a=outgrid, b=expected_grid)