test-ly-files: Display symlink creation, add `-v` to `ln`
[sunny256-utils.git] / stpl
blobb81e65cbd1634b1a2abf2259945423e28bf7cfe7
1 #!/usr/bin/env perl
3 #===============================================================
4 # stpl
5 # File ID: 6ec57c36-0903-11de-951a-000475e441b9
6 # Plot out data with gnuplot based on data type
8 # Character set: UTF-8
9 # License: GNU General Public License version 2 or later
10 # ©opyleft 2003-2004 Øyvind A. Holm <sunny@sunbase.org>
11 #===============================================================
13 use strict;
14 use warnings;
16 $| = 1;
18 use Getopt::Std;
20 our ($opt_f, $opt_h, $opt_k, $opt_L, $opt_l, $opt_o, $opt_s, $opt_t, $opt_x, $opt_y) =
21 ( "", 0, 0, 0, "", "", "", "", "", "");
22 getopts('f:hkLl:o:s:t:x:y:');
24 our $STD_TIMEFMT = "%Y-%m-%dT%H:%M:%SZ";
25 our $time_fmt = length($opt_f) ? $opt_f : $STD_TIMEFMT;
27 $opt_h && usage(0);
29 my $gnuplot_script = join(".", "/tmp/tmp.stpl", $$, substr(rand(), 2, 5), "plt");
31 if (-e $gnuplot_script) {
32 die("$gnuplot_script: Strange indeed — file already exists. Spooky.\n");
35 my $data_file = "";
37 if (defined($ARGV[0])) {
38 $data_file = $ARGV[0];
39 } else {
40 usage(1);
43 my $Lh = "[0-9a-fA-F]";
44 my $v1_templ = "$Lh\{8}-$Lh\{4}-1$Lh\{3}-$Lh\{4}-$Lh\{12}";
46 process_file($data_file);
48 exit(0);
50 sub usage {
51 # {{{
52 my $Retval = shift;
53 print <<END;
55 Syntax:
57 $0 [options] data_file
59 Options:
61 -f x Use x as date format. Default: "$STD_TIMEFMT"
62 -h Send this help to stdout
63 -k Keep temp file after plotting
64 -L Draw a line from startpoint to endpoint when it’s done
65 -lp Use linespoints instead of lines
66 -o x Use x as output format. Can be one of these:
67 fig (XFig)
68 png (.png)
69 ps (Postscript)
70 svg (Scalable Vector Graphics)
71 -sampN set samples N
72 -t"X" Use title X
73 -xrXX:YY Use X range XX:YY
74 -yrXX:YY Use Y range XX:YY
76 END
77 exit($Retval);
78 # }}}
81 sub process_file {
82 # {{{
83 my $data_file = shift;
84 unless (-r $data_file) {
85 die("$0: $data_file: File is unreadable or doesn’t exist.\n");
86 return undef;
88 if (open(GnupFP, ">$gnuplot_script")) {
89 # {{{
90 if (length($opt_x)) {
91 if ($opt_x =~ /^r(.*)/) {
92 # X range specified {{{
93 my $Range = $1;
94 if ($opt_x =~ /^r(.+:.+)$/) {
95 print(GnupFP "set xrange [$1]\n");
96 } else {
97 die("\"$Range\": Invalid X range specified\n");
99 # }}}
102 if (length($opt_y)) {
103 if ($opt_y =~ /^r(.*)/) {
104 # Y range specified {{{
105 my $Range = $1;
106 if ($opt_y =~ /^r(.+:.+)$/) {
107 print(GnupFP "set yrange [$1]\n");
108 } else {
109 die("\"$Range\": Invalid Y range specified\n");
111 # }}}
115 print(GnupFP <<END);
116 set xdata time
117 set grid
118 set timefmt "$time_fmt"
119 set format x "%Y-%m-%d"
122 if (length($opt_s)) {
123 if ($opt_s =~ /^amp/) {
124 if ($opt_s =~ /^amp(\d+)$/) {
125 print(GnupFP "set samples $1\n");
126 } else {
127 die("Invalid value in -samp option, number expected\n");
129 } else {
130 die("Unknown option: -s$opt_s\n");
133 if (length($opt_o)) {
134 if ($opt_o =~ /.*\.ps$/i) {
135 print(GnupFP "set terminal postscript enhanced color solid 8\nset output \"$opt_o\"\n");
136 } elsif ($opt_o =~ /.*\.png$/i) {
137 print(GnupFP "set terminal png\nset output \"$opt_o\"\n");
138 } elsif ($opt_o =~ /.*\.fig/i) {
139 print(GnupFP "set terminal fig color big metric\nset output \"$opt_o\"\n");
140 } elsif ($opt_o =~ /.*\.svg/i) {
141 print(GnupFP "set terminal svg\nset output \"$opt_o\"\n");
142 } else {
143 die("Unkonwn outfile type, has to be .ps, .png, .fig or .svg\n");
147 if (length($opt_t)) {
148 print(GnupFP "set title \"$opt_t\"\n");
151 my $line_type = "line";
153 if (length($opt_l)) {
154 if ($opt_l eq "p") {
155 $line_type = "linespoints";
159 # For framtidig bruk.
160 # while (<>) {
161 # if (/^(\d\d\d\d)[\- \/](\d\d)[\- \/](\d\d)T(\d\d):(\d\d):(\d\d)\s+(\d+)/) {
165 my $line_file = "$data_file.line.tmp";
166 my $line_str = "";
168 if ($opt_L) {
169 # FIXME
170 system("head -1 $data_file >$line_file");
171 system("tail -1 $data_file >>$line_file");
172 $line_str = qq{, \\\n"$line_file" using 1:2 title " " with line 3\n};
175 print(GnupFP <<END);
176 plot \\
177 "$data_file" using 1:2 title " " with $line_type linetype 1, \\
178 "$data_file" using 1:2 smooth bezier title " " with line linetype -1 linewidth 2$line_str
179 pause 999999 "$0: Press Ctrl+c when done..."
182 close(GnupFP);
183 $opt_L && unlink($line_str);
185 system("gnuplot $gnuplot_script");
186 if ($opt_k) {
187 print(STDERR "Keeping temp file $gnuplot_script\n");
188 } else {
189 unlink($gnuplot_script) || warn("$gnuplot_script: Unable to delete temporary file: $!\n");
191 if ($opt_o =~ /^.*\.ps$/i) {
192 system("gv $opt_o");
194 if ($opt_o =~ /^.*\.svg$/i) {
195 system("inkscape $opt_o");
197 # }}}
198 } else {
199 warn("$gnuplot_script: Can’t open file for write: $!\n");
201 # }}}
204 __END__
206 =pod
208 =head1 LICENCE
210 This program is free software; you can redistribute it and/or modify it
211 under the terms of the GNU General Public License as published by the
212 Free Software Foundation; either version 2 of the License, or (at your
213 option) any later version.
215 This program is distributed in the hope that it will be useful, but
216 WITHOUT ANY WARRANTY; without even the implied warranty of
217 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
218 See the GNU General Public License for more details.
220 You should have received a copy of the GNU General Public License along
221 with this program; if not, write to the Free Software Foundation, Inc.,
222 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
224 =cut
226 # vim: set fileencoding=UTF-8 filetype=perl foldmethod=marker foldlevel=0 :
227 # End of file stpl