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



# This script is the start of the NPROVS collocation process. It copies radiosonde data from the
# unified.radiosonde format, creates date directories in the nprovs collocation directory, and
# create netCDF files from the data in the unified radiosonde.


$work_dir        = "/data/data065/nprovs/source/collocate/NprovsCollocator/SondeCollocator/work_dir";
$source_dir      = "/data/data065/nprovs/source/collocate/NprovsCollocator/SondeCollocator";
$orbital_archive = "/data/data599/orbital_archive";
$output_dir      = "/data/data599/collocations/nprovs";


#print "\n\nProcessing the GOES data\n\n";

chdir($work_dir);


# Determine which date to process. This will normally be yesterday, but that
# can be overridden.

$yesterday = time();
#$yesterday -= (1 * 86400);
$yesterday -= (3 * 86400);

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

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


# Get the day before the date to process

$day_before = time();
#$day_before -= (2 * 86400);
$day_before -= (4 * 86400);

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

$date_before = sprintf("%4d%02d%02d", $year, $mon, $mday);


# Get the day after the date to process

$day_after = time();
$day_after -= (2 * 86400);

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

$date_after = sprintf("%4d%02d%02d", $year, $mon, $mday);


printf("\nProcessing radiosondes on %d\n", $date_to_process);
printf("\nLooking in the files for:\n");
printf("   Day Before:       %d\n", $date_before);
printf("   Date To Process:  %d\n", $date_to_process);
printf("   Day After:        %d\n\n", $date_after);


# Copy the files for the dates to the working directory and unzip them

$from_file            = sprintf("%s/unified.radiosonde/unified.radiosonde.reports.%d.gz", $orbital_archive, $date_before);
$gzip_file            = sprintf("%s/unified.radiosonde.reports.%d.gz", $work_dir, $date_before);
$day_before_raob_file = sprintf("%s/unified.radiosonde.reports.%d", $work_dir, $date_before);
#copy($from_file, $gzip_file);
#system "gunzip " . $gzip_file;

$from_file         = sprintf("%s/unified.radiosonde/unified.radiosonde.reports.%d.gz", $orbital_archive, $date_to_process);
$gzip_file         = sprintf("%s/unified.radiosonde.reports.%d.gz", $work_dir, $date_to_process);
$baseday_raob_file = sprintf("%s/unified.radiosonde.reports.%d", $work_dir, $date_to_process);
#copy($from_file, $gzip_file);
#system "gunzip " . $gzip_file;

$from_file           = sprintf("%s/unified.radiosonde/unified.radiosonde.reports.%d.gz", $orbital_archive, $date_after);
$gzip_file           = sprintf("%s/unified.radiosonde.reports.%d.gz", $work_dir, $date_after);
$day_after_raob_file = sprintf("%s/unified.radiosonde.reports.%d", $work_dir, $date_after);
#copy($from_file, $gzip_file);
#system "gunzip " . $gzip_file;


# Run the program to copy the data to a netCDF file

symlink $day_before_raob_file, "raob_before.file" or warn "Cannot link $day_before_raob_file to raob_before.file";
symlink $baseday_raob_file, "raob_baseday.file" or warn "Cannot link $baseday_raob_file to raob_baseday.file";
symlink $day_after_raob_file, "raob_after.file" or warn "Cannot link $day_after_raob_file to raob_after.file";
symlink $output_dir, "out.dir" or warn "Cannot link $output_dir to out.dir";


system $source_dir . "/SondeCollocator.x " . $date_before . ' ' . $date_to_process . ' ' . $date_after;


unlink "raob_before.file" or warn "Cannot unlink raob_before.file";
unlink "raob_baseday.file" or warn "Cannot unlink raob_baseday.file";
unlink "raob_after.file" or warn "Cannot unlink raob_after.file";
unlink "out.dir" or warn "Cannot unlink out.dir";


#  #system "run_ncdump";

# end of file
