#!/usr/bin/perl

######################################################
#
# This script splits the hrly-prcp-current file
# into daily files and then outputs the list of dates
# which were effected.
#
######################################################

###########################################
# 
# Get environmental variables 
#
###########################################
$NET=$ENV{'NET'};
$DATATYPE=$ENV{'DATATYPE'};
$envir=$ENV{'envir'};
$PDY=$ENV{'PDY'};
$PDYm5=$ENV{'PDYm5'};
$SENDDBN=$ENV{'SENDDBN'};
$DBNROOT=$ENV{'DBNROOT'};
$job=$ENV{'job'};

###########################################
#
# Set Input and Output Names
#
###########################################
$input_file=$ARGV[0];
$com="/com/${NET}/${envir}/${DATATYPE}.";

###########################################
# 
# Initialize Hash Tables to Store File Information
#
###########################################
%data_line_prev = ();
%data_line_new = ();
%date_list = ();

###########################################
#
# Open hrly-prcp-current file and store file
# information between PDY and PDYm5 days old
#
###########################################
open(INPUT,$input_file);

while(<INPUT>)
   {
    chop;
    ($year,$month,$day,$remainder)=split(/\s+/);
    if($month < 10)
       {
        $month="0$month";
       }
    if($day < 10)
       {
        $day="0$day";
       }
    $date="$year$month$day";
    if($date >= $PDYm5 && $date <= $PDY)
       {
        $data_line_new{$_}=$_;
        $date_list{$date}=$date;
       }
   }
close(INPUT);

###########################################
#
# Loop through PDY and PDYm5 days of info
#
###########################################
foreach $datekey (sort keys %date_list)
   {
    %date_line_prev = ();
    $date_alert=NO;

    $ext=substr($datekey,4,4);
    $output_file="$com$datekey/hrly.prcp.day.$ext";

    ###########################################
    #
    # Read Previously Stored File Info for a day
    #
    ###########################################
    open(INPUT,"$output_file");
    while(<INPUT>)
       {
        chop;
        $data_line_prev{$_}=YES;
       }
    close(INPUT);

    ###########################################
    #
    # Loop through hrly-prcp-current file info
    # and append onto that days file any none
    # duplicate data.
    #
    ###########################################
    mkdir "$com$datekey",0775;
    open(OUTPUT,">>$output_file");
    select(OUTPUT);

    foreach $key (sort keys %data_line_new)
       {
        ($year,$month,$day,$remainder)=split(/\s+/,$key);
        if($month < 10)
           {
            $month="0$month";
           }
        if($day < 10)
           {
            $day="0$day";
           }
        $date="$year$month$day";

        if($datekey == $date)
           {
            if($data_line_prev{$key} !~ /YES/)
               {
                print "$key\n";
                $date_alert=YES;
               }
           }
       }
    select(STDOUT);
    close(OUTPUT);

    if($date_alert =~ /YES/ && $SENDDBN =~ /YES/)
       {
        system("$DBNROOT/bin/dbn_alert DATA HRLY_PRCP $job $output_file");
       }
   }

exit;
