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

# This script copies data from multiple NUCAPS granule files into an daily file

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


$true = 0;
$false = 1;

$do_ods = $true;
#$do_ods = $false;


#-------------------------------------------------------------------------
# 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 = 2025;
$mon  = 3;
$mday = 1;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************


$date_to_process = ($year*10000) + ($mon*100) + $mday;
printf("Date to process:  %d\n", $date_to_process);


# Create a temporary working directory

$data_day = time();
($now_sec, $now_min, $now_hour, $now_mday, $now_mon, $now_year, $now_wday, $now_yday, $now_isdst) = localtime($data_day);
$now_year += 1900;
$now_mon++;

$now_isdst = 0;
$now_yday  = 0;
$now_wday  = 0;

$work_dir = sprintf("/data/data599/work_dirs/euiasi_metopc_%04d%02d%02d_%02d%02d%02d", $now_year, $now_mon, $now_mday, $now_hour, $now_min, $now_sec);

mkdir $work_dir;
chdir $work_dir;

mkdir $work_dir . "/ods";



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

$archive_dir  = "/data/data599/orbital_archive/euiasi_metopc";
$archive_dir2 = "/data/data215/nprovs/data/orbital_archive/euiasi_metopc";

$ods_source_dir = "/data/data065/nprovs/source/graphics/ods_capture/euiasi/version06";
$ods_dir = "/data/data065/nprovs/data/ods/eu_iasi";
$ftp_dir = "/data/www/data/nprovs/monitoring/ods";



#-------------------------------------------------------------------------
# Copy the files for the selected date from SCDR to the incoming directory

printf("\nCopying the data from SCDR...\n\n");

system "/data/starfs1/bin/scdr-files -t IASI_EUL2 -satid m03 -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $work_dir;


#-------------------------------------------------------------------------
# Set up a new EDIF file

$eidf_file_name = "euiasi_metopc_" . $date_to_process . ".eidf";

my $output_file_name = "$work_dir/" . $eidf_file_name;



# Process the granules

#my @iasifiles = glob "$work_dir/IASI_SND_02_M03_$date_to_process*";
my @iasifiles = glob "$work_dir/IASI_SND_02_M03*";

foreach $file (@iasifiles)
  {
  #print "\n-------------------------------\n";
  #print "Processing file: ". $file, "\n\n";

  # Run the program that reads the EUMETSAT IASI data and copies the
  # data to an EIDF file

  symlink $output_file_name, "out.file" or warn "Cannot link $output_file_name to out.file";

  system "$source_dir/EUIASItoEIDF.x $file";

  unlink "out.file" or warn "Cannot unlink the out.file";
  }



#-------------------------------------------------------------------------
# Create an ODS file if $do_ods is set to TRUE

if ($do_ods == $true)
  {
  $ods_file_name = sprintf("euiasi_metopc_%d%02d%02d.ods", $year, $mon, $mday);
  $ods_file = $ods_dir . "/" . $ods_file_name;


  # Run the main program
  symlink $output_file_name, "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 . "/euiasi.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 . "/createODSFromEUIASI.x \"EUIASI MetOp-C\" " . $date_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 " . $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/euiasi_asc.xml");
  $output_file  = sprintf("output:/data/www/smcd/opdb/nprovs/images/coverage/euiasi_metopc_asc_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/data/www/smcd/opdb/nprovs/images/coverage/euiasi_metopc_asc_%d_thumb.png", $date_to_process);
  $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/euiasi_des.xml");
  $output_file  = sprintf("output:/data/www/smcd/opdb/nprovs/images/coverage/euiasi_metopc_des_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/data/www/smcd/opdb/nprovs/images/coverage/euiasi_metopc_des_%d_thumb.png", $date_to_process);
  $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;



  #-------------------------------------------------------------------------
  # 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);
  }  # if ($do_ods...



#-------------------------------------------------------------------------
# Gzip the daily file and copy it to the orbital archive

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

system "gzip " . $output_file_name;

$gzipped_file = $output_file_name . ".gz";

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

#$to_file = $archive_dir2 . "/" . $eidf_file_name . ".gz";
##copy($gzipped_file, $to_file);



#-------------------------------------------------------------------------
# Remove the temporary working directory

system "rm -r " . $work_dir;

# end of file
