From 5d970e2e7b1544f85a960f4d89a2444f13150153 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 2 Oct 2024 12:20:42 +0800 Subject: [PATCH 1/4] Add a test for plotting global grids without redundant 360 longitude --- pygmt/tests/test_grdimage.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 8f0e538d270..49358c65767 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -252,3 +252,37 @@ def test_grdimage_imgout_fails(grid): fig.grdimage(grid, img_out="out.png") with pytest.raises(GMTInvalidInput): fig.grdimage(grid, A="out.png") + + +@pytest.mark.mpl_image_compare() +def test_grdimage_grid_no_redunant_360(): + """ + Test that global grids with and without redundant 360/0 longitude values work. + + Test for https://github.com/GenericMappingTools/pygmt/issues/3331. + """ + # Global grid [-180, 180, -90, 90] with redundant longitude at 180/-180 + da1 = load_earth_relief(region=[-180, 180, -90, 90]) + # Global grid [0, 360, -90, 90] with redundant longitude at 0/360 + da2 = load_earth_relief(region=[0, 360, -90, 90]) + + # Global grid [-180, 180, -90, 90] without redundant longitude at -180/180 + da3 = da1[:, 0:360] + da3.gmt.registration, da3.gmt.gtype = 0, 1 + assert da3.shape == (181, 360) + assert da3.lon.to_numpy().min() == -180.0 + assert da3.lon.to_numpy().max() == 179.0 + + # Global grid [0, 360, -90, 90] without redundant longitude at 0/360 + da4 = da2[:, 0:360] + da4.gmt.registration, da4.gmt.gtype = 0, 1 + assert da4.shape == (181, 360) + assert da4.lon.to_numpy().min() == 0.0 + assert da4.lon.to_numpy().max() == 359.0 + + fig = Figure() + kwdict = {"projection": "W120/10c", "region": "g", "frame": "+tlon=120"} + fig.grdimage(da3, **kwdict) + fig.shift_origin(xshift="w+2c") + fig.grdimage(da4, **kwdict) + return fig From d1b579335bb7f371dd83ed150c692984ea4134a6 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 2 Oct 2024 12:30:11 +0800 Subject: [PATCH 2/4] Add the baseline image --- .../baseline/test_grdimage_grid_no_redunant_360.png.dvc | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 pygmt/tests/baseline/test_grdimage_grid_no_redunant_360.png.dvc diff --git a/pygmt/tests/baseline/test_grdimage_grid_no_redunant_360.png.dvc b/pygmt/tests/baseline/test_grdimage_grid_no_redunant_360.png.dvc new file mode 100644 index 00000000000..55edc71dbfe --- /dev/null +++ b/pygmt/tests/baseline/test_grdimage_grid_no_redunant_360.png.dvc @@ -0,0 +1,5 @@ +outs: +- md5: 6145622653eaedd2b4845400aa09ac75 + size: 239431 + hash: md5 + path: test_grdimage_grid_no_redunant_360.png From d2cd67943edd3292e23a9ef0521b635c1d5502c2 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Wed, 2 Oct 2024 12:37:19 +0800 Subject: [PATCH 3/4] Mark the test as xfail for GMT<=6.5.0 --- pygmt/tests/test_grdimage.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 49358c65767..4a08de0298d 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -5,7 +5,9 @@ import numpy as np import pytest import xarray as xr +from packaging.version import Version from pygmt import Figure +from pygmt.clib import __gmt_version__ from pygmt.datasets import load_earth_relief from pygmt.exceptions import GMTInvalidInput from pygmt.helpers.testing import check_figures_equal @@ -254,6 +256,10 @@ def test_grdimage_imgout_fails(grid): fig.grdimage(grid, A="out.png") +@pytest.mark.xfail( + condition=Version(__gmt_version__) <= Version("6.5.0"), + reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/8554", +) @pytest.mark.mpl_image_compare() def test_grdimage_grid_no_redunant_360(): """ From 86b6d7d45e770385d9c9d48295b453b99269282d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 7 Oct 2024 10:04:19 +0800 Subject: [PATCH 4/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com> --- pygmt/tests/test_grdimage.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index 4a08de0298d..ee48875240a 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -269,17 +269,17 @@ def test_grdimage_grid_no_redunant_360(): """ # Global grid [-180, 180, -90, 90] with redundant longitude at 180/-180 da1 = load_earth_relief(region=[-180, 180, -90, 90]) - # Global grid [0, 360, -90, 90] with redundant longitude at 0/360 + # Global grid [0, 360, -90, 90] with redundant longitude at 360/0 da2 = load_earth_relief(region=[0, 360, -90, 90]) - # Global grid [-180, 180, -90, 90] without redundant longitude at -180/180 + # Global grid [-180, 179, -90, 90] without redundant longitude at 180/-180 da3 = da1[:, 0:360] da3.gmt.registration, da3.gmt.gtype = 0, 1 assert da3.shape == (181, 360) assert da3.lon.to_numpy().min() == -180.0 assert da3.lon.to_numpy().max() == 179.0 - # Global grid [0, 360, -90, 90] without redundant longitude at 0/360 + # Global grid [0, 359, -90, 90] without redundant longitude at 360/0 da4 = da2[:, 0:360] da4.gmt.registration, da4.gmt.gtype = 0, 1 assert da4.shape == (181, 360)