(17) Images clipped by coastlines

This example demonstrates how coast can be used to set up clip paths based on coastlines. This approach is well suited when different gridded data sets are to be merged on a plot using different CPTs. Merging the files themselves may not be doable since they may represent different data sets, as we show in this example. Here, we lay down a color map of the geoid field near India with grdimage, use coast to set up land clip paths, and then overlay topography from the ETOPO5 data set with another call to grdimage. We finally undo the clippath with a second call to coast with the option -Q (See the Figure Clipping of images using coastlines):

We also plot a color legend on top of the land. So here we basically have three layers of “paint” stacked on top of each other: the underlying geoid map, the land mask, and finally the color legend. This legend makes clear how grd2cpt distributed the colors over the range: they are not of equal length put are associated with equal amounts of area in the plot. Since the high amounts (in red) are not very prevalent, that color spans a long range.

For this image it is appropriate to use the -I option in colorbar so the legend gets shaded, similar to the geoid grid. See Of Colors and Color Legends to learn more about CPTs and ways to draw color legends.

#!/usr/bin/env bash
#		GMT EXAMPLE 17
#
# Purpose:	Illustrates clipping of images using coastlines
# GMT modules:	grd2cpt, grdimage, coast, text, makecpt
# Unix progs:	rm
#
gmt begin ex17

	# First generate geoid image w/ shading
	gmt grd2cpt @india_geoid.nc -Crainbow -H > geoid.cpt
	gmt grdimage @india_geoid.nc -I+d -JM16c -Cgeoid.cpt

	# Then use gmt coast to initiate clip path for land
	gmt coast -R@india_geoid.nc -Dl -G

	# Now generate topography image w/shading
	gmt makecpt -C150 -T-10000,10000 -N
	gmt grdimage @india_topo.nc -I+d

	# Finally undo clipping and overlay basemap
	gmt coast -Q -B+t"Clipping of Images" -B

	# Put a colorbar on top of the land mask
	gmt colorbar -DjTR+o0.8c/0.2c+w10c/0.5c+h -Cgeoid.cpt -Bx5f1 -By+lm -I

	# Add a text paragraph
	gmt text -M -Gwhite -Wthinner -C+tO -Dj-8p/8p -F+f12,Times-Roman+jRB <<- END
	> 90 -10 12p 8c j
	@_@%5%Example 17.@%%@_  We first plot the color geoid image
	for the entire region, followed by a gray-shaded @#etopo5@#
	image that is clipped so it is only visible inside the coastlines.
	END

	# Clean up
	rm -f geoid.cpt
gmt end show
../_images/ex17.png

Clipping of images using coastlines