From 790fba8874873baaf56d9998e20abc77d7675574 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 09:08:38 +0000 Subject: [PATCH 01/12] Create test_grdfilter.py --- pygmt/tests/test_grdfilter.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 pygmt/tests/test_grdfilter.py diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py new file mode 100644 index 00000000000..57f839f76a5 --- /dev/null +++ b/pygmt/tests/test_grdfilter.py @@ -0,0 +1,27 @@ +""" +Tests for grdfilter. +""" +import os + +import numpy as np +import pytest +import xarray as xr +from pygmt import grdfilter, grdinfo +from pygmt.datasets import load_earth_relief +from pygmt.exceptions import GMTInvalidInput +from pygmt.helpers import GMTTempFile + + +@pytest.fixture(scope="module", name="grid") +def fixture_grid(): + """ + Load the grid data from the sample earth_relief file. + """ + return load_earth_relief(registration="pixel") + +def test_grdfilter_fails(): + """ + Check that grdfilter fails correctly. + """ + with pytest.raises(GMTInvalidInput): + grdfilter(np.arange(10).reshape((5, 2))) From a7ee8d8f1b928a15ad61859f54d04bbcb52981bf Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 09:28:19 +0000 Subject: [PATCH 02/12] Add test_grfilter_dataarray_in_dataarray_out --- pygmt/tests/test_grdfilter.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 57f839f76a5..3017ec35c66 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -19,6 +19,21 @@ def fixture_grid(): """ return load_earth_relief(registration="pixel") +def test_grfilter_dataarray_in_dataarray_out(grid): + """ + grdfilter an input DataArray, and output as DataArray. + """ + grid = load_earth_relief(registration="pixel") + smooth_field = grdfilter(grid=grid, filter="g600", distance="4") + # check information of the output grid + assert isinstance(smooth_field, xr.DataArray) + assert smooth_field.coords["lat"].data.min() == -89.5 + assert smooth_field.coords["lat"].data.max() == 89.5 + assert smooth_field.coords["lon"].data.min() == -179.5 + assert smooth_field.coords["lon"].data.max() == 179.5 + assert smooth_field.sizes["lat"] == 180 + assert smooth_field.sizes["lon"] == 360 + def test_grdfilter_fails(): """ Check that grdfilter fails correctly. From d9b6112eefe740dd0969d38b535252bae803a3df Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 09:35:42 +0000 Subject: [PATCH 03/12] Add test_grdfilter_dataarray_in_file_out --- pygmt/tests/test_grdfilter.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 3017ec35c66..5f328d6a7ac 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -19,20 +19,35 @@ def fixture_grid(): """ return load_earth_relief(registration="pixel") + def test_grfilter_dataarray_in_dataarray_out(grid): """ grdfilter an input DataArray, and output as DataArray. """ - grid = load_earth_relief(registration="pixel") - smooth_field = grdfilter(grid=grid, filter="g600", distance="4") + # grid = load_earth_relief(registration="pixel") + result = grdfilter(grid=grid, filter="g600", distance="4") # check information of the output grid - assert isinstance(smooth_field, xr.DataArray) - assert smooth_field.coords["lat"].data.min() == -89.5 - assert smooth_field.coords["lat"].data.max() == 89.5 - assert smooth_field.coords["lon"].data.min() == -179.5 - assert smooth_field.coords["lon"].data.max() == 179.5 - assert smooth_field.sizes["lat"] == 180 - assert smooth_field.sizes["lon"] == 360 + 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 + 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 # grdcut returns None if output to a file + result = grdinfo(tmpfile.name, C=True) + assert ( + result == "-180 180 -90 90 -6147.47265625 5164.11572266 1 1 360 180 1 1\n" + ) + def test_grdfilter_fails(): """ From 533ca575ce525091c9e660696b3e2c95d72cb0b6 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 09:44:47 +0000 Subject: [PATCH 04/12] Remove unused import --- pygmt/tests/test_grdfilter.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 5f328d6a7ac..9a6c67c332d 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -1,8 +1,6 @@ """ Tests for grdfilter. """ -import os - import numpy as np import pytest import xarray as xr From eb99e02fdf5eb0e3f0452f90ceb7122a54e56a09 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 20:46:19 +0000 Subject: [PATCH 05/12] Update pygmt/tests/test_grdfilter.py Co-authored-by: Dongdong Tian --- pygmt/tests/test_grdfilter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 9a6c67c332d..bbd240d7cc9 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -22,7 +22,6 @@ def test_grfilter_dataarray_in_dataarray_out(grid): """ grdfilter an input DataArray, and output as DataArray. """ - # grid = load_earth_relief(registration="pixel") result = grdfilter(grid=grid, filter="g600", distance="4") # check information of the output grid assert isinstance(result, xr.DataArray) From be77fdb5aedae77184d9dad56122da47ff809ce4 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 20:47:18 +0000 Subject: [PATCH 06/12] Update test_grdfilter.py Fix comment --- pygmt/tests/test_grdfilter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index bbd240d7cc9..210917e1228 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -39,7 +39,7 @@ def test_grdfilter_dataarray_in_file_out(grid): """ with GMTTempFile(suffix=".nc") as tmpfile: result = grdfilter(grid, outgrid=tmpfile.name, filter="g600", distance="4") - assert result is None # grdcut returns None if output to a file + assert result is None # grdfilter returns None if output to a file result = grdinfo(tmpfile.name, C=True) assert ( result == "-180 180 -90 90 -6147.47265625 5164.11572266 1 1 360 180 1 1\n" From 7446da3a33432d863edc7d9952a6531b5da79211 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 21:07:17 +0000 Subject: [PATCH 07/12] Add numpy test for almost_equal for data mins and max --- pygmt/tests/test_grdfilter.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 210917e1228..42dffc20ccb 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -29,6 +29,8 @@ def test_grfilter_dataarray_in_dataarray_out(grid): assert result.coords["lat"].data.max() == 89.5 assert result.coords["lon"].data.min() == -179.5 assert result.coords["lon"].data.max() == 179.5 + np.testing.assert_almost_equal(result.data.min(), -6147.47265625, decimal=2) + np.testing.assert_almost_equal(result.data.max(), 5164.1157, decimal=2) assert result.sizes["lat"] == 180 assert result.sizes["lon"] == 360 From 68ef864dc0d82d2efa85bbd05f6c0e068dc6605a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 21:33:56 +0000 Subject: [PATCH 08/12] Add test_grfilter_file_in_dataarray_out and test_grdfilter_file_in_file_out --- pygmt/tests/test_grdfilter.py | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 42dffc20ccb..481eff3c4b1 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -1,6 +1,8 @@ """ Tests for grdfilter. """ +import os + import numpy as np import pytest import xarray as xr @@ -48,6 +50,48 @@ def test_grdfilter_dataarray_in_file_out(grid): ) +def test_grfilter_file_in_dataarray_out(grid): + """ + grdfilter an input grid file, and output as DataArray. + """ + outgrid = grdfilter( + "@earth_relief_01d", region="0/180/0/90", filter="g600", distance="4" + ) + 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 isinstance(outgrid, xr.DataArray) + 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 + np.testing.assert_almost_equal(outgrid.data.min(), -6147.4907, decimal=2) + np.testing.assert_almost_equal(outgrid.data.max(), 5164.06, decimal=2) + assert outgrid.sizes["lat"] == 90 + assert outgrid.sizes["lon"] == 180 + + +def test_grdfilter_file_in_file_out(): + """ + grdfilter an input grid file, and output to a grid file. + """ + with GMTTempFile(suffix=".nc") as tmpfile: + result = grdfilter( + "@earth_relief_01d", + outgrid=tmpfile.name, + region=[0, 180, 0, 90], + filter="g600", + distance="4", + ) + assert result is None # return value is None + assert os.path.exists(path=tmpfile.name) # check that outgrid exists + result = grdinfo(tmpfile.name, C=True) + assert result == "0 180 0 90 -6147.49072266 5164.06005859 1 1 180 90 1 1\n" + + def test_grdfilter_fails(): """ Check that grdfilter fails correctly. From cab61a26436ef96202ce9e8ca2ba2113551691b3 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 21:50:40 +0000 Subject: [PATCH 09/12] Update pygmt/tests/test_grdfilter.py Co-authored-by: Dongdong Tian --- pygmt/tests/test_grdfilter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 481eff3c4b1..f5fb8015a67 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -50,7 +50,7 @@ def test_grdfilter_dataarray_in_file_out(grid): ) -def test_grfilter_file_in_dataarray_out(grid): +def test_grfilter_file_in_dataarray_out(): """ grdfilter an input grid file, and output as DataArray. """ From 1b0dc75ccdae933cc32e5933b673621957570f85 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 21:52:44 +0000 Subject: [PATCH 10/12] Change import for numpy testing --- pygmt/tests/test_grdfilter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index f5fb8015a67..a9e501a42db 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -4,6 +4,7 @@ import os import numpy as np +import numpy.testing as npt import pytest import xarray as xr from pygmt import grdfilter, grdinfo @@ -68,8 +69,8 @@ def test_grfilter_file_in_dataarray_out(): assert outgrid.coords["lat"].data.max() == 89.5 assert outgrid.coords["lon"].data.min() == 0.5 assert outgrid.coords["lon"].data.max() == 179.5 - np.testing.assert_almost_equal(outgrid.data.min(), -6147.4907, decimal=2) - np.testing.assert_almost_equal(outgrid.data.max(), 5164.06, decimal=2) + 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 From 0f18800ff63f88308902050bb5678111b279690d Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 22:17:49 +0000 Subject: [PATCH 11/12] Update pygmt/tests/test_grdfilter.py Co-authored-by: Dongdong Tian --- pygmt/tests/test_grdfilter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index a9e501a42db..15cc2c934ad 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -32,8 +32,8 @@ def test_grfilter_dataarray_in_dataarray_out(grid): assert result.coords["lat"].data.max() == 89.5 assert result.coords["lon"].data.min() == -179.5 assert result.coords["lon"].data.max() == 179.5 - np.testing.assert_almost_equal(result.data.min(), -6147.47265625, decimal=2) - np.testing.assert_almost_equal(result.data.max(), 5164.1157, decimal=2) + npt.assert_almost_equal(result.data.min(), -6147.47265625, decimal=2) + npt.assert_almost_equal(result.data.max(), 5164.1157, decimal=2) assert result.sizes["lat"] == 180 assert result.sizes["lon"] == 360 From e8cddc1f7f4f1dd3fef0488775b2fa6ae1378ac3 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Thu, 28 Jan 2021 22:30:06 +0000 Subject: [PATCH 12/12] Remove duplicate test --- pygmt/tests/test_grdfilter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/tests/test_grdfilter.py b/pygmt/tests/test_grdfilter.py index 15cc2c934ad..a69bc0bbf50 100644 --- a/pygmt/tests/test_grdfilter.py +++ b/pygmt/tests/test_grdfilter.py @@ -64,7 +64,6 @@ def test_grfilter_file_in_dataarray_out(): # 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 isinstance(outgrid, xr.DataArray) assert outgrid.coords["lat"].data.min() == 0.5 assert outgrid.coords["lat"].data.max() == 89.5 assert outgrid.coords["lon"].data.min() == 0.5