#!/usr/bin/perl -w
#

# This script captures data from a NOAA IASI daily data files and writes
# the data to ODS files.

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


my $source_dir = "/data/data065/nprovs/source/graphics/ods_capture/mirs";


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



# =============================================================================
# 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 for MetOp-A

my $footprint_size = 45.0;


symlink "/data/data065/nprovs/data/matchup/DailyFiles_incoming/mirs_test_m2.mddf", "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 . "/mirs.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 . "/createODSFromMirs.x \"MIRS Test MetOp-A\" " . $day_of_data . " " . $footprint_size;


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 mirs_test_" . $day_of_data . ".ods ods";

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

# end of file
