#!/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";
$source_dir   = "/data/data065/nprovs/source/data_transfer/cosmic2";
$raw_data_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";



print "==============================================================\n";
print "Transfering COSMIC-2 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 - (3 * 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\n", ($mon+1), $mday, $year);



# ----------------------------------------------------
# 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 . "/avnPrf*_nc";
system "rm " . $raw_data_dir . "/atmPrf*_nc";
system "rm " . $raw_data_dir . "/wetPf2*_nc";


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

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

# Run the wget line

$wget = sprintf("wget -q http://data.cosmic.ucar.edu/gnss-ro/cosmic2/nrt/level2/%04d/%03d/wetPf2_nrt_%04d_%03d.tar.gz --no-check-certificate", $year, $yday, $year, $yday);

printf("%s\n", $wget);

system $wget;

# Copy the tar to the temporary older data directory

$from_file = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2/wetPf2_nrt_%04d_%03d.tar.gz", $year, $yday);

$to_file = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2_older/wetPf2_nrt_%04d_%03d.tar.gz", $year, $yday);

copy($from_file, $to_file);

# Gunzip and Untar the tarball

$gunzip = sprintf("gunzip wetPf2_nrt_%04d_%03d.tar.gz", $year, $yday);
system $gunzip;

$untar = sprintf("tar -xvf wetPf2_nrt_%04d_%03d.tar", $year, $yday);
system $untar;

# Delete the tar file

$deletetar = sprintf("rm wetPf2_nrt_%04d_%03d.tar", $year, $yday);
system $deletetar;



# ----------------------------------------------------
# avnprf

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

# Run the wget line

$wget = sprintf("wget -q http://data.cosmic.ucar.edu/gnss-ro/cosmic2/nrt/level2/%04d/%03d/avnPrf_nrt_%04d_%03d.tar.gz --no-check-certificate", $year, $yday, $year, $yday);

printf("%s\n", $wget);

system $wget;

# Copy the tar to the temporary older data directory

$from_file = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2/avnPrf_nrt_%04d_%03d.tar.gz", $year, $yday);

$to_file = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2_older/avnPrf_nrt_%04d_%03d.tar.gz", $year, $yday);

copy($from_file, $to_file);

# Gunzip and Untar the tarball

$gunzip = sprintf("gunzip avnPrf_nrt_%04d_%03d.tar.gz", $year, $yday);
system $gunzip;

$untar = sprintf("tar -xvf avnPrf_nrt_%04d_%03d.tar", $year, $yday);
system $untar;

# Delete the tar file

$deletetar = sprintf("unlink avnPrf_nrt_%04d_%03d.tar", $year, $yday);
system $deletetar;


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

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

# Run the wget line

$wget = sprintf("wget -q http://data.cosmic.ucar.edu/gnss-ro/cosmic2/nrt/level2/%04d/%03d/atmPrf_nrt_%04d_%03d.tar.gz --no-check-certificate", $year, $yday, $year, $yday);

printf("%s\n", $wget);

system $wget;

# Copy the tar to the temporary older data directory

$from_file = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2/atmPrf_nrt_%04d_%03d.tar.gz", $year, $yday);

$to_file = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2_older/atmPrf_nrt_%04d_%03d.tar.gz", $year, $yday);

copy($from_file, $to_file);

# Gunzip and Untar the tarball

$gunzip = sprintf("gunzip atmPrf_nrt_%04d_%03d.tar.gz", $year, $yday);
system $gunzip;

$untar = sprintf("tar -xvf atmPrf_nrt_%04d_%03d.tar", $year, $yday);
system $untar;

# Delete the tar file

$deletetar = sprintf("rm atmPrf_nrt_%04d_%03d.tar", $year, $yday);
system $deletetar;



# ----------------------------------------------------

# Create the daily file

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

# Remove the previous daily file

unlink $cddf_file;


# Run Cosmic2toCDDF 

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

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

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


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

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

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

copy($cddf_file, $prezip_file);

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

system "gzip " . $prezip_file;


$to_file_1 = sprintf("/data/data215/nprovs/data/orbital_archive/cosmic2/cosmic2_%d.cddf.gz", $date_to_process);

$to_file_2 = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive/cosmic2/cosmic2_%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_ods_capture";


# end of file
