Skip to content

Commit bd52c10

Browse files
weiji14seisman
andauthored
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 GenericMappingTools/gmt#5180, and PR GenericMappingTools/gmt#5181 where the bug was fixed. Co-authored-by: Dongdong Tian <[email protected]>
1 parent 1ab551d commit bd52c10

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 8f38333a67076f0125cb2d1828aa244e
3+
size: 39288
4+
path: test_grdimage_global_subset.png

pygmt/tests/test_grdimage.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def fixture_grid():
1818
return load_earth_relief(registration="gridline")
1919

2020

21+
@pytest.fixture(scope="module", name="grid_360")
22+
def fixture_grid_360(grid):
23+
"""
24+
Earth relief grid with longitude range from 0 to 360 (instead of -180 to
25+
180).
26+
"""
27+
_grid = grid.copy() # get a copy of original earth_relief grid
28+
_grid.encoding.pop("source") # unlink earth_relief NetCDF source
29+
_grid["lon"] = np.arange(0, 361, 1) # convert longitude from -180:180 to 0:360
30+
return _grid
31+
32+
2133
@pytest.fixture(scope="module", name="xrgrid")
2234
def fixture_xrgrid():
2335
"""
@@ -131,6 +143,29 @@ def test_grdimage_over_dateline(xrgrid):
131143
return fig
132144

133145

146+
@pytest.mark.mpl_image_compare
147+
def test_grdimage_global_subset(grid_360):
148+
"""
149+
Ensure subsets of grids are plotted correctly on a global map.
150+
151+
Specifically checking that xarray.DataArray grids can wrap around the left
152+
and right sides on a Mollweide projection (W) plot correctly. Note that a
153+
Cartesian grid is used here instead of a Geographic grid (i.e.
154+
GMT_GRID_IS_CARTESIAN). This is a regression test for
155+
https://github.com/GenericMappingTools/pygmt/issues/732.
156+
"""
157+
# Get a slice of South America and Africa only (lat=-90:31, lon=-180:41)
158+
sliced_grid = grid_360[0:121, 0:221]
159+
assert sliced_grid.gmt.registration == 0 # gridline registration
160+
assert sliced_grid.gmt.gtype == 0 # Cartesian coordinate system
161+
162+
fig = Figure()
163+
fig.grdimage(
164+
grid=sliced_grid, cmap="vik", region="g", projection="W0/3.5c", frame=True
165+
)
166+
return fig
167+
168+
134169
@check_figures_equal()
135170
@pytest.mark.parametrize("lon0", [0, 123, 180])
136171
@pytest.mark.parametrize("proj_type", ["H", "W"])

0 commit comments

Comments
 (0)