#!/usr/bin/perl

use Time::Local;

sub PDY2epoch ($) {
   my $PDY = shift;

   $sec=0;
   $min=0;
   $hour=0;
   ($j,$year,$mon,$day)=split(/(\d\d\d\d)(\d\d)(\d\d)/,$PDY);
   $mon=$mon-1;
   $timestring=timegm($sec,$min,$hour,$day,$mon,$year);
   return $timestring;
}
sub string2epoch ($) {
   my $string = shift;

   ($j,$year,$mon,$day,$hour,$min,$sec)=split(/(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)/,$string);
   $mon=$mon-1;
   $timestring=timegm($sec,$min,$hour,$day,$mon,$year);
   return $timestring;
}

sub time2sec ($) {
   my $timestring = shift;

   ($j,$year,$mon,$day,$hour,$min,$sec)=split(/(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)-(\d\d)/,$timestring);
   $seconds=$hour*3600+$min*60+$sec;
   return $seconds;
}

$target_file="/nw$ENV{envir}/fix/dashboard_ntcfile.targets";

%model=();
%cyc=();
%products=();
%ncep_avg=();
%model_total_products=();

%ncep_post_sec=();

$total_products=0;
open(TARGET,$target_file);
while(<TARGET>)
   {
    chop;
    ($ntcfile,$fmodel,$fcyc,$fproducts,$n_avg,$t_avg)=split(/\,/,$_);    
    $modelrun="$fmodel$fcyc";
    $model{$ntcfile}=$fmodel;
    $cyc{$ntcfile}=$fcyc;
    $products{$ntcfile}=$fproducts;
    $ncep_avg{$ntcfile}=$n_avg;
    $total_products=$total_products+$fproducts;
    $model_total_products{$modelrun}=$model_total_products{$modelrun}+$fproducts;
   }
close TARGET;

$ntc_log="$ENV{DATA}/NTC.$ARGV[0]";
open(NTC,$ntc_log);
while(<NTC>)
   {
    chop;
    if($_ =~ /Report Generated at/)
       {
        ($j1,$yr,$month,$day,$hr,$mn,$sec,$zone)=split(/.*(\d\d\d\d).(\d\d).(\d\d) (\d\d):(\d\d):(\d\d).*/,$_);
        $PDY="$yr$month$day";
        $report_excel_string="$month/$day/$yr";
        $report_timestring="$yr\-$month\-$day\-$hr\-$mn\-$sec";

        $day_start_epoch=PDY2epoch($PDY);
        $report_time_sec=time2sec($report_timestring);
        $report_time_epoch=string2epoch($report_timestring);
       }

    if($_!~/  NTC   NTC      NTC/){next;}

    ($j1,$j2,$j3,$j4,$n_time,$t_time,$delay,$ntcfile)=split(/\s+/,$_);
    $ntcfile=~s/\d\d\d\d\d\d\d\d/yyyymmdd/;

    $ncep_post_sec{$ntcfile}=string2epoch($n_time)-$day_start_epoch;
   }
close NTC;

$delay_thres=5;
$delay_over_thres=0;
$delay_0_to_thres=0;
$delay_less_0=0;
$total=0;

%model_delay_over_thres=();
%model_delay_0_to_thres=();
%model_delay_less_0=();
%model_total=();

print "<HTML>\n";
print "<TABLE>\n";
print "<TR><TD>Date</TD><TD>Model</TD><TD>Num Products</TD><TD>Filename</TD><TD>Delay Min</TD></TR>\n";
foreach $ntcfile (keys %products)
   {
    $target_mthres=$ncep_avg{$ntcfile}+$delay_thres*60;
    $target=$ncep_avg{$ntcfile};

    $modelrun="$model{$ntcfile}$cyc{$ntcfile}";
    if(($target >= 0 && $target <= $report_time_sec))
#    if(($target >= 0 && $target <= $report_time_sec)||$ncep_post_sec{$ntcfile}>=0)
       {
        ############################################
        # 5 Minute Threshold
        ############################################
        if($target_mthres <= $report_time_sec && ($ncep_post_sec{$ntcfile}<0 || $ncep_post_sec{$ntcfile} eq ""))
           {
            $delay_over_thres=$products{$ntcfile}+$delay_over_thres;
            $total=$products{$ntcfile}+$total;

            $model_delay_over_thres{$modelrun}=$products{$ntcfile}+$model_delay_over_thres{$modelrun};
            $model_total{$modelrun}=$products{$ntcfile}+$model_total{$modelrun};
            $delay_min="MISSING";
print "<TR><TD>$report_excel_string</TD><TD>$model{$ntcfile}$cyc{$ntcfile}</TD><TD>$products{$ntcfile}</TD><TD>$ntcfile</TD><TD>$delay_min</TD></TR>\n";
           }
        if(($target_mthres <= $report_time_sec && $ncep_post_sec{$ntcfile}>0) && ($ncep_post_sec{$ntcfile} ne ""))
           {
            $delay=$ncep_post_sec{$ntcfile}-$ncep_avg{$ntcfile};

            if($delay<0)
               {
                $delay_less_0=$delay_less_0+$products{$ntcfile};
                $model_delay_less_0{$modelrun}=$model_delay_less_0{$modelrun}+$products{$ntcfile};
               }
            if($delay>=0&&$delay<=$delay_thres*60)
               {
                $delay_0_to_thres=$delay_0_to_thres+$products{$ntcfile};
                $model_delay_0_to_thres{$modelrun}=$model_delay_0_to_thres{$modelrun}+$products{$ntcfile};
               }
            if($delay>$delay_thres*60)
               {
                $delay_over_thres=$delay_over_thres+$products{$ntcfile};
                $model_delay_over_thres{$modelrun}=$model_delay_over_thres{$modelrun}+$products{$ntcfile};
                $delay_min=$delay/60;
print "<TR><TD>$report_excel_string</TD><TD>$model{$ntcfile}$cyc{$ntcfile}</TD><TD>$products{$ntcfile}</TD><TD>$ntcfile</TD><TD>$delay_min</TD></TR>\n";

#print "$ntcfile $ncep_post_sec{$ntcfile} $ncep_avg{$ntcfile} $delay_min\n";
#print "$model{$ntcfile}$cyc{$ntcfile} $products{$ntcfile} $ntcfile $ncep_post_sec{$ntcfile} $ncep_avg{$ntcfile} $delay_min\n";
               }
            if($delay<(-1*$delay_thres)*60)
               {
                $delay_min=$delay/60;
                print "<TR><TD>$report_excel_string</TD><TD>$model{$ntcfile}$cyc{$ntcfile}</TD><TD>$products{$ntcfile}</TD><TD>$ntcfile</TD><TD>$delay_min</TD></TR>\n";
               }
            $total=$products{$ntcfile}+$total;
            $model_total{$modelrun}=$products{$ntcfile}+$model_total{$modelrun};
           }
       }
   }
print "</TABLE>\n";

if($total>0)
   {
    $percent=($delay_less_0+$delay_0_to_thres)/$total*100;
    $total_left=$total_products-$total;
   }
else
   {
    $percent=100;
   }

print "<TABLE>\n";
printf "<TR><TD>Date</TD><TD>Model</TD><TD>Percent >5 Minutes</TD></TR>\n",$report_excel_string,$percent;
printf "<TR><TD>%s</TD><TD>TOTAL</TD><TD>%3.4f</TD></TR>\n",$report_excel_string,$percent;

foreach $modelrun (sort keys %model_total)
   {
    $percent=($model_delay_less_0{$modelrun}+$model_delay_0_to_thres{$modelrun})/$model_total{$modelrun}*100;
    $total_left=$model_total_products{$modelrun}-$model_total{$modelrun};
    printf "<TR><TD>%s</TD><TD>%s</TD><TD>%3.4f</TD></TR>\n",$report_excel_string,$modelrun,$percent;
   }
print "</TABLE>\n";
print "</HTML>\n";
