From 7c9482967928cc0b0a86c0f81a4e37de0e903e55 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 3 Jun 2021 11:25:40 +1200 Subject: [PATCH] Add regression test for grdimage plotting an xarray.DataArray grid subset (#1314) Ensure that a sliced xarray.DataArray grid is plotted correctly on a global Mollweide projection map. This is a regression test to ensure that the bug reported in #732 does not happen in the future. Cross-reference upstream issue https://github.com/GenericMappingTools/gmt/issues/5180, and PR https://github.com/GenericMappingTools/gmt/pull/5181 where the bug was fixed. Co-authored-by: Dongdong Tian --- .../test_grdimage_global_subset.png.dvc | 4 +++ pygmt/tests/test_grdimage.py | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 pygmt/tests/baseline/test_grdimage_global_subset.png.dvc diff --git a/pygmt/tests/baseline/test_grdimage_global_subset.png.dvc b/pygmt/tests/baseline/test_grdimage_global_subset.png.dvc new file mode 100644 index 00000000000..92d2a58900d --- /dev/null +++ b/pygmt/tests/baseline/test_grdimage_global_subset.png.dvc @@ -0,0 +1,4 @@ +outs: +- md5: 8f38333a67076f0125cb2d1828aa244e + size: 39288 + path: test_grdimage_global_subset.png diff --git a/pygmt/tests/test_grdimage.py b/pygmt/tests/test_grdimage.py index a3ea9954337..836cb249705 100644 --- a/pygmt/tests/test_grdimage.py +++ b/pygmt/tests/test_grdimage.py @@ -18,6 +18,18 @@ def fixture_grid(): return load_earth_relief(registration="gridline") +@pytest.fixture(scope="module", name="grid_360") +def fixture_grid_360(grid): + """ + Earth relief grid with longitude range from 0 to 360 (instead of -180 to + 180). + """ + _grid = grid.copy() # get a copy of original earth_relief grid + _grid.encoding.pop("source") # unlink earth_relief NetCDF source + _grid["lon"] = np.arange(0, 361, 1) # convert longitude from -180:180 to 0:360 + return _grid + + @pytest.fixture(scope="module", name="xrgrid") def fixture_xrgrid(): """ @@ -131,6 +143,29 @@ def test_grdimage_over_dateline(xrgrid): return fig +@pytest.mark.mpl_image_compare +def test_grdimage_global_subset(grid_360): + """ + Ensure subsets of grids are plotted correctly on a global map. + + Specifically checking that xarray.DataArray grids can wrap around the left + and right sides on a Mollweide projection (W) plot correctly. Note that a + Cartesian grid is used here instead of a Geographic grid (i.e. + GMT_GRID_IS_CARTESIAN). This is a regression test for + https://github.com/GenericMappingTools/pygmt/issues/732. + """ + # Get a slice of South America and Africa only (lat=-90:31, lon=-180:41) + sliced_grid = grid_360[0:121, 0:221] + assert sliced_grid.gmt.registration == 0 # gridline registration + assert sliced_grid.gmt.gtype == 0 # Cartesian coordinate system + + fig = Figure() + fig.grdimage( + grid=sliced_grid, cmap="vik", region="g", projection="W0/3.5c", frame=True + ) + return fig + + @check_figures_equal() @pytest.mark.parametrize("lon0", [0, 123, 180]) @pytest.mark.parametrize("proj_type", ["H", "W"])