#!/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 and then it
# does the processing to copy the hourly files into a GOES Daily
# Data File (GDDF).
#
# 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 13-24z files from the current day and 
# the 01Z, 02z and 03z files from the next day.

print "\n";
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 $raw_data_dir_east = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/goes/satep_east";

my $raw_data_dir_west = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/goes/satep_west";


# Remove older files from the raw_data directories

my @easttempfiles = glob $raw_data_dir_east . "/MDX*";

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

my @westtempfiles = glob $raw_data_dir_west . "/MDX*";

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



# Transfer the GOES East files from the ftp server

printf("\nTransfering GOES East data\n");
chdir $raw_data_dir_east;

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

my $pattern = "MDX*";

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("goes_sfov/goes13/md")
  or die "Cannot change directories: ", $ftp->message;

mget($ftp, $pattern);


# Transfer the GOES West files from the ftp server

printf("\nTransfering GOES West data\n");
chdir $raw_data_dir_west;

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

$pattern = "MDX*";

$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("goes_sfov/goes15/md")
  or die "Cannot change directories: ", $ftp->message;

mget($ftp, $pattern);


$ftp->quit;

printf("\n");





printf("\nProcessing completed\n");






# 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
