Skip to content

Wrap grdpaste for joining two grids along their common edge - #4399

Merged
seisman merged 47 commits into
GenericMappingTools:mainfrom
Chuan1937:grdpaste
Feb 24, 2026
Merged

seisman merged 47 commits into
GenericMappingTools:mainfrom
Chuan1937:grdpaste

Conversation

@Chuan1937

@Chuan1937 Chuan1937 commented Feb 9, 2026 •

Copy link
Copy Markdown
Member

@seisman seisman added the feature Brand new feature label Feb 9, 2026
@seisman seisman added this to the 0.19.0 milestone Feb 9, 2026
Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/src/grdpaste.py Resolved
Xingchen He and others added 2 commits February 9, 2026 18:20
Co-authored-by: Dongdong Tian <[email protected]>
Co-authored-by: Dongdong Tian <[email protected]>
@seisman

seisman commented Feb 9, 2026

Copy link
Copy Markdown
Member

Also need to add it to doc/api/index.rst.

Comment thread doc/api/index.rst Resolved
Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/tests/test_grdpaste.py Resolved
Comment thread pygmt/tests/test_grdpaste.py Resolved
@Chuan1937
Chuan1937 requested a review from seisman February 10, 2026 01:21
Comment thread doc/api/index.rst Resolved
Comment thread pygmt/tests/test_grdpaste.py Resolved
Comment thread pygmt/tests/test_grdpaste.py Resolved
Co-authored-by: Dongdong Tian <[email protected]>
@seisman seisman added the needs review This PR has higher priority and needs review. label Feb 10, 2026
Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/src/grdpaste.py Resolved
@Chuan1937

Chuan1937 commented Feb 10, 2026 •

Copy link
Copy Markdown
Member Author

Could you also please check the min and max values in the result?
Must use temporary files for xarray.DataArray inputs because virtualfile_in returns all zeros when handling grdpaste.

@Chuan1937
Chuan1937 requested a review from seisman February 10, 2026 08:50
@seisman

seisman commented Feb 10, 2026

Copy link
Copy Markdown
Member

Could you also please check the min and max values in the result? Must use temporary files for xarray.DataArray inputs because virtualfile_in returns all zeros when handling grdpaste.

I think this points to an upstream GMT bug, which should be fixed.

@seisman seisman removed the needs review This PR has higher priority and needs review. label Feb 10, 2026
@Chuan1937
Chuan1937 requested a review from seisman February 21, 2026 08:22
Comment thread pygmt/tests/test_grdpaste.py Resolved
@seisman

seisman commented Feb 21, 2026

Copy link
Copy Markdown
Member

Could you please add one more test with grid1 as file and grid2 as xr.dataarray?

@Chuan1937

Chuan1937 commented Feb 22, 2026 •

Copy link
Copy Markdown
Member Author

Could you please add one more test with grid1 as file and grid2 as xr.dataarray?

When the first input is a real file path and the second is a virtual file (from a DataArray), a GMT_GRID_READ_ERROR is raised. However, if the first input is a virtual file and the second is a real file, it works normally.

success:

def test_grdpaste_grid1_file_grid2_xarray(grid, grid_bottom):
    """
    Test grdpaste with grid1 as file input and grid2 as xarray input.
    """
    with GMTTempFile(suffix=".nc") as tmp1:
        grdcut(grid, region=[-53, -49, -19, -16], outgrid=tmp1.name)
        result = grdpaste(grid1=tmp1.name, grid2=grid_bottom)
        assert isinstance(result, xr.DataArray)
        assert result.shape == (6, 4)
        # Check that the result has the expected min and max values
        assert result.min().values == 345.5
        assert result.max().values == 886.0

failure:

def test_grdpaste_grid1_xarray_grid2_file(grid, grid_top):
    """
    Test grdpaste with grid1 as xarray input and grid2 as file input.
    """
    with GMTTempFile(suffix=".nc") as tmp2:
        grdcut(grid, region=[-53, -49, -22, -19], outgrid=tmp2.name)
        result = grdpaste(grid1=grid_top, grid2=tmp2.name)
        assert isinstance(result, xr.DataArray)
        assert result.shape == (6, 4)
        # Check that the result has the expected min and max values
        assert result.min().values == 345.5
        assert result.max().values == 886.0

A temporary workaround is to detect mixed inputs in PyGMT and load the files as DataArrays, making both inputs virtual files:

 if {kind1, kind2} == {"file", "grid"}:
            if kind1 == "file":
                grid1 = xr.load_dataarray(grid1, engine="gmt", raster_kind="grid")
            else:
                grid2 = xr.load_dataarray(grid2, engine="gmt", raster_kind="grid")

This may be an issue with upstream GMT. Should we use the workaround or report the problem to upstream?
@seisman

@seisman

seisman commented Feb 23, 2026

Copy link
Copy Markdown
Member

@Chuan1937 I've added a section "Notes on the input/output" in #4395. Could you please complete the summary table so that we can better understand the current behavior/limitations?

@Chuan1937

Chuan1937 commented Feb 24, 2026 •

Copy link
Copy Markdown
Member Author

@seisman
I've completed "Notes on the input/output".

@seisman

seisman commented Feb 24, 2026

Copy link
Copy Markdown
Member

Thanks. Could you please add the tests in #4399 (comment)? You can mark the failing one as xfail.

@seisman

seisman commented Feb 24, 2026

Copy link
Copy Markdown
Member

Thanks. Could you please add the tests in #4399 (comment)? You can mark the failing one as xfail.

Wait. Maybe we should make it simple by only allowing grid1 and grid2 to be the same type (file or xarray.DataArray).

Comment thread pygmt/src/grdpaste.py Resolved
Co-authored-by: Dongdong Tian <[email protected]>
@Chuan1937

Copy link
Copy Markdown
Member Author

Thanks. Could you please add the tests in #4399 (comment)? You can mark the failing one as xfail.

Wait. Maybe we should make it simple by only allowing grid1 and grid2 to be the same type (file or xarray.DataArray).

I agree. It seems there are still quite a few issues with GMT's external interfaces lately. I'll add this test first.

Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/src/grdpaste.py Resolved
@Chuan1937
Chuan1937 requested a review from seisman February 24, 2026 12:21
Comment thread pygmt/src/grdpaste.py Resolved
Comment thread pygmt/src/grdpaste.py Resolved
Xingchen He and others added 2 commits February 24, 2026 20:40
Co-authored-by: Dongdong Tian <[email protected]>
Co-authored-by: Dongdong Tian <[email protected]>
@seisman
seisman merged commit 1ac7e7c into GenericMappingTools:main Feb 24, 2026
17 of 20 checks passed
@Chuan1937
Chuan1937 deleted the grdpaste branch February 24, 2026 12:58
@seisman seisman changed the title pygmt.grdpaste: Join two grids along their common edge Wrap grdpaste for joining two grids along their common edge Apr 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Brand new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants