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

BEGIN
  {
  $ENV{LD_LIBRARY_PATH}="LD_LIBRARY_PATH:/data/starfs1/libs/netcdf-4.2/lib:/data/starfs1/libs/hdf5-1.8.7/lib";
  }


my $true = 0;
my $false = 1;
my $do_ods = $false;


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

#$yday = dayofyear($year, $mon, $mday);

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

printf("Day to process:  %d\n", $date_to_process);

#$work_dir     = "/data/data065/nprovs/source/data_transfer/cosmic2_star";
$source_dir   = "/data/data065/nprovs/source/data_transfer/cosmic2_star";
$raw_data_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic2_star";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";



print "==============================================================\n";
print "Transfering COSMIC-2 STAR files";
print "\n\n";


# ----------------------------------------------------
# Build the names of the files that will be transferred

#my $wet_xfer_file = sprintf "/net/www/aftp/pub/smcd/scda/GNSSRO/COSMIC2/wetPrf/%04d-%02d-%02d.tar.gz", $year, $mon, $mday;

#my $atm_xfer_file = sprintf "/net/www/aftp/pub/smcd/scda/GNSSRO/COSMIC2/atmPrf/%04d-%02d-%02d.tar.gz", $year, $mon, $mday;


# ----------------------------------------------------
# Switch to the COSMIC-2 raw data directory

chdir $raw_data_dir;


# ----------------------------------------------------
# Remove any previous files from the raw data directory

printf("\nRemoving previous netCDF files from the raw data directory\n");

system "rm " . $raw_data_dir . "/atmPrf*nc";
system "rm " . $raw_data_dir . "/wetPrf*nc";


# ----------------------------------------------------
# wetprf

printf("\n\nTransferring the wetPrf file...\n\n");

my $from_file = sprintf "/net/www/aftp/pub/smcd/scda/GNSSRO/COSMIC2/wetPrf/%04d-%02d-%02d.tar.gz", $year, $mon, $mday;

if (-e $from_file)
  {
  my $to_file = sprintf "%s/%04d-%02d-%02d.tar.gz", $raw_data_dir, $year, $mon, $mday;

  # Copy the file

  copy ($from_file, $to_file);

  # Gunzip and untar the file

  system "gunzip " . $to_file;

  my $tar_file = sprintf "%s/%04d-%02d-%02d.tar", $raw_data_dir, $year, $mon, $mday;
  system "tar -xvf " . $tar_file;

  # Delete the tar file

  unlink $tar_file;

  # Move the files from the sub directory one level up to the main raw data directory

  $dir_name = sprintf("%s/%04d-%02d-%02d", $raw_data_dir, $year, $mon, $mday);

  chdir $dir_name;

  system "mv * ../";

  chdir $raw_data_dir;

  #unlink $dir_name or warn "Cannot remove the wet subdirectory";
  system "rmdir " . $dir_name;
  }


# ----------------------------------------------------
# atmprf

printf("\n\nTransferring the atmPrf file...\n\n");

$from_file = sprintf "/net/www/aftp/pub/smcd/scda/GNSSRO/COSMIC2/atmPrf/%04d-%02d-%02d.tar.gz", $year, $mon, $mday;

if (-e $from_file)
  {
  $to_file = sprintf "%s/%04d-%02d-%02d.tar.gz", $raw_data_dir, $year, $mon, $mday;

  # Copy the file

  copy ($from_file, $to_file);

  # Gunzip and untar the file

  system "gunzip " . $to_file;

  $tar_file = sprintf "%s/%04d-%02d-%02d.tar", $raw_data_dir, $year, $mon, $mday;
  system "tar -xvf " . $tar_file;

  # Delete the tar file

  unlink $tar_file;

  # Move the files from the sub directory one level up to the main raw data directory

#  $dir_name = sprintf("%s/%04d-%02d-%02d", $raw_data_dir, $year, $mon, $mday);

#  chdir $dir_name;

#  system "mv * ../";

#  chdir $raw_data_dir;

#  #unlink $dir_name;
#  system "rmdir " . $dir_name;
  }


# ----------------------------------------------------

# Create the daily file

$cddf_file = sprintf("%s/cosmic2_star_%04d%02d%02d.cddf", $nprovs_dir, $year, $mon, $mday);

# Remove the previous daily file

unlink $cddf_file;


# Run Cosmic2toCDDF 

symlink $cddf_file, "out.file" or warn "Cannot link $cddf_file to out.file";

system $source_dir . "/Cosmic2StarToCDDF.x " . $date_to_process . " " . $raw_data_dir;

unlink "out.file" or warn "Cannot unlink the out.file";


# ----------------------------------------------------
# Run the ODS capture

if ($do_ods == $true)
  {
  my $ods_source_dir = "/data/data065/nprovs/source/graphics/ods_capture/cosmic2_star";
  my $file_name = "cosmic2_star_" . $date_to_process . ".ods";
  my $ods_dir = "/data/data065/nprovs/data/ods/cosmic";
  my $ods_file = $ods_dir . "/" . $file_name;

  my $ftp_dir = "/net/www/aftp/pub/smcd/opdb/nprovs/ods";


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


  # =============================================================================
  # Create the COSMIC-2 ODS file

  symlink $cddf_file, "in.file" or warn "Cannot link to in.file";
  symlink $ods_source_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
  symlink $ods_source_dir . "/cosmic2_star.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 . "/createODSFromCosmic2Star.x \"COSMIC-2 STAR\" " . $date_to_process;

  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 " . $ods_source_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/cosmic_0-12.xml");
  $output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/cosmic2_star_asc_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/cosmic2_star_asc_%d_thumb.png", $date_to_process);
  $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/cosmic_12-24.xml");
  $output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/cosmic2_star_des_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/cosmic2_star_des_%d_thumb.png", $date_to_process);
  $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 . "/" . $file_name;

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

  copy($ods_file, $ftp_file);
  }


# ----------------------------------------------------
# Copy the daily file to the orbital archives

printf("Gzipping the daily file\n");

$gzip_file = sprintf("%s/cosmic2_star_%d.cddf.gz", $nprovs_dir, $date_to_process);

system "gzip " . $cddf_file;


$to_file_1 = sprintf("/data/data599/orbital_archive/cosmic2_star/cosmic2_star_%d.cddf.gz", $date_to_process);
$to_file_2 = sprintf("/data/data215/nprovs/data/orbital_archive/cosmic2_star/cosmic2_star_%d.cddf.gz", $date_to_process);

#$to_file_2 = sprintf("/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive/cosmic2_star/cosmic2_star_%d.cddf.gz", $date_to_process);

printf("Copying the daily file to the orbital archives\n\n");

copy($gzip_file, $to_file_1);

copy($gzip_file, $to_file_2);

unlink $gzip_file;

# end of main script



sub dayofyear
  {
  my ($year, $month, $day)=@_;
  my @days_in_month = (0,31,59,90,120,151,181,212,243,273,304,334,365);
  my $julday = $days_in_month[$month-1] + $day;

  my $is_leapyear = 0;

  if (($year % 4) == 0)
    {
    $is_leapyear = 1;
    }

  if (($year % 100) == 0)
    {
    $is_leapyear = 0;
    }

  if (($year % 400) == 0)
    {
    $is_leapyear = 1;
    }

  if (($is_leapyear == 1) && ($month >= 3))
    {
    $julday = $julday + 1;
    }

  return $julday;
  }

# end of file
