#!/usr/bin/perl -w
#copy_png.pl written by Liang chen 10/10/2007
#Copy the one day ago images from
#/data/ftp/pub/FIRE/HMS/SMOKE/TEST/

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;
#$month =$ARGV[0];
#$day = $ARGV[1];
 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_dir ="/data/ftp/pub/FIRE/HMS/SMOKE/TEST/";
#my $data_dir ="/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/new_SMOKE/";
my $data_dir ="/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA";

my @date=gmtime(time);
system('date');
print "sata @date  \n";
my $leapday = 0;
my $hour_time = `date +%H`;
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++) {
  #  print" julday days1 $julday  $jdays1[$i] $jdays1[$i+1]\n";

#    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";
#      }
#    }
#   }
# }
#$month=12;
#$day=31;
#$year=2009;
#$julday=365;
my ($year,$month,$day,$julday) = getDate($leapday);

print "test $month  $day  $year $julday \n";

for($i = 0; $i <13; $i++) {
  if($i > 0) {
    my $y1 = ($i * 20 + 203) *5;
    my $inputfile_1="G13smokeaod_$year$month$day$y1.combaod2.png"; 
    print" inputfile_1  $inputfile_1 \n";
    my $check_file_1 ="$data_dir/data/TEST/$inputfile_1";
#    my $check_exist_1 = "/usr/local/etc/httpd/htdocs/FIRE/PNGFILES/$y1.2smoke.jpg";
    my $check_exist_1 = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/PNGFILES/$y1.2smoke.png";
    if( -e $check_file_1 ) {
        print" check_exist_1 $check_exist_1 \n";
#       system("cp /data/ftp/pub/FIRE/HMS/SMOKE/TEST/$inputfile_1 /usr/local/etc/httpd/htdocs/FIRE/PNGFILES/$y1.2smoke.jpg");
        system("cp  $data_dir/data/TEST/$inputfile_1 $data_dir/PNGFILES/$y1.2smoke.jpg");
    }
    else {
#      system("cp /usr/local/etc/httpd/htdocs/FIRE/failed.gif /usr/local/etc/httpd/htdocs/FIRE/PNGFILES/$y1.2smoke.jpg");
       system("cp $data_dir/data/failed.gif $data_dir/PNGFILES/$y1.2smoke.jpg");
      print" 1111  check_file_1  $y1  $check_file_1 \n";
    }
  }
  my $y2 = ($i * 20 + 209) * 5;
  my $inputfile_2="G13smokeaod_$year$month$day$y2.combaod2.png";
  my $check_file_2 ="$data_dir/data/TEST/$inputfile_2";
#  my $check_exist_2 = "/usr/local/etc/httpd/htdocs/FIRE/PNGFILES/$y2.2smoke.jpg";
  my $check_exist_2 = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/ASDTA/PNGFILES/$y2.2smoke.png";
  if( -e $check_file_2) {
#      system("cp /data/ftp/pub/FIRE/HMS/SMOKE/TEST/$inputfile_2 /usr/local/etc/httpd/htdocs/FIRE/PNGFILES/$y2.2smoke.jpg");
       system("cp $data_dir/data/TEST/$inputfile_2 $data_dir/PNGFILES/$y2.2smoke.jpg");
     print" check_file_2 $check_file_2  \n";
  }
  else {
#    system("cp /usr/local/etc/httpd/htdocs/FIRE/failed.gif /usr/local/etc/httpd/htdocs/FIRE/PNGFILES/$y2.2smoke.jpg");
     system("cp $data_dir/data/failed.gif $data_dir/PNGFILES/$y2.2smoke.jpg");
  print"  2222 check_file_2 $check_file_2  \n";

  }

}
my $log_file="smoke5run1.log";
#system("cp /data/ftp/pub/FIRE/HMS/SMOKE/TEST/smoke5_$year$month$day.log /usr/local/etc/httpd/htdocs/FIRE/PNGFILES/smoke5.log");
system("cp $data_dir/data/TEST/smoke5_$year$month$day.log  $data_dir/PNGFILES/smoke5.log");

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);

}

