#!/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 "\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/oper_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");
chdir $satep_dir;

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

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

mget($ftp, $pattern);

$ftp->quit;

printf("\n");



# Copy the GOES East files

$from_file = $satep_dir . "MDXX0722";
$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 . "MDXX0723";
$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 . "MDXX0724";
$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 . "MDXX0701";
$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 . "MDXX0702";
$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 . "MDXX0703";
$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 . "MDXX0704";
$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 . "MDXX0705";
$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 . "MDXX0706";
$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 . "MDXX0707";
$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 . "MDXX0708";
$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 . "MDXX0709";
$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 . "MDXX0710";
$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 . "MDXX0711";
$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 . "MDXX0712";
$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
