#!/usr/bin/perl -I../bin

require "nml2perl.pl";
nml2perl($ENV{"RADSROOT"} . "/data/nml/getraw.nml");

print <<EOF;
<TABLE CLASS="form" CELLPADDING=5 CELLSPACING=2>
  <TR>
    <TD CLASS="formcaption" VALIGN=TOP ALIGN=CENTER>Altimeter</TD>
    <TD CLASS="formcaption" VALIGN=TOP ALIGN=CENTER>Phase</TD>
    <TD CLASS="formcaption" VALIGN=TOP ALIGN=CENTER>Time</TD>
    <TD CLASS="formcaption" VALIGN=TOP ALIGN=CENTER>Cycles</TD>
    <TD CLASS="formcaption" VALIGN=TOP ALIGN=CENTER>Passes</TD>
    <TD CLASS="formcaption" VALIGN=TOP ALIGN=CENTER>Records</TD>
  </TR>
EOF

my $globalrecs = 0;
my $globalpass = 0;
my $file;

#foreach $sat ("gs","e1","tx","pn","e2","g1","j1","n1","j2","c2","sa","j3","3a") {
foreach $sat ("c2") {
 undef %c0;
 undef %c1;
 undef %time0;
 undef %time1; 
 my @passindexfiles = <$sat*.cyc>;
 my $totalrecs = 0;
 my $totalpass = 0;
 my $pass0 = 1e20;
 my $pass1 = 0;
 my $phaselist = "";
 foreach $file (@passindexfiles) {
  $phase = substr($file,2,1);
  $c0{$phase} = 1e20;
  $c1{$phase} = 0;
  $time0{$phase} = 1e20;
  $time1{$phase} = 0;
  open(INDEX,$file);
  
  foreach $line (<INDEX>) {
    ($phase,$cycle,$pass0,$pass1,$start,$end,$npass,$recs) = split(' ',$line);
    $totalpass += $npass;
    $globalpass += $npass;
    $totalrecs += $recs; 
    $globalrecs += $recs;
    if ($start > 850000000000) {$start += 19000000000000;} else {$start += 20000000000000;}
    if ($end > 850000000000) {$end += 19000000000000;} else {$end += 20000000000000;}
    if ($start < $time0{$phase}) {$time0{$phase} = $start;} 
    if ($end > $time1{$phase}) {$time1{$phase} = $end;}
    if ($cycle > 990) {$cycle -= 1000;}
    if ($cycle < $c0{$phase}) {$c0{$phase} = $cycle;}
    if ($cycle > $c1{$phase}) {$c1{$phase} = $cycle;}
  }
  close(INDEX);
  $phaselist = $phaselist . substr($file,2,1);  
  $satabbr = substr($file,0,2);
  for ($i = 0; $i < @satpref; $i++) { 
    if ($satabbr eq $satpref[$i]) {$name=$satname[$i];}
  }

  $time0{$phase} =~ s/^\s+//;
  $time1{$phase} =~ s/^\s+//;

  $time0{$phase} = `convdate ymd=$time0{$phase} "+%d %b %Y"` or die "Error in convdate sec=$time0{$phase}: $!\n Sat=$sat line=$line";
  $time1{$phase} = `convdate ymd=$time1{$phase} "+%d %b %Y"` or die "Error in convdate sec=$time1{$phase}: $!\n Sat=$sat line=$line";

 }
 print <<EOF;
   <TR>
    <TD CLASS="formfield" VALIGN=TOP>$name</TD>
    <TD CLASS="formfield" VALIGN=TOP ALIGN=CENTER>
EOF
      foreach $phase (sort keys %c0) {
        print "\U$phase<BR>\n";
      }
 print <<EOF;
    </TD>
    <TD CLASS="formfield" VALIGN=TOP ALIGN=CENTER>
EOF
      foreach $phase (sort keys %c0) {
        print "$time0{$phase} - $time1{$phase}<BR>\n";
      }
print <<EOF;
    </TD>
    <TD CLASS="formfield" VALIGN=TOP ALIGN=CENTER>
EOF
      foreach $phase (sort keys %c0) {
	if ($c0{$phase} < 0) {$c0{$phase} += 1000;}
        print "$c0{$phase} - $c1{$phase}<BR>\n";
      }
 print <<EOF;
    </TD>
    <TD CLASS="formfield" ALIGN=RIGHT VALIGN=BOTTOM>$totalpass</TD>
    <TD CLASS="formfield" ALIGN=RIGHT VALIGN=BOTTOM>$totalrecs</TD>
   </TR>
EOF
}
print <<EOF;
   <TR>
     <TD CLASS="formfield" VALIGN=TOP>Total</TD>
     <TD CLASS="formfield" VALIGN=TOP></TD>
     <TD CLASS="formfield" VALIGN=TOP></TD>
     <TD CLASS="formfield" VALIGN=TOP></TD>
     <TD CLASS="formfield" VALIGN=TOP ALIGN=RIGHT>$globalpass</TD>
     <TD CLASS="formfield" VALIGN=TOP ALIGN=RIGHT>$globalrecs</TD>
   </TR>
</TABLE>
EOF
