#!/usr/bin/perl
#
#   make an html page that lists descriptively the gif images in this directory
#


# get the list of all gif images
@flist = `find . -name "*.gif" `;
chop @flist;

# print out HTML header stuff
print "<HTML>\n";
print "<head>\n";
print "<TITLE>Current ACCENT Forecast Plots</TITLE>\n";
print "<link rev=\"made\" href=\"make_html.pl\">";
print "</head>\n";
print "\n";
print "<Body>\n";
print "<h1>Current ACCENT Forecast Plots</h1>\n";

# categorize each file
foreach $file (@flist) {

   $basename = `basename $file`;
   chop $basename;
   
   if ( $basename =~ m/^[A-Z]+_[0-9]{6,8}_[^.]+\.gif/ ) {
      # Paul Newman's images
      
      # print "+++ $file\n";

      # split off the ".gif" file extension
      $front = $basename;
      $front =~ s/\.gif$//;
      # ( $front, $ext ) = split('\.',$basename);

      # split the file name into data types, date, and the rest
      ($type,$yymmdd,@rest) = split('_',$front);
      
      # reformat the date
      $year = '19' . substr($yymmdd,0,2);
      $month = substr($yymmdd,2,2);
      $day = substr($yymmdd,4,2);
      $hour="00";
      if ( substr($yymmdd,6,1) ne '' ) {
         $hour = substr($yymmdd,6,2);
      }
      $date = sprintf("%4.4d-%2.2d-%2.2d",$year,$month,$day);
      $date = $date . sprintf(" at %2.2d UT",$hour) if ($hour ne "");
      
      # map or transsection?
      if ( $rest[0] eq 'XSECT' ) {
         $bearing = $rest[1];
         $fcst = $rest[2];
         $designation="XSECT:$bearing";
      } else {
         if ( $rest[0] =~ m/^XSECT/ ) {
            $bearing = $rest[0];
            $bearing =~ s/^XSECT//;
            $fcst = $rest[1];
            $designation="XSECT:$bearing";
         } else {
            $sfc = $rest[0];
            $fcst = $rest[1];
            $designation="THETA:$sfc";
            $designation="PRESS:$sfc" if ( $sfc == 250 );
         }
      }   
      $fcst = 0 if ( $fcst eq "");
      $fcst = 0 if ( $fcst eq "ANAL");
      
      
      $supername = join('|',$type,$date,$designation,$fcst);
      $file_hash{$supername} = $file;
      # print "$file ==> $supername\n";
      
      $categ{$date} = $categ{$date} . '#' . join('|',$type,$designation,$fcst);
      
   } else {
      if ( $basename =~ m/^[A-Z]+19[0-9]{6,8}_[^_]*__[0-9]{6,6}\.gif/ ) {
         # Tom Kucsera's images
      
         # print "+++ $file\n";

         # split off the ".gif" file extension
         $front = $basename;
         $front =~ s/\.gif$//;
         # ( $front, $ext ) = split('\.',$basename);

         # split the file name into its parts
         ($beg, $fc, $empty, $rest) = split('_',$front);
         
         $type = substr($beg,0,2) . substr($rest,3,3);
         
         # print "<$beg>  <$rest> --> <$type>\n";
         
         $year = substr($beg,2,4);
         $month = substr($beg,6,2);
         $day = substr($beg,8,2);
         $hour="12";
         $hour=substr($beg,10,2); # if ( strlen($beg) > 10 );

         $date = sprintf("%4.4d-%2.2d-%2.2d",$year,$month,$day);
         $date = $date . sprintf(" at %2.2d UT",$hour) if ($hour ne "");
 
         # map 
         $sfc = substr($rest,0,3);

         $fcst = "";
         $fcst = substr($fc,2,99) ; #if (strlen($fc) > 2);

         $designation="THETA:$sfc";
         $fcst = 0 if ( $fcst eq "");
         $fcst = 0 if ( $fcst eq "ANAL");
 
 
         $supername = join('|',$type,$date,$designation,$fcst);
         $file_hash{$supername} = $file;
         # print "$file ==> $supername\n";
 
         $categ{$date} = $categ{$date} . '#' . join('|',$type,$designation,$fcst);
 
      } else {
         print STDERR "--- $file\n";
      }
   }  
 
}


# now rearrange the files by date
foreach $date (sort  { $b cmp $a} (keys(%categ))) {

   next if ( $date eq '' );
   
   print "<h2>$date</h2>\n";
   
   @supernames = split'#',$categ{$date};
   
   undef %fores;
   foreach $name (@supernames) {
      next if ( $name eq '');
      ($type, $designation, $fcst) = split('\|',$name);
      $fores{$fcst} = $fores{$fcst} . '#' . join('|', $type, $designation ) ;
   }
   
   foreach $fcst (sort(keys(%fores))) {
      
      next if ( $fcst eq '');
      
      $fcstlabel = $fcst . "-hr forecast";
      $fcstlabel = "Analysis" if ( $fcst == 0);
   
      print "     <h3>$fcstlabel</h3>\n";
      
      @xnames = split('#', $fores{$fcst});
      
      undef %desigs;
      
      foreach $xname (@xnames) {
         ($xtype, $xdesignation) = split('\|',$xname);
         $desigs{$xdesignation} = $desigs{$xdesignation} . '#' . $xtype;   
      }
      
      print "          <ul>\n";
      foreach $ydesig (sort(keys(%desigs))) {
      
         next if ( $ydesig eq '' );
         
         $desiglabel = $ydesig;
         
         ( $cutkind, $value ) = split(':',$ydesig);
         $desiglabel = "<?>";
         if ( $cutkind eq 'THETA' ) {
            $desiglabel = "$value K isentropic surface"; 
         } else {
            if ( $cutkind eq 'PRESS' ) {
               $desiglabel = "$value mb pressure surface";
            } else {
               if ( $cutkind eq 'XSECT' ) {
                  $value = "towards KSC" if ( $value eq "KEN" );
                  $value = "towards Costa Rica" if ( $value eq "COS" );
                  $desiglabel = "Trans-section along bearing $value";
               }
            }
         }
         
         print "          <li>$desiglabel\n";
      
         @types = split('#', $desigs{$ydesig});
         
         print "             <ul>\n";
         foreach $ytype (@types) {
         
            next if ( $ytype eq '');
            
            $supername = join('|',$ytype, $date, $ydesig, $fcst );
            $fname = $file_hash{$supername};
         
            # print "               $supername -> $fname\n";
            
            $typelabel = "?";
            if ( $cutkind eq 'THETA' ) {
               $typelabel = "Potential Vorticity, Montgomery Streamfunction" if ( $ytype eq 'PV' );
               $typelabel = "RDF Modified PV" if ( $ytype eq 'RD' );
               $typelabel = "MPV and streak lines" if ( $ytype eq 'TR' );
               $typelabel = "Temperature, Geopot. Height, Tropopause" if ( $ytype eq 'TU' );
               $typelabel = "Aircraft exposure plot of Fuel" if ( $ytype eq 'EA001' );
               $typelabel = "Aircraft exposure plot of NOx"  if ( $ytype eq 'EA002' );
               $typelabel = "Aircraft exposure plot of HC"   if ( $ytype eq 'EA003' );
               $typelabel = "Aircraft exposure plot of CO"   if ( $ytype eq 'EA004' );
               $typelabel = "Lightning exposure"             if ( $ytype eq 'EL001' );
            } else {
               if ( $cutkind eq 'PRESS' ) {
                  $typelabel = "Potential Vorticity, Montgomery Streamfunction" if ( $ytype eq 'PV' );
                  $typelabel = "Temperature, Geopot. Height, Tropopause" if ( $ytype eq 'TU' );
               } else {
                  if ( $cutkind eq 'XSECT' ) {
                     $typelabel = "Temperature, Wind, Tropopause" if ( $ytype eq 'TU' );
                     $typelabel = "Potential Vorticity, Potential Temperature, Tropopause" if ( $ytype eq 'PV' );
                  }
               }
            }
            
            print "               <li><a href=\"$fname\">$typelabel</a>\n";
         }
         print "             </ul>\n";
      
      }
      print "          </ul>\n";
      
   }
}
   
# print out HTML trailer stuff
print "</Body>\n";
print "</HTML>\n";

