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

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

  
# This script captures data from an NUCAPS daily data files and writes
# the data to ODS files.

print " ";
print "Capturing NUCAPS data into ODS format";
print " ";
system "date";
print "\n\n";


chdir "/data/data065/nprovs/source/graphics/ods_capture/nucaps_ccr";



# Determine the date of the data to process

$data_day = time();

#$data_day -= (2 * 86400);
$data_day -= (1 * 86400);

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

my $day_of_data = ($year * 10000) + ($mon * 100) + $mday;


# =============================================================================
# Set up the directory and file names

my $source_dir = "/data/data065/nprovs/source/graphics/ods_capture/nucaps_ccr";
my $incoming_data_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/nucaps_ccr";

my $file_name = "nucaps_ccr_" . $day_of_data . ".ods";
my $ods_dir = "/data/data065/nprovs/data/ods/nucaps";
my $ods_file = $ods_dir . "/" . $file_name;

my $ftp_dir = "/net/www/aftp/pub/smcd/opdb/nprovs/ods";


unlink $ods_file;

# =============================================================================
# Create a temporary directory into which the pieces of the file 
# will be written

chdir $source_dir;
mkdir $source_dir . "/ods";


#===============================================================================
# Create the ODS file

symlink $incoming_data_dir, "in.dir" or warn "Cannot link to viirs.dir";
symlink $source_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
symlink $source_dir . "/nucaps_ccr.xml", "parm.file" or warn "Cannot link to parm.file";
symlink $source_dir . "/parm_defs", "parmdefs.dir" or warn "Cannot link to parmdefs.dir";

system "/data/data065/nprovs/source/graphics/ods_capture/nucaps_ccr/CreateOdsFromNucapsCCR.x " . $day_of_data  . " \"NUCAPS-CCR\" \"NUCAPS CCR\"";

unlink "in.dir" or warn "Cannot unlink in.dir";
unlink "out.dir" or warn "Cannot unlink out.dir";
unlink "parm.file" or warn "Cannot unlink parm.file";
unlink "parmdefs.dir" or warn "Cannot unlink parmdefs.dir";



# Copy the filter_options.xml file to the ods directory

system "cp filtering_options.xml ods/filtering_options.xml";


# Zip everything and then delete the temporary directory and everything in it

system "zip -r " . $ods_file . " ods";

system "rm -r " . $source_dir . "/ods";

# end of file
