#!/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 . ../movies -name "*.gif" -o -name "*.mpg" `;
chop @flist;

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

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

   $basename = `basename $file`;
   chop $basename;
   
   if ( $basename =~ m/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}T[0-9]{2,2}_[^.]+\.gif/ 
     || $basename =~ m/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}T[0-9]{2,2}\%[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}T[0-9]{2,2}_[^.]+\.mpg/ ) {
      # Paul Newman's images or Leslie Lait's movies, using the new naming convention
      
      $got_file = 1;
      
      # print "+++ $file\n";
      $legalname = 1;

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

      # split the file name into its components
      ($isodate, $type, $cutkind, $fcst, $dsrc ) = split('\_',$front);
      
      # if the date is actually a date range (as it is for movies),
      # split it and take the ending date
      if ( $isodate =~ m/%/ ) {
         # the isodatrange has two parts that make up a range.  Key on the last one
         ( $beg_iso_date, $end_iso_date ) = split('\%',$isodate);
         $isodate = $end_iso_date;
      }
      
      # print STDERR "==$isodate==$type==$cutkind==$fcst\n";
      
      # reformat the isodate
      $year = substr($isodate,0,4);
      $month = substr($isodate,5,2);
      $day = substr($isodate,8,2);
      $hour="12";
      if ( substr($isodate,10,1) eq 'T' ) {
         $hour = substr($isodate,11,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 ( $cutkind =~ m/^PR[0-9]/ ) {
         # horizontal surface-pressure
         $sfc = substr($cutkind,2,999);
         $designation="PRESS:$sfc";
      } else {
         if ( $cutkind =~ m/^TH[0-9]/ ) {
            # horizontal surface-theta
            $sfc = substr($cutkind,2,999);
            $designation="THETA:$sfc";
         } else {
            if ( $cutkind =~ m/^XS/ ) {
               # vertical transsection
               ( $junk, $bearing ) = split('\+',$cutkind);
               $designation="XSECT:$bearing";
            } else {
                # print STDERR "??? <<$cutkind>> $file\n";
                $legalname = 0;
            }
         }
      }
      
         
   } else {
         $got_file = 0;
         print STDERR "--- $file\n";
   }   
      
   if ($got_file) {
 
      # forecast hour is 0 for analyses
      $fcst = 0 if ( $fcst eq "");
      $fcst = 0 if ( $fcst eq "ANAL");
      # forecast hour is marked as "X" for multiple-forecast movies
      $fcst = -1 if ( $fcst eq "X");
 
      if ( $legalname) {
 
         # these tags will be split apart and sorted in a nested way
         $supername = join('|',$type,$date,$designation,$fcst,$dsrc,$ext);
         $file_hash{$supername} = $file;
 
         # thes etags will be used to reference the file name once the other
         # tags are re-assembled
         $ubername = join('|',$fcst,$designation,$type,$dsrc,$ext);
         if ( $cat_f_d_t_s_x{$date} ne '' ) {
            $cat_f_d_t_s_x{$date} = $cat_f_d_t_s_x{$date} . '#' . $ubername;
         } else {
            $cat_f_d_t_s_x{$date} =  $ubername;
         }
 
      }

   }
 
}


# now rearrange the files by date
# and go through each date
foreach $date (sort  { $b cmp $a} (keys(%cat_f_d_t_s_x))) {

   die "Blank date!\n" if ( $date eq '' );
   next if ( $date eq '' );
   
   # HTML label
   print "<h2>$date</h2>\n";
   
   # extract the list of characteristics for this date
   @ubernames = split('#',$cat_f_d_t_s_x{$date});
   
   # re-form a list of characteristics for this date
   #  separated by forecast hour
   undef %cat_d_t_s_x;
   foreach $ubername (@ubernames) {
      next if ( $ubername eq '');
      ($fcst, @rest) = split('\|',$ubername);
      $durfer = join('|', @rest );
      if ( $cat_d_t_s_x{$fcst} ne '' ) {
         $cat_d_t_s_x{$fcst} = $cat_d_t_s_x{$fcst} . '#' . $durfer ;
      } else {
         $cat_d_t_s_x{$fcst} =  $durfer ;
      }   
   }
   
   # go through each forecast hour for this date  
   foreach $fcst (sort { $a <=> $b } (keys(%cat_d_t_s_x))) {
      
      die "Blank fcst for date $date\n" if ( $fcst eq '');
      next if ( $fcst eq '');
      
      # translate the numeric forecast hour to an English description
      $fcstlabel = $fcst . "-hr forecast";
      $fcstlabel = "Analysis" if ( $fcst == 0);
      $fcstlabel = "" if ( $fcst < 0);
   
      # HTML sub-label
      print "     <h3>$fcstlabel</h3>\n";
      
      # get the list of characteristics for this forecast hour and date
      @xnames = split('#', $cat_d_t_s_x{$fcst});
      
      # print STDERR "#######  $cat_d_t_s_x{$fcst}\n";
      
      # create a list of characteristics for this date and forecast hour, separated by 
      # the designation (i.e., the type of cut across the data)
      undef %cat_t_s_x;
      foreach $xname (@xnames) {
         ($designation, @rest) = split('\|',$xname);
         $durfer = join('|', @rest);
         if ($cat_t_s_x{$designation} ne '' ) {
            $cat_t_s_x{$designation} = $cat_t_s_x{$designation} . '#' . $durfer ;  
         } else {
            $cat_t_s_x{$designation} =  $durfer;  
         }    
      }
      
      # start an HTML list
      print "          <ul>\n";
      
      # go through each designation
      foreach $desig (sort(keys(%cat_t_s_x))) {
      
         die "Blank designation for date $date and fcst $fcst\n" if ( $desig eq '' );
         next if ( $desig eq '' );
         
         $desiglabel = $desig;
         
         # translate the designation into an English description
         ( $cutkind, $value ) = split(':',$desig);
         $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";
               }
            }
         }
         
         # get the list of characteristics for this designation+forecast+date
         @ynames = split('#', $cat_t_s_x{$desig});
         
         # print STDERR "--- $desig -------> $cat_t_s_x{$desig}\n";
         
         # create a list of characteristics for this designation+forecast+date, separated by
         # the plot type (i.e., what this is a plot of)
         undef %cat_s_x;
         foreach $yname (@ynames) {
            ($type, @rest) = split('\|',$yname);
            # print STDERR ".......> $yname\n";
            $durfer = join('|',@rest);
            if ( $cat_s_x{$type} ne '' ) {
               $cat_s_x{$type} = $cat_s_x{$type} . '#' . $durfer;
            } else {   
               $cat_s_x{$type} = $durfer;
            }
         }
         
         # start an HTML sublist
         print "          <li>$desiglabel\n";
      
         print "             <ul>\n";
         
         # go through each plot type
         foreach $type (sort(keys(%cat_s_x))) {
         
            die "Blank type\n" if ( $type eq '');
            next if ( $type eq '');
            
            # translate the plot type into an English description
            $typelabel = "?";
            if ( $cutkind eq 'THETA' ) {
               $typelabel = "Potential Vorticity, Montgomery Streamfunction" if ( $type eq 'PV' );
               $typelabel = "RDF Modified PV" if ( $type eq 'RD' );
               $typelabel = "Modified PV and streak lines" if ( $type eq 'FL' );
               $typelabel = "Modified PV" if ( $type eq 'MPV' );
               $typelabel = "Modified PV" if ( $type eq 'MP' );
               $typelabel = "Solar Exposure" if ( $type eq 'SX' );
               $typelabel = "Temperature, Geopot. Height, Tropopause" if ( $type eq 'TU' );
               $typelabel = "Aircraft exposure plot of Fuel" if ( $type eq 'EA001' );
               $typelabel = "Aircraft exposure plot of NOx"  if ( $type eq 'EA002' );
               $typelabel = "Aircraft exposure plot of HC"   if ( $type eq 'EA003' );
               $typelabel = "Aircraft exposure plot of CO"   if ( $type eq 'EA004' );
               $typelabel = "Lightning exposure"             if ( $type eq 'EL001' );
            } else {
               if ( $cutkind eq 'PRESS' ) {
                  $typelabel = "Potential Vorticity, Montgomery Streamfunction" if ( $type eq 'PV' );
                  $typelabel = "Temperature, Geopot. Height, Tropopause" if ( $type eq 'TU' );
               } else {
                  if ( $cutkind eq 'XSECT' ) {
                     $typelabel = "Temperature, Wind, Tropopause" if ( $type eq 'TU' );
                     $typelabel = "Potential Vorticity, Potential Temperature, Tropopause" if ( $type eq 'PV' );
                  }
               }
            }
            
            
            # now get the list of data sources for this type+designation+forecast+date
            @znames = split('#',$cat_s_x{$type});
            
            # create a list of characteristics for this type+designation+forecast+date, separated by
            # the data source
            undef %cat_x;
            foreach $zname (@znames) {
               ($dsrc, $ext) = split('\|',$zname);
               # print STDERR ".......> $zname\n";
               $durfer = $ext;
               if ( $cat_x{$dsrc} ne '' ) {
                  $cat_x{$dsrc} = $cat_x{$dsrc} . '#' . $durfer;
               } else {
                  $cat_x{$dsrc} = $durfer;
               }
            }
            
            # go through each data source
            foreach $dsrc (sort(keys(%cat_x))) {
            
                # translate the data source to an English description
                $dsourc = "";
                $dsourc = "(NCEP)" if ( $dsrc eq "NMC");
                $dsourc = "(GSFC DAO)" if ( $dsrc eq "ASM");
                $dsourc = "(GSFC RDF)" if ( $dsrc eq "RDF");

                # get the file name extension
                $ext = $cat_x{$dsrc};
            
                # translate the extension to a notation if needed 
                $kind_tag = "";
                $kind_tag = "[ANIMATION]" if ( $ext eq "mpg" );
 
                # re-join the characteristics and use them to get the file name
                $supername = join('|',$type, $date, $desig, $fcst, $dsrc, $ext );
                $fname = $file_hash{$supername};
 
                if ( $fname eq '' ) {
                   die "[[$supername]] Not found in file_hash\n";
                }
 
                # print STDERR "               $supername -> $fname\n";
 
                # encode any "%" characters to make legal URLs
                $lnkname = $fname;
                $lnkname =~ s/\%/%25/g;
 
 
                # print STDERR "$lnkname --> $fname\n";
 
                # print out the link
                print "               <li><a href=\"$lnkname\">$typelabel $dsourc $kind_tag</a>\n";
            }
         }
         print "             </ul>\n";
      
      }
      print "          </ul>\n";
      
   }
}
   
# print out HTML trailer stuff
print "</Body>\n";
print "</HTML>\n";

