From 62fa0b9f4a8f5edcfd17bb46748d32bfd2685db1 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 20 Dec 2021 21:25:35 +0000 Subject: [PATCH 1/2] update test_grdlandmask.py to use xarray testing --- pygmt/tests/test_grdlandmask.py | 47 ++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 4b019525ea3..7c35fd96223 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -4,36 +4,53 @@ import os import pytest -from pygmt import grdinfo, grdlandmask +from pygmt import grdinfo, grdlandmask, load_dataarray from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile +import xarray as xr +@pytest.fixture(scope="module", name="expected_grid") +def fixture_grid_result(): + """ + Load the expected grdlandmask grid result. + """ + return xr.DataArray( + data=[[0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 0.], + [0., 0., 0., 0., 0., 1.], + [0., 0., 0., 0., 0., 0.], + [0., 0., 1., 1., 0., 0.]], + coords=dict( + lon=[125.0, 126.0, 127.0, 128.0, 129.0, 130.0], + lat=[30.0, 31.0, 32.0, 33.0, 34.0, 35.0], + ), + dims=["lat", "lon"], + ) -def test_grdlandmask_outgrid(): +def test_grdlandmask_outgrid(expected_grid): """ Creates a grid land mask with an outgrid argument. """ with GMTTempFile(suffix=".nc") as tmpfile: - result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[-5, 5, -5, 5]) + result = grdlandmask(outgrid=tmpfile.name, spacing=1, region=[125, 130, 30, 35]) 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=0, per_column="n").strip().split() - ) - assert result == ["-5", "5", "-5", "5", "0", "1", "1", "1", "11", "11", "0", "1"] + temp_grid = load_dataarray(tmpfile.name) + xr.testing.assert_allclose(a=temp_grid, b=expected_grid) -def test_grdlandmask_no_outgrid(): +def test_grdlandmask_no_outgrid(expected_grid): """ Test grdlandmask with no set outgrid. """ - temp_grid = grdlandmask(spacing=1, region=[-5, 5, -5, 5]) - assert temp_grid.dims == ("lat", "lon") - assert temp_grid.gmt.gtype == 1 # Geographic grid - assert temp_grid.gmt.registration == 0 # Pixel registration - assert temp_grid.min() == 0 - assert temp_grid.max() == 1 - + result = grdlandmask(spacing=1, region=[125, 130, 30, 35]) + # check information of the output grid + assert isinstance(result, xr.DataArray) + assert result.gmt.gtype == 1 # Geographic grid + assert result.gmt.registration == 0 # Gridline registration + # check information of the output grid + xr.testing.assert_allclose(a=result, b=expected_grid) def test_grdlandmask_fails(): """ From 6ca81ca25c2bff0eddecbd9a68219a38c91d32e0 Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Mon, 20 Dec 2021 21:26:10 +0000 Subject: [PATCH 2/2] formatting --- pygmt/tests/test_grdlandmask.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pygmt/tests/test_grdlandmask.py b/pygmt/tests/test_grdlandmask.py index 7c35fd96223..138a7ff5e5f 100644 --- a/pygmt/tests/test_grdlandmask.py +++ b/pygmt/tests/test_grdlandmask.py @@ -4,10 +4,11 @@ import os import pytest -from pygmt import grdinfo, grdlandmask, load_dataarray +import xarray as xr +from pygmt import grdlandmask, load_dataarray from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile -import xarray as xr + @pytest.fixture(scope="module", name="expected_grid") def fixture_grid_result(): @@ -15,12 +16,14 @@ def fixture_grid_result(): Load the expected grdlandmask grid result. """ return xr.DataArray( - data=[[0., 0., 0., 0., 0., 0.], - [0., 0., 0., 0., 0., 0.], - [0., 0., 0., 0., 0., 0.], - [0., 0., 0., 0., 0., 1.], - [0., 0., 0., 0., 0., 0.], - [0., 0., 1., 1., 0., 0.]], + data=[ + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 1.0], + [0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 1.0, 1.0, 0.0, 0.0], + ], coords=dict( lon=[125.0, 126.0, 127.0, 128.0, 129.0, 130.0], lat=[30.0, 31.0, 32.0, 33.0, 34.0, 35.0], @@ -28,6 +31,7 @@ def fixture_grid_result(): dims=["lat", "lon"], ) + def test_grdlandmask_outgrid(expected_grid): """ Creates a grid land mask with an outgrid argument. @@ -52,6 +56,7 @@ def test_grdlandmask_no_outgrid(expected_grid): # check information of the output grid xr.testing.assert_allclose(a=result, b=expected_grid) + def test_grdlandmask_fails(): """ Check that grdlandmask fails correctly when region and spacing are not