#!/usr/bin/perl -w
#
use File::Copy;
use Time::Local;

BEGIN
  {
  $ENV{LD_LIBRARY_PATH}="LD_LIBRARY_PATH:/data/starfs1/libs/netcdf-4.2/lib:/data/starfs1/libs/hdf5-1.8.7/lib";
  }


$work_dir     = "/data/data065/nprovs/source/data_transfer/cosmic2_star";
$source_dir   = "/data/data065/nprovs/source/data_transfer/cosmic2_star";
$raw_data_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2_star";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";



print "==============================================================\n";
print "Transfering COSMIC-2 STAR files";
print " ";
system "date";
print "\n\n";


# ----------------------------------------------------
# Determine the date of the data that will be transfered
# (default is yesterday)

my $now = time();
#my $data_date = $now - (1 * 86400);
my $data_date = $now - (2 * 86400);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($data_date);

$year = 1900 + $year;
$yday = $yday + 1;

my $date_to_process = sprintf "%04d%02d%02d", $year, ($mon+1), $mday;

printf("\nDate to process: %d/%02d/%04d  (%d)\n", ($mon+1), $mday, $year, $yday);



# ----------------------------------------------------
# Switch to the COSMIC-2 raw data directory

chdir $raw_data_dir;



# ----------------------------------------------------
# Remove any previous files from the raw data directory

printf("\nRemoving previous netCDF files from the raw data directory\n");

system "rm " . $raw_data_dir . "/wetPrf*_nc";
system "rm " . $raw_data_dir . "/atmPrf*_nc";
system "rm " . $raw_data_dir . "/*.txt";


# ----------------------------------------------------
# wetprf

printf("\n\nTransferring the wetPrf files...\n");

$from_file_wet = sprintf("/net/www/aftp/pub/smcd/scda/GNSSRO/COSMIC2/wetPrf/%04d-%02d-%02d.tar.gz", $year, ($mon+1), $mday);

$gzip_file_wet = sprintf("%s/wetprf.tar.gz", $raw_data_dir);
$tar_file_wet = sprintf("%s/wetprf.tar", $raw_data_dir);

copy($from_file_wet, $gzip_file_wet);

system "gunzip " . $gzip_file_wet;
system "tar -xvf " . $tar_file_wet;
unlink $tar_file_wet;


# ----------------------------------------------------
# atmprf

printf("\n\nTransferring the atmPrf files...\n");

$from_file_atm = sprintf("/net/www/aftp/pub/smcd/scda/GNSSRO/COSMIC2/atmPrf/%04d-%02d-%02d.tar.gz", $year, ($mon+1), $mday);

$gzip_file_atm = sprintf("%s/atmprf.tar.gz", $raw_data_dir);
$tar_file_atm = sprintf("%s/atmprf.tar", $raw_data_dir);

copy($from_file_atm, $gzip_file_atm);

system "gunzip " . $gzip_file_atm;
system "tar -xvf " . $tar_file_atm;
unlink $tar_file_atm;


# ----------------------------------------------------
# Create the daily file

$cddf_file = sprintf("%s/cosmic2_star_redo.cddf", $nprovs_dir);

# Remove the previous daily file

unlink $cddf_file;



# ----------------------------------------------------
# Run Cosmic2StarToCDDF 

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

system $source_dir . "/Cosmic2StarToCDDF.x " . $date_to_process . " " . $raw_data_dir;

unlink "out.file" or warn "Cannot unlink the out.file";



# ----------------------------------------------------
# Copy the daily file to the orbital archives

printf("Gzipping the daily file\n");

$prezip_file = sprintf("%s/cosmic2_star_%d.cddf", $nprovs_dir, $date_to_process);

copy($cddf_file, $prezip_file);

$gzip_file = sprintf("%s/cosmic2_star_%d.cddf.gz", $nprovs_dir, $date_to_process);

system "gzip " . $prezip_file;


$to_file_1 = sprintf("/data/data599/orbital_archive/cosmic2_star/cosmic2_star_%d.cddf.gz", $date_to_process);
$to_file_2 = sprintf("/data/data215/nprovs/data/orbital_archive/cosmic2_star/cosmic2_star_%d.cddf.gz", $date_to_process);

#$to_file_2 = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive/cosmic2_star/cosmic2_star_%d.cddf.gz", $date_to_process);

printf("Copying the daily file to the orbital archives\n\n");

copy($gzip_file, $to_file_1);

copy($gzip_file, $to_file_2);

unlink $gzip_file;




# ----------------------------------------------------
# Run the ODS capture for the COSMIC-2 data

system "/data/data065/nprovs/scripts/graphics/run_cosmic2_star_ods_capture";

# end of file
