#!/usr/local/bin/perl
#s2iplt v1.0 Steven Lipa, North Carolina State Univ. 8/24/95
#     Usage:   s2iplt filename
#     where filename is an IBIS v2.1 or lower model file
#You need perl to run this program.  perl is available *free*
#from several archive sites, such as
#               http://www1.cis.ufl.edu/perl/
#You need gnuplot to use this program.  gnuplot is available
# *free* from Dartmouth.  Information is available at
#       http://www.cs.dartmouth.edu/gnuplot_info.html
#
eval "exec /usr/local/bin/perl -S $0 $*"
    if $running_under_some_shell;

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;

if (length($ARGV[0])==0) {print "Usage: s2iplt filename\n"; exit;}
$, = ' ';		
$\ = "\n";
open(vx,"@ARGV");
$_=<vx>;
%mult = ("m",1.0E-3,"k",1.0E+3,"n",1.0E-9,
         "u",1.0E-6,"p",1.0E-12,"f",1.0E-15,
         "T",1.0E+12,"G",1.0E+09,"M",1.0E+6,
         "V",1.0,"A",1.0);
$us2 = "using 1:2"; $us3 = "using 1:3"; $us4 = "using 1:4";

until (/^\[Model\]/i) 
   {
    $_ = <vx>;
    if (!$_) {die "No [Model]s found";}
   }
until (!$_)
   {
    ($dummy, $modname) = split(' ', $_, 3);
    until ((/^\[pulldown\]/i) || 
           (/^\[pullup\]/i) ||
           (/^\[gnd.clamp\]/i) ||
           (/^\[falling.waveform\]/i) ||
           (/^\[rising.waveform\]/i) || 
           (/^\[power.clamp\]/i))
       {
        $_ = <vx>;
        if (/^\[Model\]/i) { ($dummy, $modname) = split(' ', $_, 3); }
        if (! $_) {unlink("s2idata", "s2istart"); exit;}
       }
    until (/^\[Model\]/i)
       {
        if (! $_) {unlink("s2idata", "s2istart"); exit;}
        ($tabname, $dummy) = split(']',$_,2);
        $tabname .= "]";
        until (($temp1 =~ /\d/) && ($temp2 =~ /\d/))
           {
            $_ = <vx>;
            if (! $_) {die "Empty table $tabname found";}
            ($temp1, $temp2, $temp3, $temp4) = split(' ',$_, 4);
           }
        unlink("s2istart");
        unlink("s2idata");
        open(s2s,">s2istart");
        open(s2d,">s2idata");
        $ab3 = 0;
        $ab4 = 0;
        while ($_ !~ /^\[/)
           {
            ($temp1, $temp2, $temp3, $temp4) = split(' ',$_, 4);
            if (($temp1 =~ /\d/) && ($temp2 =~ /\d/))
               {
                #then this is data so add it to the current plot file
                ($temp1, $temp2, $temp3, $temp4) =  &scale($temp1, $temp2, $temp3, $temp4);
                printf s2d "%g ", $temp1;
                printf s2d "%g ", $temp2;
                if ($temp3 =~ /\d/) { printf s2d "%g ", $temp3;}
                else {$ab3 = 1;}
                if ($temp4 =~ /\d/) { printf s2d "%g ", $temp4;}
                else {$ab4 = 1;}
                printf s2d "\n";
               }
            $_ = <vx>;
            if (($_ =~ /^\[/i) || (!$_))
                 { 
                  if ($tabname =~ /form/) { printf s2s "set xlabel \"$modname $tabname (nanoseconds)\"\n"; }
                  else
                     { printf s2s "set xlabel \"$modname $tabname\"\n"; }
                  printf s2s "set title \"s2iplt v1.0 North Carolina State University\"\n";
                  if (!$ab3 && !$ab4)
                     {
                      printf s2s "plot \"s2idata\" $us2 title 'typ'  w l 1, \"s2idata\" $us3 title 'min' w l 2, \"s2idata\" $us4 title 'max' w l 3\n";
                     }
                  elsif (!$ab3 && $ab4)
                         {
                          printf s2s "plot 's2idata' $us2 title 'typ'  w l 1, 's2idata' $us3 title 'min' w l 2\n";
                         }
                      elsif (!$ab4 && $ab3)
                            {
                             printf s2s "plot 's2idata' $us2 title 'typ'  w l 1, 's2idata' $us3 title 'max' w l 2\n";
                            }
                          else
                            {
                             printf s2s "plot 's2idata' $us2 title 'typ'  w l 1\n";
                            }
                  printf s2s "pause -1 \"Press return to continue\" \n";
                  close(s2s);
                  close(s2d);
                  system("gnuplot s2istart");
                 }
            if (!$_) {unlink("s2idata", "s2istart"); exit; }
           }
        until ((/^\[Model\]/i) ||
               (/^\[pulldown\]/i) ||
               (/^\[pullup\]/i) ||
               (/^\[gnd.clamp\]/i) ||
               (/^\[falling.waveform\]/i) ||
               (/^\[rising.waveform\]/i) ||
               (/^\[power.clamp\]/i) ||
               (!$_) ) { $_ = <vx>; }
       }
   }
unlink("s2idata", "s2istart");

sub scale
   {
    for ($a=0; $a<4; $a++)
       {
        if ($_[$a] =~ /[mkunpfsSAVTGM]/)
           {
            $C = chop($_[$a]);
            while(($C !~ /[0123456789]/) && length($_[$a]))
               {
                $D = $C;
                $C = chop($_[$a]);
               }
            if (length($_[$a]))
               {
                $_[$a] .= $C;
                $_[$a] *= $mult{$D};
                if ((!$a) && ($tabname =~ /form/)){ $_[$a] *= 1.0E9;}
               }
           }
       }
    ($_[0], $_[1], $_[2], $_[3]);
   }
