#!/usr/bin/perl

use Time::Local;

my $nargs = @ARGV;

if ($nargs < 2)  {
   print "usage: $0: \"date (YYYYMMDD)\" \"start hour\" \n";
   exit 1;
}

# Define variables 

my $day_start_epoch=0;
my $delay=0;
my $hrinc=1;
my $mn=10;
my $mninc=15;
my $modelrun="";
my $ntcfile="";
my $report_day="";
my $report_hr="";
my $report_mn="";
my $report_month="";
my $report_sec="";
my $report_zone="";
my $report_yr="";
my $sec="00";
my $starthr="$ARGV[1]";
my $total_products=0;
my $YearMonthDay=$ARGV[0];

my $hiresw_target_file="/nw$ENV{envir}/fix/dashboard_ntcfile_hiresw.targets";
my $hr=$starthr;
my $maxhr=$starthr+12;
my $ntc_log="$ENV{DATA}/NTC.$YearMonthDay";
my $output_directory="/com/output/prod/$YearMonthDay";
my $target_file="/nw$ENV{envir}/fix/dashboard_ntcfile.targets";


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

# Define subroutines

sub add_to_target;
sub PDY2epoch;
sub string2epoch;
sub time2sec;

# Determine what HIRESW jobs ran on the CCS by looking for the existence of the job output file.
# If the job output is found add the corresponding target times to the hashes

opendir OUTPUT, "$output_directory" or die "Could not open $output_directory";
my @output_dir = readdir OUTPUT;
   
closedir OUTPUT;

foreach my $key (@output_dir)
 {  
   SWITCH: {
       if ($key =~ /hiresw_arw_ctl_forecast_00/) {add_to_target('t00z.awpreg_eastarw'); last SWITCH; }
       if ($key =~ /hiresw_arw_ctl_forecast_06/) {add_to_target('t06z.awpreg_westarw'); last SWITCH; }
       if ($key =~ /hiresw_arw_ctl_forecast_12/) {add_to_target('t12z.awpreg_eastarw'); last SWITCH; }
       if ($key =~ /hiresw_arw_ctl_forecast_18/) {add_to_target('t18z.awpreg_akarw'); last SWITCH; }
       if ($key =~ /hiresw_nmm_ctl_forecast_00/) {add_to_target('t00z.awpreg_eastnmm'); last SWITCH; }
       if ($key =~ /hiresw_nmm_ctl_forecast_06/) {add_to_target('t06z.awpreg_westnmm'); last SWITCH; }
       if ($key =~ /hiresw_nmm_ctl_forecast_12/) {add_to_target('t12z.awpreg_eastnmm'); last SWITCH; }
       if ($key =~ /hiresw_nmm_ctl_forecast_18/) {add_to_target('t18z.awpreg_aknmm'); last SWITCH; }
       if ($key =~ /hiresw_hiarw_ctl_forecast_00/) {add_to_target('t00z.awpreg_hiarw'); last SWITCH; }
       if ($key =~ /hiresw_hiarw_ctl_forecast_12/) {add_to_target('t12z.awpreg_hiarw'); last SWITCH; }
       if ($key =~ /hiresw_hinmm_ctl_forecast_00/) {add_to_target('t00z.awpreg_hinmm'); last SWITCH; }
       if ($key =~ /hiresw_hinmm_ctl_forecast_12/) {add_to_target('t12z.awpreg_hinmm'); last SWITCH; }
       if ($key =~ /hiresw_prarw_ctl_forecast_06/) {add_to_target('t06z.awpreg_prarw'); last SWITCH; }
       if ($key =~ /hiresw_prarw_ctl_forecast_18/) {add_to_target('t18z.awpreg_prarw'); last SWITCH; }
       if ($key =~ /hiresw_prnmm_ctl_forecast_06/) {add_to_target('t06z.awpreg_prnmm'); last SWITCH; }
       if ($key =~ /hiresw_prnmm_ctl_forecast_18/) {add_to_target('t18z.awpreg_prnmm'); last SWITCH; }
   }
}

# Open target time file and build several hashes

open(TARGET,$target_file);

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

# Open NTC log file and build hashes using the actual times the files were queued

open(NTC,$ntc_log);
while(<NTC>)
   {
    chop;

    # Grab date information from top of NTC status log

    if($_ =~ /Report Generated at/)
       {
        (my $j1, $report_yr, $report_month, $report_day, $report_hr, $report_mn, $report_sec, $report_zone)=split(/.*(\d\d\d\d).(\d\d).(\d\d) (\d\d):(\d\d):(\d\d).*/,$_);
        my $PDY="$report_yr$report_month$report_day";
        $day_start_epoch=PDY2epoch($PDY);
       }

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

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

    # change any file with an eight character date string in its name to 'yyyymmdd'

    $ntcfile=~s/\d\d\d\d\d\d\d\d/yyyymmdd/;

    # Logic test to always take the most recent time, this
    # takes care of times when files are sent multiple times
    # in a day

    my $epoch_of_file=string2epoch($n_time)-$day_start_epoch;

    if ( !defined $ncep_post_sec{$ntcfile} ) {
       $ncep_post_sec{$ntcfile}=$epoch_of_file;
    }
    elsif ($epoch_of_file > $ncep_post_sec{$ntcfile}) {
       $ncep_post_sec{$ntcfile}=$epoch_of_file;
    }
       
   }
close NTC;

# Determine statistics by comparing target hashes with actual queue time hashes

while($hr < $maxhr && $hr < 24 )
   {
    while($mn < 60 )
       {
        my $report_timestring="$report_yr\-$report_month\-$report_day\-$hr\-$mn\-$sec";
        my $report_time_sec=time2sec($report_timestring);
        my $report_time_epoch=string2epoch($report_timestring);
        my $report_excel_string="$report_month/$report_day/$report_yr</TD><TD>$hr:$mn";

        my $delay_thres=15;
        my $delay_less_thres=0;
        my $delay_less_10=0;
        my $delay_less_5=0;
        my $delay_n5=0;
        my $delay_n10=0;
        my $delay_n15=0;
        my $delay_over_thres=0;
        my $percent_15=0;
        my $percent_10=0;
        my $percent_5=0;
        my $percent_n5=0;
        my $percent_n10=0;
        my $percent_n15=0;
        my $target=0; 
        my $target_mthres=0;
        my $total=0;

        foreach $ntcfile (keys %products)
           {
            $target=$ncep_avg{$ntcfile};

            if(($target >= 0 && $target <= $report_time_sec))
               {
                ############################################
                # 5 Minute Threshold
                ############################################
                $target_mthres=$ncep_avg{$ntcfile}+$delay_thres*60;
                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;
                   }
                if($target_mthres <= $report_time_sec || $ncep_post_sec{$ntcfile}>0)
                   {
                    $delay=$ncep_post_sec{$ntcfile}-$ncep_avg{$ntcfile};

                    if($delay>=(-5*60))
                       {
                        $delay_n5=$delay_n5+$products{$ntcfile};
                       }
                    if($delay>=(-10*60))
                       {
                        $delay_n10=$delay_n10+$products{$ntcfile};
                       }
                    if($delay>=(-15*60))
                       {
                        $delay_n15=$delay_n15+$products{$ntcfile};
                       }
                    if($delay<=5*60)
                       {
                        $delay_less_5=$delay_less_5+$products{$ntcfile};
                       }
                    if($delay<=10*60)
                       {
                        $delay_less_10=$delay_less_10+$products{$ntcfile};
                       }
                    if($delay<=$delay_thres*60)
                       {
                        $delay_less_thres=$delay_less_thres+$products{$ntcfile};
                       }
                    if($delay>$delay_thres*60)
                       {
                        $delay_over_thres=$delay_over_thres+$products{$ntcfile};
                       }
                    $total=$products{$ntcfile}+$total;
                   }
               }
           }

        if($total>0)
           {
            $percent_15=($delay_less_thres)/$total*100;
            $percent_10=($delay_less_10)/$total*100;
            $percent_5=($delay_less_5)/$total*100;
            $percent_n5=($delay_n5)/$total*100;
            $percent_n10=($delay_n10)/$total*100;
            $percent_n15=($delay_n15)/$total*100;
           }
        else
           {
            $percent_15=100;
            $percent_10=100;
            $percent_5=100;
            $percent_n5=100;
            $percent_n10=100;
            $percent_n15=100;
           }
        printf "<TR><TD>%s</TD><TD>%3.6f</TD><TD>%3.6f</TD><TD>%3.6f</TD></TR>\n",$report_excel_string,$percent_15,$percent_10,$percent_5;

        $mn=$mn+$mninc;
       }
    $mn=10;
    $hr=$hr+$hrinc;
    if($hr<10){$hr="0$hr"};
   }

sub PDY2epoch ($) {

   # Accept the PDY date and return the equivalent epoch value

   my $PDY = shift;

   my $sec=0;
   my $min=0;
   my $hour=0;
   (my $j, my $year, my $mon, my $dy)=split(/(\d\d\d\d)(\d\d)(\d\d)/,$PDY);
   $mon=$mon-1;
   my $timestring=timegm($sec,$min,$hour,$dy,$mon,$year);
   return $timestring;
}

sub string2epoch ($) {

   # Accept NTC date string and return the equivalent epoch value

   my $string = shift;

   my $sec=0;
   my $min=0;
   my $hour=0;

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

sub time2sec ($) {

   # Accept the NTC date string and return the equivalent value in seconds

   my $timestring = shift;

   my $sec=0;
   my $min=0;
   my $hour=0;
   my $seconds=0;
   (my $j, my $year, my $mon, my $dy, $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;
}

sub add_to_target ($) {

   # Add the baseline values for any hiresw jobs that ran to the appropriate hashes.

   my $NCEPName = shift;
   open(HIRESWTARGET,$hiresw_target_file);

   while(<HIRESWTARGET>)
   {
    chop;
    (my $ntcfile, my $fmodel, my $fcyc, my $fproducts, my $n_avg, my $t_avg)=split(/\,/,$_);
    if ($ntcfile =~ $NCEPName){
      $modelrun="$fmodel$fcyc";
      $model{$ntcfile}=$fmodel;
      $cyc{$ntcfile}=$fcyc;
      $products{$ntcfile}=$fproducts;
      $ncep_avg{$ntcfile}=$n_avg;
      $total_products=$total_products+$fproducts;
      if (!defined($model_total_products{$modelrun})) {$model_total_products{$modelrun}=0;}
      $model_total_products{$modelrun}=$model_total_products{$modelrun}+$fproducts;
    }
   }
 close HIRESWTARGET;
}

