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

# This script copies data from the various MIRS platforms into daily data files

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.

# ***********************
# ***********************
# ***********************
my $year = $ARGV[0];
my $mon  = $ARGV[1];
my $mday = $ARGV[2];
# ***********************
# ***********************
# ***********************


$source_data_dir = "/data/smcd10/MiRS/archive/nc/n21_atms";
$incoming_dir    = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/temp";

$nprovs_dir      = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";
$archive_dir     = "/data/data599/orbital_archive/mirs_noaa21";
$archive_dir2    = "/data/data215/nprovs/data/orbital_archive/mirs_noaa21";

$source_dir = "/data/data065/nprovs/source/data_transfer/mirs/netcdf";




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



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

my @tempfiles = glob $incoming_dir . "/NPR-MIRS*.nc";

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



#-------------------------------------------------------------------------
# Transfer the granules from MIRS ftp to the incoming directory

$from_file = sprintf("%s/n21_atms_nc_%04d-%02d-%02d_v11r9dev.tar.gz", $source_data_dir, $year, $mon, $mday);
$to_file   = sprintf("%s/n21_atms_nc_%04d-%02d-%02d_v11r9dev.tar.gz", $incoming_dir, $year, $mon, $mday);

printf("\nCopying the data from the MIRS FTP:\n");
printf("  From File:  %s\n", $from_file);
printf("  To File:    %s\n\n", $to_file);


copy($from_file, $to_file);

chdir $incoming_dir;

# Unzip the file

printf("Gunzipping the file...\n");
system "gunzip " . $to_file;

# Untar the file

printf("Untarring the file...\n");

$unzipped_file = sprintf("%s/n21_atms_nc_%04d-%02d-%02d_v11r9dev.tar", $incoming_dir, $year, $mon, $mday);

system "tar -xvf " . $unzipped_file . " --strip-components=3";

unlink $unzipped_file;



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

$mddf_file = $nprovs_dir . "/mirs_noaa21_reprocessed.mddf";

# Remove the previous output file (if it exists)

unlink $mddf_file;


# Initialize the MDDF

symlink $mddf_file, "fort.12" or warn "Cannot link $mddf_file to fort.12";

system "/data/data065/nprovs/source/data_transfer/mirs/initmddf/initMDDF.x";

unlink "fort.12" or warn "Cannot unlink the unit.12 file";


#-------------------------------------------------------------------------
# Run MirsToMDDF for each EDR

chdir $incoming_dir or warn "Cannot chdir into the incoming directory";

#$file_location_dir = $incoming_dir . sprintf("/nc/n21_atms/%4d-%02d-%02d", $year, $mon, $mday);
#$file_location_dir = $incoming_dir;

my @mirsfiles = glob $incoming_dir . "/NPR-MIRS-SND*n21*";

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

  # Build the name of a matching IMG file

  $string1 = "NPR-MIRS-SND";
  $string2 = "NPR-MIRS-IMG";

  $img_file = $snd_file;
  $img_file =~ s/$string1/$string2/g;

  if (-e $img_file)
    {

    # Link the output file

    symlink $mddf_file, "out.file" or warn "Cannot link $mddf_file to unit.12";

    # Run MIRStoMDDF

    system $source_dir . "/MIRStoMDDF.x $snd_file $img_file $data_date";

    # Unlink the file

    unlink "out.file" or warn "Cannot unlink the unit.12 file";
    }
  else
    {
    printf("\n\n***** NO MATCH MADE *****\n\n");
    }
  }



#-------------------------------------------------------------------------
# Copy the daily file to another file with the date attached and gzip it

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

$gzip_name = sprintf("mirs_noaa21_%d%02d%02d.mddf", $year, $mon, $mday);

$temp_file = $nprovs_dir . "/" . $gzip_name;

copy($mddf_file, $temp_file);

system "gzip " . $temp_file;

$gzipped_file = $temp_file . ".gz";

# Copy the gzipped file to both archives

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

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

#printf("Copy From: %s\n", $gzipped_file);
#printf("Copy To:   %s\n", $to_file);

# Remove the gzipped file

unlink $gzipped_file;



#-------------------------------------------------------------------------
# Remove the reprocessed file

unlink $mddf_file;

# end of file
