# Merge grids as mosaic in PyGMT

**URL:** <https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855>\
**Category:** PyGMT Q&A\
**Created:** [April 29, 2024, 8:17pm UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855 "2024-04-29T20:17:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Feva67](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Feva67](https://forum.generic-mapping-tools.org/u/Feva67)\
**Post date:** [April 29, 2024, 8:17pm UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/1 "2024-04-29T20:17:04Z")

</div>

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.

 ![Figure 2024-04-29 171324](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/c/c9f35721d982ca1602bd296b9dd765591542c005.png)  
**The multiple grids plotted one over the other**

 ![Figure 2024-04-29 171329](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/f/fb1574bc8f8c78b5a18871232a5d3116d4f63788.png)  
**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!

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [May 1, 2024, 11:16am UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/2 "2024-05-01T11:16:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![EJFielding](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/ejfielding/32/82_2.png) [@EJFielding](https://forum.generic-mapping-tools.org/u/EJFielding)\
**Post date:** [May 1, 2024, 4:30pm UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/3 "2024-05-01T16:30:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Feva67](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Feva67](https://forum.generic-mapping-tools.org/u/Feva67)\
**Post date:** [May 2, 2024, 8:09pm UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/4 "2024-05-02T20:09:48Z")

</div>

Thanks! I’ll check if the format is compatible

---

<div class="post-metadata">

**Author:** ![Andreas](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Andreas](https://forum.generic-mapping-tools.org/u/Andreas)\
**Post date:** [May 3, 2024, 9:40am UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/5 "2024-05-03T09:40:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [May 3, 2024, 10:54am UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/6 "2024-05-03T10:54:44Z")

</div>

Looks like a job for grdblend.

---

<div class="post-metadata">

**Author:** ![Feva67](https://avatars.discourse-cdn.com/v4/letter/f/51bf81/32.png) [@Feva67](https://forum.generic-mapping-tools.org/u/Feva67)\
**Post date:** [May 3, 2024, 4:40pm UTC](https://forum.generic-mapping-tools.org/t/merge-grids-as-mosaic-in-pygmt/4855/7 "2024-05-03T16:40:32Z")

</div>

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”  
> )
