Skip to content

Commit

Permalink
Add regression test for grdimage plotting an xarray.DataArray grid su…
Browse files Browse the repository at this point in the history
…bset (#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
GenericMappingTools/gmt#5180, and PR
GenericMappingTools/gmt#5181 where the
bug was fixed.

Co-authored-by: Dongdong Tian <[email protected]>
  • Loading branch information
weiji14 and seisman authored Jun 2, 2021
1 parent 1ab551d commit bd52c10
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pygmt/tests/baseline/test_grdimage_global_subset.png.dvc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
outs:
- md5: 8f38333a67076f0125cb2d1828aa244e
size: 39288
path: test_grdimage_global_subset.png
35 changes: 35 additions & 0 deletions pygmt/tests/test_grdimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down Expand Up @@ -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"])
Expand Down

0 comments on commit bd52c10

Please sign in to comment.