|
5 | 5 | import numpy as np
|
6 | 6 | import pytest
|
7 | 7 | import xarray as xr
|
| 8 | +from packaging.version import Version |
8 | 9 | from pygmt import Figure
|
| 10 | +from pygmt.clib import __gmt_version__ |
9 | 11 | from pygmt.datasets import load_earth_relief
|
10 | 12 | from pygmt.exceptions import GMTInvalidInput
|
11 | 13 | from pygmt.helpers.testing import check_figures_equal
|
@@ -252,3 +254,41 @@ def test_grdimage_imgout_fails(grid):
|
252 | 254 | fig.grdimage(grid, img_out="out.png")
|
253 | 255 | with pytest.raises(GMTInvalidInput):
|
254 | 256 | fig.grdimage(grid, A="out.png")
|
| 257 | + |
| 258 | + |
| 259 | +@pytest.mark.xfail( |
| 260 | + condition=Version(__gmt_version__) <= Version("6.5.0"), |
| 261 | + reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/8554", |
| 262 | +) |
| 263 | +@pytest.mark.mpl_image_compare() |
| 264 | +def test_grdimage_grid_no_redunant_360(): |
| 265 | + """ |
| 266 | + Test that global grids with and without redundant 360/0 longitude values work. |
| 267 | +
|
| 268 | + Test for https://github.com/GenericMappingTools/pygmt/issues/3331. |
| 269 | + """ |
| 270 | + # Global grid [-180, 180, -90, 90] with redundant longitude at 180/-180 |
| 271 | + da1 = load_earth_relief(region=[-180, 180, -90, 90]) |
| 272 | + # Global grid [0, 360, -90, 90] with redundant longitude at 360/0 |
| 273 | + da2 = load_earth_relief(region=[0, 360, -90, 90]) |
| 274 | + |
| 275 | + # Global grid [-180, 179, -90, 90] without redundant longitude at 180/-180 |
| 276 | + da3 = da1[:, 0:360] |
| 277 | + da3.gmt.registration, da3.gmt.gtype = 0, 1 |
| 278 | + assert da3.shape == (181, 360) |
| 279 | + assert da3.lon.to_numpy().min() == -180.0 |
| 280 | + assert da3.lon.to_numpy().max() == 179.0 |
| 281 | + |
| 282 | + # Global grid [0, 359, -90, 90] without redundant longitude at 360/0 |
| 283 | + da4 = da2[:, 0:360] |
| 284 | + da4.gmt.registration, da4.gmt.gtype = 0, 1 |
| 285 | + assert da4.shape == (181, 360) |
| 286 | + assert da4.lon.to_numpy().min() == 0.0 |
| 287 | + assert da4.lon.to_numpy().max() == 359.0 |
| 288 | + |
| 289 | + fig = Figure() |
| 290 | + kwdict = {"projection": "W120/10c", "region": "g", "frame": "+tlon=120"} |
| 291 | + fig.grdimage(da3, **kwdict) |
| 292 | + fig.shift_origin(xshift="w+2c") |
| 293 | + fig.grdimage(da4, **kwdict) |
| 294 | + return fig |
0 commit comments