Merge grids as mosaic in PyGMT

Hello,

I’m trying to merge multiple, non overlapping (as far as I know) grids. I understand that it can be done with standard GMT with the grdcut and grdpaste functions, or maybe grdcut isn’t needed because they shouldn’t overlap. What would be the optimal way to do this through PyGMT? I tried using xarray functions to little success, my resulting grid is weirdly striped and out of bounds as shown in the pictures.


The multiple grids plotted one over the other


The resulting grid after trying to merge them

Any idea as to why I’m getting this result? Is there a better way to approach this?

Thank you very much!

Hello @Feva67,

Helping you only based on the posted images appears difficult.
Can you please provide the code you are using for combining and plotting the grids, and also provide the data / grids you are using?

I would use the GDAL “gdal_merge.py” program if you have GDAL installed and the files are in a GDAL compatible format.

Thanks! I’ll check if the format is compatible

You could dump the grid files with grd2xyz, find the new (combined) region with info, and re-format with xyz2grd.

Looks like a job for grdblend.

Did it with grdblend, using @Andreas 's idea to format the files properly:

for dem in dems:
print(dem)

grid=pygmt.grd2xyz(dem, output_type=‘pandas’, outfile=None)
pygmt.xyz2grd(grid,region=region,spacing=spacing,outgrid=dem.removesuffix(‘.img’)+“.grd”)

with open(‘list_of_dems.txt’, ‘w’) as f:
for line in dems:
f.write(f"{line.removesuffix(‘.img’)}.grd\n")

with Session() as lib:
lib.call_module(“grdblend”,
“list_of_dems.txt
-Rd-74/-71/-51/-46
-I30e
-G/mergeddem.dem”
)