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


# ***********************
# ***********************
# ***********************
$year = 2020;
$mon  = 1;
$mday = 23;
# ***********************
# ***********************
# ***********************

$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/nucaps_temp/n20_test_20200123";
$incoming_template = "NUCAPS-EDR_v2r0_j01_";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";

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


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 = sprintf("%s/nucaps_2_5_2_2_noaa20_%04d%02d%02d.iddf", $nprovs_dir, $year, $mon, $mday);

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

# end of file
