#!/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 = 2019;
$mon  = 3;
$mday = 12;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************


$incoming_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/nucaps_ccr";
$incoming_template = "NUCAPS-CCR";

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



#-------------------------------------------------------------------------
# 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 . "*";

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-CCR -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $incoming_dir;



#-------------------------------------------------------------------------
# Create the file name
my $file_name = "nucaps_ccr_" . $data_date . ".ods";
my $ods_file = $ods_dir . "/" . $file_name;




#-------------------------------------------------------------------------
# Create a temporary directory into which the pieces of the file 
# will be written

chdir $ods_source_dir;
mkdir $ods_source_dir . "/ods";


#-------------------------------------------------------------------------
# Create the ODS file

symlink $incoming_dir, "in.dir" or warn "Cannot link to in.dir";
symlink $ods_source_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
symlink $ods_source_dir . "/nucaps_ccr.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 . "/CreateOdsFromNucapsCCR.x " . $data_date  . " \"NUCAPS-CCR\" \"NUCAPS CCR\"";

unlink "in.dir" or warn "Cannot unlink in.dir";
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 " . $ods_source_dir . "/ods";




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

my $ftp_file = $ftp_dir . "/" . $file_name;

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

copy($ods_file, $ftp_file);



# =============================================================================
# Search the ftp directory and remove any file that was created
# more than 5 days ago

my @tempftpfiles = glob $ftp_dir . "/nucaps_ccr*.ods";

foreach $file (@tempftpfiles)
  {
  if (-M $file > 4)
    {
    unlink $file;
    }
  }



# =============================================================================
# Search the ods directory and remove any file that was created
# more than 30 days ago

my @tempodsfiles = glob $ods_dir . "/nucaps_ccr*.ods";

foreach $file (@tempodsfiles)
  {
  if (-M $file > 29)
    {
    unlink $file;
    }
  }

# end of file
