chiark / gitweb /
hexterm
[chiark-utils.git] / scripts / gnucap2genspic
1 #!/usr/bin/perl
2 #   gnucap2genspic - Copyright 2004 Ian Jackson - see below
3 #
4 # Reads the output from gnucap and outputs a `genspic' file
5 # for use with genspic2gnuplot.  Takes no arguments or options.
6 #
7 # Limitations
8 #
9 #  Only Freq (.AC) and Time (.TRAN) plots have been tested.  If
10 #   other types go wrong they can probably be fixed by adding code for
11 #   them to startplot().
12 #
13 #  Displaying voltages and currents on the same .TRAN graph won't work
14 #   well because they currently have to have the same Y scale.  This
15 #   could be fixed by assigning carefully to $mmm in startplot().
16 #
17 #  This whole scheme is very clumsy.  There should be a driver program
18 #  with command-line option parsing.
19
20 # This program and its documentation are free software; you can
21 # redistribute them and/or modify them under the terms of the GNU
22 # General Public License as published by the Free Software Foundation;
23 # either version 2, or (at your option) any later version.
24
25 # This program and its documentation are distributed in the hope that
26 # they will be useful, but WITHOUT ANY WARRANTY; without even the
27 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
28 # PURPOSE.  See the GNU General Public License for more details.
29
30 # You should have received a copy of the GNU General Public License along
31 # with this program; if not, write to the Free Software Foundation, Inc.,
32 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33
34 die if @ARGV;
35
36 %facttimes= qw(f 1e-15
37                p 1e-12
38                n 1e-9
39                u 1e-6
40                m 1e-3
41                K 1e3
42                Meg 1e6
43                G 1e9
44                T 1e12);
45
46 sub startplot () {
47     $logxy= 0;
48     for ($yn=1; $yn<=$#columns; $yn++) {
49         $mmm[$yn]= 'y';
50     }
51     if ($kind eq 'Freq') {
52         $logxy= 1;
53         for ($yn=1; $yn<=$#columns; $yn++) {
54             die unless $columns[$yn] =~ m/.*([MP])\(\d+\)$/i;
55             $mmm[$yn]= 'y2' if uc $1 eq 'P';
56         }
57     }
58     printf "S %s %d", $cplot, $logxy or die $!;
59     for ($yn=1; $yn<=$#columns; $yn++) {
60         printf " %s:%s", $mmm[$yn], $columns[$yn] or die $!;
61     }
62     print "\n" or die $!;
63 }
64 sub endplot () {
65     return unless defined $kind;
66     print "T\n" or die $!;
67 }
68
69 $readahead= <STDIN>;
70 for (;;) {
71     $linesofar= $readahead;
72     for (;;) {
73         $readahead= <STDIN>;
74         last unless $readahead =~ s/^\+//;
75         die unless length $linesofar;
76         $linesofar =~ s/\n$//;
77         $linesofar .= $readahead;
78     }
79     $_= $linesofar;
80     last unless length;
81     s/\s+$//;
82     
83     if (m/^\#(\w+)/) {
84         endplot();
85         $kind= $1;
86         @columns= split /\s+/;
87         $cplot= $kind;
88         startplot();
89         next;
90     } elsif (!defined $kind) {
91         next;
92     } elsif (s/^\s+//) {
93         @numbers= split /\s+/;
94         map {
95             if (m/^(\-?\d+\.\d*)([A-Za-z]+)$/) {
96                 die "factor $2" unless exists $facttimes{$2};
97                 $_= $1*$facttimes{$2};
98             }
99         } @numbers;
100         print "D @numbers\n" or die $!;
101     } else {
102         die "$_ ?";
103     }
104 }
105 die "no plots" unless defined $kind;
106 endplot();
107 print "F\n" or die $!;
108
109 # $Id: gnucap2genspic,v 1.2 2004-03-25 00:29:59 ianmdlvl Exp $