add gmtxy-image2

This commit is contained in:
张壹 2020-04-24 10:55:53 +08:00
parent bda099fa66
commit 3498dc2f90
2 changed files with 211 additions and 1 deletions

View File

@ -20,7 +20,7 @@ do
case $arg in
h)
printf "simple grid file mapping using GMT script under x-y plane. The template accepts a grid (.nc .grid) file as input and outputs a .png and a .eps file. \
For futher explanations, please look for GMT's manuscripts.\nAuthor: Yi Zhang (zhangyi.cugwuhan@gmail.com)\n"
For further explanations, please look for GMT's manuscripts.\nAuthor: Yi Zhang (zhangyi.cugwuhan@gmail.com)\n"
printf "usage: ${0##*/} -i<grid-data> [-r<xmin>/<xmax>/<ymin>/<ymax>] [-u<unit>] [-c<cpt-file>] [-a<x-label>,<y-label>] [-g]\n"
printf "%s\t%s\n" "-i" "input grid file"
printf "%s\t%s\n" "-r" "data range. The template will ditect the input data range automaticly, the use of this option will overwrite the range."

210
gmtxy-image2.sh Executable file
View File

@ -0,0 +1,210 @@
#!/bin/bash
# 包含GMT自带脚本文件 其中包含了一些有用的功能 比如获取网格文件的范围
. gmt_shell_functions.sh
# 包含dispOption脚本
. dispOptions
#GMT显示平面数据脚本输入文件为网格文件没有包含网格化语句因为网格化过程中的情况多样化建议在其他脚本中个别添加再调用此脚本
# 初始化参数
data='null'
outpsfile='null'
gridData='null'
unit='m'
color='rainbow'
range='null'
overwriteRange='null'
overwrite=0
labels=("x (m)" "y (m)")
plotgrad=0
axistick=("a" "a") #横纵坐标轴间隔 a表示自动
bartick="a" #色标轴标轴间隔 a表示自动
frameset="wEsNZ"
plot_colorbar=1
annotation="null"
keep_open=0
from_open=">"
image_origin=("0.5" "0.5")
bar_origin=("0" "0")
polyfile='null'
# 从命令行获取参数
while getopts "hi:o:r:u:c:a:G:t:v:f:s:p:X:x:gnKO" arg
do
case $arg in
h)
dispTitle "${0##*/}" "simple grid file mapping using GMT script under x-y plane. \
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] \
[-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 \
used if this option is not set."
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 \
if -t option is not set."
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. \
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 \
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."
exit 0;;
i)
data=$OPTARG;;
o)
outpsfile=$OPTARG;;
u)
unit=$OPTARG;;
c)
color=$OPTARG;;
t)
axistick=(${OPTARG//,/ });;
X)
image_origin=(${OPTARG//,/ });;
x)
bar_origin=(${OPTARG//,/ });;
v)
bartick=$OPTARG;;
f)
frameset=$OPTARG;;
r)
overwrite=1
overwriteRange=$OPTARG;;
a)
OLD_IFS="${IFS}"
IFS=";"
labels=(${OPTARG})
IFS="${OLD_IFS}";;
g)
plotgrad=1;;
n)
plot_colorbar=0;;
G)
gridData=$OPTARG;;
s)
annotation=$OPTARG;;
p)
polyfile=$OPTARG;;
K)
keep_open=1;;
O)
from_open="-O >>";;
?)
printf "error: unknow argument\nuse -h option to see help information\n"
exit 1;;
esac
done
# 进行必要的参数检查
if [[ $data == "null" ]]; then
printf "error: no input file name\nuse -h option to see help information\n"
exit 1
else
# 初始化临时文件名
cptfile=user.cpt
if [[ ${outpsfile} == 'null' ]]; then
psfile=${data%.*}.ps
jpgfile=${data%.*}.png
else
psfile=${outpsfile}.ps
jpgfile=${outpsfile}.png
fi
# 获取网格范围
if [[ $overwrite == 1 ]]; then
range=${overwriteRange}
else
range=$(gmt_get_gridregion ${data})
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
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
else
gmt grdgradient ${gridData} -G${gradfile} -Nt -A0/45
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
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 \
-X${image_origin[0]}i -Y${image_origin[1]}i -K -P ${from_open} $psfile
fi
# 画多边形
if [[ $polyfile != 'null' ]]; then
gmt psxy ${polyfile} -JX1.5i/${pic_height}i -W0.25p,black,- -R${range} -L -K -O >> $psfile
fi
#添加号码
if [[ ${annotation} != 'null' ]]; then
gmt pstext -R${range} -JX1.5i/${pic_height}i -K -O <<- EOF >> $psfile
${annotation}
EOF
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
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
fi
fi
if [[ ${keep_open} == 0 ]]; then
# 这一句什么都不做 唯一的作用就是关闭ps文件
gmt pstext -R${range} -JX -O <<- EOF >> $psfile
EOF
# 输出 eps 和 png 文件
gmt psconvert $psfile -A -TEG -E300
# 删除临时文件 使用linux终端rm命令
rm $cptfile $psfile gmt.history gmt.conf
if [[ $plotgrad == 1 ]]; then
rm $gradfile
fi
# 在终端显示图像 此命令需要imgcat.sh脚本和iTerm终端
#imgcat $jpgfile
# 打开图片文件 此命令使用MacOS终端open命令
open $jpgfile
fi
fi