#!/usr/bin/perl -w
## send_input.bsh
## send input data to Satepsanon fro STAR

use Time::Local;
use strict;

#dirpath=$(dirname $0)
#. $dirpath/run_Dust_script.bsh

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 $FTPANON="oper\@10.144.3.43";
my $FTPANON_input_dir="/data/ftp/pub/DUST/input";

system('date');
print "Send input data to Satepsanon  @date  \n";

my ($envar, $enval);
open IN, ". ./run_Dust_script.bsh; env |"
  or die "Could not run shell: $!\n";
while ( <IN> ) {
  chomp;
  ($envar,$enval) = split /=/,$_,2;
  $ENV{$envar} = $enval;
  print "envar  $ENV{$envar}  \n";
}
close IN;
my $local_dir_1 = $ENV{'local_dir'};


$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";


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

}

