#!/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

$incoming_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/mirs/metopc/temp";

$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";
#$archive_dir  = "/data/data215/nprovs/data/orbital_archive/mirs_metopc";
$archive_dir2 = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive/mirs_metopc";

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

chdir $incoming_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;

$data_date = $ARGV[0];
$year      = $ARGV[0] / 10000;
$mon       = ($ARGV[0] % 10000) / 100;
$mday      = $ARGV[0] % 100;

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


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

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

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


#foreach $file (@tempfiles)
#  {
#  if (-M $file > 2)
#    {
#    unlink $file;
#    }
#  }



#-------------------------------------------------------------------------
# Copy the files for the selected date from FTP to the incoming directory

printf("\nCopying the data from the STAR FTP...\n\n");


$ftp_file = sprintf("metopC_amsua_nc_%4d-%02d-%02d_v11r3dev", $year, $mon, $mday);

#$from_file = "/data/aftp-smcd/mirs/archive/dev/nc/metopC_amsua/" . $ftp_file . ".tar.gz";
$from_file = "/data/data411/pub/archive/nc/metopC_amsua/" . $ftp_file . ".tar.gz";
$to_file = $incoming_dir . "/" . $ftp_file . ".tar.gz";

printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);

copy($from_file, $to_file);

system "gunzip $to_file";

system "tar -xvf " . $incoming_dir . "/" . $ftp_file . ".tar";


$temp_dir = sprintf("%s/nc/metopC_amsua/%d-%02d-%02d", $incoming_dir, $year, $mon, $mday);

system "mv " . $temp_dir . "/*.nc " . $incoming_dir;

unlink $incoming_dir . "/" . $ftp_file . ".tar";

system "rmdir $temp_dir";
system "rmdir " . $incoming_dir . "/nc/metopC_amsua";
system "rmdir " . $incoming_dir . "/nc";


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

if ($process_specific_date == 1)
  {
  $mddf_file = $nprovs_dir . "/mirs_metopc.mddf";
  }
else
  {
  $mddf_file = $nprovs_dir . "/mirs_metopc_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

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

printf("file dir: %s\n", $file_location_dir);

my @mirsfiles = glob $file_location_dir . "/NPR-MIRS-SND*ma3*";

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";
    system "/data/data065/nprovs/source/data_transfer/mirs/netcdf/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_metopc_%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);

# Remove the gzipped file

unlink $gzipped_file;



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

if ($process_specific_date == 0)
  {
  unlink $mddf_file;
  }

# end of file
