#!/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 
# SATEPSANONE 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 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/";

my $gddf_file = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/goes.gddf";

my $source_dir = "/data/data065/nprovs/source/data_transfer/goes";




# Remove older files from the raw_data directories

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

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



# Transfer the GOES GRETfiles from the ftp server

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("/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 . "MDX13113";
$to_file   = $raw_data_dir . "gret_east_1_13.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;

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

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

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

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

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

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

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

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

$from_file = $satep_dir . "MDX13122";
$to_file   = $raw_data_dir . "gret_east_1_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_1_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_1_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_2_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_2_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_2_03.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;


# Copy the GOES West files

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

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

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

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

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

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

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

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

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

$from_file = $satep_dir . "MDXX0746";
$to_file   = $raw_data_dir . "gret_west_1_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_1_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_1_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_2_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_2_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_2_03.dat";
copy($from_file, $to_file) or warn "*** Unable to copy " . $from_file;





# ===========================================================
# Copy the hourly files to the GDDF

chdir $source_dir;


# Remove the previous GDDF

unlink $gddf_file or warn "Cannot delete the gddf file\n";



# Calculate the date of the data to be processed. By default, this
# is usually yesterday. To override, replace $data_date with the
# date in the form YYYYMMDD

#$data_day = time();
#$data_day -= 86400;

#($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtim#e($data_day);
#$year += 1900;
#$mon++;
#$sec = 0;
#$min = 0;
#$hour = 12;
#$wday = 0;
#$yday = 0;
#$isdst = 0;

#$data_date = sprintf("%4d%02d%02d", $year, $mon, $mday);
##$data_date = 20080730;

#printf("Date of data to process:  %s\n\n", $data_date);



# Link the actual files names to the symbolic file names used 
# within the main program

#east

symlink $raw_data_dir . "gret_east_0_22.dat", "east_01.file" or warn "Cannot link gret_east_0_22.dat to east_01.file";
symlink $raw_data_dir . "gret_east_0_23.dat", "east_02.file" or warn "Cannot link gret_east_0_23.dat to east_02.file";
symlink $raw_data_dir . "gret_east_0_24.dat", "east_03.file" or warn "Cannot link gret_east_0_24.dat to east_03.file";
symlink $raw_data_dir . "gret_east_1_01.dat", "east_04.file" or warn "Cannot link gret_east_1_01.dat to east_04.file";
symlink $raw_data_dir . "gret_east_1_02.dat", "east_05.file" or warn "Cannot link gret_east_1_02.dat to east_05.file";
symlink $raw_data_dir . "gret_east_1_03.dat", "east_06.file" or warn "Cannot link gret_east_1_03.dat to east_06.file";
symlink $raw_data_dir . "gret_east_1_04.dat", "east_07.file" or warn "Cannot link gret_east_1_04.dat to east_07.file";
symlink $raw_data_dir . "gret_east_1_05.dat", "east_08.file" or warn "Cannot link gret_east_1_05.dat to east_08.file";
symlink $raw_data_dir . "gret_east_1_06.dat", "east_09.file" or warn "Cannot link gret_east_1_06.dat to east_09.file";
symlink $raw_data_dir . "gret_east_1_07.dat", "east_10.file" or warn "Cannot link gret_east_1_07.dat to east_10.file";
symlink $raw_data_dir . "gret_east_1_08.dat", "east_11.file" or warn "Cannot link gret_east_1_08.dat to east_11.file";
symlink $raw_data_dir . "gret_east_1_09.dat", "east_12.file" or warn "Cannot link gret_east_1_09.dat to east_12.file";
symlink $raw_data_dir . "gret_east_1_10.dat", "east_13.file" or warn "Cannot link gret_east_1_10.dat to east_13.file";
symlink $raw_data_dir . "gret_east_1_11.dat", "east_14.file" or warn "Cannot link gret_east_1_11.dat to east_14.file";
symlink $raw_data_dir . "gret_east_1_12.dat", "east_15.file" or warn "Cannot link gret_east_1_12.dat to east_15.file";
symlink $raw_data_dir . "gret_east_1_13.dat", "east_16.file" or warn "Cannot link gret_east_1_13.dat to east_16.file";
symlink $raw_data_dir . "gret_east_1_14.dat", "east_17.file" or warn "Cannot link gret_east_1_14.dat to east_17.file";
symlink $raw_data_dir . "gret_east_1_15.dat", "east_18.file" or warn "Cannot link gret_east_1_15.dat to east_18.file";
symlink $raw_data_dir . "gret_east_1_16.dat", "east_19.file" or warn "Cannot link gret_east_1_16.dat to east_19.file";
symlink $raw_data_dir . "gret_east_1_17.dat", "east_20.file" or warn "Cannot link gret_east_1_17.dat to east_20.file";
symlink $raw_data_dir . "gret_east_1_18.dat", "east_21.file" or warn "Cannot link gret_east_1_18.dat to east_21.file";
symlink $raw_data_dir . "gret_east_1_19.dat", "east_22.file" or warn "Cannot link gret_east_1_19.dat to east_22.file";
symlink $raw_data_dir . "gret_east_1_20.dat", "east_23.file" or warn "Cannot link gret_east_1_20.dat to east_23.file";
symlink $raw_data_dir . "gret_east_1_21.dat", "east_24.file" or warn "Cannot link gret_east_1_21.dat to east_24.file";
symlink $raw_data_dir . "gret_east_1_22.dat", "east_25.file" or warn "Cannot link gret_east_1_22.dat to east_25.file";
symlink $raw_data_dir . "gret_east_1_23.dat", "east_26.file" or warn "Cannot link gret_east_1_23.dat to east_26.file";
symlink $raw_data_dir . "gret_east_1_24.dat", "east_27.file" or warn "Cannot link gret_east_1_24.dat to east_27.file";
symlink $raw_data_dir . "gret_east_2_01.dat", "east_28.file" or warn "Cannot link gret_east_2_01.dat to east_28.file";
symlink $raw_data_dir . "gret_east_2_02.dat", "east_29.file" or warn "Cannot link gret_east_2_02.dat to east_29.file";
symlink $raw_data_dir . "gret_east_2_03.dat", "east_30.file" or warn "Cannot link gret_east_2_03.dat to east_30.file";

# west

symlink $raw_data_dir . "gret_west_0_22.dat", "west_01.file" or warn "Cannot link gret_west_0_22.dat to west_01.file";
symlink $raw_data_dir . "gret_west_0_23.dat", "west_02.file" or warn "Cannot link gret_west_0_23.dat to west_02.file";
symlink $raw_data_dir . "gret_west_0_24.dat", "west_03.file" or warn "Cannot link gret_west_0_24.dat to west_03.file";
symlink $raw_data_dir . "gret_west_1_01.dat", "west_04.file" or warn "Cannot link gret_west_1_01.dat to west_04.file";
symlink $raw_data_dir . "gret_west_1_02.dat", "west_05.file" or warn "Cannot link gret_west_1_02.dat to west_05.file";
symlink $raw_data_dir . "gret_west_1_03.dat", "west_06.file" or warn "Cannot link gret_west_1_03.dat to west_06.file";
symlink $raw_data_dir . "gret_west_1_04.dat", "west_07.file" or warn "Cannot link gret_west_1_04.dat to west_07.file";
symlink $raw_data_dir . "gret_west_1_05.dat", "west_08.file" or warn "Cannot link gret_west_1_05.dat to west_08.file";
symlink $raw_data_dir . "gret_west_1_06.dat", "west_09.file" or warn "Cannot link gret_west_1_06.dat to west_09.file";
symlink $raw_data_dir . "gret_west_1_07.dat", "west_10.file" or warn "Cannot link gret_west_1_07.dat to west_10.file";
symlink $raw_data_dir . "gret_west_1_08.dat", "west_11.file" or warn "Cannot link gret_west_1_08.dat to west_11.file";
symlink $raw_data_dir . "gret_west_1_09.dat", "west_12.file" or warn "Cannot link gret_west_1_09.dat to west_12.file";
symlink $raw_data_dir . "gret_west_1_10.dat", "west_13.file" or warn "Cannot link gret_west_1_10.dat to west_13.file";
symlink $raw_data_dir . "gret_west_1_11.dat", "west_14.file" or warn "Cannot link gret_west_1_11.dat to west_14.file";
symlink $raw_data_dir . "gret_west_1_12.dat", "west_15.file" or warn "Cannot link gret_west_1_12.dat to west_15.file";
symlink $raw_data_dir . "gret_west_1_13.dat", "west_16.file" or warn "Cannot link gret_west_1_13.dat to west_16.file";
symlink $raw_data_dir . "gret_west_1_14.dat", "west_17.file" or warn "Cannot link gret_west_1_14.dat to west_17.file";
symlink $raw_data_dir . "gret_west_1_15.dat", "west_18.file" or warn "Cannot link gret_west_1_15.dat to west_18.file";
symlink $raw_data_dir . "gret_west_1_16.dat", "west_19.file" or warn "Cannot link gret_west_1_16.dat to west_19.file";
symlink $raw_data_dir . "gret_west_1_17.dat", "west_20.file" or warn "Cannot link gret_west_1_17.dat to west_20.file";
symlink $raw_data_dir . "gret_west_1_18.dat", "west_21.file" or warn "Cannot link gret_west_1_18.dat to west_21.file";
symlink $raw_data_dir . "gret_west_1_19.dat", "west_22.file" or warn "Cannot link gret_west_1_19.dat to west_22.file";
symlink $raw_data_dir . "gret_west_1_20.dat", "west_23.file" or warn "Cannot link gret_west_1_20.dat to west_23.file";
symlink $raw_data_dir . "gret_west_1_21.dat", "west_24.file" or warn "Cannot link gret_west_1_21.dat to west_24.file";
symlink $raw_data_dir . "gret_west_1_22.dat", "west_25.file" or warn "Cannot link gret_west_1_22.dat to west_25.file";
symlink $raw_data_dir . "gret_west_1_23.dat", "west_26.file" or warn "Cannot link gret_west_1_23.dat to west_26.file";
symlink $raw_data_dir . "gret_west_1_24.dat", "west_27.file" or warn "Cannot link gret_west_1_24.dat to west_27.file";
symlink $raw_data_dir . "gret_west_2_01.dat", "west_28.file" or warn "Cannot link gret_west_2_01.dat to west_28.file";
symlink $raw_data_dir . "gret_west_2_02.dat", "west_29.file" or warn "Cannot link gret_west_2_02.dat to west_29.file";
symlink $raw_data_dir . "gret_west_2_03.dat", "west_30.file" or warn "Cannot link gret_west_2_03.dat to west_30.file";

# Other files

symlink $gddf_file, "out.file" or warn "Cannot link $gddf_file to out.file";

symlink $source_dir . "monitor.file", "monitoring.file" or warn "Cannot link monitor.file to monitoring.file";


# Run the main program

#system $source_dir . "/GretToGDDF.x east";
#system $source_dir . "/GretToGDDF.x west";
system $source_dir . "/GretToGDDF.x both";


# Unlink the symbolic files

unlink "out.file" or warn "Cannot delete out.file";
unlink "monitoring.file" or warn "Cannot delete monitoring.file";

unlink "east_01.file" or warn "Cannot delete east_01.file";
unlink "east_02.file" or warn "Cannot delete east_02.file";
unlink "east_03.file" or warn "Cannot delete east_03.file";
unlink "east_04.file" or warn "Cannot delete east_04.file";
unlink "east_05.file" or warn "Cannot delete east_05.file";
unlink "east_06.file" or warn "Cannot delete east_06.file";
unlink "east_07.file" or warn "Cannot delete east_07.file";
unlink "east_08.file" or warn "Cannot delete east_08.file";
unlink "east_09.file" or warn "Cannot delete east_09.file";
unlink "east_10.file" or warn "Cannot delete east_10.file";
unlink "east_11.file" or warn "Cannot delete east_11.file";
unlink "east_12.file" or warn "Cannot delete east_12.file";
unlink "east_13.file" or warn "Cannot delete east_13.file";
unlink "east_14.file" or warn "Cannot delete east_14.file";
unlink "east_15.file" or warn "Cannot delete east_15.file";
unlink "east_16.file" or warn "Cannot delete east_16.file";
unlink "east_17.file" or warn "Cannot delete east_17.file";
unlink "east_18.file" or warn "Cannot delete east_18.file";
unlink "east_19.file" or warn "Cannot delete east_19.file";
unlink "east_20.file" or warn "Cannot delete east_20.file";
unlink "east_21.file" or warn "Cannot delete east_21.file";
unlink "east_22.file" or warn "Cannot delete east_22.file";
unlink "east_23.file" or warn "Cannot delete east_23.file";
unlink "east_24.file" or warn "Cannot delete east_24.file";
unlink "east_25.file" or warn "Cannot delete east_25.file";
unlink "east_26.file" or warn "Cannot delete east_26.file";
unlink "east_27.file" or warn "Cannot delete east_27.file";
unlink "east_28.file" or warn "Cannot delete east_28.file";
unlink "east_29.file" or warn "Cannot delete east_29.file";
unlink "east_30.file" or warn "Cannot delete east_30.file";

unlink "west_01.file" or warn "Cannot delete west_01.file";
unlink "west_02.file" or warn "Cannot delete west_02.file";
unlink "west_03.file" or warn "Cannot delete west_03.file";
unlink "west_04.file" or warn "Cannot delete west_04.file";
unlink "west_05.file" or warn "Cannot delete west_05.file";
unlink "west_06.file" or warn "Cannot delete west_06.file";
unlink "west_07.file" or warn "Cannot delete west_07.file";
unlink "west_08.file" or warn "Cannot delete west_08.file";
unlink "west_09.file" or warn "Cannot delete west_09.file";
unlink "west_10.file" or warn "Cannot delete west_10.file";
unlink "west_11.file" or warn "Cannot delete west_11.file";
unlink "west_12.file" or warn "Cannot delete west_12.file";
unlink "west_13.file" or warn "Cannot delete west_13.file";
unlink "west_14.file" or warn "Cannot delete west_14.file";
unlink "west_15.file" or warn "Cannot delete west_15.file";
unlink "west_16.file" or warn "Cannot delete west_16.file";
unlink "west_17.file" or warn "Cannot delete west_17.file";
unlink "west_18.file" or warn "Cannot delete west_18.file";
unlink "west_19.file" or warn "Cannot delete west_19.file";
unlink "west_20.file" or warn "Cannot delete west_20.file";
unlink "west_21.file" or warn "Cannot delete west_21.file";
unlink "west_22.file" or warn "Cannot delete west_22.file";
unlink "west_23.file" or warn "Cannot delete west_23.file";
unlink "west_24.file" or warn "Cannot delete west_24.file";
unlink "west_25.file" or warn "Cannot delete west_25.file";
unlink "west_26.file" or warn "Cannot delete west_26.file";
unlink "west_27.file" or warn "Cannot delete west_27.file";
unlink "west_28.file" or warn "Cannot delete west_28.file";
unlink "west_29.file" or warn "Cannot delete west_29.file";
unlink "west_30.file" or warn "Cannot delete west_30.file";


#===================================================================
# Copy the daily files to the orbital archive and the backup archive

$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";
$archive_dir2  = "/data/data215/nprovs/data/orbital_archive";
$archive_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive";


printf("Copying the daily files to the archive directory\n\n");


# Get yesterday's date
$data_day = time();
$data_day -= 86400;

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($data_day);
$year += 1900;
$mon++;
$sec = 0;
$min = 0;
$hour = 0;
$wday = 0;
$yday = 0;
$isdst = 0;

$from_file = $nprovs_dir . "/goes.gddf";
$to_file = $archive_dir . sprintf("/goes/goes_%d%02d%02d.gddf", $year, $mon, $mday);

printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);

copy($from_file, $to_file);

system "gzip $to_file";



$to_file = $archive_dir2 . sprintf("/goes/goes_%d%02d%02d.gddf", $year, $mon, $mday);

printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);

copy($from_file, $to_file);

system "gzip $to_file";






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
