#!/usr/bin/perl -w
#

use Net::FTP;
use File::Copy;
use Time::Local;


# This script copies a partial day of GOES hourly files from the 
# anonymous FTP directory to the raw data directory.
#
# Because the GOES directory only contains 24 hours of data and each 
# hourly file is overwritten every 24 hours, it is necesary to do one 
# transfer in the morning on one at the end of the day when the main 
# script is run. That should insure that the data are transferred
# without timing issues causing problems.
#
# This script transfers the 22z, 23z and 24z files from the previous
# day and the 1-12z files from the current day.


print "\n";
#print "Transfering the GOES data from SATEPSDIST1E";
print "Transfering the GOES data from SATEPSANONE";
print "\n";
system "date";
print "\n";



# Set up the directories and files where everything of importance 
# is located

my $satep_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/goes/files_from_satep/";

my $raw_data_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/goes/east_and_west/";


# Remove older files from the raw_data directories

my @sateptempfiles = glob $satep_dir . "MDXX*";

foreach $file (@sateptempfiles)
  {
  unlink $file;
  }



# Transfer the GOES GRET files from the ftp server

#printf("\nTransfering GOES GRET data from satepsdist1e\n");
printf("\nTransfering GOES GRET data from satepsanone\n");
chdir $satep_dir;

system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0725";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0726";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0727";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0728";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0729";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0730";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0731";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0732";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0733";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0734";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0735";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0736";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0737";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0738";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0739";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0740";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0741";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0742";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0743";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0744";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0745";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0746";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0747";
system "wget --wait=5 http://satepsanone.nesdis.noaa.gov/pub/goes_sfov/goes15/md/MDXX0748";

#my $ftp_server = "satepsanone.nesdis.noaa.gov";
#my $user_id = '#####';
#my $password = '#####';

#my $goes13_pattern = "MDX131*";
#my $goes15_pattern = "MDXX07*";

#my $ftp = Net::FTP->new($ftp_server, Debug => 0)
#  or die "Cannot connect to: " . $ftp_server . "\n";

#$ftp->login()
#  or die "Cannot login: ", $ftp->message;

#$ftp->binary
#  or die "Cannot change to binary mode: ", $ftp->message;

##$ftp->cwd("mcidas")
##  or die "Cannot change directories: ", $ftp->message;

#$ftp->cwd("/goes_sfov/goes13/md")
#  or die "Cannot change directories: ", $ftp->message;

#mget($ftp, $goes13_pattern);

#$ftp->cwd("/goes_sfov/goes15/md")
#  or die "Cannot change directories: ", $ftp->message;

#mget($ftp, $goes15_pattern);

#$ftp->quit;

#printf("\n");



# Copy the GOES East files

$from_file = $satep_dir . "MDX13122";
$to_file   = $raw_data_dir . "gret_east_0_22.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13123";
$to_file   = $raw_data_dir . "gret_east_0_23.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13124";
$to_file   = $raw_data_dir . "gret_east_0_24.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13101";
$to_file   = $raw_data_dir . "gret_east_1_01.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13102";
$to_file   = $raw_data_dir . "gret_east_1_02.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13103";
$to_file   = $raw_data_dir . "gret_east_1_03.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13104";
$to_file   = $raw_data_dir . "gret_east_1_04.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13105";
$to_file   = $raw_data_dir . "gret_east_1_05.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13106";
$to_file   = $raw_data_dir . "gret_east_1_06.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13107";
$to_file   = $raw_data_dir . "gret_east_1_07.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13108";
$to_file   = $raw_data_dir . "gret_east_1_08.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13109";
$to_file   = $raw_data_dir . "gret_east_1_09.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13110";
$to_file   = $raw_data_dir . "gret_east_1_10.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13111";
$to_file   = $raw_data_dir . "gret_east_1_11.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDX13112";
$to_file   = $raw_data_dir . "gret_east_1_12.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;


# Copy the GOES West files

$from_file = $satep_dir . "MDXX0746";
$to_file   = $raw_data_dir . "gret_west_0_22.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0747";
$to_file   = $raw_data_dir . "gret_west_0_23.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0748";
$to_file   = $raw_data_dir . "gret_west_0_24.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0725";
$to_file   = $raw_data_dir . "gret_west_1_01.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0726";
$to_file   = $raw_data_dir . "gret_west_1_02.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0727";
$to_file   = $raw_data_dir . "gret_west_1_03.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0728";
$to_file   = $raw_data_dir . "gret_west_1_04.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0729";
$to_file   = $raw_data_dir . "gret_west_1_05.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0730";
$to_file   = $raw_data_dir . "gret_west_1_06.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0731";
$to_file   = $raw_data_dir . "gret_west_1_07.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0732";
$to_file   = $raw_data_dir . "gret_west_1_08.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0733";
$to_file   = $raw_data_dir . "gret_west_1_09.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0734";
$to_file   = $raw_data_dir . "gret_west_1_10.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0735";
$to_file   = $raw_data_dir . "gret_west_1_11.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

$from_file = $satep_dir . "MDXX0736";
$to_file   = $raw_data_dir . "gret_west_1_12.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;



# Use the subroutine mget to transfer all of the files on the
# server that match the pattern. Before each file is transfered,
# however, check to see if it already exists. If it does then 
# do not bother transfering it again.

sub mget
  {
  my ($ftp, $fpattern) = @_;

  foreach my $file($ftp->ls($fpattern))
    {
#    if (! (-e $file))
#      {
      printf("  Transfering:  %s\n", $file);
      $ftp->get($file) or warn $ftp->message;
#      }
    }
  }

# end of file
