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

# ***********************
# ***********************
# ***********************
$year = 2024;
$mon  = 11;
$mday = 10;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************


$true  = 0;
$false = 1;

$do_ods = $true;
#$do_ods = $false;


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

$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";

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

$footprint_size = 17.0;




#-------------------------------------------------------------------------
# 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 . "/NPR-MIRS*.nc";

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



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

printf("\nCopying the data from SCDR:\n");

system "/data/starfs1/bin/scdr-files -t NPR_MIRS_IMG --re-file _n21_ -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $incoming_dir;

system "/data/starfs1/bin/scdr-files -t NPR_MIRS_SND --re-file _n21_ -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $incoming_dir;

#$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.

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



#-------------------------------------------------------------------------
# Create an ODS file

if ($do_ods == $true)
  {

  # Create a temporary working directory

  $data_day = time();
  ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($data_day);
  $year += 1900;
  $mon++;

  $work_dir = sprintf("/data/data599/ods/work_dirs/mirs_noaa21_%04d%02d%02d_%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec);

  mkdir $work_dir;
  chdir $work_dir;

  mkdir $work_dir . "/ods";

  #my $work_dir = $incoming_dir;
  #chdir $work_dir;

  $ods_file_name = sprintf("mirs_noaa21_%d.ods", $data_date);
  $ods_file = $ods_dir . "/" . $ods_file_name;


  # Create the temporpary directory into which everything will be written
  #mkdir $work_dir . "/ods";


  # Run the main program
  symlink $mddf_file, "in.file" or warn "Cannot link to in.file";
  symlink $work_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
  symlink $ods_source_dir . "/mirs.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 . "/createODSFromMirs.x \"MIRS NOAA-21\" " . $data_date . " " . $footprint_size;

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


  #-------------------------------------------------------------------------
  # Create an image from the ODS file and send the image to the STAR web site
  # for routine monitoring

  # Build the command line executable for ascending and then run it

  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/mirs_asc.xml");
  $output_file  = sprintf("output:/data/www/smcd/opdb/nprovs/images/coverage/mirs_noaa21_asc_%d.png", $data_date);
  $thumb_file   = sprintf("thumbnail:/data/www/smcd/opdb/nprovs/images/coverage/mirs_noaa21_asc_%d_thumb.png", $data_date);
  $thumb_width  = sprintf("thumbwidth:100");
  $thumb_height = sprintf("thumbheight:75");
  $verbose      = sprintf("verbose:no");
  $file_info    = sprintf("file_info:no");

  $exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

  system $exec;


  # Build the command line executable for descending and then run it

  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/mirs_des.xml");
  $output_file  = sprintf("output:/data/www/smcd/opdb/nprovs/images/coverage/mirs_noaa21_des_%d.png", $data_date);
  $thumb_file   = sprintf("thumbnail:/data/www/smcd/opdb/nprovs/images/coverage/mirs_noaa21_des_%d_thumb.png", $data_date);
  $thumb_width  = sprintf("thumbwidth:100");
  $thumb_height = sprintf("thumbheight:75");
  $verbose      = sprintf("verbose:no");
  $file_info    = sprintf("file_info:no");

  $exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

  system $exec;



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

  my $ftp_file = $ftp_dir . "/" . $ods_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 . "/mirs_*.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 . "/mirs_*.ods";

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


  # Remove the temporary working directory

  #system "rm -r " . $work_dir . "/ods";
  system "rm -r " . $work_dir;
  }  # if ($do_ods == $true...


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

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

# end of file
