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



# ----------------------------------------------------
# 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;



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

$cddf_file = sprintf("%s/cosmic2_star.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 . "/Test.x " . $date_to_process . " " . $raw_data_dir;

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


# end of file
