#!/usr/bin/perl -w
#check_smoke_file.pl


use Time::Local;
use strict;

my $month;
my $day;
my $julday=0;
my $fourcentury=0;
my $century=0;
my $leapyear=0;
my $year = 0;
my $i=0;
my $y=0;
my $y1=0;
my $y2 = 0;
my $isLeap = 0;

my @days1 = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
my @days2 = (0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
my @jdays1 = (0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
my @jdays2 = (0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366);

#my $data_png_dir= "/home/oper/SMOKE5/PACKAGE5";
#my $data_png_dir = "/data/ftp/pub/FIRE/HMS/SMOKE/TEST";
#my $data_grd_dir = "/data/ftp/pub/FIRE/HMS/SMOKE";
#my $data_png_dir = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/new_SMOKE/data/TEST";
#my $data_grd_dir = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/new_SMOKE/data";
my $data_png_dir = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/data/TEST";
my $data_grd_dir = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/data";

my @date=gmtime(time);
system('date');
print "sata @date  \n";
my $hour_time = `date +%H`;
print"test222 $hour_time\n";
my $leapday =  0;
if ($hour_time < 18) {
  $leapday = 2;
}
else {
  $leapday = 1;
}
print"leapday $leapday  $hour_time\n";
#  $year=$date[5]+1900;
#  $julday=$date[7]+1 - $leapday;
#  $fourcentury=$year % 4;
#  $century=$year % 100;
#  $leapyear=$year % 400;

#if (( $fourcentury ==0 && $century != 0 )||$leapyear ==0) {
#  $isLeap = 1;
# print " leap  $isLeap\n";
#}
#if ($isLeap ==0) {
#  for ($i=0; $i<=12; $i++) {

#    if ($julday > $jdays1[$i] && $julday <= $jdays1[$i+1]) {
#      $month = $i + 1;
#      if($month < 10) {
#        $month = "0$month";
#      }
#      $day = $julday - $jdays1[$i];
#      if($day < 10) {
#        $day = "0$day";
#      }
#      print "mon  day  $month   $day  \n";
#    }
#  }
# }
# else {
#  for ($i=0; $i<=12; $i++) {
#    if ($julday > $jdays2[$i] && $julday <= $jdays2[$i+1]) {
#       $month = $i + 1;
#      if($month < 10) {
#        $month = "0$month";
#      }
#      $day = $julday - $jdays2[$i];
#      if($day < 10) {
#        $day = "0$day";
#      }
#    }
#   }
# }
#$julday=365;
#$day=31;
#$year=2009;
#$month=12;
my ($year,$month,$day,$julday) = getDate($leapday);
print "testRRRRR $month  $day  $year $julday \n";
#open(OUTPUT, ">>/data/ftp/pub/FIRE/HMS/SMOKE/TEST/smoke5_$year$month$day.log");
#open(OUTPUT, ">>/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/new_SMOKE/data/TESTsmoke5_$year$month$day.log");
open(OUTPUT, ">>/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/data/TEST/smoke5_$year$month$day.log");
#print "fileYYy /data/ftp/pub/FIRE/HMS/SMOKE/TEST/smoke5_$year$month$day.log\n";

#$y1 = ($i * 20 + 203) *5;
#$y2 = ($i * 20 + 209) * 5;
for($i = 0; $i <13; $i++) {
  if($i > 0) {
    $y1 = ($i * 20 + 203) *5;
    my $check_exist_1 = "$data_png_dir/G13smokeaod_$year$month$day$y1.combaod2.png";
    if( -e $check_exist_1) {
     print OUTPUT "|G13smokeaod_$year$month$day$y1.combaod2.jpg|COMPLETED OKAY|\n";
    }
    else {
     print OUTPUT "|G13smokeaod_$year$month$day$y1.combaod2.jpg|FAILED!FAILED!|\n";
    }
  }
  $y2 = ($i * 20 + 209) * 5;
  my $check_exist_2 = "$data_png_dir/G13smokeaod_$year$month$day$y2.combaod2.png";
  if( -e $check_exist_2) {
    print OUTPUT "|G13smokeaod_$year$month$day$y2.combaod2.jpg|COMPLETED OKAY|\n";
  }
  else {
    print OUTPUT "|G13smokeaod_$year$month$day$y2.combaod2.jpg|FAILED!FAILED!|\n";
  }

}

#for($i = 0; $i <13; $i++) {
#  if($i > 0) {
#  $y1 = ($i * 20 + 203) *5;
#   my $check_grd_1 = "$data_grd_dir/G13.$year$month$day$y1.all.aod_conc.NAM3.grd";
#   if( -e $check_grd_1) {
#     print OUTPUT "|G13.$year$month$day$y1.all.aod_conc.NAM3.grd|COMPLETED OKAY|\n";
#   }
#   else {
#     print OUTPUT "|G13.$year$month$day$y1.all.aod_conc.NAM3.grd|FAILED!FAILED!|\n";
#   }
#  }
#  $y2 = ($i * 20 + 209) * 5;
#  my $check_grd_2 = "$data_grd_dir/G13.$year$month$day$y2.all.aod_conc.NAM3.grd";
#  if( -e $check_grd_2) {
#    print OUTPUT "|G13.$year$month$day$y2.all.aod_conc.NAM3.grd|COMPLETED OKAY|\n";
#  }
#  else {
#    print OUTPUT "|G13.$year$month$day$y2.all.aod_conc.NAM3.grd|FAILED!FAILED!|\n";
#  }
#}

#for($i = 0; $i <12; $i++) {
# $y1 = $i + 11;
# my $check_grd_3 = "$data_grd_dir/G13.$year$month$day$y1.2smoke.combaod.hmshysplitcomb2.NAM3.grd";
# if( -e $check_grd_3) {
#   print OUTPUT "|G13.$year$month$day$y1.2smoke.combaod.hmshysplitcomb2.NAM3.grd|COMPLETED OKAY|\n";
# }
# else {
#   print OUTPUT "|G13.$year$month$day$y1.2smoke.combaod.hmshysplitcomb2.NAM3.grd|FAILED!FAILED!|\n";
# }
#} 
close(OUTPUT);

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 = "0" . $julday_in;
 }

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

}

