add -S option to gmtxy-image2

This commit is contained in:
张壹 2020-04-25 21:13:28 +08:00
parent 3498dc2f90
commit 1da2101cb2

View File

@ -3,7 +3,12 @@
. gmt_shell_functions.sh
# 包含dispOption脚本
. dispOptions
#GMT显示平面数据脚本输入文件为网格文件没有包含网格化语句因为网格化过程中的情况多样化建议在其他脚本中个别添加再调用此脚本
# GMT显示平面数据脚本输入文件为网格文件没有包含网格化语句因为网格化过程中的情况多样化建议在其他脚本中个别添加再调用此脚本
# 准备几个常用的排版参数组 记录的参数包括 图片的宽度 默认坐标标示设定 色标位置 色标的大小 标示字体大小
com_layouts=("1.5i,WesNZ,0.1i/-0.2i,1.3i/0.05i+h,10.5p" \
"1.5i,WeSnZ,1.65i/0.05i,1.3i/0.05i,10.5p" \
"1.2i,WesNZ,0.1i/-0.1i,1.0i/0.03i+h,7.5p" \
"1.2i,WeSnZ,1.3i/0.05i,1.0i/0.05i,7.5p")
# 初始化参数
data='null'
outpsfile='null'
@ -17,7 +22,7 @@ labels=("x (m)" "y (m)")
plotgrad=0
axistick=("a" "a") #横纵坐标轴间隔 a表示自动
bartick="a" #色标轴标轴间隔 a表示自动
frameset="wEsNZ"
frameset="null"
plot_colorbar=1
annotation="null"
keep_open=0
@ -25,8 +30,10 @@ from_open=">"
image_origin=("0.5" "0.5")
bar_origin=("0" "0")
polyfile='null'
lay_num=0
run_command=1
# 从命令行获取参数
while getopts "hi:o:r:u:c:a:G:t:v:f:s:p:X:x:gnKO" arg
while getopts "hi:o:L:r:u:c:l:G:t:v:f:a:p:X:x:gnKOS" arg
do
case $arg in
h)
@ -34,39 +41,45 @@ do
The template accepts a grid (.nc or .grd) file as input and outputs a .png and a .eps file. \
For further explanations, please look for GMT's manuscripts."
dispAuthorInfo "Dr. Yi Zhang (yizhang-geo@zju.edu.cn)"
dispUsage "${0##*/} -i<grid-data> [-o<img-file>] [-r<xmin>/<xmax>/<ymin>/<ymax>] [-u<unit>] \
[-c<cpt-file>] [-t<number>,<number>] [-v<number>] [-a<x-label>;<y-label>] [-g] [-G<grad-file>] \
[-f<wEsNZ>] [-p<poly-file>] [-n] [-s<x-coor>,<y-coor>,<annotation>] [-K] [-O] \
dispUsage "${0##*/} -i<grid-data> [-o<img-file>] [-L<number>] [-r<xmin>/<xmax>/<ymin>/<ymax>] [-u<unit>] \
[-c<cpt-file>] [-t<number>,<number>] [-v<number>] [-l<x-label>;<y-label>] [-g] [-G<grad-file>] \
[-f<wEsNZ>] [-p<poly-file>] [-n] [-a<x-coor>,<y-coor>,<annotation>] [-K] [-O] [-S] \
[-X<x-offset>,<y-offset>] [-x<x-offset>,<y-offset>]"
dispOptionShort "-i" "input grid file."
dispOptionShort "-o" "output image file. File extension is not needed. the input filename will be \
dispOptionShort "-i" "Input grid file."
dispOptionShort "-o" "Output image file. File extension is not needed. the input filename will be \
used if this option is not set."
dispOptionShort "-r" "data range. The template will ditect the input data range automaticly, \
dispOptionShort "-L" "Select layouts. enter a number to select the layout to use (0-3). \
The default is zero."
dispOptionShort "-r" "Data range. The template will ditect the input data range automaticly, \
the use of this option will overwrite the range."
dispOptionShort "-u" "data unit. The default is meter."
dispOptionShort "-c" "color cpt. The default is rainbow."
dispOptionShort "-t" "intervals of axis's labels. the script will set the intervals automatically \
dispOptionShort "-u" "Data unit. The default is meter."
dispOptionShort "-c" "Color cpt. The default is rainbow."
dispOptionShort "-t" "Intervals of axis's ticks. the script will set the intervals automatically \
if -t option is not set."
dispOptionShort "-v" "intervals of color bar's labels. the script will set the intervals \
dispOptionShort "-v" "Intervals of color bar's labels. the script will set the intervals \
automatically if -v option is not set."
dispOptionShort "-a" "axis labels separated by semicolons. The deafults are x (m) and y (m)."
dispOptionShort "-g" "use grdgradient. The default is false."
dispOptionShort "-G" "provide a different input grid data for the use of grdgradient. \
dispOptionShort "-l" "Axis labels separated by semicolons. The deafults are x (m) and y (m)."
dispOptionShort "-g" "Use grdgradient. The default is false."
dispOptionShort "-G" "Provide a different input grid data for the use of grdgradient. \
This should be used with the '-g' option at the same time."
dispOptionShort "-f" "Set frames for mapping axises. The default is wEsNZ."
dispOptionShort "-n" "Don't plot color bar."
dispOptionShort "-s" "Add an annotation symbol on the map. you need to enter a string contains both \
dispOptionShort "-a" "Add an annotation on the map. you need to enter a string contains both \
coordinates and text of the annotation, such as '10,10,(a)'."
dispOptionShort "-p" "Plot polygons via a file."
dispOptionShort "-K" "Keep the .ps file open."
dispOptionShort "-O" "Continue from a previous .ps file."
dispOptionShort "-X" "Move the starting point for mapping image."
dispOptionShort "-x" "Move the starting point for mapping color bar."
dispOptionShort "-S" "Instead of running the script, print a executable script on screen. \
This option is designed to help building data specified scripts using the template."
exit 0;;
i)
data=$OPTARG;;
o)
outpsfile=$OPTARG;;
L)
lay_num=$OPTARG;;
u)
unit=$OPTARG;;
c)
@ -84,7 +97,7 @@ do
r)
overwrite=1
overwriteRange=$OPTARG;;
a)
l)
OLD_IFS="${IFS}"
IFS=";"
labels=(${OPTARG})
@ -95,7 +108,7 @@ do
plot_colorbar=0;;
G)
gridData=$OPTARG;;
s)
a)
annotation=$OPTARG;;
p)
polyfile=$OPTARG;;
@ -103,6 +116,8 @@ do
keep_open=1;;
O)
from_open="-O >>";;
S)
run_command=0;;
?)
printf "error: unknow argument\nuse -h option to see help information\n"
exit 1;;
@ -122,7 +137,14 @@ else
psfile=${outpsfile}.ps
jpgfile=${outpsfile}.png
fi
# 获取排版
layouts=(${com_layouts[${lay_num}]//,/ })
# 获取坐标轴设置
if [[ ${frameset} == 'null' ]]; then
frameset=${layouts[1]}
fi
# 获取网格范围
if [[ $overwrite == 1 ]]; then
range=${overwriteRange}
@ -131,80 +153,175 @@ else
fi
#根据横纵坐标的范围计算图片的长和高 我们默认图片宽度为1.5i
range_coor=(${range//// })
pic_height=`echo "scale=4; 1.5 * (${range_coor[3]}-${range_coor[2]})/(${range_coor[1]}-${range_coor[0]})"|bc`
# 设置绘图参数
gmt gmtset \
FONT_ANNOT_PRIMARY=10.5p,Times-Roman,black \
MAP_FRAME_PEN=thinnest,black \
MAP_GRID_PEN_PRIMARY=thinnest,black \
MAP_TICK_PEN_PRIMARY=thinnest,black \
MAP_TICK_LENGTH_PRIMARY=1p/0.5p \
MAP_TITLE_OFFSET=7.5p \
MAP_GRID_CROSS_SIZE_PRIMARY=2p \
FONT_LABEL=10.5p,Times-Roman,black \
MAP_FRAME_AXES=${frameset} \
MAP_LABEL_OFFSET=2.5p \
MAP_ANNOT_OFFSET_PRIMARY=2.5p
pic_height=`echo "scale=4; ${layouts[0]%i} * (${range_coor[3]}-${range_coor[2]})/(${range_coor[1]}-${range_coor[0]})"|bc`
# 输出一个bash脚本的头部到屏幕
if [[ ${run_command} == 0 && ${from_open} == '>' ]]; then
echo "#!/bin/bash"
fi
# 设置绘图参数
if [[ ${run_command} == 0 && ${from_open} == '>' ]]; then
echo "gmt gmtset \
FONT_ANNOT_PRIMARY=${layouts[4]},Times-Roman,black \
MAP_FRAME_PEN=thinnest,black \
MAP_GRID_PEN_PRIMARY=thinnest,black \
MAP_TICK_PEN_PRIMARY=thinnest,black \
MAP_TICK_LENGTH_PRIMARY=1p/0.5p \
MAP_TITLE_OFFSET=7.5p \
MAP_GRID_CROSS_SIZE_PRIMARY=2p \
FONT_LABEL=${layouts[4]},Times-Roman,black \
MAP_FRAME_AXES=${frameset} \
MAP_LABEL_OFFSET=2.5p \
MAP_ANNOT_OFFSET_PRIMARY=2.5p"
else
gmt gmtset \
FONT_ANNOT_PRIMARY=${layouts[4]},Times-Roman,black \
MAP_FRAME_PEN=thinnest,black \
MAP_GRID_PEN_PRIMARY=thinnest,black \
MAP_TICK_PEN_PRIMARY=thinnest,black \
MAP_TICK_LENGTH_PRIMARY=1p/0.5p \
MAP_TITLE_OFFSET=7.5p \
MAP_GRID_CROSS_SIZE_PRIMARY=2p \
FONT_LABEL=${layouts[4]},Times-Roman,black \
MAP_FRAME_AXES=${frameset} \
MAP_LABEL_OFFSET=2.5p \
MAP_ANNOT_OFFSET_PRIMARY=2.5p
fi
# 处理-B选项的参数
if [[ ${axistick[0]} == 'a' ]]; then
axistick[0]=${axistick[0]}g
axistick[1]=${axistick[1]}g
else
axistick[0]=${axistick[0]}g${axistick[0]}
axistick[1]=${axistick[1]}g${axistick[1]}
fi
if [[ ${run_command} == 0 ]]; then
echo "gmt grd2cpt ${data} -C${color} -R${range} -Z -D > ${cptfile}"
else
gmt grd2cpt ${data} -C${color} -R${range} -Z -D > ${cptfile}
fi
gmt grd2cpt ${data} -C${color} -R${range} -Z -D > $cptfile
if [[ $plotgrad == 1 ]]; then
gradfile=${data%.*}Grad.nc
if [[ $gridData == 'null' ]]; then
gmt grdgradient ${data} -G${gradfile} -Nt -A0/45
if [[ ${run_command} == 0 ]]; then
echo "gmt grdgradient ${data} -G${gradfile} -Nt -A0/45"
else
gmt grdgradient ${data} -G${gradfile} -Nt -A0/45
fi
else
gmt grdgradient ${gridData} -G${gradfile} -Nt -A0/45
if [[ ${run_command} == 0 ]]; then
echo "gmt grdgradient ${gridData} -G${gradfile} -Nt -A0/45"
else
gmt grdgradient ${gridData} -G${gradfile} -Nt -A0/45
fi
fi
gmt grdimage ${data} -R${range} -C${cptfile} -I${gradfile} \
-Bx${axistick[0]}g${axistick[0]}+l"${labels[0]}" -By${axistick[1]}g${axistick[1]}+l"${labels[1]}" \
-JX1.5i/${pic_height}i -X${image_origin[0]}i -Y${image_origin[1]}i -K -P ${from_open} $psfile
if [[ ${run_command} == 0 ]]; then
echo "gmt grdimage ${data} -R${range} -C${cptfile} -I${gradfile} \
-Bx${axistick[0]}+l\"${labels[0]}\" -By${axistick[1]}+l\"${labels[1]}\" \
-JX${layouts[0]}/${pic_height}i -X${image_origin[0]}i -Y${image_origin[1]}i -K -P ${from_open} $psfile"
else
gmt grdimage ${data} -R${range} -C${cptfile} -I${gradfile} \
-Bx${axistick[0]}+l"${labels[0]}" -By${axistick[1]}+l"${labels[1]}" \
-JX${layouts[0]}/${pic_height}i -X${image_origin[0]}i -Y${image_origin[1]}i -K -P ${from_open} $psfile
fi
else
gmt grdimage ${data} -R${range} -C${cptfile} -Bx${axistick[0]}g${axistick[0]}+l"${labels[0]}" \
-By${axistick[1]}g${axistick[1]}+l"${labels[1]}" -JX1.5i/${pic_height}i \
if [[ ${run_command} == 0 ]]; then
echo "gmt grdimage ${data} -R${range} -C${cptfile} -Bx${axistick[0]}+l\"${labels[0]}\" \
-By${axistick[1]}+l\"${labels[1]}\" -JX${layouts[0]}/${pic_height}i \
-X${image_origin[0]}i -Y${image_origin[1]}i -K -P ${from_open} $psfile"
else
gmt grdimage ${data} -R${range} -C${cptfile} -Bx${axistick[0]}+l"${labels[0]}" \
-By${axistick[1]}+l"${labels[1]}" -JX${layouts[0]}/${pic_height}i \
-X${image_origin[0]}i -Y${image_origin[1]}i -K -P ${from_open} $psfile
fi
fi
# 画多边形
if [[ $polyfile != 'null' ]]; then
gmt psxy ${polyfile} -JX1.5i/${pic_height}i -W0.25p,black,- -R${range} -L -K -O >> $psfile
if [[ ${run_command} == 0 ]]; then
echo "gmt psxy ${polyfile} -JX${layouts[0]}/${pic_height}i -W0.25p,black,- -R${range} -L -K -O >> $psfile"
else
gmt psxy ${polyfile} -JX${layouts[0]}/${pic_height}i -W0.25p,black,- -R${range} -L -K -O >> $psfile
fi
fi
#添加号码
if [[ ${annotation} != 'null' ]]; then
gmt pstext -R${range} -JX1.5i/${pic_height}i -K -O <<- EOF >> $psfile
if [[ ${run_command} == 0 ]]; then
echo "gmt pstext -R${range} -JX${layouts[0]}/${pic_height}i -K -O <<- EOF >> $psfile"
echo ${annotation}
echo "EOF"
else
gmt pstext -R${range} -JX${layouts[0]}/${pic_height}i -K -O <<- EOF >> $psfile
${annotation}
EOF
EOF
fi
fi
#-C${cptfile}+Uk 使用km色标单位除1000
#如果unit等于km则在cptfile后面添加+Uk
if [[ ${plot_colorbar} == 1 ]]; then
if [[ ${unit} == 'km+Uk' ]]; then
gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -C${cptfile}+Uk -Bx${bartick} -By+l${unit} \
-X${bar_origin[0]}i -Y${bar_origin[1]}i -O -K >> $psfile
if [[ ${run_command} == 0 ]]; then
echo "gmt psscale -Dx${layouts[2]}+w${layouts[3]} -C${cptfile}+Uk -Bx${bartick} -By+l${unit} \
-X${bar_origin[0]}i -Y${bar_origin[1]}i -O -K >> $psfile"
else
gmt psscale -Dx${layouts[2]}+w${layouts[3]} -C${cptfile}+Uk -Bx${bartick} -By+l${unit} \
-X${bar_origin[0]}i -Y${bar_origin[1]}i -O -K >> $psfile
fi
else
gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -C${cptfile} -Bx${bartick} -By+l${unit} \
-X${bar_origin[0]}i -Y${bar_origin[1]}i -O -K >> $psfile
if [[ ${run_command} == 0 ]]; then
echo "gmt psscale -Dx${layouts[2]}+w${layouts[3]} -C${cptfile} -Bx${bartick} -By+l${unit} \
-X${bar_origin[0]}i -Y${bar_origin[1]}i -O -K >> $psfile"
else
gmt psscale -Dx${layouts[2]}+w${layouts[3]} -C${cptfile} -Bx${bartick} -By+l${unit} \
-X${bar_origin[0]}i -Y${bar_origin[1]}i -O -K >> $psfile
fi
fi
fi
if [[ ${keep_open} == 0 ]]; then
# 这一句什么都不做 唯一的作用就是关闭ps文件
gmt pstext -R${range} -JX -O <<- EOF >> $psfile
EOF
if [[ ${run_command} == 0 ]]; then
echo "gmt pstext -R${range} -JX -O <<- EOF >> $psfile"
echo "EOF"
else
gmt pstext -R${range} -JX -O <<- EOF >> $psfile
EOF
fi
# 输出 eps 和 png 文件
gmt psconvert $psfile -A -TEG -E300
if [[ ${run_command} == 0 ]]; then
echo "gmt psconvert $psfile -A -TEG -E300"
else
gmt psconvert $psfile -A -TEG -E300
fi
# 删除临时文件 使用linux终端rm命令
rm $cptfile $psfile gmt.history gmt.conf
if [[ ${run_command} == 0 ]]; then
echo "rm $cptfile $psfile gmt.history gmt.conf"
else
rm $cptfile $psfile gmt.history gmt.conf
fi
if [[ $plotgrad == 1 ]]; then
rm $gradfile
if [[ ${run_command} == 0 ]]; then
echo "rm $gradfile"
else
rm $gradfile
fi
fi
# 在终端显示图像 此命令需要imgcat.sh脚本和iTerm终端
#imgcat $jpgfile
# 打开图片文件 此命令使用MacOS终端open命令
open $jpgfile
if [[ ${run_command} == 1 ]]; then
open $jpgfile
fi
fi
fi