#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig.stanford.edu>
# Creation Date: Mon Oct  8 22:46:08 PDT 2007
# Last Modified: Mon Oct  8 22:46:08 PDT 2007
# Last Modified: Fri Nov  2 03:59:42 PST 2007 (two to three random sequences)
# Filename:      /disk/linux2/craigsites/mazurka/website/info/dyn/gbdyn/mazurka30-2/addaverage
# Syntax:        perl 5
#
# Description:	Add an average dynamic curve, and add random test cases.
#

use strict;

my $file = $ARGV[0];
my $line;
my @numbs;
my @totals;
my $i;
my $count = 0;

open (FILE, $file) or die "Cannot open $file for reading\n";

while ($line = <FILE>) {
   chomp $line;
   $line =~ s/\s*$//;
   @numbs = split(/\t/, $line);
   for ($i=2; $i<@numbs; $i++) {
      $totals[$i] += $numbs[$i];
   }
   $count++;
}
close FILE;

my $totalsum = 0.0;
my $totalcount = 0;
for ($i=2; $i<@numbs; $i++) {
   $totals[$i] /= $count;
   $totalsum += $totals[$i];
   $totalcount++;
   # $totals[$i] = int($totals[$i] * 100 + 0.5) / 100.0;
}

$totalsum /= $totalcount;


open (FILE, $file) or die "Cannot open $file for reading\n";

my $sum;
my $value;
my $names = 0;
my $rand;
while ($line = <FILE>) {
   chomp $line;
   $line =~ s/\s*$//;
   if ($line =~ /\tpid/) {
      print "$line\t.\t.\t.\n";
      next;
   }
   if (!$names && $line =~ /^[A-Z]/i) {
      print "$line\tAverage\tRandom 1\tRandom 2\tRandom 3\n";
      $names = 1;
      next;
   }
   $sum = 0.0;
   @numbs = split(/\t/, $line);
   $count = 0;
   for ($i=2; $i<@numbs; $i++) {
      $sum += $numbs[$i] - $totals[$i];
      $count++;
   }
   $value = $sum / $count + $totalsum;
   $value = int($value * 100.0 + 0.5) / 100.0;
   print $line;
   print "\t$value";
   $rand = int(rand() * 100000.0 + 0.5) / 100000.0;
   print "\t$rand";
   $rand = int(rand() * 100000.0 + 0.5) / 100000.0;
   print "\t$rand";
   $rand = int(rand() * 100000.0 + 0.5) / 100000.0;
   print "\t$rand";
   print "\n";
}
close FILE;




