gmt-template/gmtsph-regional.sh

189 lines
7.3 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# 包含GMT自带脚本文件 其中包含了一些有用的功能 比如获取网格文件的范围
. gmt_shell_functions.sh
#GMT显示平面数据脚本输入文件为网格文件没有包含网格化语句因为网格化过程中的情况多样化建议在其他脚本中个别添加再调用此脚本
# 初始化参数
data='null'
2018-06-13 02:19:16 +08:00
gridData='null'
unit='m'
color='rainbow'
range='null'
overwriteRange='null'
overwrite=0
labels=("longitude (degree)" "latitude (degree)")
plot_para=(0 0 0 0)
plotgrad=0
2018-03-23 05:15:04 +08:00
nocpt=0
2018-03-26 04:21:55 +08:00
inverseCPT=0
2018-04-03 01:50:22 +08:00
coastline=0
2018-04-04 02:05:44 +08:00
axistick=("a" "a") #横纵坐标轴间隔 a表示自动
2018-04-27 02:46:24 +08:00
bartick="a" #色标轴标轴间隔 a表示自动
2019-11-23 14:17:03 +08:00
polyfile='null'
2018-04-27 02:46:24 +08:00
# 不同图幅的绘图参数
pageChoice='small' # 默认尺寸为small
exSmallpage="1i,0.08i/-0.25i,0.85i/0.03i"
2019-03-20 10:41:20 +08:00
smallPage="1.5i,0.125i/-0.3i,1.2i/0.05i" # 图大小,色标位置,色标尺寸
2019-03-18 07:33:37 +08:00
middlePage="2i,0.2i/-0.35i,1.5i/0.05i"
2018-04-27 02:59:00 +08:00
largePage="2.5i,0.35i/-0.4i,1.75i/0.08i"
# 从命令行获取参数
2019-11-23 14:17:03 +08:00
while getopts "hi:r:u:c:a:t:v:l:G:s:gnpb" arg
do
case $arg in
h)
printf "simple regional data mapping using GMT script. The template accepts a grid (.nc .grid) file as input and outputs a .png and a .eps file. \
2018-04-27 03:17:44 +08:00
For further explanations, please look for GMT's manuscripts.\nAuthor: Yi Zhang (zhangyi.cugwuhan@gmail.com)\n"
2018-06-13 02:19:16 +08:00
printf "usage: ${0##*/} -i<grid-data> [-r<xmin>/<xmax>/<ymin>/<ymax>] [-u<unit>] [-c<cpt-file>] [-a<x-label>,<y-label>] [-t<x-tick>,<y-tick>] [-v<c-tick>] [-l<size>] [-g] [-G<grad-data>] [-b] [-n] [-p]\n"
printf "%s\t%s\n" "-i" "input grid file"
2018-04-04 02:05:44 +08:00
printf "%s\t%s\n" "-r" "data range. The template will detect the input data range automatically, the use of this option will overwrite the range."
2018-06-06 06:20:35 +08:00
printf "%s\t%s\n" "-u" "data unit. The default is meter. use 'km+Uk' to append '+Uk' option to the psscale command."
2018-03-23 05:15:04 +08:00
printf "%s\t%s\n" "-c" "color cpt. The default is rainbow. The script will use grd2cpt to recalculate the color scale file, use -n option to disable the use of grd2cpt."
printf "%s\t%s\n" "-a" "axis labels. The deafults are x (m) and y (m)."
2018-04-04 02:05:44 +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-07-26 02:44:08 +08:00
printf "%s\t%s\n" "-l" "image layout and size. three available options are exsmall, small(1.5 inch wide), middle and large."
2018-04-03 01:50:22 +08:00
printf "%s\t%s\n" "-b" "draw coast line."
printf "%s\t%s\n" "-g" "use grdgradient. The default is false."
2018-06-13 02:19:16 +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"
2018-04-04 02:05:44 +08:00
printf "%s\t%s\n" "-n" "disable the use of grd2cpt."
2018-03-26 04:21:55 +08:00
printf "%s\t%s\n" "-p" "inverse the color pattern specified by the -c option, no use if -n option is implemented."
2019-11-23 14:17:03 +08:00
printf "%s\t%s\n" "-s" "plot polygons via a file."
exit 0;;
i)
data=$OPTARG;;
u)
unit=$OPTARG;;
c)
color=$OPTARG;;
r)
overwrite=1
overwriteRange=$OPTARG;;
a)
labels=(${OPTARG//,/ });;
2018-04-04 02:05:44 +08:00
t)
axistick=(${OPTARG//,/ });;
v)
bartick=$OPTARG;;
2018-04-27 02:46:24 +08:00
l)
pageChoice=$OPTARG;;
2018-04-03 01:50:22 +08:00
b)
coastline=1;;
g)
plotgrad=1;;
2018-06-13 02:19:16 +08:00
G)
gridData=$OPTARG;;
2018-03-23 05:15:04 +08:00
n)
nocpt=1;;
2018-03-26 04:21:55 +08:00
p)
inverseCPT=1;;
2019-11-23 14:17:03 +08:00
s)
polyfile=$OPTARG;;
?)
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
# 初始化临时文件名
2018-06-09 02:24:40 +08:00
cptfile=${data%.*}.cpt
psfile=${data%.*}.ps
jpgfile=${data%.*}.png
# 获取网格范围
if [[ $overwrite == 1 ]]; then
range=${overwriteRange}
# 别忘了这里要加上括号 才能初始化数组
range_sp=(${range//// })
# 注意bash并不支持浮点运算 所以需要使用bc命令 bc命令是一个计算数学表达式工具 参数是一个数学表达式字符串
plot_para[0]=$(echo "${range_sp[0]} * 0.5 + ${range_sp[1]} * 0.5"|bc)
plot_para[1]=$(echo "${range_sp[2]} * 0.5 + ${range_sp[3]} * 0.5"|bc)
plot_para[2]=$(echo "${plot_para[1]} * 0.5 + ${range_sp[2]} * 0.5"|bc)
plot_para[3]=$(echo "${plot_para[1]} * 0.5 + ${range_sp[3]} * 0.5"|bc)
else
range=$(gmt_get_gridregion ${data})
range_sp=(${range//// })
plot_para[0]=$(echo "${range_sp[0]} * 0.5 + ${range_sp[1]} * 0.5"|bc)
plot_para[1]=$(echo "${range_sp[2]} * 0.5 + ${range_sp[3]} * 0.5"|bc)
plot_para[2]=$(echo "${plot_para[1]} * 0.5 + ${range_sp[2]} * 0.5"|bc)
plot_para[3]=$(echo "${plot_para[1]} * 0.5 + ${range_sp[3]} * 0.5"|bc)
fi
# 设置绘图参数
gmt gmtset \
2019-03-18 07:33:37 +08:00
FONT_ANNOT_PRIMARY=7.5p,Times-Roman,black \
MAP_FRAME_PEN=thinnest,black \
2018-04-02 09:45:40 +08:00
MAP_FRAME_WIDTH=1p \
MAP_TICK_LENGTH_PRIMARY=1.5p/1p \
MAP_TICK_PEN_PRIMARY=thinnest,black \
MAP_TITLE_OFFSET=6.5p \
MAP_GRID_PEN_PRIMARY=thinnest,grey,-- \
2018-03-19 14:30:24 +08:00
FONT_LABEL=8p,Times-Roman,black \
MAP_FRAME_AXES=WESnZ \
2018-03-31 02:55:11 +08:00
COLOR_NAN=white \
MAP_LABEL_OFFSET=1p\
2018-07-26 02:44:08 +08:00
MAP_ANNOT_OFFSET_PRIMARY=2.5p
2018-04-27 02:46:24 +08:00
# 设置色标文件
2018-03-23 05:15:04 +08:00
if [[ $nocpt == 1 ]]; then
2018-03-23 05:25:03 +08:00
cptfile=${color}
2018-03-23 05:21:10 +08:00
else
2018-03-26 04:21:55 +08:00
if [[ $inverseCPT == 1 ]]; then
2019-03-07 14:43:41 +08:00
gmt grd2cpt ${data} -R${range} -C${color} -D -M -I > $cptfile
2018-03-26 04:21:55 +08:00
else
2019-03-07 14:43:41 +08:00
gmt grd2cpt ${data} -R${range} -C${color} -D -M > $cptfile
2018-03-26 04:21:55 +08:00
fi
2018-03-23 05:15:04 +08:00
fi
2018-04-27 02:46:24 +08:00
# 设置绘图尺寸 如果识别参数失败 则还是按small处理
if [[ $pageChoice == 'small' ]]; then
pagePara=(${smallPage//,/ })
2018-07-26 02:44:08 +08:00
elif [[ $pageChoice == 'exsmall' ]]; then
pagePara=(${exSmallpage//,/ })
2018-04-27 02:59:00 +08:00
elif [[ $pageChoice == 'middle' ]]; then
pagePara=(${middlePage//,/ })
elif [[ $pageChoice == 'large' ]]; then
pagePara=(${largePage//,/ })
2018-04-27 02:46:24 +08:00
else
pagePara=(${smallPage//,/ })
fi
2018-03-23 05:15:04 +08:00
if [[ $plotgrad == 1 ]]; then
2018-06-10 09:00:35 +08:00
gradfile=${data%.*}Grad.nc
2018-06-13 02:19:16 +08:00
if [[ $gridData == 'null' ]]; then
gmt grdgradient ${data} -G${gradfile} -Nt -A0/45
else
gmt grdgradient ${gridData} -G${gradfile} -Nt -A0/45
fi
2018-04-27 02:46:24 +08:00
gmt grdimage ${data} -R${range} -C${cptfile} -I${gradfile} -Bx${axistick[0]}g+l"${labels[0]}" -By${axistick[1]}g+l"${labels[1]}" -JL${plot_para[0]}/${plot_para[1]}/${plot_para[2]}/${plot_para[3]}/${pagePara[0]} -K -P > $psfile
else
2018-04-27 02:46:24 +08:00
gmt grdimage ${data} -R${range} -C${cptfile} -Bx${axistick[0]}g+l"${labels[0]}" -By${axistick[1]}g+l"${labels[1]}" -JL${plot_para[0]}/${plot_para[1]}/${plot_para[2]}/${plot_para[3]}/${pagePara[0]} -K -P > $psfile
fi
2018-04-03 01:50:22 +08:00
if [[ $coastline == 1 ]]; then
2019-11-23 14:17:03 +08:00
gmt pscoast -R${range} -JL${plot_para[0]}/${plot_para[1]}/${plot_para[2]}/${plot_para[3]}/${pagePara[0]} -W0.25p -Dc -A5000 -K -O -P >> $psfile
fi
if [[ $polyfile != 'null' ]]; then
gmt psxy ${polyfile} -JL${plot_para[0]}/${plot_para[1]}/${plot_para[2]}/${plot_para[3]}/${pagePara[0]} -W0.25p -R${range} -L -K -O >> $psfile
2018-04-03 01:50:22 +08:00
fi
2018-05-04 02:55:09 +08:00
#-C${cptfile}+Uk 使用km色标单位除1000
#如果unit等于km则在cptfile后面添加+Uk
2018-06-06 06:20:35 +08:00
if [[ ${unit} == 'km+Uk' ]]; then
2019-03-07 11:52:21 +08:00
gmt psscale -Dx${pagePara[1]}+w${pagePara[2]}+h -C${cptfile}+Uk -Bx${bartick} -By+l"km" -O >> $psfile
2018-05-04 02:55:09 +08:00
else
gmt psscale -Dx${pagePara[1]}+w${pagePara[2]}+h -C${cptfile} -Bx${bartick} -By+l${unit} -O >> $psfile
fi
2018-03-19 14:30:24 +08:00
gmt psconvert $psfile -A -TEG -E300
# 删除临时文件 使用linux终端rm命令
2018-03-23 05:27:38 +08:00
rm $psfile gmt.history gmt.conf
if [[ $plotgrad == 1 ]]; then
rm $gradfile
fi
2018-03-23 05:27:38 +08:00
if [[ $nocpt == 0 ]]; then
rm $cptfile
fi
# 在终端显示图像 此命令需要imgcat.sh脚本和iTerm终端
#imgcat $jpgfile
# 打开图片文件 此命令使用MacOS终端open命令
open $jpgfile
fi