#!/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";
  }



#-------------------------------------------------------------------------
# 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 = 2021;
$mon  = 10;
$mday = 26;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************
#-------------------------------------------------------------------------


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

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

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



$incoming_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/heap_validation/M01/" . $data_date;
$incoming_template = "NUCAPS-EDR_v3r0_m01_";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";

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

$ods_source_dir = "/data/data065/nprovs/source/graphics/ods_capture/iasi/version2";
$ods_dir = "/data/data065/nprovs/data/ods";


chdir $source_dir;




#-------------------------------------------------------------------------
# Set the name of the output file. This will have the date attached to it
# if a specific date is being processed.

$iddf_file = $nprovs_dir . "/nucaps_heap_metopb_" . $data_date . ".iddf";

# Remove the previous output file (if it exists)

unlink $iddf_file;



#-------------------------------------------------------------------------
# Run NUCAPStoIDDF for each NUCAPS file

my @nucapsfiles = glob $incoming_dir . "/" . $incoming_template . "*.nc";

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

  # Link the files

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

  # Run NUCAPStoIDDF

  system $source_dir . "/IASItoIDDF.x " . $data_date . " " . $file;

  # Unlink the files

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




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

my $work_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/heap_validation";
chdir $work_dir;

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


# Create the temporary directory into which everything will be written

mkdir $work_dir . "/ods";


# Run the main program
symlink $iddf_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 . "/iasi.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 . "/createODSFromIASI.x \"NUCAPS HEAP MetOp-B\" " . $data_date;

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


# end of file
