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

use Time::Local;
use strict;
my $month=0;
my $day=0;
my $year = 0;
my $mm = 0;
my $dd = 0;
my $julday=0;
my $i = 0;
my $leapday = 0;
my $local_dir = "/home/oper/Dust/OSPO_Package_Dust_GRIB/scripts";
my $output_dir = "/data/Dust/output";
my $number_begin =16;
my $number_end = 23;
my @date = ();
my $remotr_gp5_dir = "/data/web/mirror/www.ssd.noaa.gov/htdocs/PS/FIRE/DUST/PNGFILES_DUST";
my $GP5="sabweb\@10.144.3.87";
my $FTPANON="oper\@10.144.3.43";
my $DIST1="oper\@10.144.3.38";
my $GP50="sabweb\@140.90.195.34";
my $dist1_dir="/data/ftp/pub/DUST/";
my $FTPANON_dir="/data/ftp/pub/DUST/output";
my $GP5="sabweb\@10.144.3.87";
my $FTPANON_input_dir="/data/ftp/pub/DUST/input";



system('date');

print "Sned images to WEB  @date  \n";

$year=$ARGV[0];
$julday=$ARGV[1];
$mm=$ARGV[2];
$dd=$ARGV[3];

if (length($year) eq 0 ) {

$leapday = 1;

 print " before $leapday  \n";

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

}

print "year555  $year  $mm $dd $julday  \n";

for ($i=$number_begin; $i<=$number_end; $i++) {

my $check_exist_1= "$output_dir/$year$mm$dd/AOD/MYDdustconc.v6.3.4.$year$mm$dd.hr$i.png"; 
### send to GP50

 if( -e $check_exist_1) {
#   system("scp $check_exist_1 sabweb\@140.90.195.34:$remotr_gp5_dir/MYDdust.$i.jpg");
#    system("scp $check_exist_1 $GP50\:$remotr_gp5_dir/MYDdust.$i.jpg");
 }
 else {
   system(" chmod 755 $local_dir/NO_DUST_Data.gif ");
#   system("scp  $local_dir/NoDustDataTime.gif sabweb\@140.90.195.34:$remotr_gp5_dir/MYDdust.$i.jpg");
#   system("scp  $local_dir/NoDustDataTime.gif $GP50\:$remotr_gp5_dir/MYDdust.$i.jpg");
 }
}
### send to GP5
for ($i=$number_begin; $i<=$number_end; $i++) {

my $check_exist_1= "$output_dir/$year$mm$dd/AOD/MYDdustconc.v6.3.4.$year$mm$dd.hr$i.png";

 if( -e $check_exist_1) {
  system("scp $check_exist_1 $GP5\:$remotr_gp5_dir/MYDdust.$i.jpg");
  }
 else {
#   system(" chmod 755 $local_dir/NO_DUST_Data.gif ");
#   system("scp  $local_dir/NoDustDataTime.gif sabweb\@140.90.195.34:$remotr_gp5_dir/MYDdust.$i.jpg");
   system("scp  $local_dir/NoDustDataTime.gif $GP5\:$remotr_gp5_dir/MYDdust.$i.jpg");
 }
}

#### send to Dist1
for ($i=$number_begin; $i<=$number_end; $i++) {
my $check_exist_2= "$output_dir/$year$mm$dd/GRIB/MYDdust.aod_conc.v6.3.4.$year$mm$dd.hr$i.grib";
my $check_exist_3= "$output_dir/$year$mm$dd/NetCDF/MODIS_DUST.$year$mm$dd.$i.nc.gz";

#  if( -e $check_exist_2) {
#    system("scp $check_exist_2 oper\@10.144.3.38:/data/ftp/pub/DUST/ ");
#    system("scp $check_exist_3 oper\@10.144.3.38:/data/ftp/pub/DUST/ ");
    system("scp $check_exist_2 $DIST1\:$dist1_dir ");
    system("scp $check_exist_3 $DIST1\:$dist1_dir ");


#  }
}

#### send to Satepsanon
for ($i=$number_begin; $i<=$number_end; $i++) {
 my $check_exist_3= "$output_dir/$year$mm$dd/NetCDF/MODIS_DUST.$year$mm$dd.$i.nc.gz";
 my $check_exist_1= "$output_dir/$year$mm$dd/AOD/MYDdustconc.v6.3.4.$year$mm$dd.hr$i.png";
 my $check_exist_2= "$output_dir/$year$mm$dd/GRIB/MYDdust.aod_conc.v6.3.4.$year$mm$dd.hr$i.grib";
 my $check_exist_8= "$output_dir/$year$mm$dd/MYD_ADP_*hdf";

 system("scp $check_exist_3 $FTPANON\:$FTPANON_dir ");
 system("scp  $check_exist_1 $FTPANON\:$FTPANON_dir ");
 system("scp $check_exist_2 $FTPANON\:$FTPANON_dir ");
 system("scp $check_exist_8 /data/ftp/pub/DUST/granule_output/ ");
}
 my $check_exist_4 = "/data/Dust/input/MOD35_L2.P$year$julday\*.hdf";
 my $check_exist_5 = "/data/Dust/input/MOD03.P$year$julday\*.hdf";
 my $check_exist_6 = "/data/Dust/input/MOD021KM.P$year$julday\*.hdf";
 my $check_exist_7 = "/data/Dust/input/MYD04_L2/MOD04_L2.P$year$julday\*.hdf";

system("scp $check_exist_4 $FTPANON\:$FTPANON_input_dir  "); 
system("scp $check_exist_5 $FTPANON\:$FTPANON_input_dir  ");
system("scp $check_exist_6 $FTPANON\:$FTPANON_input_dir  ");
system("scp $check_exist_7 $FTPANON\:$FTPANON_input_dir  ");


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

}

