|
| 1 | +""" |
| 2 | +Test pygmt.grdpaste. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import xarray as xr |
| 7 | +from packaging.version import Version |
| 8 | +from pygmt import grdcut, grdpaste |
| 9 | +from pygmt.clib import __gmt_version__ |
| 10 | +from pygmt.exceptions import GMTTypeError |
| 11 | +from pygmt.helpers import GMTTempFile |
| 12 | +from pygmt.helpers.testing import load_static_earth_relief |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(scope="module", name="grid") |
| 16 | +def fixture_grid(): |
| 17 | + """ |
| 18 | + Load the grid data from the sample earth_relief file. |
| 19 | + """ |
| 20 | + return load_static_earth_relief() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module", name="grid_top") |
| 24 | +def fixture_grid_top(grid): |
| 25 | + """ |
| 26 | + Load the top part of the grid data from the sample earth_relief file. |
| 27 | + """ |
| 28 | + return grdcut(grid, region=[-53, -49, -19, -16]) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope="module", name="grid_bottom") |
| 32 | +def fixture_grid_bottom(grid): |
| 33 | + """ |
| 34 | + Load the bottom part of the grid data from the sample earth_relief file. |
| 35 | + """ |
| 36 | + return grdcut(grid, region=[-53, -49, -22, -19]) |
| 37 | + |
| 38 | + |
| 39 | +def test_grdpaste_file_in_file_out(grid): |
| 40 | + """ |
| 41 | + Test grdpaste with file input and file output. |
| 42 | + """ |
| 43 | + with ( |
| 44 | + GMTTempFile(suffix=".nc") as tmp1, |
| 45 | + GMTTempFile(suffix=".nc") as tmp2, |
| 46 | + GMTTempFile(suffix=".nc") as tmpout, |
| 47 | + ): |
| 48 | + grdcut(grid, region=[-53, -49, -19, -16], outgrid=tmp1.name) |
| 49 | + grdcut(grid, region=[-53, -49, -22, -19], outgrid=tmp2.name) |
| 50 | + result = grdpaste(grid1=tmp1.name, grid2=tmp2.name, outgrid=tmpout.name) |
| 51 | + assert result is None # grdpaste returns None if output to a file |
| 52 | + temp_grid = xr.load_dataarray(tmpout.name, engine="gmt", raster_kind="grid") |
| 53 | + assert isinstance(temp_grid, xr.DataArray) |
| 54 | + assert temp_grid.shape == (6, 4) |
| 55 | + # Check that the result has the expected min and max values |
| 56 | + assert temp_grid.min().values == 345.5 |
| 57 | + assert temp_grid.max().values == 886.0 |
| 58 | + |
| 59 | + |
| 60 | +def test_grdpaste_file_in_xarray_out(grid): |
| 61 | + """ |
| 62 | + Test grdpaste with file input and xarray output. |
| 63 | + """ |
| 64 | + with ( |
| 65 | + GMTTempFile(suffix=".nc") as tmp1, |
| 66 | + GMTTempFile(suffix=".nc") as tmp2, |
| 67 | + ): |
| 68 | + grdcut(grid, region=[-53, -49, -19, -16], outgrid=tmp1.name) |
| 69 | + grdcut(grid, region=[-53, -49, -22, -19], outgrid=tmp2.name) |
| 70 | + result = grdpaste(grid1=tmp1.name, grid2=tmp2.name) |
| 71 | + assert isinstance(result, xr.DataArray) |
| 72 | + assert result.shape == (6, 4) |
| 73 | + # Check that the result has the expected min and max values |
| 74 | + assert result.min().values == 345.5 |
| 75 | + assert result.max().values == 886.0 |
| 76 | + |
| 77 | + |
| 78 | +# TODO(GMT>6.6.0): Remove the xfail marker. |
| 79 | +@pytest.mark.xfail( |
| 80 | + condition=Version(__gmt_version__) <= Version("6.6.0"), |
| 81 | + reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/8901", |
| 82 | +) |
| 83 | +def test_grdpaste(grid_top, grid_bottom): |
| 84 | + """ |
| 85 | + Test grdpaste by pasting two grids together along their common edge. |
| 86 | + """ |
| 87 | + # Paste the two grids back together |
| 88 | + result = grdpaste(grid1=grid_top, grid2=grid_bottom) |
| 89 | + # Check that the result is a DataArray |
| 90 | + assert isinstance(result, xr.DataArray) |
| 91 | + # Check that the result has the expected shape |
| 92 | + # grid_top has 3x4, grid_bottom has 3x4, so result should have 6x4 |
| 93 | + assert result.shape == (6, 4) |
| 94 | + # Check that the result has the expected min and max values |
| 95 | + assert result.min().values == 345.5 |
| 96 | + assert result.max().values == 886.0 |
| 97 | + |
| 98 | + |
| 99 | +# TODO(GMT>6.6.0): Remove the xfail marker. |
| 100 | +@pytest.mark.xfail( |
| 101 | + condition=Version(__gmt_version__) <= Version("6.6.0"), |
| 102 | + reason="Upstream bug fixed in https://github.com/GenericMappingTools/gmt/pull/8901", |
| 103 | +) |
| 104 | +def test_grdpaste_outgrid(grid_top, grid_bottom): |
| 105 | + """ |
| 106 | + Test grdpaste with outgrid parameter. |
| 107 | + """ |
| 108 | + # Paste the two grids back together and save to file |
| 109 | + with GMTTempFile(suffix=".nc") as tmpfile: |
| 110 | + result = grdpaste(grid1=grid_top, grid2=grid_bottom, outgrid=tmpfile.name) |
| 111 | + assert result is None # grdpaste returns None if output to a file |
| 112 | + temp_grid = xr.load_dataarray(tmpfile.name, engine="gmt", raster_kind="grid") |
| 113 | + assert isinstance(temp_grid, xr.DataArray) |
| 114 | + assert temp_grid.shape == (6, 4) |
| 115 | + # Check that the result has the expected min and max values |
| 116 | + assert temp_grid.min().values == 345.5 |
| 117 | + assert temp_grid.max().values == 886.0 |
| 118 | + |
| 119 | + |
| 120 | +def test_grdpaste_mixed_inputs_file_xarray(grid, grid_bottom): |
| 121 | + """ |
| 122 | + Test that mixing file and xarray inputs raises GMTTypeError. |
| 123 | + """ |
| 124 | + with GMTTempFile(suffix=".nc") as tmp1: |
| 125 | + grdcut(grid, region=[-53, -49, -19, -16], outgrid=tmp1.name) |
| 126 | + # This should raise GMTTypeError because we're mixing file and xarray inputs |
| 127 | + with pytest.raises(GMTTypeError): |
| 128 | + grdpaste(grid1=tmp1.name, grid2=grid_bottom) |
0 commit comments