#!/usr/bin/perl
### monitoring_smoke_east.pl
### monitor Smoke_East
###
use Class::Struct;
use CGI qw(param);
use Time::Local;
use strict;

#my @mint = ("0015","0045","0115","0145","0215","0245", "0315","0345","1015","1045", "1115",
#            "1145","1215","1245", "1315", "1345","1415", "1445", "1515", "1545", "1615", "1645",
#            "1715", "1745", "1815", "1845", "1915", "1945", "2015", "2045","2115", "2145",
#            "2215", "2245", "2315", "2345");
my @mint = ("1045","1115","1145","1215","1245", "1315", "1345","1415", "1445", "1515", "1545", "1615", "1645",
            "1715", "1745", "1815", "1845", "1915", "1945", "2015", "2045","2115", "2145",
            "2215", "2245");


my %output_dir = qw (
   grd /data/new_SMOKE/GOES_AOD
   dat /data/new_SMOKE/Thresh20_40/COMB_AOD_CLOUD_FIRE/2SCHEMES
   jpg /data/new_SMOKE/Image
);

my %output_prefix = qw (
  grd G13.
  dat G13.
  jpg G13smokeaod_
);

my %output_suffix = qw (
 grd .2smoke.combaod.hmshysplitcomb2.NAM3.grd
 dat .2smoke.combaod.hmshysplitcomb2.dat
 jpg .combaod2.png
);

my %remoteSateps_dir = qw (
 grd  /data/ftp/pub/FIRE/HMS/SMOKE
 dat  /data/ftp/pub/FIRE/HMS/SMOKE/binary
 jpg  /data/ftp/pub/FIRE/HMS/SMOKE/TEST
);

my %remoteGP5_dir = qw (
 grd /data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/PNGFILES
);
my %dist1_suffix = qw (
 grd \$PASDAT.GOES-13.SMOKEE_GRD.
 dat \$PASDAT.GOES-13.SMOKEE_COMB2.
 jpg \$PASDAT.GOES-13.SMOKEE_JPG.
);

my $isLeap = 0;
my $julday=0;
my $month=0;
my $day=0;
my $year = 0;
my $leapday = 0;
my @date = ();
my $i =0;
my $mm = 0;
my $dd = 0;
my $getmint = ' ';
my $getmint_dist1 = ' ';
my $label_name = ' ';
my $ProdId_name = '';
my $IngestGASPFile_name = ' ';
my $IngestHySPFile_1_name = ' ';
my $IngestHySPFile_2_name = ' ';
my $ProdStarID_name = ' ';
my $ProdGrdFile = ' ';
my $getStart = ' ';
my $error_number = 0;
my $file = ' ';
my $IngestGASPFile_dir = "/data/new_SMOKE/AOD/";
my $ProdImg_dir = "/data/new_SMOKE/Image";
my $outputFile = "Smoke_East_Table.txt";
my $output_dir = "/data/new_SMOKE/table";
my $errorFile = "Smoke_East_ERROR.txt";
my $cclist = ' ';
my $emailList = ' ';

system('date');
print "sata @date  \n";
my $hour_time = `date +%H`;
if ($hour_time < 18) {
  $leapday = 2;
}
else {
   $leapday = 1;
}
#$leapday = 295;
print " before $leapday  \n";

my ($year, $mm, $dd, $julday) = getDate($leapday);

print "year555  $year  $mm $dd $julday  \n";
writehead($output_dir,$outputFile);
open(EASTFILE, ">> $output_dir/$outputFile") || die "Can not open  $outputFile for writing: $!";

#writehead();
writehead($output_dir,$errorFile);

#open(MFILE, ">> $output_dir/$outputFile") || die "Can not open  $outputFile for writing: $!";
open(ERROREASTFILE,"> $output_dir/$errorFile") || die "Can not open  $errorFile for writing: $!";
write_errorhead();

`imglist.k DPD/SMOKE.-50 > /home/oper/new_SMOKE/imglist.txt`;
my $number1=scalar(@mint);

for ($i=0; $i<$number1; $i++) {
 $error_number = 0;
 $ProdId_name = ' ';

##### column 1   ProdId_name  ######
 my $check_exist_1= "$ProdImg_dir/G13smokeaod_$year$mm$dd$mint[$i].combaod2.png";

# my $check_exist_2 = "$ProdImg_dir/G13smoke_$year$mm$dd$mint_2[$i].combaod2.jpg";
# print " check 1  $check_exist_1 \n";
# print "check 2 $check_exist_2  \n";
 if( -e $check_exist_1) {

 $ProdId_name = "$year$mm$dd$mint[$i]";
 $getmint = $mint[$i];
 $getmint_dist1 = $mint[$i];
 }
 else {
  $getmint = $mint[$i];
  $ProdId_name = "Fail!Fail!!";
  $getmint_dist1 = "Fail";
 }
  $label_name = $year.$mm.$dd.$getmint;
print " SSSSSSname11: $i     $ProdId_name   $label_name   \n";


###  column 2 GASP  ##########
 my $getGASP = InputGASP($year,$julday,$mint[$i]);
 print "getGASP $getGASP \n";
 chomp($getGASP);
 if ($getGASP eq 'Failure     ') {
   $error_number = $error_number + 1;
   print " iNNNNNNN \n";
 }
### column 3 HYSPLIT1  ######
 my $getHYSPLIT = InputHYSPLIT($year,$mm,$dd);
 chomp($getHYSPLIT);
 if ($getHYSPLIT eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 4 HYSPLIT2 ########
 my $hysplit2 = $leapday+1;
 my ($year_2, $mm_2, $dd_2, $julday_2) = getDate($hysplit2);
 print "inhysplit 2222: $year_2,$mm_2,$dd_2  \n";

# my $getHYSPLIT_2 = InputHYSPLIT($year_2,$mm_2,$dd_2);
# print "getHYSPLIT_2  $getHYSPLIT_2  \n";
# chomp($getHYSPLIT_2);
# if ($getHYSPLIT_2 eq 'Failure     ') {
#   $error_number = $error_number + 1;
# }

### column 5 Start time  #####
 my ($getStart,$getEnd) = getStartTime($year,$mm,$dd);
 chomp($getStart);
 if ($getStart eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 6 produc Grd  #####
 my $grd_name = substr($ProdId_name,0,10);
 print " grd_name $grd_name $mint[$i]  \n";
 my $getProdGrd = ' ';
 if ($mint[$i] eq '1045' ) {
    $getProdGrd = 'notscheduled';
 }
 else {
   $getProdGrd = getProduc($year,$mm,$dd,$output_dir{grd},$output_prefix{grd},$output_suffix{grd},$grd_name);
 chomp($getProdGrd);
 }

 if ($getProdGrd eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 7 Produc Dat ######
 my $outputDat_dir = $output_dir{dat}."/".$year.$mm.$dd;
 print "outputDat_dir $outputDat_dir  $ProdId_name\n";
 my $getProdDat = getProduc($year,$mm,$dd,$outputDat_dir,$output_prefix{dat},$output_suffix{dat},$ProdId_name);
 chomp($getProdDat);
 if ($getProdDat eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 8 Produc Img #####
 my $getProdImg = getProduc($year,$mm,$dd,$output_dir{jpg},$output_prefix{jpg},$output_suffix{jpg},$ProdId_name);
 chomp($getProdImg);
 if ($getProdImg eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 9 Satepsnon Grd ####
 my $remotrSize ='oper\@10.144.3.43';
 my $getSatepsnonGrd = ' ' ;
 if ($mint[$i] eq '1045' ) {
   $getSatepsnonGrd = 'notscheduled';
 }
 else {
  $getSatepsnonGrd = getSateps($year,$mm,$dd,$remotrSize,$remoteSateps_dir{grd},$output_prefix{grd},$output_suffix{grd},$grd_name);
print "getSatepsnonGrd $getSatepsnonGrd \n";
 chomp($getSatepsnonGrd);
 }

 if ($getSatepsnonGrd eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 10 Satepsanon Dat #####
 my $getSatepsnonDat = getSateps($year,$mm,$dd,$remotrSize,$remoteSateps_dir{dat},$output_prefix{dat},$output_suffix{dat},$ProdId_name);
 chomp($getSatepsnonDat);
 if ($getSatepsnonDat eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 11 Satepsanon Img #####
 my $getSatepsnonImg = getSateps($year,$mm,$dd,$remotrSize,$remoteSateps_dir{jpg},$output_prefix{jpg},$output_suffix{jpg},$ProdId_name);
 chomp($getSatepsnonImg);
 if ($getSatepsnonImg eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 12 GP5 Img  #######

 my $getGP5Img = getGP5Img($ProdId_name);
 chomp($getGP5Img);
 if ($getGP5Img eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 13 dist1 Grd ####
#  $remotrSize ='pas\@satepsdist1.nesdis.noaa.gov';
   $remotrSize ='pas\@10.144.3.38';
 my $getmint_2 = substr($getmint_dist1,0,2).'00';
 my $getDist1Grd = ' ';
 if ($mint[$i] eq '1045' ) {
   $getDist1Grd = 'notscheduled';
 }
 else {
  $getDist1Grd = getDist1($year,$julday,$remotrSize,$dist1_suffix{grd},$getmint_2);
 chomp($getDist1Grd);
 }
 if ($getDist1Grd eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column 14 dist1 dat  ###
 my $getDist1Dat = getDist1($year,$julday,$remotrSize,$dist1_suffix{dat},$getmint);
 chomp($getDist1Dat);
 if ($getDist1Dat eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

###  column 15 dist1 img  ####
 my $getDist1Img = getDist1($year,$julday,$remotrSize,$dist1_suffix{jpg},$getmint);
 chomp($getDist1Img);
 if ($getDist1Img eq 'Failure     ') {
   $error_number = $error_number + 1;
 }

### column for AREA files on dist1
$remotrSize ='spider@10.144.3.38';
printf " ProdId_name = $ProdId_name \n";

my $getDist1AREA = getDist1AREA($year,$ProdId_name,$remotrSize,$getmint);
 if ($getDist1AREA eq 'Failure     ') {
   $error_number = $error_number + 1;
 }


printf " getDist1AREA = $getDist1AREA  \n";

 if ($error_number > 0 ) {
   print ERROREASTFILE " $label_name ","  $getGASP ","  $getHYSPLIT "," $getStart ","  $getProdDat "," $getProdImg "," $getProdGrd "."\n"  ;
 }
# if ($getGASP eq "Failure     ") {
#  print " send to help \n";
#  print ERRORFILE " $ProdId_name GASP File $getGASP \n";
# }

# print "$year,$julday,$mint[$i]  $smoke_westData->IngestGASPFile; \n";
  print " getProdGASP $getGASP  \n";
  print EASTFILE "$label_name ","  $getGASP ","  $getHYSPLIT ","  $getStart "," $getProdDat "," $getProdImg "," $getProdGrd "," $getSatepsnonGrd ","  $getSatepsnonImg ", "   $getSatepsnonDat ","   $getGP5Img ","   $getDist1Grd ","    $getDist1Dat   ","   $getDist1Img  ","   $getDist1AREA  ","   $getEnd "."\n"  ;
# print EASTFILE "$label_name ","  $getGASP "," $getHYSPLIT_2 ","  $getHYSPLIT ","  $getStart "," $getProdDat ","$getProdImg "," $getProdGrd ",   $getGP5Img ","   $getDist1Grd ","    $getDist1Dat   ","   $getDist1Img  ","   $getDist1AREA  ","   $getEnd "."\n"  ;

# print MFILE " $getSatepsnonGrd "," $getSatepsnonGrd ", " $getSatepsnonImg "."\n"  ;
}
my $file_line = `cat /data/new_SMOKE/table/Smoke_East_ERROR.txt | wc -l`;

print "TTTTTT $file_line  \n";
if ($file_line > 6 ) {
 my ($year_now, $mm_now, $dd_now, $julday_now) = getDate(0);
 open(EMILFILE, ">$output_dir/mailSmoke_East_ERROR.txt ") || die "Can not open mailSmoke_ERROR.txt for writing: $!";
 print EMILFILE "On $year_now$mm_now$dd_now the ASDTA Smoke East product has experienced more than three\n"; 
 print EMILFILE "consecutive processing failures for $year$mm$dd. This exceeds the number of \n";
 print EMILFILE "allowable failures, so please open a help ticket and notify the point of \n";
 print EMILFILE "contact.\n";
 print EMILFILE "Additional datails may be obtained from the product monitoring logs at \n";
 print EMILFILE "ftp://satepsanone.nesdis.noaa.gov/FIRE/HMS/SMOKE_WEEKLY/East   \n";
close(EMILFILE);
# system("/bin/mail -s 'Smoke_East  error ' Liang.chen\@noaa.gov < /data/new_SMOKE/table/mailSmoke_East_ERROR.txt "); 
# system("/bin/mail -s 'Smoke_East  error ' Matthew.Seybold\@noaa.gov < /data/new_SMOKE/table/mailSmoke_East_ERROR.txt ");
#$cclist = "-c  'ESPCoperations\@noaa.gov' 'Liang.chen\@noaa.gov' ";
$cclist = "-c  'ESPCoperations\@noaa.gov' ";
#$cclist = "-c  'Liang.chen\@noaa.gov'  'Liqun.Ma\@noaa.gov'";
$emailList = "'Liang.chen\@noaa.gov' ";
system("/bin/mail -s 'Smoke_East  error ' $cclist  $emailList < /data/new_SMOKE/table/mailSmoke_East_ERROR.txt ");


# system("/bin/mail -s 'Smoke_East  error ' Liang.chen\@noaa.gov < /data/new_SMOKE/Smoke_East_ERROR.txt ");
}
#####system("/home/oper/new_SMOKE/getWeek_monitoring_east.pl ");

close(EASTFILE);
close(ERROREASTFILE);
print "end  \n";



sub getDate
{
# my $In_leapday = @_;
 print " in getDate leap    @_ \n";
my $In_leapday = shift(@_);
my  $yday_in = time()-86400*($In_leapday);
my  ($sec_in,$min_in,$hour_in,$D_in,$M_in,$Y_in,$W_in,$J_in,$I_in) = localtime($yday_in);
print "test2  $sec_in,$min_in,$hour_in,$D_in,$M_in,$Y_in,$W_in,$J_in,$I_in   \n";

 my $year_in = $Y_in + 1900;
  my $mm_in =  $M_in + 1;
 if ($mm_in < 10 ) {
  $mm_in = "0$mm_in";
 }
  my $dd_in = $D_in;
 if ( $dd_in < 10 ) {
  $dd_in = "0$dd_in";
 }
 my $julday_in = $J_in + 1;
 print "year  $year_in  $mm_in $dd_in $julday_in  \n";

  if ($julday_in < 10)
 {
    $julday_in = "00" . $julday_in;
 }
 elsif (($julday_in < 100) && ($julday_in > 9))
 {
    $julday_in = "0" . $julday_in;
 }

 return ($year_in,$mm_in,$dd_in,$julday_in);

}

sub writehead 
{
my  ($output_dir,$outputFile) = @_;
 open(MFILE, ">> $output_dir/$outputFile") || die "Can not open  $outputFile for writing: $!";
print EASTFILE "1/2HourProduct Ingest VIS     Ingest         Productiot    Production    Production    Production    Distribution   Distribution    Distribution    Distribution    Distribution     Distribution to   Distribution to  Distribution to  Project\n"; 
print EASTFILE "Label Time     GASP File      HYSPLIT1DayOld System Start  .dat          .jpg          .grd          satepsanon-ftp satepsanon-ftp  satepsanon-ftp  GP5 - web       satepsdist1-NCDC satepsdist1-NCDC  satepsdist1-NCDC satepsdist1-AREA System End \n";
print EASTFILE "               Timestamp      Timestamp      Time          Timestamp     Timestamp     Timestamp     .grd Timestamp .jpg Timestamp  .dat Timestamp  Timestam        .grd Timestamp   .dat Timestam     .jpg Timestamp   AREA Timestamp   Time       \n";
print EASTFILE "YYYYMMDDhhmm   YYYYMMDDhhmm   YYYYMMDDhhmm   YYYYMMDDhhmm  YYYYMMDDhhmm  YYYYMMDDhhmm  YYYYMMDDhhmm  YYYYMMDDhhmm   YYYYMMDDhhmm    YYYYMMDDhhmm    YYYYMMDDhhmm    YYYYMMDDhhmm     YYYYMMDDhhmm      YYYYMMDDhhmm     YYYYMMDDhhmm     YYYYMMDDhhmm \n";
}
sub write_errorhead 
{
 my $infile = @_;
 
print ERROREASTFILE "1/2HourProduct Ingest VIS     Ingest         Productiot    Production    Production    Production    Distribution   Distribution    Distribution    Distribution    Distribution     Distribution to   Distribution to  Distribution to  Project\n";
print ERROREASTFILE "Label Time     GASP File      HYSPLIT1DayOld System Start  .dat          .jpg          .grd          satepsanon-ftp satepsanon-ftp  satepsanon-ftp  GP5 - web       satepsdist1-NCDC satepsdist1-NCDC  satepsdist1-NCDC satepsdist1-AREA System End \n";
print ERROREASTFILE "               Timestamp      Timestamp      Time          Timestamp     Timestamp     Timestamp     .grd Timestamp .jpg Timestamp  .dat Timestamp  Timestam        .grd Timestamp   .dat Timestam     .jpg Timestamp   AREA Timestamp   Time       \n";
print ERROREASTFILE "YYYYMMDDhhmm   YYYYMMDDhhmm    YYYYMMDDhhmm   YYYYMMDDhhmm  YYYYMMDDhhmm  YYYYMMDDhhmm  YYYYMMDDhhmm  YYYYMMDDhhmm   YYYYMMDDhhmm    YYYYMMDDhhmm    YYYYMMDDhhmm    YYYYMMDDhhmm     YYYYMMDDhhmm      YYYYMMDDhhmm     YYYYMMDDhhmm     YYYYMMDDhhmm \n";
}

sub InputGASP
{
  my ($year,$jday, $hhmm) = @_;
  my $searchTime = 1;
  my $InputGASP_dir = "/data/new_SMOKE/AOD";
  my $inPattern = $year.$jday.$hhmm . "*" . "_i18_US.all.aod.gz";
#  my $inPattern = $year.$jday.$hhmm . "*";

  my $inFile = ' ';
  my $lsinFile = ' ';
  my @splitMsg = ' ';
  my $mon = ' ';
  my $day = ' ';
  my $digMon = ' ';
  my $retur_data = 'Failure     ';
  my @inFileList = ' ';
  my $filesize = 0;

  my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);

 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

#  print "YYYY $year,$jday, $hhmm $InputGASP_dir  \n";
#  print "inPattern $inPattern\n";

  chdir("$InputGASP_dir") || die "Cannot change to directory $InputGASP_dir";

#  my (@inFileList) = `find $InputGASP_dir -name "$inPattern" -mtime $searchTime  -exec ls {} ';'|sort`;
#  my (@inFileList) = `find $InputGASP_dir -name "$inPattern" -mtime $searchTime  -exec ls {} ';
  @inFileList = `ls -l $inPattern`;
#  print "inFileList  @inFileList \n";
  if (@inFileList ne ' ' ) {
    foreach $inFile (@inFileList) {
     chomp($inFile);
#     print " inFile  $inFile  \n";

#     $lsinFile = `ls -l $inFile`;
#     print " lsinFile $lsinFile  \n";
#    if ($inFile ne ' ') {
     @splitMsg = split(/\s+/, $inFile);
     $mon = $splitMsg[5];
     $mon =~ tr/a-z/A-Z/;
     $digMon = $months{$mon};
     $day = $splitMsg[6];
     if ($day < 10 ) {
       $day = "0$day";
     }
     $hhmm = $splitMsg[7];
     $hhmm =~ s/\://g;
#    print "HH $mon $day $hhmm  \n";
#   $in_point->IngestGASPFile($hhmm);
     $filesize = $splitMsg[4];
#     print " size : $filesize  \n";
     if ($filesize > 100) {
       $retur_data = $year_today.$digMon.$day.$hhmm;
     }
    }
   }
   return  $retur_data;
}



sub InputHYSPLIT
{
 my ($year,$mm, $dd) = @_;
 my $InputHYS_dir = "/data/new_SMOKE/arrH";
 my $inPattern = "hmshysplit$year$mm$dd" . ".txt";
 my @inFileList = ' ';
 my $inFile = ' ';
 my @splitMsg = ' ';
 my  $mon = 0;
 my $digMon = 0;
 my $hhmm = 0;
 my $retur_data = 'Failure     ';
 my $filesize = 0;

 my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);
 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

 print " input hys : $inPattern  \n";

 chdir("$InputHYS_dir") || die "Cannot change to directory $InputHYS_dir";
 @inFileList = `ls -l $inPattern`;

 print "inFileList  @inFileList \n";
 if (@inFileList ne ' ' ) {
  foreach $inFile (@inFileList) {
     chomp($inFile);
     print " inFile  $inFile  \n";
     @splitMsg = split(/\s+/, $inFile);
     $mon = $splitMsg[5];
     $mon =~ tr/a-z/A-Z/;
     $digMon = $months{$mon};
     $day = $splitMsg[6];
     if ($day < 10) {
       $day ="0$day";
     }
     $hhmm = $splitMsg[7];
     $hhmm =~ s/\://g;
    print " in InputHYSPLIT HH $mon $day $hhmm  \n";
     $filesize = $splitMsg[4];
     if ($filesize > 70 ) {
#     if ($filesize > 35 ) {
       $retur_data = $year_today.$digMon.$day.$hhmm;
     }
    }
   }
   return  $retur_data;
}

sub getStartTime
{
 my ($year,$mm, $dd) = @_;
 my $InputHYS_dir = "/data/new_SMOKE/logs";
 my $inPattern = "smoke_east_monitoring.log";
# my $inPattern = "smoke555.log";
 my @inFileList = ' ';
 my $startmsg = ' ';
 my @splitMsg = ' ';
 my $mon = ' ';
 my $day =  0;
 my $hhmmss = ' ';
 my $digMon = ' ';
 my $hhmm = ' ';
 my $inFile = ' ';
 my $startTime = ' ';
 my $endTime = ' ';

  my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);
  my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

 chdir("$InputHYS_dir");
 @inFileList = `cat $inPattern`;
 if (@inFileList ne ' ' ) {
    $startmsg = `grep "started" $inPattern`;
    print "MSG : $startmsg    \n";
    chomp($startmsg);
    @splitMsg = split(/\s+/, $startmsg);
    $mon = $splitMsg[1];
    $mon =~ tr/a-z/A-Z/;
    $digMon = $months{$mon};
    $day = $splitMsg[2];
    if ($day < 10 ) {
     $day = "0$day";
    }
    $hhmmss =substr($splitMsg[3],0,5);
    $hhmmss =~ s/\://g;

    $startTime = $year_today. $digMon . $day . $hhmmss;
    print " in getStartTime $digMon $day $hhmmss $year  \n";

    $startmsg = `grep "ended" $inPattern`;
    chomp($startmsg);
    @splitMsg = split(/\s+/, $startmsg);
    $mon = $splitMsg[5];
    $mon =~ tr/a-z/A-Z/;
    $digMon = $months{$mon};
    $day = $splitMsg[6];
    if ($day < 10 ) {
      $day = "0$day";
    }
    $hhmmss =substr($splitMsg[7],0,5);
    $hhmmss =~ s/\://g;

    $endTime = $year_today. $digMon . $day . $hhmmss;
    print " in getStartTime  End : $endTime   \n";

 }


  return $startTime,$endTime;
}

sub getProduc
{
 my ($year,$mm, $dd,$output_dir,$output_prefix,$output_suffix,$ProdId_name) = @_;
 my @inFileList = ' ';
 my $inFile = ' ';
 my @splitMsg = ' ';
 my  $mon = 0;
 my $digMon = 0;
 my $hhmm = 0;
 my $retur_data = 'Failure     ';
 my $filesize = 0;

 my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);
 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

 my $inPattern = "$output_prefix$ProdId_name$output_suffix";

print " file name :$inPattern  $output_prefix   $output_suffix\n";

 chdir("$output_dir");
  @inFileList = `ls -l $inPattern`;
 print "ingetProduc:  @inFileList \n";

 if (@inFileList ne ' ' ) {
  foreach $inFile (@inFileList) {
     chomp($inFile);
     print " inFile  $inFile  \n";
     @splitMsg = split(/\s+/, $inFile);
     $mon = $splitMsg[5];
     $mon =~ tr/a-z/A-Z/;
     $digMon = $months{$mon};
     $day = $splitMsg[6];
     if ($day < 10 ) {
      $day = "0$day";
     }
     $hhmm = $splitMsg[7];
     $hhmm =~ s/\://g;
#    print " in InputHYSPLIT HH $mon $day $hhmm  \n";
     $filesize = $splitMsg[4];
     if ($filesize > 70 ) {
       $retur_data = $year_today.$digMon.$day.$hhmm;
     }
    }
   }

print "getProduc retur_data: $retur_data\n";
return $retur_data;
}
sub getSateps
{
 my ($year,$mm, $dd,$remoteSite,$remoteFimmaDataDir,$output_prefix,$output_suffix,$ProdId_name) = @_;
  my @inFileList = ' ';
 my $inFile = ' ';
 my @splitMsg = ' ';
 my  $mon = 0;
 my $digMon = 0;
 my $hhmm = ' ';
 my $hhmm_1 = ' ';
 my $hhmm_2 = ' ';
 my $retur_data = 'Failure     ';
 my $filesize = 0;

 my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);

 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

 my $inPattern = "$output_prefix$ProdId_name$output_suffix";
 my $sftpfile1 = '/data/new_SMOKE/logs/getRemote_File.txt';

open  ( LUOUT,  ">$sftpfile1" )  ||  die " Can't seem to open $sftpfile1: $!";
  print  LUOUT   "sftp $remoteSite << EOF\n";
  print  LUOUT   "    cd  $remoteFimmaDataDir \n";
  print  LUOUT   "    ls -l $inPattern \n";
  print  LUOUT   "    bye\n";
   print  LUOUT   "EOF\n";
close ( LUOUT );
chmod  (0777,  $sftpfile1) ;

#-----------------------------------------------------------------------------
open ( SFTPOUT,  "$sftpfile1 |" );

 @inFileList = <SFTPOUT>;
 print "in remote sateps @inFileList \n";


 if (@inFileList ne ' ' ) {
   foreach $inFile (@inFileList) {
     chomp($inFile);
     @splitMsg = split(/\s+/, $inFile);
     $mon = $splitMsg[-4];
     $mon =~ tr/a-z/A-Z/;
     $digMon = $months{$mon};
     $day = $splitMsg[-3];
     if ($day < 10) {
       $day = "0$day";
     }
     $hhmm = $splitMsg[-2];
     $hhmm =~ s/\://g;
     $filesize = $splitMsg[-5];
     if ($filesize > 70 ) {
       $retur_data = $year_today.$digMon.$day.$hhmm;
     }
   }
 }
 close(SFTPOUT);
print "in remote sateps : $retur_data \n";
 return $retur_data;

}

sub getGP5Img
{
  my ($ProdId_name) = @_;
  my @inFileList = ' ';
 my $inFile = ' ';
 my @splitMsg = ' ';
 my  $mon = 0;
 my $digMon = 0;
 my $hhmm = 0;
 my $retur_data = 'Failure     ';
 my $filesize = 0;

 my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);
 my $output_suffix = '.2smoke.jpg';
 
 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

 $ProdId_name = substr($ProdId_name,8,4);
print " in gp5 date: $ProdId_name \n";

 my $inPattern = "$ProdId_name$output_suffix";
 my $sftpfile1 = '/data/new_SMOKE/logs/getRemote_GP5_File.txt';
 my $remoteSite = 'sabweb\@10.144.3.87';
 my $remoteFimmaDataDir = '/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/PNGFILES';

 open  ( LUOUT,  ">$sftpfile1" )  ||  die " Can't seem to open $sftpfile1: $!";
  print  LUOUT   "sftp $remoteSite << EOF\n";
  print  LUOUT   "    cd  $remoteFimmaDataDir \n";
  print  LUOUT   "    ls -l $inPattern \n";
  print  LUOUT   "    bye\n";
   print  LUOUT   "EOF\n";
close ( LUOUT );
chmod  (0777,  $sftpfile1) ;

#-----------------------------------------------------------------------------
open ( SFTPOUT,  "$sftpfile1 |" );

 @inFileList = <SFTPOUT>;
 print "in remote sateps @inFileList \n";

 if (@inFileList ne ' ' ) {
   foreach $inFile (@inFileList) {
     chomp($inFile);
     print " inFile  $inFile  \n";
     @splitMsg = split(/\s+/, $inFile);
     $mon = $splitMsg[-4];
     $mon =~ tr/a-z/A-Z/;
     $digMon = $months{$mon};
     $day = $splitMsg[-3];
     if ($day < 10) {
      $day = "0$day";
     }
     $hhmm = $splitMsg[-2];
     $hhmm =~ s/\://g;
#    print " in InputHYSPLIT HH $mon $day $hhmm  \n";
     $filesize = $splitMsg[-5];
     if ($filesize > 70 ) {
       $retur_data = $year_today.$digMon.$day.$hhmm;
     }
   }
 }
 close(SFTPOUT);
print "in remote sateps : $retur_data \n";
 return $retur_data;

}
sub getDist1
{
 my ($year,$julday,$remotSite,$output_prefix,$ProdId_name) = @_;
  my @inFileList = ' ';
 my  @inFileList_2 = ' ';
 my $inFile = ' ';
 my $inFile_2 = ' ';
 my @splitMsg = ' ';
 my  $mon = ' ';
 my $digMon = 0;
 my $hhmm = 0;
 my $retur_data = 'Failure     ';
 my $filesize = 0;
 my $inPattern = $output_prefix.'D'.$year.$julday.'.T'.$ProdId_name.'Z';
 my $inPattern_2 = $output_prefix.'J'.$year.$julday.'.T'.$ProdId_name.'Z';
 my $sftpfile1 = '/data/new_SMOKE/logs/getRemote_Dist1_File.txt';
 my $sftpfile2 = '/data/new_SMOKE/logs/getRemote_Dist1_File_2.txt';
 my $remoteDataDir = '/data/pas/archive';

 my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);

 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

open  ( LUOUT,  ">$sftpfile1" )  ||  die " Can't seem to open $sftpfile1: $!";
  print  LUOUT   "sftp $remotSite << EOF\n";
  print  LUOUT   "    cd  $remoteDataDir \n";
  print  LUOUT   "    ls -l $inPattern \n";
  print  LUOUT   "    bye\n";
   print  LUOUT   "EOF\n";
close ( LUOUT );
chmod  (0777,  $sftpfile1) ;

open  ( LUOUT,  ">$sftpfile2" )  ||  die " Can't seem to open $sftpfile1: $!";
 print  LUOUT   "sftp $remotSite << EOF\n";
 print  LUOUT   "    cd  $remoteDataDir \n";
 print  LUOUT   "    ls -l $inPattern_2 \n";
 print  LUOUT   "    bye\n";
 print  LUOUT   "EOF\n";
close ( LUOUT );
chmod  (0777,  $sftpfile2) ;

#-----------------------------------------------------------------------------
open ( SFTPOUT,  "$sftpfile1 |" );
open ( SFTPOUT_2,  "$sftpfile2 |" );
 @inFileList = <SFTPOUT>;
 @inFileList_2 = <SFTPOUT_2>;
 print "in remote dist1 @inFileList \n";
 print "in remote dist1 inFileList_2: @inFileList_2 \n";
 if (@inFileList ne ' ' ) {
   foreach $inFile (@inFileList) {
     chomp($inFile);
     print " inFile  $inFile  \n";
     @splitMsg = split(/\s+/, $inFile);
     $mon = $splitMsg[-4];
     $mon =~ tr/a-z/A-Z/;
     $digMon = $months{$mon};
     $day = $splitMsg[-3];
     if ($day < 10 ) {
       $day = "0$day";
     }
     $hhmm = $splitMsg[-2];
     $hhmm =~ s/\://g;
    print " in dist1  HH $mon $day $hhmm  \n";
     $filesize = $splitMsg[-5];
     if ($filesize > 70 ) {
       $retur_data = $year_today.$digMon.$day.$hhmm;
       print "in dist1 first :$retur_data   \n";
     }
   }
  }
  if  ( $retur_data eq 'Failure     ' )  {
     foreach $inFile_2 (@inFileList_2) {
       chomp($inFile_2);
       print " inFile_2 $inFile_2  \n";
       my @splitMsg_2 = split(/\s+/, $inFile_2);
       my $mon_2 = $splitMsg_2[-4];
       $mon_2 =~ tr/a-z/A-Z/;
       my $digMon_2 = $months{$mon_2};
       my $day_2 = $splitMsg_2[-3];
       if ($day_2 < 10 ) {
          $day_2 = "0$day_2";
       }
       my $hhmm_2 = $splitMsg_2[-2];
      $hhmm_2 =~ s/\://g;
      print " in dist1 222 $mon_2 $day_2 $hhmm_2  \n";
      my $filesize_2 = $splitMsg_2[-5];
      if ($filesize_2 > 70 ) {
        $retur_data = $year_today.$digMon_2.$day_2.$hhmm_2;
      }
   }
 }
 close(SFTPOUT);
 close(SFTPOUT_2);
print "in remote dist1 : $retur_data \n";
 return $retur_data;

}
sub getTimestamp_dist1
{
 my @remote_fileList = $ARGV[0];
 my $inFile = ' ';
 my $retur_data = ' ';
 my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);

 my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

 foreach $inFile (@remote_fileList) {
   chomp($inFile);
   my @splitMsg = split(/\s+/, $inFile);
   my $mon = $splitMsg[-4];
   $mon =~ tr/a-z/A-Z/;
   my $digMon = $months{$mon};
   my $day = $splitMsg[-3];
   if ($day < 10 ) {
     $day = "0$day";
   }
   my $hhmm = $splitMsg[-2];
   $hhmm =~ s/\://g;
   print " in dist1  6666 $mon $day $hhmm  \n";
   my $filesize = $splitMsg[-5];
   if ($filesize > 70 ) {
     $retur_data = $year_today.$digMon.$day.$hhmm;
   }
 }

 return $retur_data;
}

sub getDist1AREA
{
 my ($year1,$time,$remotSite,$ProdId_name) = @_;

my %months = qw(NONE 0 JAN 01 FEB 02 MAR 03 APR 04 MAY 05 JUN 06
                   JUL 07 AUG 08 SEP 09 OCT 10 NOV 11 DEC 12);


 my @inFileList = ' ';
 my $inFile = ' ';
 my $imglist_file = '/home/oper/new_SMOKE/imglist.txt';
 my $sct_dir = '/home/oper/new_SMOKE/';
 my @splitMsg = ' ';
 my $day = ' ';
 my $hhmm = ' ';
 my $mon= ' ';
 my $i = 0;
 my $digMon = ' ';
 my $retur_data = 'Failure     ';

chdir("$sct_dir");

my ($year_today, $mm_today, $dd_today, $julday_today) = getDate(0);

print " time1 $time  \n";

#open(OUT1,  "<$imglist_file") || die " Can't seem to open $sct_dir$imglist_file1: $!";
open(OUT1,  "< /home/oper/new_SMOKE/imglist.txt") || die " Can't seem to open $imglist_file: $!";

 @inFileList = <OUT1>;
$i=0;
   foreach $inFile (@inFileList) {
    if ($i > 2) {
     chomp($inFile);
     printf "inFile = $inFile  \n";
     @splitMsg = split(/\s+/, $inFile);
     $day = $splitMsg[-7];
     if ($day < 10) {
       $day = "0$day";
     }

     $mon = $splitMsg[-6];
     $digMon = $months{$mon};

     $hhmm = $splitMsg[-4];
     $hhmm =~ s/\://g;
     $hhmm = substr($hhmm,0,4);
     print "time mon day hhmm== $digMon  $day  $hhmm \n";
 my $area_time= $year1.$digMon.$day.$hhmm;
print "test111 $area_time  $time  \n";
     if ( $time eq $area_time ) {
      $retur_data = $area_time;
     }
    }
    $i = $i + 1;
   }
 close(OUT1);
 return $retur_data;


}

