2017-10-23 11:29:46 +08:00
#!/bin/bash
# 包含GMT自带脚本文件 其中包含了一些有用的功能 比如获取网格文件的范围
. gmt_shell_functions.sh
2018-09-16 06:51:19 +08:00
# 包含dispOption脚本
2023-03-19 19:41:55 +08:00
. dispOptions.sh
2017-10-23 11:29:46 +08:00
#GMT显示平面数据脚本, 输入文件为网格文件, 没有包含网格化语句因为网格化过程中的情况多样化, 建议在其他脚本中个别添加再调用此脚本
# 初始化参数
data = 'null'
2018-06-13 02:19:16 +08:00
gridData = 'null'
2017-10-23 11:29:46 +08:00
unit = 'm'
color = 'rainbow'
2023-03-20 21:19:15 +08:00
direct_cpt = 'user.cpt'
2017-10-23 11:29:46 +08:00
range = 'null'
overwriteRange = 'null'
overwrite = 0
labels = ( "longitude (degree)" "latitude (degree)" )
plotgrad = 0
2018-05-04 02:47:17 +08:00
clon = 0
2018-08-30 05:56:20 +08:00
polyfile = 'null'
2023-03-19 19:41:55 +08:00
bartick = "a" #色标轴标轴间隔 a表示自动
2017-10-23 11:29:46 +08:00
# 从命令行获取参数
2023-03-20 21:19:15 +08:00
while getopts "hi:r:u:c:C:a:G:l:p:v:g" arg
2017-10-23 11:29:46 +08:00
do
case $arg in
h)
2018-09-16 06:51:19 +08:00
dispTitle " ${ 0 ##*/ } " " simple global wide data mapping using GMT script. 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."
2018-09-16 06:59:47 +08:00
dispAuthorInfo "Yi Zhang (zhangyi.cugwuhan@gmail.com)"
2018-09-16 06:51:19 +08:00
dispUsage " ${ 0 ##*/ } -i<grid-data> [-r<xmin>/<xmax>/<ymin>/<ymax>] [-l<lon>] [-u<unit>] [-c<cpt-file>] [-a<x-label>,<y-label>] [-g] [-G<grad-data>] [-p<polygon-file>] "
dispOptionShort "-i" "input grid file"
dispOptionShort "-r" "data range. The template will ditect the input data range automaticly, the use of this option will overwrite the range."
dispOptionShort "-l" "central longitude value of the plot, the default value is 0"
dispOptionShort "-u" "data unit. The default is meter. use 'km+Uk' to append '+Uk' option to the psscale command"
dispOptionShort "-c" "color cpt. The default is rainbow."
2023-03-20 21:19:15 +08:00
dispOptionShort "-C" "color cpt. Use the input cpt file directly."
2023-03-19 19:41:55 +08:00
dispOptionShort "-v" "intervals of color bar's labels. the script will set the intervals automatically if -v option is not set."
2018-09-16 06:51:19 +08:00
dispOptionShort "-a" "axis labels. 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 "-p" "plot polygons via a file."
2017-10-23 11:29:46 +08:00
exit 0; ;
i)
data = $OPTARG ; ;
u)
unit = $OPTARG ; ;
c)
color = $OPTARG ; ;
2023-03-20 21:19:15 +08:00
C)
direct_cpt = $OPTARG ; ;
2017-10-23 11:29:46 +08:00
r)
overwrite = 1
overwriteRange = $OPTARG ; ;
2018-05-04 02:47:17 +08:00
l)
clon = $OPTARG ; ;
2023-03-19 19:41:55 +08:00
v)
bartick = $OPTARG ; ;
2017-10-23 11:29:46 +08:00
a)
labels = ( ${ OPTARG //,/ } ) ; ;
g)
plotgrad = 1; ;
2018-06-13 02:19:16 +08:00
G)
gridData = $OPTARG ; ;
2018-08-30 05:56:20 +08:00
p)
polyfile = $OPTARG ; ;
2017-10-23 11:29:46 +08:00
?)
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
# 初始化临时文件名
2023-03-20 21:19:15 +08:00
cptfile = ${ direct_cpt }
2017-10-23 11:29:46 +08:00
psfile = ${ data %.* } .ps
jpgfile = ${ data %.* } .png
# 获取网格范围
if [ [ $overwrite = = 1 ] ] ; then
range = ${ overwriteRange }
else
range = $( gmt_get_gridregion ${ data } )
fi
# 设置绘图参数
gmt gmtset \
2018-06-14 08:34:10 +08:00
FONT_ANNOT_PRIMARY = 7p,Times-Roman,black \
2017-10-23 11:29:46 +08:00
MAP_FRAME_PEN = thinnest,black \
MAP_TICK_LENGTH_PRIMARY = 1.5p/1p \
MAP_TICK_PEN_PRIMARY = thinnest,black \
MAP_TITLE_OFFSET = 6.5p \
MAP_GRID_CROSS_SIZE_PRIMARY = 2p \
2018-06-14 08:34:10 +08:00
FONT_LABEL = 7p,Times-Roman,black \
2017-10-23 11:29:46 +08:00
MAP_FRAME_AXES = WESnZ \
MAP_LABEL_OFFSET = 2.5p
2023-03-20 21:19:15 +08:00
if [ [ $cptfile = = 'user.cpt' ] ] ; then
gmt grd2cpt ${ data } -C${ color } -Z -D > $cptfile
fi
2017-10-23 11:29:46 +08:00
if [ [ $plotgrad = = 1 ] ] ; then
2018-06-13 02:19:16 +08:00
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
2018-06-14 08:34:10 +08:00
gmt grdimage ${ data } -R${ range } -C${ cptfile } -I${ gradfile } -Bxag+l" ${ labels [0] } " -Bpya40g40+l" ${ labels [1] } " -JN${ clon } /2i -K -P > $psfile
2017-10-23 11:29:46 +08:00
else
2018-06-14 08:34:10 +08:00
gmt grdimage ${ data } -R${ range } -C${ cptfile } -Bxag+l" ${ labels [0] } " -Bpya40g40+l" ${ labels [1] } " -JN${ clon } /2i -K -P > $psfile
2017-10-23 11:29:46 +08:00
fi
2018-08-30 05:56:20 +08:00
if [ [ $polyfile != 'null' ] ] ; then
2023-03-20 21:19:15 +08:00
#-L force closed polygon
#gmt psxy $polyfile -JN${clon}/2i -R${range} -L -K -O >> $psfile
gmt psxy $polyfile -JN${ clon } /2i -R${ range } -W0.1p -K -O >> $psfile
2018-08-30 05:56:20 +08:00
fi
2018-05-04 02:55:09 +08:00
#-C${cptfile}+Uk 使用km( 色标单位除1000
#如果unit等于km则在cptfile后面添加+Uk
2018-06-12 04:34:39 +08:00
if [ [ ${ unit } = = 'km+Uk' ] ] ; then
2023-03-19 19:41:55 +08:00
gmt psscale -Dx0.4i/-0.3i+w1.2i/0.05i+h -C${ cptfile } +Uk -Bx${ bartick } -By+l"km" -O >> $psfile
2018-05-04 02:55:09 +08:00
else
2023-03-19 19:41:55 +08:00
gmt psscale -Dx0.4i/-0.3i+w1.2i/0.05i+h -C${ cptfile } -Bx${ bartick } -By+l${ unit } -O >> $psfile
2018-05-04 02:55:09 +08:00
fi
2018-03-19 14:30:24 +08:00
gmt psconvert $psfile -A -TEG -E300
2017-10-23 11:29:46 +08:00
# 删除临时文件 使用linux终端rm命令
2023-03-20 21:19:15 +08:00
if [ [ $cptfile = = 'user.cpt' ] ] ; then
rm $cptfile
fi
rm $psfile gmt.history gmt.conf
2017-10-23 11:29:46 +08:00
if [ [ $plotgrad = = 1 ] ] ; then
rm $gradfile
fi
# 在终端显示图像 此命令需要imgcat.sh脚本和iTerm终端
#imgcat $jpgfile
# 打开图片文件 此命令使用MacOS终端open命令
open $jpgfile
fi