img2grd - Extract subset of img file in Mercator or Geographic format
img2grd imgfile -Ggrdfile -Rregion -Ttype [ -C ] [ -D[minlat/maxlat] ] [ -E ] [ -Iminutes ] [ -M ] [ -Nnavg ] [ -S[scale] ] [ -V[level] ] [ -Wmaxlon ] [ -n<flags> ]
Note: No space is allowed between the option flag and the associated arguments.
img2grd reads an img format file, extracts a subset, and writes it to a grid file. The -M option dictates whether or not the Spherical Mercator projection of the img file is preserved or if a Geographic grid should be written by undoing the Mercator projection. If geographic grid is selected you can also request a resampling onto the exact -R given.
The -M option should be excluded if you need the output grid to be in geographic coordinates. To extract data in the region -R-40/40/-70/-30 from world_grav.img.7.2 and reproject to yield geographic coordinates, you can try
img2grd world_grav.img.16.1 -Gmerc_grav.nc -R-40/40/-70/-30 -V
Because the latitude spacing in the img file is equidistant in Mercator units, the resulting grid will not match the specified -R exactly, and the latitude spacing will not equal the longitude spacing. If you need an exact match with your -R and the same spacing in longitude and latitude, use the -E option:
img2grd world_grav.img.16.1 -Gmerc_grav.nc -R-40/40/-70/-30 -E -V
Since the img files are in a Mercator projection, you should NOT extract a geographic grid if your plan is to make a Mercator map. If you did that you end of projecting and reprojection the grid, loosing short-wavelength detail. Better to use -M and plot the grid using a linear projection with the same scale as the desired Mercator projection (see GMT Example 29). To extract data in the region -R-40/40/-70/-30 from world_grav.img.7.2, run
gmt img2grd -M world_grav.img.7.2 -Gmerc_grav.nc -R-40/40/-70/-30 -V
Note that the -V option tells us that the range was adjusted to -R-40/40/-70.0004681551/-29.9945810754. We can also use grdinfo to find that the grid file header shows its region to be -R0/80/0/67.9666667 This is the range of x,y we will get from a Spherical Mercator projection using -R-40/40/-70.0004681551/-29.9945810754 and -Jm1. Thus, to take ship.lonlatgrav and use it to sample the merc_grav.nc, we can do this:
gmt set PROJ_ELLIPSOID Sphere gmt mapproject -R-40/40/-70.0004681551/-29.9945810754 -Jm1i ship.lonlatgrav | \ gmt grdtrack -Gmerc_grav.nc | gmt mapproject \ -R-40/40/-70.0004681551/-29.9945810754 -Jm1i -I > ship.lonlatgravsat
It is recommended to use the above method of projecting and unprojecting the data in such an application, because then there is only one interpolation step (in grdtrack). If one first tries to convert the grid file to lon,lat and then sample it, there are two interpolation steps (in conversion and in sampling).
To make a lon,lat grid from the above grid we can use
gmt grdproject merc_grav.nc -R-40/40/-70.0004681551/-29.9945810754 -Jm1i -I -D2m -Ggrav.nc
In some cases this will not be easy as the -R in the two coordinate systems may not align well. When this happens, we can also use (in fact, it may be always better to use)
gmt grd2xyz merc_grav.nc | gmt mapproject \ -R-40/40/-70.0004681551/-29.994581075 -Jm1i -I | \ gmt surface -R-40/40/-70/70 -I2m -Ggrav.nc
To make a Mercator map of the above region, suppose our gmt.conf value for PROJ_LENGTH_UNIT is inch. Then since the above merc_grav.nc file is projected with -Jm1i it is 80 inches wide. We can make a map 8 inches wide by using -Jx0.1i on any map programs applied to this grid (e.g., grdcontour, grdimage, grdview), and then for overlays which work in lon,lat (e.g., psxy, pscoast) we can use the above adjusted -R and -Jm0.1 to get the two systems to match up.
However, we can be smarter than this. Realizing that the input img file had pixels 2.0 minutes wide (or checking the nx and ny with grdinfo merc_grav.nc) we realize that merc_grav.nc used the full resolution of the img file and it has 2400 by 2039 pixels, and at 8 inches wide this is 300 pixels per inch. We decide we do not need that many and we will be satisfied with 100 pixels per inch, so we want to average the data into 3 by 3 squares. (If we want a contour plot we will probably choose to average the data much more (e.g., 6 by 6) to get smooth contours.) Since 2039 isn’t divisible by 3 we will get a different adjusted -R this time:
gmt img2grd -M world_grav.img.7.2 -Gmerc_grav_2.nc -R-40/40/-70/-30 -N3 -V
This time we find the adjusted region is -R-40/40/-70.023256525/-29.9368261101 and the output is 800 by 601 pixels, a better size for us. Now we can create an artificial illumination file for this using grdgradient:
gmt grdgradient merc_grav_2.nc -Gillum.nc -A0/270 -Ne0.6
and if we also have a CPT file called “grav.cpt” we can create a color shaded relief map like this:
gmt grdimage merc_grav_2.nc -Iillum.nc -Cgrav.cpt -Jx0.1i -K > map.ps gmt psbasemap -R-40/40/-70.023256525/-29.9368261101 -Jm0.1i -Ba10 -O >> map.ps
Suppose you want to obtain only the constrained data values from an img file, in lat/lon coordinates. Then run img2grd with the -T2 option, use grd2xyz to dump the values, pipe through grep -v NaN to eliminate NaNs, and pipe through mapproject with the inverse projection as above.