Debian Codegraph @

gbplot

raw
1 #!/bin/sh
2 # gbplot ver. 1.1 Copyright (C) 2009-2010 Giulio Bottazzi
3
4 #read command line options; the position of the last option is saved
5 #in OPTIND
6 while getopts "T:o:t:p:ihl:C:v-:" opt
7 do
8 case $opt in
9 -)
10 case "${OPTARG}" in
11 help) help=yes;;
12 version) version=yes;;
13 esac;;
14 T) terminal=$OPTARG;;
15 o) outfile=$OPTARG;;
16 t) title=$OPTARG;;
17 p) prestring=$OPTARG;;
18 i) interactive=yes;;
19 h) help=yes;;
20 l) setlog=$OPTARG;;
21 C) precommand="${precommand}; $OPTARG";;
22 v) verbose=yes;;
23 \?) help=yes;;
24 esac
25 done
26
27 if [ "$version" = "yes" ]; then
28
29 cat - <<EOF
30 gbplot ver. 5.6
31
32 Copyright (C) 2009-2014 Giulio Bottazzi
33
34 This program is free software; you can redistribute it and/or
35 modify it under the terms of the GNU General Public License
36 (version 2) as published by the Free Software Foundation.
37
38 This program is distributed in the hope that it will be useful,
39 but WITHOUT ANY WARRANTY; without even the implied warranty of
40 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 GNU General Public License for more details.
42
43 Written by Giulio Bottazzi
44 Report bugs to <[email protected]>
45 Package home page <http://cafim.sssup.it/~giulio/software/gbutils/index.html>
46 EOF
47 exit
48
49 fi
50
51
52 if [ "$help" = "yes" ]; then
53
54 cat - <<EOF
55
56 Usage: gbplot [options] [command] [specstring] < datafile
57
58 This command produce simple plots from the command line using the
59 'gnuplot' program. 'comand' is one of the gnuplot commands: 'plot' or
60 'splot' while 'specstring' is a list of gnuplot specifications. In
61 order to escape shell interpretation place 'specstring' between double
62 quotes. It is possible to specify the type of terminal and output file
63 name as command line options. It is also possible to start an
64 interactive session, useful to adjust plot parameters by hand. The
65 program can be used in a shell pipe, as in
66
67 cat datafile | gbplot [options] [command] [specstring]
68
69 Options:
70 -h print this help
71 -i start interactive session after the plot
72 -T set a different terminal
73 -o set an ouptut file
74 -t assign a title to the plot
75 -p prepend expression to plot command
76 -l set log; possible values x,y and xy
77 -C command to be run before the plot
78 -v verbose output: print commands to standard error
79 --help same as '-h'
80 --version print the program version
81
82
83 Examples:
84
85 Two plots: one using line and one using points.
86 echo "1\n2\n3" | gbplot plot "u 0:1 w l title 'line', '' u 0:1 w p title 'point'"
87
88 Save the plot in PDF format in a file named 'test.pdf'
89 echo "1\n2\n3" | gbplot -T pdf -o test.pdf plot "u 0:1 w l title 'line'"
90
91 Set log on the y axis
92 echo "1\n2\n3" | gbplot -l y plot "u 0:1 w l title 'line', '' u 0:1 w p title 'point'"
93
94 Set log on the x axis and a grid
95 echo "1\n2\n3" | gbplot -l x -C "set grid" plot "u 0:1 w l title 'line', '' u 0:1 w p title 'point'"
96
97 Set a grid and the position of the legend
98 echo "1\n2\n3" | gbplot -C "set grid" -C "set key bottom right" plot "u 0:1 w l title 'line', '' u 0:1 w p title 'point'"
99
100 Compute the value of the gamma function on a set of points; gawk is used to filer the relevant numbers
101 echo "1\n2\n3" | gbplot -C "set table" plot 'u (gamma(\$1))' | gawk '/ i/{print \$2}'
102
103 EOF
104
105 exit
106
107 fi
108
109 #store provided data
110 cat - > .gb.data
111
112 #check the syntax and define plotting parameters
113 #plotcmd=${@:$OPTIND:1} <- bashism
114
115 if [ $OPTIND -gt 1 ]; then
116 shift `expr $OPTIND - 1`
117 fi
118 plotcmd=$1
119
120 if [ "$plotcmd" != "plot" ] && [ "$plotcmd" != "splot" ]; then
121 echo "provide a plot or splot command"
122 exit
123 fi
124
125 #firstpar=${@:$(( OPTIND+1 )):1} <- bashism
126 #plotpar=${@:$(( OPTIND+2 ))} <- bashism
127
128 shift
129 firstpar=$1
130
131 if [ "$#" -gt 0 ]; then
132 shift
133 plotpar=$@
134 fi
135
136 #identify range specification
137 if echo "$firstpar" | grep '\[*\]' > /dev/null; then
138 plotcmd="$plotcmd $firstpar"
139 else
140 plotpar="$firstpar $plotpar"
141 fi
142
143 #set the default terminal
144 if echo "set term" | gnuplot 2>&1 | grep wx > /dev/null; then
145 DEFTERM=wxt
146 else
147 DEFTERM=x11
148 fi
149
150 #-----------------------------------------------
151
152
153 if [ "$interactive" = "yes" ]; then #interactive session
154 #create temporary files
155 CMDFILE=`tempfile`
156 ENDFILE=`tempfile`
157
158 # --- initial gnuplot commands
159
160 #set the default terminal
161 echo "set term $DEFTERM noraise" >> $CMDFILE
162
163 #next line useful for screen to start in present directory
164 echo "cd \"$PWD\"" >> $CMDFILE
165 #no need of irrelevant titles
166 echo "set key noautotitle" >> $CMDFILE
167 #prepend expression
168 if [ "$prestring" != "" ]; then
169 echo "$prestring" >> $CMDFILE
170 fi
171 #add title
172 if [ "$title" != "" ]; then
173 echo "set title \"$title\"" >> $CMDFILE
174 fi
175 #set log scale
176 if [ "$setlog" != "" ]; then
177 echo "set log $setlog" >> $CMDFILE
178 fi
179 #add pre-plot command
180 if [ "$precommand" != "" ]; then
181 echo "$precommand" >> $CMDFILE
182 fi
183 echo "$plotcmd \".gb.data\" $plotpar" >> $CMDFILE
184 echo "show plot" >> $CMDFILE
185
186 # ----------------------------
187
188 # --- final gnuplot commands
189
190 #prepare for final output if 'terminal' or 'outfile' is specified
191 if [ "$terminal" != "" ]; then
192 echo "set term $terminal" >> $ENDFILE
193 fi
194
195 if [ "$outfile" != "" ]; then
196 echo "set out \"$outfile\"" >> $ENDFILE
197 echo "replot" >> $ENDFILE
198 echo "set out" >> $ENDFILE
199 fi
200
201 #remove temporary files
202 echo "!rm .gb.data" >> $ENDFILE
203 echo "!rm $CMDFILE $ENDFILE" >> $ENDFILE
204
205 # ----------------------------
206
207 if [ "$verbose" = "yes" ]; then
208 cat $CMDFILE > /dev/stderr
209 echo "-- session -- " > /dev/stderr
210 cat $ENDFILE > /dev/stderr
211 fi
212
213 if [ "$TERM" = "screen" ]; then
214 #start gnuplot in a new screen window
215 screen -X screen gnuplot $CMDFILE - $ENDFILE
216 elif [ "$TERM" = "aterm" ]; then
217 #start gnuplot in a new terminal
218 aterm -e gnuplot $CMDFILE - $ENDFILE
219 else
220 #start gnuplot in a new terminal
221 $TERMCMD -- gnuplot $CMDFILE - $ENDFILE
222 fi
223
224 else #non-interactive session
225 #create temporary files
226 CMDFILE=`tempfile`
227
228 #next line useful for screen to start in present directory
229 echo "cd \"$PWD\"" >> $CMDFILE
230 #no need of irrelevant titles
231 echo "set key noautotitle" >> $CMDFILE
232 if [ "$terminal" != "" ]; then
233 echo "set term $terminal" >> $CMDFILE
234 else
235 echo "set term $DEFTERM noraise" >> $CMDFILE
236 fi
237
238 if [ "$outfile" != "" ]; then
239 echo "set out \"$outfile\"" >> $CMDFILE
240 fi
241 #prepend expression
242 if [ "$prestring" != "" ]; then
243 echo "$prestring" >> $CMDFILE
244 echo "$prestring"
245 fi
246 #add title
247 if [ "$title" != "" ]; then
248 echo "set title \"$title\"" >> $CMDFILE
249 fi
250 #set log scale
251 if [ "$setlog" != "" ]; then
252 echo "set log $setlog" >> $CMDFILE
253 fi
254 #add pre-plot command
255 if [ "$precommand" != "" ]; then
256 echo "$precommand" >> $CMDFILE
257 fi
258
259 echo "$plotcmd \".gb.data\" $plotpar" >> $CMDFILE
260
261 if [ "$verbose" = "yes" ]; then
262 cat $CMDFILE > /dev/stderr
263 fi
264
265 gnuplot -persist ${CMDFILE}
266
267 rm .gb.data
268 rm "$CMDFILE"
269
270 fi