#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
# Creation Date: Sun Sep 10 00:36:18 PDT 2006
# Last Modified: Sun Sep 10 00:36:18 PDT 2006
# Filename:      /project/mazurka/website/auto/bynote/pid9048-06/makeplainnumbers
# Syntax:        perl 5
#
# Description:   Convert matlab junk into readable numbers
#


use strict;

my $line;
my @pieces;
my $i;
my $output;

while ($line = <>) {
   chomp $line;
   $line =~ s/^\s+//;
   $line =~ s/\s+$//;

   if ($line =~ /^\s*\#/) {
      print "$line\n";
      next;
   }
   if ($line =~ /^\s*;/) {
      print "$line\n";
      next;
   }
   if ($line =~ /^\s*$/) {
      print "$line\n";
      next;
   }

   $line =~ s/^\s+//;
   $line =~ s/\s+$//;

   @pieces = split(/\s+/, $line);
   my $len;
   my $frac;

   for ($i=0; $i<@pieces; $i++) {
      $pieces[$i] = getNumber($pieces[$i]);
      if ($i == 3 || $i == 4) {
         $pieces[$i] = int($pieces[$i] * 1000.0 + 0.5) / 1000.0;
         $len  = 0;
         $frac = 0;
         if ($pieces[$i] =~ /\.(\d+)/) {
            $frac = $1;
            $len = length($frac);
            $pieces[$i] .= "0" if $len < 3;
            $pieces[$i] .= "0" if $len < 2;
         } else {
            $len = 3;
            $pieces[$i] .= ".000";
         }
      }
      if ($i == 5) {
         $pieces[$i] = int($pieces[$i] * 10.0 + 0.5) / 10.0;
         if ($pieces[$i] !~ /\./) {
            $pieces[$i] .= ".0";
         }
      }
   }

   $output = join("\t", @pieces);
   print "$output\n";

}


exit(0);



##########################################################################


##############################
##
## getNumber --  
##

sub getNumber {
   my ($string) = @_;
   my $exponent;
   my $mantissa;

   if ($string =~ /^\s*$/) {
      return "error";
   }

   my $value;
   $string =~ /^([0-9\.\+\-]+)e([0-9\+\-]+)/i;
   $mantissa = $1;
   $exponent = $2;
   $value = $mantissa * (10 ** $exponent);
   return $value;
}



