Merging Two Grid Files in GMT
Merging Two Grid Files in GMT
- This is easy if you want to merge the files on a horizontal or vertical line (use grdcut and grdpaste, see GMT documentation). However, if you need to merge two grd files differently, you need a different approach (ie: merging an oddly shaped multibeam bathymetry profile with regional bathymetry). Here is one method that allows a lot of freedom:
- Sample grid file A (the low resolution one, if they aren't equal) to the same resolution as B (the high res version). Use grdsample for this.
grdsample A.grd -Rregion -Iresolution -Ghires_A.grd
- Use grdcut to make sure both have the same dimensions. Ensure that they both use the same projection.
grdcut A.grd -Rregion -Ghires_A_trimmed.grd
- Use grdmask to define a polygon. This polygon will be used to replace a section of grid file A with grid file B. The NaN part is important because it defines all the values inside the polygon as being NaN, which will be replaced later with new values from grid file B. The polygon used here is a closed triangle.
grdmask -Rregion -Iresolution -N1/1/NaN << END -Gclip.grd
x1 y1
x2 y1
x2 y2
x1 y1
END
- Apply this polygon "mask" to A using grdmath. This will set all values inside the polygon to NaN, but wont touch values outside the polygon.
grdmath B.grd clip.grd MUL = B_clipped.grd
- Finally use grdmath to combine the two files using XOR (A will replace B only where B has NaN values). This will create a merged grid file, called merged.grd. Its a somewhat complicated procedure, but very powerful.
grdmath B_clipped.grd hires_A_trimmed.grd XOR = merged.grd