From fcfd49e1b51d04cca57c907cba27fb5e85008725 Mon Sep 17 00:00:00 2001 From: pi Date: Mon, 19 Mar 2018 11:34:26 -0700 Subject: [PATCH] add three scripts add imgcat imgchange and showxyz --- imgcat.sh | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ imgchange.sh | 28 +++++++++++++++ showxyz.sh | 89 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 216 insertions(+) create mode 100755 imgcat.sh create mode 100755 imgchange.sh create mode 100755 showxyz.sh diff --git a/imgcat.sh b/imgcat.sh new file mode 100755 index 0000000..0adb03c --- /dev/null +++ b/imgcat.sh @@ -0,0 +1,99 @@ +#!/bin/bash + +# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux; +# ST, and for all ESCs in to be replaced with ESC ESC. It +# only accepts ESC backslash for ST. +function print_osc() { + if [[ $TERM == screen* ]] ; then + printf "\033Ptmux;\033\033]" + else + printf "\033]" + fi +} + +# More of the tmux workaround described above. +function print_st() { + if [[ $TERM == screen* ]] ; then + printf "\a\033\\" + else + printf "\a" + fi +} + +# print_image filename inline base64contents +# filename: Filename to convey to client +# inline: 0 or 1 +# base64contents: Base64-encoded contents +function print_image() { + print_osc + printf '1337;File=' + if [[ -n "$1" ]]; then + printf 'name='`echo -n "$1" | base64`";" + fi + if $(base64 --version 2>&1 | grep GNU > /dev/null) + then + BASE64ARG=-d + else + BASE64ARG=-D + fi + echo -n "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}' + printf ";inline=$2" + printf ":" + echo -n "$3" + print_st + printf '\n' +} + +function error() { + echo "ERROR: $*" 1>&2 +} + +function show_help() { + echo "Usage: imgcat filename ..." 1>& 2 + echo " or: cat filename | imgcat" 1>& 2 +} + +## Main + +if [ -t 0 ]; then + has_stdin=f +else + has_stdin=t +fi + +# Show help if no arguments and no stdin. +if [ $has_stdin = f -a $# -eq 0 ]; then + show_help + exit +fi + +# Look for command line flags. +while [ $# -gt 0 ]; do + case "$1" in + -h|--h|--help) + show_help + exit + ;; + -*) + error "Unknown option flag: $1" + show_help + exit 1 + ;; + *) + if [ -r "$1" ] ; then + print_image "$1" 1 "$(base64 < "$1")" + else + error "imgcat: $1: No such file or directory" + exit 2 + fi + ;; + esac + shift +done + +# Read and print stdin +if [ $has_stdin = t ]; then + print_image "" 1 "$(cat | base64)" +fi + +exit 0 \ No newline at end of file diff --git a/imgchange.sh b/imgchange.sh new file mode 100755 index 0000000..64199c8 --- /dev/null +++ b/imgchange.sh @@ -0,0 +1,28 @@ +#!/bin/bash +data='null' +outdata='null' +while getopts "hi:r" arg +do + case $arg in + h) + printf "some commonly used tricks of imagemagick \n" + printf "usage: ${0##*/} -i [-r] \n" + printf "%s\t%s\n" "-i" "input image file" + printf "%s\t%s\n" "-r" "remove white background of the input image and apply a transparent background." + exit 0;; + i) + data=$OPTARG;; + + r) + outdata=${data%.*}-nobg.png + convert ${data} -bordercolor white -border 1x1 -matte -fill none -fuzz 20% -draw 'matte 0,0 floodfill' -shave 1x1 ${outdata};; + ?) + 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 +fi \ No newline at end of file diff --git a/showxyz.sh b/showxyz.sh new file mode 100755 index 0000000..3836a26 --- /dev/null +++ b/showxyz.sh @@ -0,0 +1,89 @@ +#!/bin/bash +# 包含GMT自带脚本文件 其中包含了一些有用的功能 比如获取网格文件的范围 +. gmt_shell_functions.sh +# 本脚本的功能为显示一个规则网的三列数据 +# 我们会用到之前编写的另几个脚本来生成结果图片 这里专门提供了一个参数来传递参数到调用的相应脚本中 + +data='null' +range='null' +overwriteRange='null' +overwrite=0 +interval='null' +cols='0,1,2' +script='null' +outname='null' +geoframe=0 +nogridfile=0 +while getopts "ht:o:r:i:c:s:gn" arg +do + case $arg in + h) + printf "plot xyz like data \nAuthor: Yi Zhang (zhangyi.cugwuhan@gmail.com)\n" + printf "usage: ${0##*/} -t -i/ [-o] [-r///] [-c,,...] [-s] [-g] [-n] \n" + printf "%s\t%s\n" "-t" "input table file." + printf "%s\t%s\n" "-o" "output file name, the input table name will be used if this option is absent." + printf "%s\t%s\n" "-i" "data intervals in x and y directions." + 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" "-c" "select data columns will be used for plotting, the default is 0,1,2." + printf "%s\t%s\n" "-s" "additional commands that will be pasted down to plotting scripts." + printf "%s\t%s\n" "-g" "the default poltting script used here is \"gmtxy-image\", set -g option to use \"gmtsph-regional\"." + printf "%s\t%s\n" "-n" "do not save .nc file, the default will save a output .nc grid file." + exit 0;; + t) + data=$OPTARG;; + o) + outname=$OPTARG;; + i) + interval=$OPTARG;; + r) + overwrite=1 + overwriteRange=$OPTARG;; + c) + cols=$OPTARG;; + s) + script=$OPTARG;; + g) + geoframe=1;; + n) + nogridfile=1;; + ?) + printf "error: unknow argument\nuse -h option to see help information\n" + exit 1;; + esac +done + +if [[ $data == 'null' || $interval == 'null' ]]; then + printf "error: no input file name or no -i option setted\nuse -h option to see help information\n" + exit 1 +else + if [[ $outname == 'null' ]]; then + ncfile=${data%.*}.nc + else + ncfile=${outname} + fi + # 获取网格范围 + if [[ $overwrite == 1 ]]; then + range=${overwriteRange} + else + range=$(gmt_get_region ${data}) + fi + + xyz2grd ${data} -G${ncfile} -R${range} -I${interval} -i${cols} + if [[ $geoframe == 1 ]]; then + if [[ $script == 'null' ]]; then + gmtsph-regional -i ${ncfile} + else + gmtsph-regional -i ${ncfile} ${script} + fi + else + if [[ $script == 'null' ]]; then + gmtxy-image -i ${ncfile} + else + gmtxy-image -i ${ncfile} ${script} + fi + fi + + if [[ $nogridfile == 1 ]]; then + rm ${ncfile} + fi +fi \ No newline at end of file