#!/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 = 2023;
$mon  = 7;
$mday = 25;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************


$incoming_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/nucaps";
$incoming_template = "NUCAPS-EDR_v3r0_j01_";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";

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


chdir $source_dir;


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

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

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



#-------------------------------------------------------------------------
# Remove all previous files from the input raw data directory

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

foreach $file (@tempfiles)
  {
  unlink $file;
  }



#-------------------------------------------------------------------------
# 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 NUCAPS-EDR --re-file _j01_ -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $incoming_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_noaa20_scaletest_20230725.iddf";

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
