#!/usr/bin/perl -TI../ -I./
#
# product browser
#
# $Id$
#
# get action test
#
# April 2000
#--------------------------------------------------------------------
use CGI;
require "config.pl";
require "util.pl";
require "timelocal.pl";

($PROGRAM = $0) =~ s%.*/%%;
$|=1;
$ENV{'PATH'} = '';
$THIS_DOCDIR = $OPS_DOCDIR;
$THIS_REAL_DOCDIR = $OPS_REAL_DOCDIR;
$THIS_CGIDIR = $OPS_CGIDIR;
$LOOPER = "$CGIDIR/loop.pl.test";
$TARGET = 'target="newwindow"';
$latest = 0;
$HCOLOR = "lightblue";
$GREY = "#EEEEEE";
$CATEGORY = "ops";


$FORM = new CGI;

$howmany = $FORM->param("howmany");
#$category = $FORM->param("category");
$category = $CATEGORY;
$platform = $FORM->param("platform");
$prod = $FORM->param("prod");

#User selected Start/End date option. Process date range inputs
if($howmany =~ /^Use Start\/End Dates/i) {
  $start = $FORM->param("start");
  $end = $FORM->param("end");
} elsif($howmany =~ /^latest\s*$/i) {
  $start = "-0";
  $end = "";
  $latest = 1;
} elsif($howmany =~ /^last\s*(\d+)\s*hours\s*$/i) {
  $start = -$1;
  $end = "";
} elsif($howmany =~ /^last\s*day$/i) {
  $start = "";
  $end = "";
} elsif($howmany =~ /^all\s*$/i) {
  $start = "all";
  $end = "";
} else {
  $start = "";
  $end = "";
}

@files = &file_query(\$category,\$platform,\$prod,\$start,\$end);    

if($#files < 0) {
 zprint_error("No files found");
}

$prod =~ s/\*//g if($prod =~ /\w/);

#$forecast_prod = 0;
#if(grep /^$platform$/i, @FORECASTS_PRODUCTS) {
#  $forecast_prod = 1;
#}

#
# Check if a forecast product
#
$forecast_prod = 0;
$group = &find_group($category,$platform);
$forecast_prod = 1 if($group =~ /FORECAST/i);

#--------------------------------------------------------------------
#
# Generate Output
#
#--------------------------------------------------------------------
#
#Display the latest image
#
if($latest && !$forecast_prod) {
  $file = $files[-1];
  $file =~ s/^$DOCROOT//;
  #if($file =~ /^\.?\/?([\w\.\-\/\_]+)\s*$/) {
  if($file =~ /^([\w\.\-\/\_]+)\s*$/) {
    $file = $1;
  } else {
    zprint_error("Bad file name value:$file");
  }
  
  ($file =~ /.*\.(.+?)$/) && ($suffix = $1);
  if (grep /^$suffix$/i, @IMGSFXS) {
    exec "$REAL_CGIDIR/imagewrap $file";
    print STDERR "insbr error: $!\n";
  } elsif(grep /^$suffix$/i, @TXTSFXS) {
    exec "$REAL_CGIDIR/preformat $file";
    print STDERR "insbr error: $!\n";
  } elsif(grep /^$suffix$/i, @HTMLSFXS) {
    exec "$REAL_CGIDIR/htmlwrap $file";
    print STDERR "insbr error: $!\n";
  }
}

print $FORM->header();
if (-e "$THIS_REAL_DOCDIR/head.html") {
  $header = join "",`/bin/cat $THIS_REAL_DOCDIR/head.html`;
  $header =~ s/(\<\/title\>)/: $name_short$1/i;
  print $header;
}
print "<center> <br>\n";

#
# Display a Forecast table for forecast products
#
if($forecast_prod) {
  %PRODS = ();
  for (@files) {
    s/\s*$//;
    s/^$DOCROOT//;
    $file = $_;
    $file =~ s/.*\///;
    $file =~ s/\s*$//;

    ($fcategory,$fplatform,$pdate,$subp) = split /\./, $file,4;
    if($subp =~ /^\d+_\w+/) {
      ($fhour,$subp) = split /_/, $subp,2;
    } else {
      $fhour = "0-> ";
    }

    $subp =~ s/\.[^\.]+$//;

    #
    # Convert forecast hour to date
    #
    $year = (substr $pdate,0,4) - 1900;
    $month = (substr $pdate,4,2) - 1;
    $day = substr $pdate,6,2;
    $hour = substr $pdate,8,2;
    $sec=0;$min=0;
    eval {
      ($sec,$min,$hour,$day,$month,$year,$wday,$yday,$isdst) = gmtime(
            $fhour*3600 + timegm($sec,$min,$hour,$day,$month,$year));
    };
    if($@) { 
      print STDERR "date_browse: $@\n";
      next;
    }

    $year+=1900;
    $month = sprintf("%2.2d",$month+1);
    $day = sprintf("%2.2d",$day);
    $hour = sprintf("%2.2d",$hour);

    # 
    # Save forecast dates and model info in hash.
    #
    $time = substr $pdate,8;
    if($time !~ /\d/) {
      $count++;
      #$time = "????$count";
      $time = "0->";
    }
    ($pdate_str = $pdate) =~ s/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)/$1\/$2\/$3 $4:$5/;
    $PRODS{"$pdate_str"}{$year}{$month}{$day}{$hour}{$time} = "$_;$fhour";
  }

  if($latest) {
    @rdates = sort keys %PRODS;
    $rdate_last = @rdates[-1];
    %PRODS_LAST = ();
    $PRODS_LAST{"$rdate_last"} = $PRODS{"$rdate_last"};
    &print_forecast_table($platform,$prod,%PRODS_LAST);
  } else {
    &print_forecast_table($platform,$prod,%PRODS);
  }

}  else {

  #******************************************************************
  #
  # Create Product Table
  #
  #******************************************************************
  if ( $#files < 0 ) {
    print "<table border=1 cellpadding=2 cellspacing=0>\n";
    print "<caption>Products</caption>\n";
    print "<tr><td><b>No Products for: $name_short</b></td></tr>\n";
    print "</table>\n";
  } else {
     for (@files) {
       s/\s*$//;
       s/^$DOCROOT//;
       $file = $_;
       $file =~ s/.*\///;
       $file =~ s/\s*$//;
       #($prod,$pdate,$subp) = split /\./, $file;
       ($dum,$dum,$pdate,$subp) = split /\./, $file;
       $year  = substr $pdate,0,4;
       $month = substr $pdate,4,2;
       $day   = substr $pdate,6,2;
       $hour = substr $pdate,8,2;
       $time = substr $pdate,8;
       if($hour !~ /\d/) {
         $hour = '00';
       }
       $hour = $hour.'0'x(2-length($hour));
       if($time !~ /\d/) {
         $count++;
         #$time = "????$count";
         $time = "0->";
       }
       $PROD{$year}{$month}{$day}{$hour}{$time} = $_;
     }
  
     &print_prod_table($platform,$prod,%PROD);
  }
}
print "<p>\n";
#print "<hr>\n";
print "<p>\n";

print "</center><p>\n";
print $FOOTER;
exit;

exit 0;
sub do_error {
  local($errstr) = @_;
  print <<EOERR;
<H1>Error</H1>
An error has occurred.
$errstr
Please send mail to
<EM><A HREF="mailto:www\@joss.ucar.edu">www\@joss.ucar.edu</A></EM>
and tell us what you were doing when the error occurred. Thank you.
<HR>
EOERR
  exit 0;
}

#******************************************************************
# subroutine: print_prod_table
#
# inputs:
#
#  $PROD      - Product name.
#  $SUBPROD   - Subproduct name.
#  %prod         - input hash for table entries.
#
#   %prods must be of the form:
#    $prods{year}{month}{day}{hour}{time} = "file[;name]";
#   where year  = yyyy
#         month = mm (1-12)
#         day   = dd (1-31)
#         hour  = hh (0-23)
#         time  = hhmmss...
#         file  = url path to file
#         ;name = optional string to use for link name. If not present
#                 will use 'time' above for link name.
#
#******************************************************************

sub print_prod_table {

  local($PROD,$SUBPROD,%prod) = @_;
  local($year,$month,$day,$hour,$time);
  local($prod,$subp,$nsubp,$tprod);
  local($fullprod,$fullsubp);
  local($name,$totcol,$colspan,$nspace);

  # For cosmetic purposes only. Used for indenting table tags.
  local($s0,$s1,$s2,$s3,$s4);
  $s0 = "";
  $s1 = $s0."\t"x1;
  $s2 = $s0."\t"x2;
  $s3 = $s0."\t"x3;
  $s4 = $s0."\t"x4;

  local(@ALLurl)=();
  local(@ALLhrlyurl) = ();
  local(@ALL3hrlyurl) = ();
  local(%allurl_across) = ();
  local(%hrlyurl_flag) = ();
  local(%hrlyurl_across) = ();
  local(%allurl_down) = ();
  local(%hrlyurl_down) = ();
  local(%hrlyurl_downflag) = ();
  local($maximages) = 120;

  # Stuff the hour range into hash 'hours'
  # and generate 'Movie All' link
  for $year (sort keys %prod) {
   for $month (sort keys %{$prod{$year}}) {
    for $day (sort keys %{$prod{$year}{$month}}) {
     $lastallurl_across = -1;
     for $hour (sort keys %{$prod{$year}{$month}{$day}}) {
      $hours{$hour} = 1;   
      for $time (sort keys %{$prod{$year}{$month}{$day}{$hour}}) {
        $link = $prod{$year}{$month}{$day}{$hour}{$time};
        ($link,$dummy) = split /;/, $link;
        $link =~ s/^\.\///;
        ($link =~ /.*\.(.+?)$/) && ($suffix = $1);

        #
        # Construct all the Movie url's
        #
        if (grep /^$suffix$/i, @IMGSFXS) { 
         $lastallurl_across++;
         ($imglink = $link) =~ s/.*\///;
         $parm = "&img=" . $imglink;
         push(@{$allurl_across{$year}{$month}{$day}}, "$year$month$day$hour");
         #push(@{$allurl_across{$year}{$month}{$day}}, $parm);
         push(@{$hrlyurl_across{$year}{$month}{$day}}, $parm)
           if(!$hrlyurl_flag{$year}{$month}{$day}{$hour});
        
         #push(@ALLurl, $parm);
         push(@ALLurl, "$year$month$day$hour");
         push(@ALLhrlyurl, $parm)
           if(!$hrlyurl_flag{$year}{$month}{$day}{$hour});

         #push(@ALL3hrlyurl, $parm) if($time =~ /^(024|054|084|114|144|174|204|234)/ && $PROD =~ /goes/i);
         push(@ALL3hrlyurl, $parm) if($time =~ /^(02|05|08|11|14|17|20|23)3[8-9]/ && $PROD =~ /goes/i);
        
         push(@{$allurl_down{$hour}}, $parm);
         push(@{$hrlyurl_down{$hour}}, $parm)
           if(!$hrlyurl_flag{$year}{$month}{$day}{$hour});

         $hrlyurl_flag{$year}{$month}{$day}{$hour} = 1;
        }

      }
     }
    }
   }
  }


  # Calculate total column span
  #$totcol=2 + keys %hours;
  $totcol= keys %hours;
  
  #
  # Start table and print the days the products span.
  #

  print "$s0<font size=+2>\n";
  $fullprod = $LONG_NAMES{$PROD} ? $LONG_NAMES{$PROD} : $PROD;
  print "$s1<b><a class=status title='$fullprod' href=\"javascript:alert('$PROD:  $fullprod')\" onMouseOver=\"status='$fullprod'\;return true\" onMouseOut=\"status=''\">$PROD</a></b>\n"; 
  $fullsubp = $LONG_NAMES{"$PROD:$SUBPROD"} ? $LONG_NAMES{"$PROD:$SUBPROD"} : $SUBPROD;
  print "$s1<a class=status title='$fullsubp' href=\"javascript:alert('$SUBPROD:  $fullsubp')\" onMouseOver=\"status='$fullsubp'\;return true\" onMouseOut=\"status=''\">($SUBPROD)</a>\n"; 
  print "$s0</font><br>\n";

  $info_link = $LINKS{lc $PROD};
  print "($info_link)" if($info_link);      
#<HACK>
#if($PROD =~ /^MIPS/i) {
#  print " (<a href=\"/ihop/catalog/mips_doc.html\">MIPS Wind Product Information</a>)";
#}
#if($PROD =~ /^HARLIE/i) {
#  print " (<a href=\"/ihop/catalog/HARLIE_info.html\">HARLIE Product Information</a>)";
#}
#</HACK>

  #print "$s0<table cols=$totcol width=\"80%\" border=1 cellpadding=2 cellspacing=0>\n";
  #print "$s0<table cols=$totcol border=1 cellpadding=2 cellspacing=0>\n";
  print "$s0<table border=1 cellpadding=2 cellspacing=0>\n";

  print "$s1<tr><td>&nbsp;</td>\n"; 
  print "$s2<td colspan=$totcol align=center>Hours(UTC)</td>\n"; 
  print "$s2<td>&nbsp;</td>\n"; 
  print "$s1</tr>\n"; 

  print "$s1<tr align=center>\n";
  print "$s2<td rowspan=1>\n";
  print "${s3}Date\n";
  print "$s2</td>\n";
  print "$s2</td>\n";

  #
  # Print the hours for which there are products for each day
  #
  for $hour (sort keys %hours) {
    print "$s2<td  bgcolor=$HCOLOR align=center>\n";
    print "$s3<b>$hour</b>\n";
    print "$s2</td>\n";
  }

  print "$s2<td bgcolor=$GREY>&nbsp;Movies</td>\n"; 
  print "$s1</tr>\n";


  #
  # Start of main table.
  #
  for $year (sort keys %prod) {
   for $month (sort keys %{$prod{$year}}) {
    for $day (sort keys %{$prod{$year}{$month}}) {
     print "$s1<tr><td>$year/$month/$day</td>\n";
     for $hour (sort keys %hours) {
      if($prod{$year}{$month}{$day}{$hour}) {
       print "$s2<td align=center>\n";
       $nspace = ""; #So we won't print a link and a space
       for $time (sort keys %{$prod{$year}{$month}{$day}{$hour}}) {
        if($link = $prod{$year}{$month}{$day}{$hour}{$time}) {
         ($link,$name) = split /;/, $link;
         if(!$name) {
           $name = $time;
         } else { 
           $name .= "hr";
         }
         $link =~ s/^\.\///;
         $file = $link; $file =~ s/.*\///;;
         print "$s3<a style=\"text-decoration: none\" href=\"";
         ($file =~ /.*\.(.+?)$/) && ($suffix = $1);
         SWITCH: {
           if (grep /^$suffix$/i, @IMGSFXS) { 
              print "$CGIDIR/imagewrap.nonav?$link"; last SWITCH; }
           if (grep /^$suffix$/i, @TXTSFXS) { 
              print "$CGIDIR/preformat.nonav?$link"; last SWITCH; }
           if (grep /^$suffix$/i, @HTMLSFXS) { 
              print "$CGIDIR/htmlwrap.nonav?$link"; last SWITCH; }
           print "$link";
         }
         print "\" $TARGET><font size=-2>${name}</font></a><br>\n";
         #print "\"><font size=-2>${name}</font></a><br>\n";
         $nspace = 1;
        } elsif(!$nspace) {
         print "${s3}&nbsp;\n";
         $nspace = 1;
        }
       }
       print "$s2</td>\n";
      } else {
       print "$s2<td>&nbsp;</td>\n";
      }

     }


     #
     # Print the movie links at right side of table
     #
     print "$s2<td bgcolor=$GREY>\n";

     if($#{$allurl_across{$year}{$month}{$day}} > 0) {
       $pstart = ${$allurl_across{$year}{$month}{$day}}[0];
       $pend = ${$allurl_across{$year}{$month}{$day}}[-1];
       print "$s3<font size=2><a href=\"$LOOPER?category=$CATEGORY&platform=$PROD&prod=$SUBPROD&start=$pstart&end=$pend\" $TARGET>all</a></font>\n";
       #($turl = join "",@{$allurl_across{$year}{$month}{$day}}) =~ s/^&//;
       #print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>all</a></font>\n";
       if($#{$hrlyurl_across{$year}{$month}{$day}} > 0 && 
          $#{$hrlyurl_across{$year}{$month}{$day}} < $#{$allurl_across{$year}{$month}{$day}}) {
         ($turl = join "",@{$hrlyurl_across{$year}{$month}{$day}}) =~ s/^&//;
         print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>hourly</a></font>\n";
       }
     } else {
       print "$s3&nbsp;\n";
     }




     print "$s2</td>\n";
     print "$s1</tr>\n";

    }

   }
  }

  if($#ALLurl > $lastallurl_across) {
    #
    # Print the Movie links at bottom of table
    #
    print "$s1<tr>\n";
    print "$s2<td bgcolor=$GREY>Movies -&gt;</td>\n";
    for $hour (sort keys %hours) {
  
      print "$s2<td align=center bgcolor=$GREY>\n";
      if($#{$allurl_down{$hour}} > 0) {
        ($turl = join "",@{$allurl_down{$hour}}) =~ s/^&//;
        print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>all</a></font>\n";

        if($#{$hrlyurl_down{$hour}} > 0 && 
           $#{$hrlyurl_down{$hour}} < $#{$allurl_down{$hour}}) {
          ($turl = join "",@{$hrlyurl_down{$hour}}) =~ s/^&//;
          print "<br>$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>hrly</a></font>\n";
        }
      } else {
        print "$s3&nbsp;\n";
      }
  
      print "$s2</td>\n";
  
    }
    #
    # Print the ALL Movie link at bottom right corner of table
    #
    print "$s2<td align=center bgcolor=$GREY>\n";
    if($#ALLurl > 0) {
      #($turl = join "",@ALLurl) =~ s/^&//;
      if($#ALLurl > $maximages) {
        print "$s3<font size=2>ALL*</font>\n";
      } else {
        $pstart = $ALLurl[0];
        $pend = $ALLurl[-1];
        print "$s3<font size=2><a href=\"$LOOPER?category=$CATEGORY&platform=$PROD&prod=$SUBPROD&start=$pstart&end=$pend\" $TARGET>ALL</a></font>\n";
        #print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>ALL</a></font>\n";
      }
      if($#ALLhrlyurl > 0 && $#ALLhrlyurl < $#ALLurl) {
        ($turl = join "",@ALLhrlyurl) =~ s/^&//;
        if($#ALLhrlyurl > $maximages) {
          print "$s3<font size=2>HRLY*</font>\n";
        } else {
          print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>HRLY</a></font>\n";
        }
      }
if($PROD =~ /goes/i) {
      if($#ALL3hrlyurl > 0 && $#ALL3hrlyurl < $#ALLurl) {
        ($turl = join "",@ALL3hrlyurl) =~ s/^&//;
        if($#ALL3hrlyurl > $maximages) {
          print "$s3<font size=2>3HRLY*</font>\n";
        } else {
          print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>3HRLY</a></font>\n";
        }
      }
}
    } else {
      print "$s3&nbsp;\n";
    }
    print "$s2</td>\n";
    print "$s1</tr>\n";

    if($#ALLurl > $maximages || $#ALLhrlyurl > $maximages || $#ALL3hrlyurl > $maximages) {
      $footnote = "<p><font size=-2>* Maximum number of $maximages looped images exceeded for this link. Please reduce the time span to use this option.</font>";
    }

  }



  print "$s0</table>\n";


  print $footnote;
 
}

#******************************************************************
# subroutine: print_forecast_table
#
# inputs:
#
#  $PROD      - Product name.
#  $SUBPROD   - Subproduct name.
#  %prod      - input hash for table entries.
#
#   %prods must be of the form:
#    $prods{run_date}{year}{month}{day}{hour}{time} = "file[;name]";
#   where year  = yyyy
#         month = mm (1-12)
#         day   = dd (1-31)
#         hour  = hh (0-23)
#         time  = hhmmss...
#         file  = url path to file
#         ;name = optional string to use for link name. If not present
#                 will use 'time' above for link name.
#
#******************************************************************

sub print_forecast_table {

  #local($caption,$top_row_name,%prods) = @_;
  local($PROD,$SUBPROD,%prods) = @_;
  local(%dates) = ();
  local($year,$month,$day,$hour,$time);
  local($prod,$subp,$nsubp,$tprod);
  local($fullprod,$fullsubp);
  local($url,$nurl);
  local($name,$totcol,$colspan,$nspace);

  # For cosmetic purposes only. Used for indenting table tags.
  local($s0,$s1,$s2,$s3,$s4);
  $s0 = "";
  $s1 = $s0."\t"x1;
  $s2 = $s0."\t"x2;
  $s3 = $s0."\t"x3;
  $s4 = $s0."\t"x4;

  # Stuff the date and hour range into hash 'dates'
  for $run_date (keys %prods) {
   #for $subp (keys %{$prods{$run_date}}) {
    $nsubp++;
    for $year (keys %{$prods{$run_date}}) {
     for $month (keys %{$prods{$run_date}{$year}}) {
      for $day (keys %{$prods{$run_date}{$year}{$month}}) {
       for $hour (keys %{$prods{$run_date}{$year}{$month}{$day}}) {
        $dates{$year}{$month}{$day}{$hour} = 1;   
       }
      }
     }
    }
   #}
  }


  # Calculate total column span
  #$totcol=2;
  for $year (sort keys %dates) {
   for $month (sort keys %{$dates{$year}}) {
    for $day (sort keys %{$dates{$year}{$month}}) {
     for $hour (sort keys %{$dates{$year}{$month}{$day}}) {
       $totcol++;
     }
    }
   }
  }

  #
  # Start table and print the days the products span.
  #
 
  print "$s0<font size=+2>\n";
  $fullprod = $LONG_NAMES{$PROD} ? $LONG_NAMES{$PROD} : $PROD;
  print "$s1<b><a class=status title='$fullprod' href=\"javascript:alert('$PROD: $fullprod')\" onMouseOver=\"status='$fullprod'\;return true\" onMouseOut=\"status=''\">$PROD</a></b>\n";
  $fullsubp = $LONG_NAMES{"$PROD:$SUBPROD"} ? $LONG_NAMES{"$PROD:$SUBPROD"} : $SUBPROD;
  print "$s1<a class=status title='$fullsubp' href=\"javascript:alert('$SUBPROD: $fullsubp')\" onMouseOver=\"status='$fullsubp'\;return true\" onMouseOut=\"status=''\">($SUBPROD)</a>\n";
  print "$s0</font><br>\n";
   
  #print "$s0<table cols=$totcol width=\"80%\" border=1 cellpadding=2 cellspacing=0>\n";
  #print "$s0<table cols=$totcol border=1 cellpadding=2 cellspacing=0>\n";
  print "$s0<table border=1 cellpadding=2 cellspacing=0>\n";
  #print "$s1<caption>$caption</caption>\n";

  print "$s1<tr><td>&nbsp;</td>\n";
  print "$s2<td colspan=$totcol align=center>Forecast Times(UTC)</td>\n";
  print "$s2<td>&nbsp;</td>\n";
  print "$s1</tr>\n";         


  print "$s1<tr align=center>\n";
  print "$s2<td rowspan=2>\n";
  print "${s3}Model Run/Analysis<br>Times(UTC)\n";
  print "$s2</td>\n";
  for $year (sort keys %dates) {
   for $month (sort keys %{$dates{$year}}) {
    for $day (sort keys %{$dates{$year}{$month}}) {
     $colspan = keys %{$dates{$year}{$month}{$day}};
     $smonth = $NUMTOMONTH[$month];
     print "$s2<td  bgcolor=$HCOLOR colspan=$colspan align=center>\n";
     print "$s3<b>$day $smonth $year</b>\n";
     print "$s2</td>\n";
    }
   }
  }
  print "$s2<td rowspan=2 bgcolor=$GREY>\n";
  print "$s3 Movies\n";
  print "$s2</td>\n";
  print "$s1</tr>\n";

  #
  # Print the hours for which there are products for each day
  #
  print "$s1<tr>\n";
  #$totcol=2;
  for $year (sort keys %dates) {
   for $month (sort keys %{$dates{$year}}) {
    for $day (sort keys %{$dates{$year}{$month}}) {
     for $hour (sort keys %{$dates{$year}{$month}{$day}}) {
      print "$s2<td  bgcolor=$HCOLOR align=center>\n";
      print "$s3<b>$hour</b>\n";
      print "$s2</td>\n";
      #$totcol++;
     }
    }
   }
  }
  print "$s1</tr>\n";


  %url_down=();
  $bottom_movies = 0; 
  #
  # Start of main table.
  #
  for $run_date (sort keys %prods) {
   print "$s1<tr align=center valign=top>\n"; 
   #print "$s2<td colspan=$totcol align=left bgcolor=$GREY>\n"; 
   print "$s2<td align=center nowrap>$run_date</td>\n"; 

#<HACK>
#if($prod =~ /^lidar/i) {
#  print " (<a href=\"/ace-asia/catalog/lidar_map.gif\">lidar station map</a>)";
#}
#if($prod =~ /^sounding/i) {
#  print " (<a href=\"/ace-asia/catalog/sounding_sites.gif\">sounding sites map</a>)";
#}
#</HACK>

    $url="";
    $nurl=0;
    for $year (sort keys %dates) {
     for $month (sort keys %{$dates{$year}}) {
      for $day (sort keys %{$dates{$year}{$month}}) {
       for $hour (sort keys %{$dates{$year}{$month}{$day}}) {


        if($prods{$run_date}{$year}{$month}{$day}{$hour}) {
         print "$s2<td align=center>\n";
         $nspace = ""; #So we won't print a link and a space
         for $time (sort keys %{$prods{$run_date}{$year}{$month}{$day}{$hour}}) {
          if($link = $prods{$run_date}{$year}{$month}{$day}{$hour}{$time}) {
           ($link,$name) = split /;/, $link;
           if(!$name) {
             $name = $time;
           } else { 
             $name .= "hr" if($name !~ /->/);
           }
           $link =~ s/^\.\///;
           $file = $link; $file =~ s/.*\///;;

           ($imglink = $link) =~ s/.*\///;
           $parm = "&img=" . $imglink;
           $url.= $parm;
           $nurl++;
push(@{$url_down{$year}{$month}{$day}{$hour}}, $parm); 

$bottom_movies = 1 if($#{$url_down{$year}{$month}{$day}{$hour}} > 0); 


           print "$s3<a style=\"text-decoration: none\" href=\"";
           ($file =~ /.*\.(.+?)$/) && ($suffix = $1);
           SWITCH: {
             if (grep /^$suffix$/i, @IMGSFXS) { 
                print "$CGIDIR/imagewrap.nonav?$link"; last SWITCH; }
             if (grep /^$suffix$/i, @TXTSFXS) { 
                print "$CGIDIR/preformat.nonav?$link"; last SWITCH; }
             if (grep /^$suffix$/i, @HTMLSFXS) { 
                print "$CGIDIR/htmlwrap.nonav?$link"; last SWITCH; }
             print "$link";
           }
           print "\" $TARGET><font size=-2>${name}</font></a><br>\n";
           $nspace = 1;
          } elsif(!$nspace) {
           print "${s3}&nbsp;\n";
           $nspace = 1;
          }
         }
         print "$s2</td>\n";
        } else {
         print "$s2<td>&nbsp;</td>\n";
        }

       }
      }
     }
    }

    #
    # Print the javascript movie link
    #
    print "$s2<td bgcolor=$GREY>\n";
    $url =~ s/^\&//;
    if($nurl > 1) {
      print "$s3<font size=2><a href=\"$LOOPER?$url\" $TARGET>all</a></font>\n";
    } else {
      print "$s3&nbsp;\n";
    }
    print "$s2</td>\n";
    print "$s1</tr>\n";

  }

#========================================
  if($bottom_movies) {
    #
    # Print the movie links at bottom of table.
    #
    print "$s1<tr align=center>\n";
    print "$s2<td bgcolor=$GREY>Movies -&gt;</td>\n";
  
    for $year (sort keys %dates) {
     for $month (sort keys %{$dates{$year}}) {
      for $day (sort keys %{$dates{$year}{$month}}) {
       for $hour (sort keys %{$dates{$year}{$month}{$day}}) {
        print "$s2<td align=center bgcolor=$GREY>\n";
        if($#{$url_down{$year}{$month}{$day}{$hour}} > 0) { 
         ($turl = join "",@{$url_down{$year}{$month}{$day}{$hour}}) =~ s/^&//;
         print "$s3<font size=2><a href=\"$LOOPER?$turl\" $TARGET>all</a></font>\n";   
        } else {
          print "$s3&nbsp;\n";
        }
        print "$s2</td>\n";
       }
      }
     }
    }
  
    print "$s2<td>&nbsp;</td>\n";
    print "$s1</tr>\n";
  }  

#========================================
  if($nsubp > 25) {
    #
    # Print the dates again at bottom of table.
    #
    print "$s1<tr align=center>\n";
    print "$s2<td rowspan=2>\n";
    print "${s3}$top_row_name\n";
    print "$s2</td>\n";
  
    # Print the hours for which there are products for each day
    #$totcol=2;
    for $year (sort keys %dates) {
     for $month (sort keys %{$dates{$year}}) {
      for $day (sort keys %{$dates{$year}{$month}}) {
       for $hour (sort keys %{$dates{$year}{$month}{$day}}) {
        print "$s2<td  bgcolor=$HCOLOR align=center>\n";
        print "$s3<b>$hour</b>\n";
        print "$s2</td>\n";
        #$totcol++;
       }
      }
     }
    }
  
    print "$s2<td rowspan=2>\n";
    print "$s3&nbsp;\n";
    print "$s2</td>\n";
    print "$s1</tr>\n";
  
    # Print days for which there are products
    print "$s1<tr>\n";
    for $year (sort keys %dates) {
     for $month (sort keys %{$dates{$year}}) {
      for $day (sort keys %{$dates{$year}{$month}}) {
       $colspan = keys %{$dates{$year}{$month}{$day}};
       $smonth = $NUMTOMONTH[$month];
       print "$s2<td  bgcolor=$HCOLOR colspan=$colspan align=center>\n";
       print "$s3<b>$day $smonth $year</b>\n";
       print "$s2</td>\n";
      }
     }
    }
    print "$s1</tr>\n";
  }

  print "$s0</table>\n";
}

