chiark / gitweb /
1cb0fff5e5657d7b1dad97f2bfe68ae5a6987f81
[chiark-utils.git] / scripts / genspic2gnuplot
1 #!/usr/bin/perl
2 #   genspic2gnuplot - Copyright 2004 Ian Jackson - see below
3 #
4 # Reads a `genspic' file on stdin.  Takes exactly one arg, <pfx>.
5 #
6 # Produces various output files:
7 #  <pfx>.gnuplots.sh                     run this to display results
8 #  <pfx>,<Kind><N>.gnuplot-cmd           gnuplot script for displaying:
9 #  <pfx>,<Kind><N>-<M>.gnuplot-data      gnuplot-format input data
10 #  <pfx>,gnuplot-fifo                    working fifo for .gnuplots.sh
11 # where
12 #  <Kind> is Freq or Time (according to the type of analysis)
13 #  <N>    is the count, starting at 0, of which report this is from gnucap
14 #  <M>    is the individual column of Y data
15 #
16 # Limitations
17 #
18 #  There's no easy way to mess with the gnuplot settings.
19 #
20 #  This whole scheme is very clumsy.  There should be a driver program
21 #  with command-line option parsing.
22
23 # This program and its documentation are free software; you can
24 # redistribute them and/or modify them under the terms of the GNU
25 # General Public License as published by the Free Software Foundation;
26 # either version 2, or (at your option) any later version.
27
28 # This program and its documentation are distributed in the hope that
29 # they will be useful, but WITHOUT ANY WARRANTY; without even the
30 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
31 # PURPOSE.  See the GNU General Public License for more details.
32
33 # You should have received a copy of the GNU General Public License along
34 # with this program; if not, write to the Free Software Foundation, Inc.,
35 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36
37 die unless @ARGV==1;
38 die if $ARGV[0] =~ m/^\-/;
39
40 ($ofb)= @ARGV;
41 $sof= "$ofb.gnuplots.sh";
42 open A, "> $sof" or die $!;
43 system 'chmod','+x',"$sof"; $? and die $?;
44 print A <<END
45 #!/bin/sh
46 set -e
47 fi=$ofb,gnuplot-fifo
48 rm -f \$fi
49 mkfifo -m 600 \$fi
50 END
51     or die $!;
52
53 for (;;) {
54     ($c,@s)= split /\s+/, <STDIN>;
55 print STDERR ">$c< >@s<\n";
56     if ($c eq 'S') {
57         ($cplot,$logxy,@columns) = @s;
58         $cplot .= $counter{$cplot}++;
59         unshift @columns, 'x:';
60         @mmm= map { s/^(\w+)\:// or die; $1; } @columns;
61         open S, "> $ofb,$cplot.gnuplot-cmd" or die $!;
62         print S <<END
63 set data style linespoints
64 set title '$cplot'
65 END
66         or die $!;
67         print S "set logscale xy\n" or die $! if $logxy;
68         print S "set y2tics autofreq\n" or die $! if grep { $_ eq 'y2' } @mmm;
69         undef %min;
70         undef %max;
71         for ($yn=1; $yn<=$#columns; $yn++) {
72             open "O$yn", "> $ofb,$cplot-$yn.gnuplot-data" or die $!;
73         }
74     } elsif ($c eq 'T') {
75         die unless @mmm;
76         die if @s;
77         foreach $mmm (keys %min) {
78             print S "set ${mmm}range [$min{$mmm}:$max{$mmm}]\n" or die $!;
79         }
80         $sep= "plot ";
81         for ($yn=1; $yn<=$#columns; $yn++) {
82             close "O$yn" or die $!;
83             $mmm[$yn] =~ m/^y2?$/ or die "$mmm[$yn]";
84             $axes= $mmm[$yn]; $axes =~ s/^y$/y1/;
85             $yoff= 1-$yn;
86             print S "$sep\\\n".
87                 " '$ofb,$cplot-$yn.gnuplot-data'".
88                     " axes x1$axes title '$columns[$yn]'"
89                         or die $!;
90             $sep= ',';
91         }
92         print S "\n\npause -1\n" or die $!;
93         close S or die $!;
94         print A "  gnuplot $ofb,$cplot.gnuplot-cmd <\$fi &\n" or die $!;
95         @mmm=@columns=();
96     } elsif ($c eq 'D') {
97         die unless @mmm;
98         @numbers= @s;
99 print STDERR "$#numbers $#columns.\n";
100         die unless @numbers == @columns;
101         for ($yn=0; $yn<=$#columns; $yn++) {
102             $_= $numbers[$yn];
103             $mmm= $mmm[$yn];
104             $min{$mmm}= $_ unless exists($min{$mmm}) && $min{$mmm} <= $_;
105             $max{$mmm}= $_ unless exists($max{$mmm}) && $max{$mmm} >= $_;
106             if ($yn) {
107                 printf {"O$yn"} "%s %s\n", $numbers[0], $_
108                     or die $!;
109             }
110         }
111     } elsif ($c eq 'F') {
112         last;
113     } else {
114         die;
115     }
116 }
117
118 print A <<END
119 exec 3>\$fi
120 printf 'hit return to quit: '
121 read
122 exec 3>&-
123 END
124     or die $!;
125 close A or die $!;
126
127 print ": generated ; $sof\n" or die $!;
128
129 # $Id: genspic2gnuplot,v 1.2 2004-03-25 00:29:59 ianmdlvl Exp $