Most community-contributed GMT plotting scripts are written as Linux bash scripts, which can be challenging for Windows users unfamiliar with bash syntax. This guide explains how to adapt bash scripts for use on Windows by converting them to batch (.bat) files.
The core differences between bash and batch scripts are straightforward:
- Comments:
#becomesrem - Variable declaration:
PS=map.psbecomesset PS=map.ps - Variable expansion:
$PSbecomes%PS% - File deletion:
rm files.*becomesdel files.*
Most of these transformations can be handled efficiently using search-and-replace in any text editor. Simply search for # and replace with rem, and replace variable references like $PS, $R, and $J with %PS%, %R%, and %J% respective.
However, more complex bash constructs require a deeper understanding of batch file syntax. Below are common conversions you'll encounter:
Capturing Command Output
In bash, you can capture command output direct into a variable:
T1=$(gmt grdinfo world6m.grd -T1000)
gmt makecpt -Crainbow $T1 > 1.cpt
Batch files require a different approach using either a for loop or temporary files:
rem Method 1: Using for loop
for /f %%i in ('gmt grdinfo world6m.grd -T1000') do set T1=%%i
gmt makecpt -Crainbow %T1% > 1.cpt
rem Method 2: Using temporary file
gmt grdinfo world6m.grd -T1000 > tmp.txt
set /p T1=<tmp.txt
gmt makecpt -Crainbow %T1% > 1.cpt
Heredoc Input
Bash supports heredoc syntax for multi-line input:
gmt psxy -R -J -Sc0.5c -Gred -W0.5p,black -K -O << EOF >> $PS
112.35 36.56
113.78 42.15
114.50 37.60
EOF
In batch files, you must write the data to a temporary file first, then pass it to the command:
echo 112.35 36.56 > tmp
echo 113.78 42.15 >> tmp
echo 114.50 37.60 >> tmp
gmt psxy tmp -R -J -Sc0.5c -Gred -W0.5p,black -O -K >> %PS%
Unix Utilities
If your script uses grep, cut, wc, cat, or similar utilities, download the UnixTools package and extract it to you're GMT bin directory. Also note that bash's awk must be replaced with gawk, and single quotes should be converted to double quotes.
Complete Example
Consider the following bash script:
#!/bin/bash
OUTPUT=map.ps
REGION=-R108/125/33/43
PROJECTION=-JM6i
gmt psbasemap $REGION $PROJECTION -B3 -K > $OUTPUT
LEVEL=$(gmt grdinfo huabei.grd -T100)
gmt makecpt -Ctopo $LEVEL > huabei.cpt
gmt grdimage -R -J huabei.grd -Chuabei.cpt -K -O >> $OUTPUT
echo 112 36 > stations.txt
echo 120 40 >> stations.txt
echo 115 39 >> stations.txt
gmt psxy stations.txt -R -J -St0.5c -Gblack -K -O >> $OUTPUT
awk '{if (NR>1) print $4,$3}' event.dat | gmt psxy -R -J -Sc0.4c -Gblack -O >> $OUTPUT
rm -f gmt.* huabei.cpt stations.txt
The equivalent batch file would be:
set OUTPUT=map.ps
rem Define geographic extent
set REGION=-R108/125/33/43
rem Set map projection
set PROJECTION=-JM6i
gmt psbasemap %REGION% %PROJECTION% -B3 -K > %OUTPUT%
for /f %%i in ('gmt grdinfo huabei.grd -T100') do set LEVEL=%%i
gmt makecpt -Ctopo %LEVEL% > huabei.cpt
gmt grdimage -R -J huabei.grd -Chuabei.cpt -K -O >> %OUTPUT%
echo 112 36 > stations.txt
echo 120 40 >> stations.txt
echo 115 39 >> stations.txt
gmt psxy stations.txt -R -J -St0.5c -Gblack -K -O >> %OUTPUT%
gawk "{if (NR>1) print $4,$3}" event.dat | gmt psxy -R -J -Sc0.4c -Gblack -O >> %OUTPUT%
del gmt.* huabei.cpt stations.txt