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

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

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


# Determine the date of the data to process for the default image captures

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

#my $hours_to_process = "0 6 12 18";
my $hours_to_process = "12";



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

my $source_dir = "/data/data065/nprovs/source/graphics/ods_capture/gfs_v2";
my $ods_dir = "/data/data065/nprovs/source/data_transfer/gfs";

$file_name = "gfs_v2_" . $day_of_data . ".ods";
$ods_file = $ods_dir . "/" . $file_name;

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


# =============================================================================
# Create an ODS file for the GFS

symlink "/data/data065/nprovs/source/data_transfer/gfs/gfs_47lvl.gadf", "in.file" or warn "Cannot link to in.file";
symlink $source_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
symlink $source_dir . "/gfs.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 $source_dir . "/createODSFromGFS.x \"GFS V2 Test\" " . $day_of_data . " " . $hours_to_process;

unlink "in.file" or warn "Cannot unlink in.file";
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
