#!/usr/bin/perl

#######################################################################
#
# runtime_status.pl
#
# Takes 30 day average start and stop time data for NCEP production
# jobs a creates various HTML documents for the web.
#
#######################################################################

$COMOUT = $ENV{'COMOUT'};
$COMOUTruntime= $ENV{'COMOUTruntime'};
$PDY = $ENV{'PDY'};
$PDYm1 = $ENV{'PDYm1'};
$RUNTFIX = $ENV{'FIXrun'};
$interProcDir = $ENV{COMOUTinter};

chop($TIME = `date -u +"%H:%M:%S"`);
chop($TIME_est = `date +"%H:%M %Z"`);
chop($DATE = `date -u +"%d %b %Y  %H:%M GMT"`);
chop($HOUR = `date -u +%H`);

$ave30day = "$COMOUT/ave30/ave30day.short";
$status_file = "$COMOUT/ave30/production_status";
$status_html = "$COMOUT/ave30/prdst_main.html";
$statusfix = "$RUNTFIX/runtime_status";
$target_time_file= "$RUNTFIX/runtime_target_times";
$completed_file = "$COMOUTruntime/$PDY.log";
$completed_filem1 = "$COMOUTruntime/$PDYm1.log";
# JY $running_file = "$COMOUT/interProcess/limbo.hold";
$running_file = "$interProcDir/limbo.hold";

$job_search = "/cgi-bin/nwprod/product_stat.pl?jobname=";

#
# Define subroutines
#
sub time2sec ($) {
   my $tm = shift;
   ($hour,$min,$sec)=split(/\:/,$tm);
   $seconds=($hour*3600)+($min*60)+$sec;
   return $seconds;
}

sub sec2time ($) {
   my $tm = shift;

   if($tm >= 86400)
      {
       $hour=int (($tm-86400)/3600);
       $min=int (($tm-86400-$hour*3600)/60);
       $sec=int($tm-86400-($hour*3600)-($min*60));
      }
   else
      {
       $hour=int $tm/3600;
       $min=int (($tm-$hour*3600)/60);
       $sec=int($tm-($hour*3600)-($min*60));
      }
   if($hour < 10){$hour="0$hour"};
   if($min < 10){$min="0$min"};
   if($sec < 10){$sec="0$sec"};
   $timestring="$hour:$min:$sec";
   return $timestring;
}

#
# Define all hash tables
#
%a30start = ();
%a30stop = ();
%a30start_sec = ();
%a30stop_sec = ();
%target_time = ();
%job30 = ();
%complete = ();
%comp_time = ();
%completem1 = ();
%compm1_time = ();
%running = ();
%start_time = ();
%suite_status = ();
%txtcolor = (0 => "#000000",
	     1 => "#3366FF",
	     2 => "#000000",
	     3 => "#000000",
	     4 => "#000000",
	     5 => "#000000",
	     6 => "#000000",
	     7 => "#FF0000",
	     8 => "#000000",
	     9 => "#000000",
	     10 => "#000000",
	     11 => "#000000",
	     12 => "#000000",
	     13 => "#FF0000",
	     14 => "#000000",
	     15 => "#FFFF00",
	     16 => "#FF6600",
	     17 => "#FF0000",
	     18 => "#FF0000",
	     19 => "#CC33CC",
	     20 => "#FF0000",
	     21 => "#FF0000",
	     22 => "#FFCC00",
	     );
%bgcolor = (0 => "#FFFFFF",
	    1 => "#FFFFFF",
	    2 => "#FFFF00",
	    3 => "#FF6600",
	    4 => "#FF0000",
	    5 => "#FF0000",
	    6 => "#CC33CC",
	    7 => "#000000",
	    8 => "#FFFF00",
	    9 => "#FF6600",
	    10 => "#FF0000",
	    11 => "#FF0000",
	    12 => "#CC33CC",
	    13 => "#000000",
	    14 => "#00CC33",
	    15 => "#00CC33",
	    16 => "#00CC33",
	    17 => "#00CC33",
	    18 => "#00CC33",
	    19 => "#00CC33",
	    20 => "#000000",
	    21 => "#663300",
	    22 => "#000000",
	    );
%flash = (0 => "",
	  1 => "",
	  2 => "",
	  3 => "",
	  4 => "",
	  5 => "<blink>",
	  6 => "<blink>",
	  7 => "<blink>",
	  8 => "",
	  9 => "",
	  10 => "",
	  11 => "<blink>",
	  12 => "<blink>",
	  13 => "<blink>",
	  14 => "",
	  15 => "",
	  16 => "",
	  17 => "",
	  18 => "<blink>",
	  19 => "<blink>",
	  20 => "<blink>",
	  21 => "<blink>",
	  22 => "",
	  );
%flashoff = (0 => "",
	     1 => "",
	     2 => "",
	     3 => "",
	     4 => "",
	     5 => "</blink>",
	     6 => "</blink>",
	     7 => "</blink>",
	     8 => "",
	     9 => "",
	     10 => "",
	     11 => "</blink>",
	     12 => "</blink>",
	     13 => "</blink>",
	     14 => "",
	     15 => "",
	     16 => "",
	     17 => "",
	     18 => "</blink>",
	     19 => "</blink>",
	     20 => "</blink>",
	     21 => "</blink>",
	     22 => "",
	     );

#
# Read in 30 day averages into hash tables
#
open( STATS, "$ave30day" ) or die "Can't open $ave30day";
while ( <STATS> ) {
    chop;
    #
    # Parse line using whitespace as delimiter
    #
    ( $jid, $avestart, $avestop ) = split( /\s+/ );

    $job30{$jid} = $jid;
    $a30start{$jid} = sec2time($avestart * 60);
    $a30stop{$jid} = sec2time($avestop * 60);
    $a30start_sec{$jid} = $avestart * 60;
    $a30stop_sec{$jid} = $avestop * 60;
}
close STATS;

#
# Read in target times into hash tables
#
open( STATS, "$target_time_file" ) or die "Can't open $target_time_file";
while ( <STATS> ) {
    chop;
    #
    # Parse line using whitespace as delimiter
    #
    ( $jid, $target_ti ) = split( /\|/ );
    
    $target_time{$jid} = $target_ti;
}
close STATS;

#
# Read in the completed jobs for PDY
#
open( STATS, "$completed_file" ) or die "Can't open $completed_file";
while ( <STATS> ) {
    chop;
    #    
    # Parse line using whitespace as delimiter
    #
    ( $jid, $startdate, $starttime, $stopdate, $stoptime ) = split( /\s+/ );
    
    $complete{$jid} = $jid;
    $comp_time{$jid} = $stoptime;
    $comp_start_time{$jid} = $starttime;
}
close STATS;

#
# Read in the completed jobs for PDYm1
#
open( STATS, "$completed_filem1" ) or die "Can't open $completed_filem1";
while ( <STATS> ) {
    chop;
    #    
    # Parse line using whitespace as delimiter
    #
    ( $jid, $startdate, $starttime, $stopdate, $stoptime ) = split( /\s+/ );
    
    $completem1{$jid} = $jid;
    $compm1_time{$jid} = $stoptime;
}
close STATS;

#
# Read in the running jobs
#
open( STATS, "$running_file" ) or die "Can't open $running_file";
while ( <STATS> ) {
    chop;
    #    
    # Parse line using whitespace as delimiter
    #
    ( $jid, $startdate, $starttime ) = split( /\s+/ );

    $running{$jid} = $jid;
    $start_time{$jid} = $starttime;
}
close STATS;

#
# Create web pages for individual suites
#
open( TEXT, ">$status_file" ) or die "Can't open $status_file";
$frame_flag=0;
$table_flag=0;
$target=0;
$canceled = "NULL";
open( FIX, "$statusfix" ) or die "Can't open $statusfix";
while ( <FIX> ) {
    chop;
    #
    # Parse line using colon as the delimiter
    #
    ( $jid, $key, $jid2, $product, $graph ) = split( /\:/ );
    
    $cond=0;
    if ( $jid eq "" ) {
	print TEXT "${key}|||||\n";
	$suite = $key;
	$suite =~ tr/ /_/;
#	$suite =~ tr/\*\*//;
	if ( $key !~ m#CYCLE# ) {
	     if ( $frame_flag != 0 ) {
		 print "</table>\n";
		 close FRAME;
		 open ( FRAME, ">$COMOUT/ave30/prdst_${suite}.html" ) or die "Can't open $COMOUT/ave30/prdst_${suite}.html";
		 select ( FRAME );
	     } else {
		 open ( FRAME, ">$COMOUT/ave30/prdst_${suite}.html" ) or die "Can't open $COMOUT/ave30/prdst_${suite}.html";
		 select ( FRAME );
		 $frame_flag=1;
	     }
	     #print "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"150 ; URL=/NCO/PMB/nwprod/prodstat/${suite}.html\">\n";
	     print "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"150\">\n";
	     print "<meta http-equiv=\"pragma\" content=\"no-cache\">\n";
	     print "<meta http-equiv=\"Expires\" content=\"Fri, 08 Dec 2000 120000 GMT\">\n";
	     print "</HEAD> \n";
	     print "<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000EE\" vlink=\"#551A8B\" alink=\"#FF0000\">\n";
	     print "<center>$key\n";
	     print "<table BORDER COLS=5 WIDTH=\"100%\" >\n";
	     print "<tr>\n";
	     print "<td><font color=\"#006600\">EVENT</font></td>\n";
	     print "<td><font color=\"#006600\">Average Start Time</font></td>\n";
	     print "<td><font color=\"#006600\">Average End Time</font></td>\n";
	     print "<td><font color=\"#006600\">STATUS</font></td>\n";
	     print "<td><font color=\"#006600\">COMMENTS</font></td></tr>\n";
	 } else {
	     $suite_status{$suite} = 99;
	 }
    } else {
	( $junk, $tracker_job ) = split( /.*\//,$jid2 );
	if ( $jid =~ m#$canceled# ) {
	     print TEXT "$key|$a30start{$jid}|$a30stop{$jid2}|CANCELED||CANCELED\n";
	     $status = "CANCELED";
	     $comment = "CANCELED";
	     $cond = 22;
	 } elsif ( $jid =~ m#gdas18# && $HOUR >= 12 && $completem1{$jid2} ne "" ) {
		   $diff = $a30start_sec{$jid} - time2sec($TIME);
		   if ( $a30start_sec{$jid} < 21600 ) { 
		       $diff = $a30start_sec{$jid} + 86400 - time2sec($TIME);
		   }
		   $status = "START DELAYED";
		   if ( $diff >= -900 ) {
		       $comment = "ON-TIME";
		       $status = "SCHEDULED";
		       $cond=1;
		   } elsif ( $diff >= -1800 ) {
		       $comment = "15-30 MIN. LATE";
		       $cond=8;
		   } elsif ( $diff >= -2700 ) {
		       $comment = "31-45 MIN. LATE";
		       $cond=9;
		   } elsif ( $diff >= -3600 ) {
		       $comment = "46-60 MIN. LATE";
		       $cond=10;
		   } elsif ( $diff >= -7200 ) { 
		       $comment = "1-2 HOURS LATE"; 
		       $cond=11;
		   } elsif ( $diff >= -10800 ) {
		       $comment = "2-3 HOURS LATE";
		       $cond=12;
		   } else { 
		       $comment = ">3  HOURS LATE";
		       $cond=13;
		   }
		   print TEXT "$key|$a30start{$jid}|$a30stop{$jid2}|$status||$comment\n";
	       } elsif ( $complete{$jid2} ne "" ) {
	     $diff = $a30stop_sec{$jid2} - time2sec($comp_time{$jid2});
	     if ( $a30stop_sec{$jid2} < 21600 && time2sec($comp_time{$jid2}) > 64800 ) { 
		 $diff = $a30stop_sec{$jid2} + 86400 - time2sec($comp_time{$jid2});
	     }
	     $status = "COMPLETE-$comp_time{$jid2}";
	     if ( $diff >= -900 ) {
		 $comment = "ON-TIME";
		 $cond=0;
	     } elsif ( $diff >= -1800 ) {
		 $comment = "15-30 MIN. LATE";
		 $cond=2;
	     } elsif ( $diff >= -2700 ) {
		 $comment = "31-45 MIN. LATE";
		 $cond=3;
	     } elsif ( $diff >= -3600 ) {
		 $comment = "46-60 MIN. LATE";
		 $cond=4;
	     } elsif ( $diff >= -7200 ) {
		 $comment = "1-2 HOURS LATE";
		 $cond=5;
	     } elsif ( $diff >= -10800 ) {
		 $comment = "2-3 HOURS LATE";
		 $cond=6;
	     } else {
		 $comment = ">3  HOURS LATE";
		 $cond=7;
	     }
	     print TEXT "$key|$a30start{$jid2}|$a30stop{$jid2}|COMPLETE|$comp_time{$jid2}|$comment\n";
	 } elsif ( $jid =~ m#prod12# && $HOUR < 12 && $completem1{$jid2} ne "" ) {
		   $diff = $a30stop_sec{$jid2} - time2sec($compm1_time{$jid2});
		   if ( $a30stop_sec{$jid2} < 21600 && time2sec($compm1_time{$jid2}) > 64800 ) { 
		       $diff = $a30stop_sec{$jid2} + 86400 - time2sec($compm1_time{$jid2});
		   }
		   $status = "COMPLETE-$compm1_time{$jid2}";
		   if ( $diff >= -900 ) {
		       $comment = "ON-TIME";
		       $cond=0;
		   } elsif ( $diff >= -1800 ) {
		       $comment = "15-30 MIN. LATE";
		       $cond=2;
		   } elsif ( $diff >= -2700 ) {
		       $comment = "31-45 MIN. LATE";
		       $cond=3;
		   } elsif ( $diff >= -3600 ) {
		       $comment = "46-60 MIN. LATE";
		       $cond=4;
		   } elsif ( $diff >= -7200 ) {
		       $comment = "1-2 HOURS LATE";
		       $cond=5;
		   } elsif ( $diff >= -10800 ) {
		       $comment = "2-3 HOURS LATE";
		       $cond=6;
		   } else {
		       $comment = ">3  HOURS LATE";
		       $cond=7;
		   }
		   print TEXT "$key|$a30start{$jid}|$a30stop{$jid2}|COMPLETE|$compm1_time{$jid2}|$comment\n";
	       } elsif ( $running{$jid} ne "" ) {
		   #
		   # If this is the first running job in the list of all jobs
		   # then place a HTML target on the page.
		   #
		   if ( $target == 0 ) {
		       $target=1;
		       print "<a NAME=\"TARGET\"></a>\n";
		   }
		   $diff = $a30start_sec{$jid} - time2sec($start_time{$jid});
		   $end_lag = $a30stop_sec{$jid2} - time2sec($TIME) - $diff;
		  $status = "RUNNING-$start_time{$jid}";
		  if ( $diff >= -900 ) {
		      $comment = "ON-TIME";
		      $cond=14;
		  } elsif ( $diff >= -1800 ) {
		      $comment = "15-30 MIN. LATE";
		      $cond=15;
		  } elsif ( $diff >= -2700 ) {
		      $comment = "31-45 MIN. LATE";
		      $cond=16;
		  } elsif ( $diff >= -3600 ) {
		      $comment = "46-60 MIN. LATE";
		      $cond=17;
		  } elsif ( $diff >= -7200 ) { 
		      $comment = "1-2 HOURS LATE"; 
		      $cond=18;
		  } elsif ( $diff >= -10800 ) {
		      $comment = "2-3 HOURS LATE";
		      $cond=19;
		  } else { 
		      $comment = ">3  HOURS LATE";
		      $cond=20;
		  }
                  if ( $end_lag <= -900 ) {
		      $comment = "JOB RUNNING TOO LONG";
		      $cond=21;
		  }
		   if ( $end_lag <= -7200 ) {
		       $status = "CANCELED";
		       $comment = "CANCELED";
		       $cond = 22;
		   }
	    print TEXT "$key|$a30start{$jid}|$a30stop{$jid2}|RUNNING|$start_time{$jid}|$comment\n";
	       } elsif ( $complete{$jid} ne "" && $complete{$jid2} eq "" ) {
		   #
		   # This is for the case where the job used to signal the
		   # start is different than the job that signals the 
		   # completeion
		   #

		   #
		   # If this is the first running job in the list of all jobs
		   # then place a HTML target on the page.
		   #
		   if ( $target == 0 ) {
		       $target=1;
		       print "<a NAME=\"TARGET\"></a>\n";
		   }
		   $diff = $a30start_sec{$jid} - time2sec($comp_start_time{$jid});
		  $end_lag = $a30stop_sec{$jid2} - time2sec($TIME) - $diff;
		  $status = "RUNNING-$comp_start_time{$jid}";
		  if ( $diff >= -900 ) {
		      $comment = "ON-TIME";
		      $cond=14;
		  } elsif ( $diff >= -1800 ) {
		      $comment = "15-30 MIN. LATE";
		      $cond=15;
		  } elsif ( $diff >= -2700 ) {
		      $comment = "31-45 MIN. LATE";
		      $cond=16;
		  } elsif ( $diff >= -3600 ) {
		      $comment = "46-60 MIN. LATE";
		      $cond=17;
		  } elsif ( $diff >= -7200 ) { 
		      $comment = "1-2 HOURS LATE"; 
		      $cond=18;
		  } elsif ( $diff >= -10800 ) {
		      $comment = "2-3 HOURS LATE";
		      $cond=19;
		  } else { 
		      $comment = ">3  HOURS LATE";
		      $cond=20;
		  }
                  if ( $end_lag <= -900 ) {
		      $comment = "JOB RUNNING TOO LONG";
		      $cond=21;
		  }
		   if ( $end_lag <= -7200 ) {
		       $status = "CANCELED";
		       $comment = "CANCELED";
		       $cond = 22;
		   }
	    print TEXT "$key|$a30start{$jid}|$a30stop{$jid2}|RUNNING|$comp_start_time{$jid}|$comment\n";
	} else {
	    $diff = $a30start_sec{$jid} - time2sec($TIME);
	    $status = "START DELAYED";
	    if ( $diff >= -900 ) {
		$comment = "ON-TIME";
		$status = "SCHEDULED";
		$cond=1;
	    } elsif ( $diff >= -1800 ) {
		$comment = "15-30 MIN. LATE";
		$cond=8;
	    } elsif ( $diff >= -2700 ) {
		$comment = "31-45 MIN. LATE";
		$cond=9;
	    } elsif ( $diff >= -3600 ) {
		$comment = "46-60 MIN. LATE";
		$cond=10;
            } elsif ( $diff >= -7200 ) { 
		$comment = "1-2 HOURS LATE"; 
		$cond=11;
	    } elsif ( $diff >= -10800 ) {
		$comment = "2-3 HOURS LATE";
		$cond=12;
	    } else { 
		$comment = ">3  HOURS LATE";
		$cond=13;
	    }
	    print TEXT "$key|$a30start{$jid}|$a30stop{$jid2}|$status||$comment\n";
	}
	$suite_status{$suite} = $suite_status{$suite} <= $cond ? $cond : $suite_status{$suite};
	print "<tr BGCOLOR=\"$bgcolor{$cond}\">\n";
	if ( $product ne "NO" && $graph ne "NO" ) {
	    print "<td><a href=\"$job_search$tracker_job\"><img SRC=\"pics/mini-doc1.gif\"></a><img SRC=\"pics/mini-graph.gif\"><font color=\"$txtcolor\">$key</font></td>\n";
	} elsif ( $graph ne "NO" ) {
	    print "<td><img SRC=\"pics/mini-graph.gif\"><font color=\"$txtcolor\">$key</font></td>\n";
	} elsif ( $product ne "NO" ) {
	    print "<td><a href=\"$job_search$tracker_job\"><img SRC=\"pics/mini-doc1.gif\"></a><font color=\"$txtcolor\">$key</font></td>\n";
	} else {
	    print "<td><font color=\"$txtcolor{$cond}\">$key</font></td>\n";
	}
	print "<td><font color=\"$txtcolor{$cond}\">$a30start{$jid}</font></td>\n";
	print "<td><font color=\"$txtcolor{$cond}\">$a30stop{$jid2}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$target_time{$jid2}</font></td>\n";
	print "<td>$flash{$cond}<font color=\"$txtcolor{$cond}\">$status</font>$flashoff{$cond}</td>\n";
	print "<td>$flash{$cond}<font color=\"$txtcolor{$cond}\">$comment</font>$flashoff{$cond}</td></tr>\n";
    }
}
close FIX;

print "</table></center></body>\n";

#
# Create the ledgend html document
#
open ( LEGEND, ">$COMOUT/ave30/prdst_legend.html" ) or die "Can't open $COMOUT/ave30/prdst_legend.html";
select ( LEGEND );
print "<meta http-equiv=\"pragma\" content=\"no-cache\">\n";
print "<meta http-equiv=\"Expires\" content=\"Fri, 08 Dec 2000 120000 GMT\">\n";
print "</HEAD> \n";
print "<body text=\"#000000\" bgcolor=\"#FFFFFF\">\n";
print "<br><a NAME=\"LEGEND\"></a>\n";
print "<table border WIDTH=\"100%\"><th colspan=\"4\">LEGEND</th>\n";
print "<tr><td colspan=\"4\"><center>When a job's status is \"RUNNING\" the time next to it is today's start time.</center></td></tr>\n";
print "<tr><td colspan=\"4\"><center>When a job's status is \"COMPLETE\" the time next to it is today's end time.<br></td></tr>\n";
print "<tr><td BGCOLOR=\"#FFFFFF\"><font color=\"#3366FF\">SCHEDULED</font></td>\n";
print "<td>NOT RUNNING: NO FORESEEN DELAYS</td>\n";
print "<td BGCOLOR=\"#000000\"><font color=\"#FFCC00\">CANCELED</font></td>\n";
print "<td>HAS BEEN CANCELED</td></tr>\n";
print "<tr><td>COMPLETE</td>\n";
print "<td>FINISHED ON TIME</td>\n";
print "<td BGCOLOR=\"#00CC33\">RUNNING</td>\n";
print "<td>RUNNING   ON TIME</td></tr>\n";
print "<tr><td BGCOLOR=\"#FFFF00\">COMPLETE/START DELAYED</td>\n";
print "<td>FINISHED/DELAYED 15-30 MINUTES</td>\n";
print "<td BGCOLOR=\"#00CC33\"><font color=\"#FFFF00\">RUNNING</font></td>\n";
print "<td>RUNNING   15-30 MINUTES LATE</td></tr>\n";
print "<tr><td BGCOLOR=\"#FF9900\">COMPLETE/START DELAYED</td>\n";
print "<td>FINISHED/DELAYED 31-45 MINUTES</td>\n";
print "<td BGCOLOR=\"#00CC33\"><font color=\"#FF9900\">RUNNING</font></td>\n";
print "<td>RUNNING   31-45 MINUTES LATE</td></tr>\n";
print "<tr><td BGCOLOR=\"#FF0000\">COMPLETE/START DELAYED</td>\n";
print "<td>FINISHED/DELAYED 46-60 MINUTES</td>\n";
print "<td BGCOLOR=\"#00CC33\"><font color=\"#FF0000\">RUNNING</font></td>\n";
print "<td>RUNNING   46-60 MINUTES LATE</td></tr>\n";
print "<tr><td BGCOLOR=\"#FF0000\"><blink>COMPLETE/START DELAYED</blink></td>\n";
print "<td>FINISHED/DELAYED 1-2 HOURS</td>\n";
print "<td BGCOLOR=\"#00CC33\"><font color=\"#FF0000\"><blink>RUNNING</blink></font></td>\n";
print "<td>RUNNING   1-2 HOURS LATE</td></tr>\n";
print "<tr><td BGCOLOR=\"#CC33CC\"><blink>COMPLETE/START DELAYED</blink></td>\n";
print "<td>FINISHED/DELAYED 2-3 HOURS</td>\n";
print "<td BGCOLOR=\"#00CC33\"><font color=\"#CC33CC\"><blink>RUNNING</blink></font></td>\n";
print "<td>RUNNING   2-3  HOURS LATE</td></tr>\n";
print "<tr><td BGCOLOR=\"#000000\"><font color=\"#FF0000\"><blink>COMPLETE/START DELAYED</blink></font></td>\n";
print "<td>FINISHED/DELAYED MORE THAN 3 HOURS</td>\n";
print "<td BGCOLOR=\"#000000\"><font color=\"#FF0000\"><blink>RUNNING</blink></font></td>\n";
print "<td>RUNNING   MORE THAN 3 HOURS LATE</td></tr>\n";
print "</table>\n";
print "<br><center>[The average start and stop times listed on this page are based upon a 30-day running average.]</center></body>\n";
close LEGEND;

#
# Create the main page
#
open( HTML, ">$status_html" ) or die "Can't open $status_html";
select ( HTML );
print "<HTML>\n";
print "<HEAD>\n";
#print "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"150 ; URL=/NCO/PMB/nwprod/prodstat/production_status.html\">\n";
print "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"150\">\n";
print "<meta http-equiv=\"pragma\" content=\"no-cache\">\n";
print "<meta http-equiv=\"Expires\" content=\"Fri, 08 Dec 2000 120000 GMT\">\n";
print "<TITLE> Current Status of NCEP Model Production </TITLE>\n";
print "<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#0000EE\" vlink=\"#551A8B\" alink=\"#FF0000\">\n";
print "<CENTER>\n";
print "<table WIDTH=\"100%\"><tr><td>\n";
print "<CENTER><IMG SRC=\"pics/ncep_80.gif\"></center></td>\n";
print "<td><h3><CENTER>CURRENT STATUS OF THE NCEP PRODUCTION SUITE<br>\n";
###BAG print "<td><h3><CENTER>CURRENT STATUS OF THE NCEP PARALLEL PRODUCTION SUITE<br>\n";
print "VALID AT  $DATE ($TIME_est)</center></h3></td></tr></table>\n";
print "<h4>GIF images of NCEP's forecast models can be viewed <A HREF=\"http://mag.ncep.noaa.gov\" target=\"top\">here</A>.\n";
print "<table BORDER COLS=5 WIDTH=\"100%\" ><tr><td><center>CURRENT STATUS OF COMMS TO NCEP WCOSS</center></td><td BGCOLOR=\"00CC33\"><center><font color=\"000000\">UP</font></center></td></tr></table>\n";

$table_flag=0;
open( FIX, "$statusfix" ) or die "Can't open $statusfix";
while ( <FIX> ) {
    chop;
    #
    # Parse line using colon as the delimiter
    #
    ( $jid, $suitename, $jid2, $product, $graph ) = split( /\:/ );
    
    if ( $jid eq "" ) {
	$key=$suitename;
	$key =~ tr/ /_/;
	$suitename =~ s/GLOBAL ENSEMBLE/GFS ENS/;
	$suitename =~ s/MODELS//;
	if ( $suite_status{$key} == 99 ) {
	    if ( $table_flag !=0 ) {
		while ( $cnt % 4 != 0 ) { 
		    print "<td><center>.</center></td>\n"; 
		    $cnt++;
		}
		print "</table><table border width=\"100%\"><th colspan=\"4\"><b>$suitename</b></th>\n";
		$cnt=0;    
	    } else {
		$table_flag=1;
		print "<table border width=\"100%\"><th colspan=\"4\">$suitename</th>\n";
		$cnt=0;
	    }
	} else {
	    if ( $cnt % 4 == 0 ) { print "<tr>"; }
	    print "<td bgcolor=\"$bgcolor{$suite_status{$key}}\">";
	    print "<font color=\"$txtcolor{$suite_status{$key}}\">";
	    print "$blink{$suite_status{$key}}<center><a href=\"prdst_${key}.html\" target=\"frame2\" style=\"color: $txtcolor{$suite_status{$key}}\">$suitename</a>";
	    print "$blinkoff{$suite_status{$key}}</font></center></td>";
	    if ( $cnt % 4 == 3 ) { print "</tr>\n"; }
	    $cnt++;
	}
    }
}
while ( $cnt % 4 != 0 ) { 
    print "<td><center>.</center></td>\n"; 
    $cnt++;
}
print "</table>\n";

print "<br><TABLE COLS=2 WIDTH=\"100%\"><tr><td><center>";
print "<A HREF=\"http://tgftp.nws.noaa.gov/tgstatus/\" target=\"top\">NCEP/NWS STATUS MESSAGES</A></center></td>\n";
print "<td><center><a href=\"prdst_legend.html\" target=\"frame2\">LEGEND</a></center></td></tr></table>\n";

print "<hr>For questions or comments related to this website, send mail <A HREF=\"mailto:sdm\@noaa.gov\">here</A>.\n";
print "<br>Subscribe to the NCEP Production Suite change notification mailing list <A HREF=\"https://lstsrv.ncep.noaa.gov/mailman/listinfo/ncep.list.rfcmemo\"  target=\"top\">here</A>.<hr>\n";
print "<hr><TABLE BORDER=2 CELLPADDING=2 CELLSPACING=2><TR><TD><FONT SIZE=\"-1\"><A HREF=\"http://www.nws.noaa.gov/disclaimer.html\"  target=\"top\"><B><EM>National Weather Service Web Page Disclaimer</EM></B></A></font></TD></TR></table></body>\n";

close FRAME;
close FIX;
close TEXT;
close HTML;
