2017-10-23 11:29:46 +08:00
#!/bin/bash
# 包含GMT自带脚本文件 其中包含了一些有用的功能 比如获取网格文件的范围
. gmt_shell_functions.sh
#GMT显示平面数据脚本, 输入文件为网格文件, 没有包含网格化语句因为网格化过程中的情况多样化, 建议在其他脚本中个别添加再调用此脚本
# 初始化参数
data = 'null'
2018-07-09 04:36:39 +08:00
gridData = 'null'
2017-10-23 11:29:46 +08:00
unit = 'm'
color = 'rainbow'
range = 'null'
overwriteRange = 'null'
overwrite = 0
labels = ( "x (m)" "y (m)" )
plotgrad = 0
2018-07-09 05:03:28 +08:00
axistick = ( "a" "a" ) #横纵坐标轴间隔 a表示自动
bartick = "a" #色标轴标轴间隔 a表示自动
2017-10-23 11:29:46 +08:00
# 从命令行获取参数
2018-07-09 05:03:28 +08:00
while getopts "hi:r:u:c:a:G:t:v:g" arg
2017-10-23 11:29:46 +08:00
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. \
2020-04-24 10:55:53 +08:00
For further explanations, please look for GMT' s manuscripts.\n Author: Yi Zhang ( zhangyi.cugwuhan@gmail.com) \n "
2017-10-23 11:29:46 +08:00
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."
printf "%s\t%s\n" "-u" "data unit. The default is meter."
printf "%s\t%s\n" "-c" "color cpt. The default is rainbow."
2018-07-09 05:03:28 +08:00
printf "%s\t%s\n" "-t" "intervals of axis's labels. the script will set the intervals automatically if -t option is not set."
printf "%s\t%s\n" "-v" "intervals of color bar's labels. the script will set the intervals automatically if -v option is not set."
2018-11-16 06:51:51 +08:00
printf "%s\t%s\n" "-a" "axis labels separated by semicolons. The deafults are x (m) and y (m)."
2017-10-23 11:29:46 +08:00
printf "%s\t%s\n" "-g" "use grdgradient. The default is false."
2018-07-09 04:36:39 +08:00
printf "%s\t%s\n" "-G" "provide a different input grid data for the use of grdgradient. This should be used with the '-g' option at the same time"
2017-10-23 11:29:46 +08:00
exit 0; ;
i)
data = $OPTARG ; ;
u)
unit = $OPTARG ; ;
c)
color = $OPTARG ; ;
2018-07-09 05:03:28 +08:00
t)
axistick = ( ${ OPTARG //,/ } ) ; ;
v)
bartick = $OPTARG ; ;
2017-10-23 11:29:46 +08:00
r)
overwrite = 1
overwriteRange = $OPTARG ; ;
a)
2018-11-16 06:51:51 +08:00
OLD_IFS = " ${ IFS } "
IFS = ";"
labels = ( ${ OPTARG } )
IFS = " ${ OLD_IFS } " ; ;
2017-10-23 11:29:46 +08:00
g)
plotgrad = 1; ;
2018-07-09 04:36:39 +08:00
G)
gridData = $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
# 初始化临时文件名
cptfile = user.cpt
psfile = ${ data %.* } .ps
jpgfile = ${ data %.* } .png
# 获取网格范围
if [ [ $overwrite = = 1 ] ] ; then
range = ${ overwriteRange }
else
range = $( gmt_get_gridregion ${ data } )
fi
2018-11-16 06:51:51 +08:00
#根据横纵坐标的范围计算图片的长和高 我们默认图片宽度为1.5i
range_coor = ( ${ range //// } )
2020-06-08 22:28:15 +08:00
pic_height = ` echo " scale=4; 1.5 * ( ${ range_coor [3] } - ${ range_coor [2] } )/( ${ range_coor [1] } - ${ range_coor [0] } ) " | bc`
2017-10-23 11:29:46 +08:00
# 设置绘图参数
gmt gmtset \
2019-02-21 03:44:07 +08:00
FONT_ANNOT_PRIMARY = 10.5p,Times-Roman,black \
2018-11-16 06:51:51 +08:00
MAP_FRAME_PEN = thinnest,black \
MAP_GRID_PEN_PRIMARY = thinnest,black \
MAP_TICK_PEN_PRIMARY = thinnest,black \
2018-07-09 05:03:28 +08:00
MAP_TICK_LENGTH_PRIMARY = 1p/0.5p \
2018-11-16 06:51:51 +08:00
MAP_TITLE_OFFSET = 7.5p \
2018-07-09 05:03:28 +08:00
MAP_GRID_CROSS_SIZE_PRIMARY = 2p \
2019-02-21 03:44:07 +08:00
FONT_LABEL = 10.5p,Times-Roman,black \
MAP_FRAME_AXES = wEsNZ \
2018-11-16 06:51:51 +08:00
MAP_LABEL_OFFSET = 2.5p \
2018-07-09 05:03:28 +08:00
MAP_ANNOT_OFFSET_PRIMARY = 2.5p
2017-10-23 11:29:46 +08:00
2018-07-09 04:36:39 +08:00
gmt grd2cpt ${ data } -C${ color } -R${ range } -Z -D > $cptfile
2017-10-23 11:29:46 +08:00
if [ [ $plotgrad = = 1 ] ] ; then
2018-07-09 04:36:39 +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
2020-04-16 10:35:02 +08:00
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 -K -P > $psfile
2017-10-23 11:29:46 +08:00
else
2020-04-16 10:35:02 +08:00
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 -K -P > $psfile
2018-07-09 05:03:28 +08:00
fi
#-C${cptfile}+Uk 使用km( 色标单位除1000
#如果unit等于km则在cptfile后面添加+Uk
if [ [ ${ unit } = = 'km+Uk' ] ] ; then
2019-02-21 03:44:07 +08:00
gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -C${ cptfile } +Uk -Bx${ bartick } -By+l${ unit } -O >> $psfile
2018-07-09 05:03:28 +08:00
else
2019-02-21 03:44:07 +08:00
gmt psscale -Dx0.1i/-0.2i+w1.3i/0.05i+h -C${ cptfile } -Bx${ bartick } -By+l${ unit } -O >> $psfile
2017-10-23 11:29:46 +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命令
rm $cptfile $psfile gmt.history gmt.conf
if [ [ $plotgrad = = 1 ] ] ; then
rm $gradfile
fi
# 在终端显示图像 此命令需要imgcat.sh脚本和iTerm终端
#imgcat $jpgfile
# 打开图片文件 此命令使用MacOS终端open命令
open $jpgfile
fi