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


$baseday_minus_one = $ARGV[0];
$baseday           = $ARGV[1];
$baseday_plus_one  = $ARGV[2];

printf("Date to be processed:  %d   %d   %d\n\n", $baseday_minus_one, $baseday, $baseday_plus_one);


#$source_dir = "/data/data599/orbital_archive/unified.radiosonde";
$temp_dir   = "/data/data065/nprovs/scripts/data_input/reprocess/special_capture/data";
$target_dir = "/net/www/aftp/pub/smcd/opdb/mpettey";



# Copy the files for the selected date from SCDR to the incoming directory

printf("\nCopying the data (%d) from SCDR...\n", $baseday_minus_one);

$year  = $baseday_minus_one / 10000;
$month = ($baseday_minus_one % 10000) / 100;
$day   = $baseday_minus_one % 100;

$date_string = sprintf("%04d-%02d-%02d", $year, $month, $day);

system "/data/starfs1/bin/scdr-files -t NUCAPS-EDR --re-file _j01_ -d " . $date_string . " | xargs -L100 -P2 cp -t " . $temp_dir;


printf("Copying the data (%d) from SCDR...\n", $baseday);

$year  = $baseday / 10000;
$month = ($baseday % 10000) / 100;
$day   = $baseday % 100;

$date_string = sprintf("%04d-%02d-%02d", $year, $month, $day);

system "/data/starfs1/bin/scdr-files -t NUCAPS-EDR --re-file _j01_ -d " . $date_string . " | xargs -L100 -P2 cp -t " . $temp_dir;


printf("Copying the data (%d) from SCDR...\n\n", $baseday_plus_one);

$year  = $baseday_plus_one / 10000;
$month = ($baseday_plus_one % 10000) / 100;
$day   = $baseday_plus_one % 100;

$date_string = sprintf("%04d-%02d-%02d", $year, $month, $day);

system "/data/starfs1/bin/scdr-files -t NUCAPS-EDR --re-file _j01_ -d " . $date_string . " | xargs -L100 -P2 cp -t " . $temp_dir;



# Delete some of the baseday-1 and baseday+1 files

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d0*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d10*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d11*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d12*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d13*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d14*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d15*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d16*", $temp_dir, $baseday_minus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d17*", $temp_dir, $baseday_minus_one);
system $delete;


$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d07*", $temp_dir, $baseday_plus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d08*", $temp_dir, $baseday_plus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d09*", $temp_dir, $baseday_plus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d1*", $temp_dir, $baseday_plus_one);
system $delete;

$delete = sprintf("rm %s/NUCAPS-EDR_v3r0_j01_s%d2*", $temp_dir, $baseday_plus_one);
system $delete;



# Tar the file

chdir $temp_dir;

system "tar -cvf nucaps_noaa20.tar *.nc";

# Gzip the file

system "gzip nucaps_noaa20.tar";


# Copy the tar file to the target directory

$from_file = sprintf("%s/nucaps_noaa20.tar.gz", $temp_dir);
$to_file   = sprintf("%s/nucaps_noaa20_%d.tar.gz", $target_dir, $baseday);
copy($from_file, $to_file);


# Remove the files from the temp directory

system "rm *.nc";
system "rm *.gz";



#-------------------------------------------------------------------------
# Remove all previous files from the input raw data directory

#my @tempfiles = glob $incoming_dir . "/" . $incoming_template . "*.nc";

#foreach $file (@tempfiles)
#  {
#  unlink $file;
#  }



# end of file
