How to trim a contour map?

Hello everyone!

Could you please give me a hint how I can crop my contour map that was created using surface command? I feel like I should use gridcut, however it seems I need t specify my region of interest by -R, whereas I would like to cut everything outside the red polygon. I have looked at “select” command but it seems it takes only points. Thank you in advance.

gmt begin contour png
Rg=-R958800/959700/2117300/2118000
gmt surface vel.txt $Rg -JX20c -I25 -Gvelmap.nc
gmt grdview velmap.nc -Ccolors.cpt -Qs
gmt grdcontour velmap.nc -C0.5 -A0.5+f12p,Helvetica,black -Gd10c -S4
-Wa0.75p,black -Wc0.25p,black
gmt plot polygon.txt -W2p,red
gmt plot gps_lamb.csv -i0,1 $Rg -JX20c -B -Sc0.5c -Gblack
gmt colorbar -DJBC -Bx1+l"Velocity (mm hr@±1@+)" -Ccolors.cpt
gmt end show

Maybe not the optimal way, but I think you could use grd2xyz to dump the grid, pipe it trough gmtselect (remove points outside the polygon) and then pipe that to xyz2grd to make a new grid.

grd2xyz velmap.nc | gmtselect -Fpolygon.txt | xyz2grd $(gmt info -I25 polygon.txt ) -I25 -Gvelmap_clip.nc

You can try “gmt clip”.

1 Like

Thank you, @Andreas and @seisman! GMT clip did the job. For those who have the same question, I have just added one line of code
“gmt clip $Rg -JX20c polygon.txt” in the beginning.

Remember to turn off clipping with another gmt clip -c after you are done. May not matter to this plot but will matter to other things you may do. Clipping is an ON then OFF paired operation.

1 Like

The ability to cut a grid based on a polygon - would this be a nice addition to grdcut?

E.g.

gmt grdcut in.grd -Gout.nc -Fcut_polygon.gmt

What is the recommended way to cut a grid based on a polygon?

1 Like

You can use grdmask. Create a grid with NaN outside and 1 inside the polygon and then multiplied both grids.

Andreas has a point though. It seems like a good option to have for a module like grdcut. The grdmask alternative requires two steps and setting of some options. For the -Fpoly.txt option I think we simply want to retain all values inside the polygon and set the outside to NaN. So -F would make that much simpler.

Although a trivial(?) task for a seasoned gmt user, I think this would be a very nice addition to grdcut. It is a fairly common operation. Saving time is always nice.

One thing though; retaining all values inside the polygon and set the outside to NaN is nice, but shouldnt there also be calculated a new -R based on the polygon? I can see many cases where using the original -R from the input grid, would cause a too big region to be used, containing only NaN’s.

Yes of course, it is grdcut so it would cut the rectangular bounding box of the polygon, but unless your polygon is a rectangle there will be inside and outside parts.

Sounds great! [padding - post must be > 20 chars]

I took the liberty to create a feature request; https://github.com/GenericMappingTools/gmt/issues/5004.

1 Like

Implemented and merged into master: grdcut -Fpolygon[+c][+i].
+c crops grid region to bounding box [Default uses inout grid region, possibly modified by -R]
+i inverts the fill and sets all nodes inside the polygon to NaN [Default sets outside nodes to NaN]

1 Like

A big thank you to Paul et al for implementing this!