| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # |
| 3 | # Copyright (c) 2009-2026 by David Sandwell and Paul Wessel |
| 4 | # Credit to Joaquim Luis for adding KML output from ps2raster |
| 5 | # |
| 6 | # Shell script that will generate a Google Earth png tile from |
| 7 | # Sandwell/Smith's 1x1 min Mercator topo.18.1img grid and add |
| 8 | # a basic KML wrapper for use in any Google Earth version. |
| 9 | |
| 10 | # Change these only if you know what you are doing! Note this script |
| 11 | # is hardwired to do bathymetry. You must change many things to have |
| 12 | # it plot crustal ages or gravity. |
| 13 | #------------------ |
| 14 | TOPO=topo.18.1.img |
| 15 | INC=1 |
| 16 | DPI=240 |
| 17 | #------------------ |
| 18 | |
| 19 | if [scip-shell system . . `[`. $# -eq 0 ]; then |
| 20 | catscip-shell system . . cat. << EOF >&2 |
| 21 | img2google - Create Google Earth KML tiles from the $TOPO bathymetry grid |
| 22 | |
| 23 | Usage: img2google -Rwest/east/south/north [imgfile] [-A<mode>[<altitude>]] [-C] [-F<fademin/fademax>] |
| 24 | [-Gprefix] [-L<LODmin/LODmax] [-N<name] [-T<title>] [U<url>] [-V] [-Z[+]] |
| 25 | |
| 26 | -R Specify the region of interest |
| 27 | OPTIONAL ARGUMENTS: |
| 28 | imgfile is the 1x1 min topo img to use [$TOPO] |
| 29 | -A Altitude mode [S]. Append altitude in m if mode = g|A|s. |
| 30 | -C clip data above sealevel [No clipping] |
| 31 | -F sets distances over which we fade from opaque to transparent [no fading] |
| 32 | -G Set output file prefix [Default is topoN|S<north>E|W<west>] |
| 33 | -L Set level of detail (LOD) in pixels. Image goes inactive in GE when there are fewer |
| 34 | than minLOD pixels or more than maxLOD pixels visible. -1 means never invisible. |
| 35 | -N Append KML document layer name ["topoN|S<north>E|W<west>"] |
| 36 | -T Append KML document title name ["Predicted bathymetry"] |
| 37 | -V Optionally run in verbose mode |
| 38 | -U Specify a remote URL for the image [local] |
| 39 | -Z Create a zipped *.kmz file; append + to remove original KML/PNG files [no zipping] |
| 40 | EOF |
| 41 | exit |
| 42 | fi |
| 43 | |
| 44 | # Process the command line arguments |
| 45 | |
| 46 | A="" |
| 47 | C="" |
| 48 | F="" |
| 49 | G="" |
| 50 | L="" |
| 51 | N="" |
| 52 | R="" |
| 53 | T="" |
| 54 | U="" |
| 55 | V="" |
| 56 | Z="" |
| 57 | while [scip-shell system . . `[`. ! x"$1" = x ]; do |
| 58 | case $1 |
| 59 | in |
| 60 | -R*) R=$1; # Got the region |
| 61 | shift;; |
| 62 | -A*) A=$1; # set altitude mode |
| 63 | shift;; |
| 64 | -C) C=$1; # use clipping |
| 65 | shift;; |
| 66 | -F*) F=$1; # Got fade settings |
| 67 | shift;; |
| 68 | -G*) G=$1; # Got the output namestem |
| 69 | shift;; |
| 70 | -L*) L=$1; # Got level of detail settings |
| 71 | shift;; |
| 72 | -N*) N=$1; # Got the KML layername string |
| 73 | shift;; |
| 74 | -T*) T=$1; # Got the KML title string |
| 75 | shift;; |
| 76 | -V) V=$1; # Verbose run |
| 77 | shift;; |
| 78 | -U*) U=$1; # Got a URL prefix |
| 79 | shift;; |
| 80 | -Z*) Z=$1; # Make KMZ file |
| 81 | shift;; |
| 82 | -*) echoscip-shell system . . echo. "$0: Unrecognized option $1" 1>&2; # Bad option argument |
| 83 | exit 1;; |
| 84 | *) TOPO=$1; # The input file name |
| 85 | shift;; |
| 86 | esac |
| 87 | done |
| 88 | if [scip-shell system . . `[`. X"$R" = "X" ]; then |
| 89 | echoscip-shell system . . echo. "$0: ERROR: Must specify the region:" 1>&2 |
| 90 | fi |
| 91 | |
| 92 | # 1. Make sure we have the img file either locally or via $GMT_IMGDIR |
| 93 | |
| 94 | if [scip-shell system . . `[`. ! -f $TOPO ]; then |
| 95 | if [scip-shell system . . `[`. "X$GMT_IMGDIR" = "X" ] || [scip-shell system . . `[`. ! -f $GMT_IMGDIR/$TOPO ]; then |
| 96 | echoscip-shell system . . echo. "img2google: Cannot find $TOPO - exiting" |
| 97 | exit 1 |
| 98 | fi |
| 99 | fi |
| 100 | |
| 101 | # Compute dimension of plot in inches and use that as our papersize |
| 102 | w=$(echoscip-shell system . . echo. $R | awkscip-shell system . . awk. -F/ '{print substr($1,3)}') |
| 103 | e=$(echoscip-shell system . . echo. $R | awkscip-shell system . . awk. -F/ '{print $2}') |
| 104 | s=$(echoscip-shell system . . echo. $R | awkscip-shell system . . awk. -F/ '{print $3}') |
| 105 | n=$(echoscip-shell system . . echo. $R | awkscip-shell system . . awk. -F/ '{print $4}') |
| 106 | |
| 107 | # Extend by two boundary rows/cols |
| 108 | |
| 109 | w2=$(gmt math -Q -fg --FORMAT_GEO_OUT=ddd:mm $w $INC 60 DIV 2 MUL SUB =) |
| 110 | e2=$(gmt math -Q -fg --FORMAT_GEO_OUT=ddd:mm $e $INC 60 DIV 2 MUL ADD =) |
| 111 | s2=$(gmt math -Q -fg --FORMAT_GEO_OUT=ddd:mm $s $INC 60 DIV 2 MUL SUB =) |
| 112 | n2=$(gmt math -Q -fg --FORMAT_GEO_OUT=ddd:mm $n $INC 60 DIV 2 MUL ADD =) |
| 113 | W=$(gmt math -Q -fg $e2 $w2 SUB =) |
| 114 | H=$(gmt math -Q -fg $n2 $s2 SUB =) |
| 115 | R2=-R$w2/$e2/$s2/$n2 |
| 116 | |
| 117 | # 2. Extract the (x,y,z) of constrained nodes by extracting a grid with |
| 118 | # NaNs were unconstrained, exclude NaNs, and capture points whose z < 0 |
| 119 | |
| 120 | gmt img2grd $TOPO $R2 -T2 -S1 $V -G$$.tile.nc |
| 121 | gmt grd2xyz $$.tile.nc -s -bod | gmt select -Z-15000/-2.0 -bi3d > $$.track.xyz |
| 122 | |
| 123 | # 3. get the topo data, this time including unconstrained estimates |
| 124 | |
| 125 | gmt img2grd $TOPO $R2 -T1 -S1 -E $V -G$$.tile.nc |
| 126 | |
| 127 | # 4. make the image |
| 128 | |
| 129 | # Create list of desired, irregular contours |
| 130 | catscip-shell system . . cat. << EOF > $$.intervals |
| 131 | -10000 C |
| 132 | -9000 C |
| 133 | -8000 C |
| 134 | -7000 C |
| 135 | -6000 C |
| 136 | -5000 C |
| 137 | -4000 C |
| 138 | -3000 C |
| 139 | -2000 C |
| 140 | -1000 C |
| 141 | -5500 C |
| 142 | -4500 C |
| 143 | -3500 C |
| 144 | -2500 C |
| 145 | -1500 C |
| 146 | -500 C |
| 147 | EOF |
| 148 | # -2000 contour drawn separately with black, heavier line |
| 149 | echoscip-shell system . . echo. "-2000 C" > $$.int2000 |
| 150 | gmt makecpt -Ctopo -Z > $$.cpt |
| 151 | gmt grdgradient $$.tile.nc $V -A340 -G$$.nc |
| 152 | gmt grdmath $$.nc 12000 DIV = $$.tile_grad.nc |
| 153 | gmt psclip $R -Jx1id -Y0 -X0 -P -K -N -T $V --PROJ_ELLIPSOID=WGS-84 --PS_MEDIA=${W}ix${H}i > $$.ps |
| 154 | #Set up (0,0) to match the even LL corner |
| 155 | off=$(gmt math -Q $INC 2 MUL 60 DIV NEG =) |
| 156 | if [scip-shell system . . `[`. X"$C" = "X" ]; then # No clipping, just lay down image |
| 157 | gmt grdimage $$.tile.nc -I$$.tile_grad.nc -C$$.cpt -Jx1id -Y$off -X$off -O -K $V --PROJ_ELLIPSOID=WGS-84 >> $$.ps |
| 158 | else # Use GSHHS high clip path to only show ocean areas |
| 159 | gmt pscoast $R -Jx1id -Y$off -X$off -O -K $V --PROJ_ELLIPSOID=WGS-84 -Dh -Sc >> $$.ps |
| 160 | gmt grdimage $$.tile.nc -I$$.tile_grad.nc -C$$.cpt -J -O -K $V --PROJ_ELLIPSOID=WGS-84 >> $$.ps |
| 161 | fi |
| 162 | gmt psxy $$.track.xyz -J $R -Sc.005i -Gblack $V -O -K --PROJ_ELLIPSOID=WGS-84 >> $$.ps |
| 163 | gmt grdcontour $$.tile.nc -J -C$$.intervals -Wfaint,80 -O -K $V --PROJ_ELLIPSOID=WGS-84 >> $$.ps |
| 164 | gmt grdcontour $$.tile.nc -J -C$$.int2000 -W1p,black -O -K $V --PROJ_ELLIPSOID=WGS-84 >> $$.ps |
| 165 | if [scip-shell system . . `[`. X"$C" = "X" ]; then |
| 166 | gmt psxy -R -J -O -K -T >> $$.ps |
| 167 | else |
| 168 | gmt pscoast -O -K -Q >> $$.ps |
| 169 | fi |
| 170 | gmt psclip -C -O >> $$.ps |
| 171 | # |
| 172 | # 5. make the geotiff file |
| 173 | # |
| 174 | if [scip-shell system . . `[`. X"$G" = "X" ]; then |
| 175 | xtag=$(echoscip-shell system . . echo. $w | awkscip-shell system . . awk. '{if ($1 < 0.0) {printf "W%g\n", -$1} else if ($1 > 180.0) {printf ("W%g\n", 360-$1)} else {printf "E%g\n", $1}}') |
| 176 | ytag=$(echoscip-shell system . . echo. $n | awkscip-shell system . . awk. '{if ($1 < 0.0) {printf "S%g\n", -$1} else {printf "N%g\n", $1}}') |
| 177 | name="topo${ytag}${xtag}" |
| 178 | else |
| 179 | name=$(echoscip-shell system . . echo. $G | awkscip-shell system . . awk. '{print substr($1,3)}') |
| 180 | fi |
| 181 | if [scip-shell system . . `[`. X"$A" = "X" ]; then |
| 182 | A=+aS |
| 183 | else |
| 184 | A=+a$(echoscip-shell system . . echo. $A | awkscip-shell system . . awk. '{print substr($1,3)}') |
| 185 | fi |
| 186 | if [scip-shell system . . `[`. ! X"$F" = "X" ]; then |
| 187 | F=+f$(echoscip-shell system . . echo. $F | awkscip-shell system . . awk. '{print substr($1,3)}') |
| 188 | fi |
| 189 | if [scip-shell system . . `[`. ! X"$L" = "X" ]; then |
| 190 | L=+l$(echoscip-shell system . . echo. $L | awkscip-shell system . . awk. '{print substr($1,3)}') |
| 191 | fi |
| 192 | if [scip-shell system . . `[`. ! X"$U" = "X" ]; then |
| 193 | U=+u$(echoscip-shell system . . echo. $U | awkscip-shell system . . awk. '{print substr($1,3)}') |
| 194 | fi |
| 195 | if [scip-shell system . . `[`. X"$N" = "X" ]; then |
| 196 | N="+n$name" |
| 197 | else |
| 198 | N=+n$(echoscip-shell system . . echo. $N | awkscip-shell system . . awk. '{print substr($0,3)}') |
| 199 | fi |
| 200 | if [scip-shell system . . `[`. X"$T" = "X" ]; then |
| 201 | T="+tPredicted bathymetry" |
| 202 | else |
| 203 | T=+t$(echoscip-shell system . . echo. $T | awkscip-shell system . . awk. '{print substr($0,3)}') |
| 204 | fi |
| 205 | mvscip-shell system . . mv. -f $$.ps $name.ps |
| 206 | gmt psconvert $name.ps -E${DPI} -A- -TG -W+k${A}${F}${L}"${N}${T}"${U} $V |
| 207 | if [scip-shell system . . `[`. ! "X$Z" = "X" ]; then # zip up as one archive |
| 208 | zip -rq9 $name.kmz $name.kml $name.png |
| 209 | if [scip-shell system . . `[`. "X$Z" = "X-Z+" ]; then |
| 210 | rmscip-shell system . . rm. -f $name.kml $name.png |
| 211 | fi |
| 212 | fi |
| 213 | # 6. clean up |
| 214 | rmscip-shell system . . rm. -f $$.* $name.ps |
| 215 | exit 0 |