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

# This script extracts a single day of data from an ATOVS RRODF file
# and creates a single day file



#-------------------------------------------------------------------------
# Variables that can be changed to affect the date to be processed, the location
# of the input data and the location of the output

# Uncomment $year, $mon and $mday to process a specific day. If they are
# commented, then yesterday's data will be processed.

# ***********************
# ***********************
# ***********************
$year = 2022;
$mon  = 1;
$mday = 25;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************


$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";
$archive_dir  = "/data/data599/orbital_archive/atovs_m1";
$archive_dir2 = "/data/data215/nprovs/data/orbital_archive/atovs_m1";
#$archive_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive/atovs_m1";

$work_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/atovs/m1";

$source_dir = "/data/data065/nprovs/source/data_transfer/atovs";

$ods_source_dir = "/data/data065/nprovs/source/graphics/ods_capture/atovs/atovs1000";
$ods_dir = "/data/data065/nprovs/data/ods/atovs";
#$ftp_dir = "/net/www/aftp/pub/smcd/opdb/nprovs/ods";



chdir $work_dir or warn "Could not chdir to the raw data directory: ".$work_dir;


print "\nExtracting a single day of data from an ATOVS RRODF\n";


#-------------------------------------------------------------------------
# If $year, $mon or $mday are set, then that date will be that date
# that is processed. Otherwise, the date to process will be yesterday.

$process_specific_date = 0;

if (($year == 0) || ($mon == 0) || ($mday == 0))
  {
  $data_day = time();
  $data_day -= 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;

  $process_specific_date = 1;
  }

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

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



#-------------------------------------------------------------------------

my $big_file   = $nprovs_dir . "/rrodf1000.m1.bkp";
my $small_file = $nprovs_dir . sprintf("/atovs_rrodf1000_%4d%02d%02d.m1", $year, $mon, $mday);

unlink $small_file;


symlink $big_file, "in.file" or warn "Cannot link to in.file";
symlink $small_file, "out.file" or warn "Cannot link to out.file";

system $source_dir . "/ExtractDayFromRRODF1000.x " . $day_of_data;

unlink "in.file" or warn "Cannot unlink in.file";
unlink "out.file" or warn "Cannot unlink out.file";



#-------------------------------------------------------------------------
# Create an ODS file

$ods_file_name = sprintf("atovs_metopb_%d%02d%02d.ods", $year, $mon, $mday);
$ods_file = $ods_dir . "/" . $ods_file_name;


# Create the temporpary directory into which everything will be written

mkdir $work_dir . "/ods";


# Run the main program
symlink $small_file, "in.file" or warn "Cannot link to in.file";
symlink $work_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
symlink $ods_source_dir . "/atovs1000.xml", "parm.file" or warn "Cannot link to parm.file";
symlink $ods_source_dir . "/parm_defs", "parmdefs.dir" or warn "Cannot link to parmdefs.dir";

system $ods_source_dir . "/createODSFromAtovs1000.x \"ATOVS MetOp-B (M1)\" " . $day_of_data;

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 " . $ods_source_dir . "/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 " . $work_dir . "/ods";



#-------------------------------------------------------------------------
# Create an image from the ODS file and send the image to the STAR web site
# for routine monitoring

# Build the command line executable for ascending and then run it

$input_file   = sprintf("file:%s", $ods_file);
$parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/atovs_asc.xml");
$output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/atovs_metopb_asc_%d.png", $day_of_data);
$thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/atovs_metopb_asc_%d_thumb.png", $day_of_data);
$thumb_width  = sprintf("thumbwidth:100");
$thumb_height = sprintf("thumbheight:75");
$verbose      = sprintf("verbose:no");
$file_info    = sprintf("file_info:no");

$exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

system $exec;


# Build the command line executable for descending and then run it

$input_file   = sprintf("file:%s", $ods_file);
$parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/atovs_des.xml");
$output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/atovs_metopb_des_%d.png", $day_of_data);
$thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/atovs_metopb_des_%d_thumb.png", $day_of_data);
$thumb_width  = sprintf("thumbwidth:100");
$thumb_height = sprintf("thumbheight:75");
$verbose      = sprintf("verbose:no");
$file_info    = sprintf("file_info:no");

$exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

system $exec;



#-------------------------------------------------------------------------
# Gzip the daily file and copy it to the archives

printf("\n\nCopying the daily file to the archive directories\n\n");

system "gzip " . $small_file;

$gzip_name = sprintf("atovs_rrodf1000_%d%02d%02d.m1.gz", $year, $mon, $mday);

$gzipped_file = $small_file . ".gz";

# Copy the gzipped file to both archives

$to_file = $archive_dir . "/" . $gzip_name;
copy($gzipped_file, $to_file);

$to_file = $archive_dir2 . "/" . $gzip_name;
copy($gzipped_file, $to_file);

# Remove the gzipped file

unlink $gzipped_file;



#-------------------------------------------------------------------------
# Copy the ODS file to the FTP site

#my $ftp_file = $ftp_dir . "/" . $ods_file_name;

#printf("Copying from: %s\n", $ods_file);
#printf("          to: %s\n\n", $ftp_file);

#copy($ods_file, $ftp_file);


# end of file
