#!/usr/bin/perl
#this code takes an AVS field output by print field and turns it into a simple
#text file that is easily read by idl

$num = scalar(@ARGV);
if ($num != 1) {
  die "Usage: fixCDAweb.pl <CDAweb_file> \n";
}

print "Converting $ARGV[0] \n";
open(CDA,"$ARGV[0]") || die "Unable to open $ARGV[0] \n";
open(TMPFILE,">/tmp/cvt.tmp") || die "Unable to create /tmp/cvt.tmp \n";
open(NEW,">$ARGV[0].new") || die "Unable to open $ARGV[0].new \n";

$first = 0;
$second = 1;
$count = 0;
while(<CDA>) {
  if (/yyyy/) {$flag = 1;}
  if (1 == $flag) {
    if (1 == $first) {
       if (/-999.900/ || /0E\+31/ || /99999.9/ || /9999.99/) {
         print,"bad data pint \n";
       } else {
         s/-([0-9]*)-/ \1 /;
         s/:([0-9]*):/ \1 /;
         print TMPFILE;
         $count++;
       }
      if (1 == $second) {
        @words = split(' ', $_);
	@date = split('-', $words[0]);
	@time = split(':', $words[1]);		    

        $numcols = scalar(@words) + scalar(@date) + scalar(@time) - 2;
        print "@words with $numcols \n";
	if ($numcols > 0){
	    $second = 0;
	}
      }
    }
    $first = 1;
  }
}
close(CDA);
close(TMPFILE); 
print NEW $count-4, " $numcols  \n";
close(NEW);
$eval = `cat /tmp/cvt.tmp >> $ARGV[0].new`;
