Skip to content

Comments

pygmt.grdpaste: Join two grids along their common edge#4399

Open
Chuan1937 wants to merge 37 commits intoGenericMappingTools:mainfrom
Chuan1937:grdpaste
Open

pygmt.grdpaste: Join two grids along their common edge#4399
Chuan1937 wants to merge 37 commits intoGenericMappingTools:mainfrom
Chuan1937:grdpaste

Conversation

@Chuan1937
Copy link
Member

@Chuan1937 Chuan1937 commented Feb 9, 2026

@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
Chuan1937 and others added 2 commits February 9, 2026 18:20
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@seisman
Copy link
Member

seisman commented Feb 9, 2026

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

@Chuan1937 Chuan1937 requested a review from seisman February 10, 2026 01:21
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@seisman seisman added the needs review This PR has higher priority and needs review. label Feb 10, 2026
Chuan1937 and others added 5 commits February 10, 2026 15:51
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@Chuan1937
Copy link
Member Author

Chuan1937 commented Feb 10, 2026

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
Copy link
Member

seisman commented Feb 10, 2026

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
Copy link
Member Author

Chuan1937 commented Feb 15, 2026

There are 4 cases: file in file out, file in xarray out, xarray in file out, xarray in xarray out. At least the first case works with gmt 6.6, right?

Yes, But test_grdpaste only include case 3, 4. Do I need to add case1,2?

@seisman
Copy link
Member

seisman commented Feb 15, 2026

There are 4 cases: file in file out, file in xarray out, xarray in file out, xarray in xarray out. At least the first case works with gmt 6.6, right?

Yes, But test_grdpaste only include case 3, 4. Do I need to add case1,2?

Yes please

Chuan1937 and others added 3 commits February 17, 2026 09:11
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@Chuan1937 Chuan1937 requested a review from seisman February 17, 2026 01:30
Chuan1937 and others added 6 commits February 20, 2026 20:28
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@Chuan1937 Chuan1937 requested a review from seisman February 20, 2026 12:48
Chuan1937 and others added 5 commits February 21, 2026 16:15
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
@Chuan1937 Chuan1937 requested a review from seisman February 21, 2026 08:22
@seisman
Copy link
Member

seisman commented Feb 21, 2026

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

@Chuan1937
Copy link
Member Author

Chuan1937 commented Feb 22, 2026

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

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