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

# Point to the absolute path of the directory containing NprovsTools.pm
use lib '/data/data065/nprovs/lib'; 
use NprovsTools;


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



# This script transfers data for COSMIC-2 STAR and then runs the program
# that adds the data to a previously existing NPROVS collocation file.

printf("***********************************************************************\n");
printf("******************** Start of adding COSMIC-2 STAR ********************\n");
system "date";
printf("\n\n");


#---------------------------------------------------------------
# Define the directories that will be used

$source_dir      = "/data/data065/nprovs/source/collocate/AddRoToNprovs";
$collocation_dir = "/data/data599/collocations/nprovs";
$orbital_archive = "/data/data599/orbital_archive/ro_cosmic2_star";


#---------------------------------------------------------------
# Create a temporary working directory

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

#$work_dir = sprintf("/data/data599/work_dirs/nprovs_c2star_%04d%02d%02d_%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec);
$work_dir = "/data/data065/nprovs/source/collocate/AddRoToNprovs/work_dir";
#mkdir $work_dir;
chdir $work_dir;


#---------------------------------------------------------------
# Set the date range and loop through it

#$date_to_process = $ARGV[0];
$date_to_process = 20261001;

$date_to_process_minus_one   = NprovsTools::getPreviousDay($date_to_process);
$date_to_process_plus_one    = NprovsTools::getNextDay($date_to_process);


printf("  Date to process:            %d\n", $date_to_process);
printf("  Date to process minus one:  %d\n", $date_to_process_minus_one);
printf("  Date to process plus one:   %d\n\n", $date_to_process_plus_one);


$date_to_process_minus_one_file = sprintf("%s/cosmic2_star_%d.tar.gz", $orbital_archive, $date_to_process_minus_one);
$date_to_process_file           = sprintf("%s/cosmic2_star_%d.tar.gz", $orbital_archive, $date_to_process);
$date_to_process_plus_one_file  = sprintf("%s/cosmic2_star_%d.tar.gz", $orbital_archive, $date_to_process_plus_one);


# Copy baseday minus one

printf(" Copying data for baseday minus one (%d)...\n", $date_to_process_minus_one);

if (-e $date_to_process_minus_one_file)
  {
#  copyFilesFromOrbitalArchive($date_to_process_minus_one);
  }
else
  {
#  copyFilesFromUMD($date_to_process_minus_one);
  }

# Copy baseday

printf(" Copying data for baseday (%d)...\n", $date_to_process);

if (-e $date_to_process_file)
  {
#  copyFilesFromOrbitalArchive($date_to_process);
  }
else
  {
#  copyFilesFromUMD($date_to_process);
  }

# Copy baseday plus one

printf(" Copying data for baseday plus one (%d)...\n", $date_to_process_plus_one);

if (-e $date_to_process_plus_one_file)
  {
#  copyFilesFromOrbitalArchive($date_to_process_plus_one);
  }
else
  {
#  copyFilesFromUMD($date_to_process_plus_one);
  }


#---------------------------------------------------------------
# Copy the NPROVS collocation file to the work directory

$from_file  = sprintf("%s/nprovs_%d.nc", $collocation_dir, $date_to_process);
$collocation_file = sprintf("%s/nprovs_%d.nc", $work_dir, $date_to_process);
copy($from_file, $collocation_file);


#---------------------------------------------------------------
# If an NPROVS collocation file for the date does not exist, then
# do not do anything else

if (-e $collocation_file)
  {
  $output_file = sprintf("%s/nprovs_%d.nc.temp", $work_dir, $date_to_process);

  #-------------------------------------------------------------------------
  # Run the program to collocate the COSMIC-2 STAR to the collocation

  printf("\n  Collocating COSMIC-2 STAR for date:  %d\n\n", $date_to_process);

  symlink $collocation_file, "in.file" or warn "Cannot link $collocation_file to in.file";
  symlink $output_file, "out.file" or warn "Cannot link $output_file to out.file";
  symlink $work_dir, "in.dir" or warn "Cannot link $work_dir to in.dir";

  my $exit_code = system $source_dir . "/AddRoToNprovs.x " . $date_to_process . " 592 'RO COSMIC-2 STAR'";

  unlink "in.dir" or warn "Cannot unlink in.dir";
  unlink "out.file" or warn "Cannot unlink out.file";
  unlink "in.file" or warn "Cannot unlink in.file";


  #-------------------------------------------------------------------------
  # Copy the temporary output file to the output directory

  if ($exit_code != 0)
    {
    printf("\nBad return code (%d). The collocation file will not be updated.\n\n", $exit_code);
    }
  else
    {
    #$to_file  = sprintf("%s/nprovs_%d.nc", $collocation_dir, $date_to_process);

    #printf("\nCopying the updated collocation file to the collocation directory\n");
    #printf("   From:  %s\n", $output_file);
    #printf("   To:    %s\n\n", $to_file);

    #copy($output_file, $to_file);

    #unlink $output_file;
    }
  }  # if (-e $collocation_file...



#---------------------------------------------------------------
# Remove the temporary working directory

#system "rm -r " . $work_dir;


printf("\n\n");
system "date";
printf("********************* End of adding COSMIC-2 STAR *********************\n");
printf("***********************************************************************\n");




# end of main script
#=================================================================


sub copyFilesFromUMD
  {
  my ($date) = @_;

  $year  = int($date / 10000);
  $month = int(($date % 10000) / 100);
  $day   = int($date % 100);
#  $yday  = NprovsTools::getDayOfYear($year, $month, $day);

  # Transfer the wet files
  
  printf("\nTransferring Wet files from UMd...\n");

  $wget = sprintf("wget -q https://gpsmet.umd.edu/star_gnssro/data/cosmic2/nrt/wetPrf/%04d-%02d-%02d.tar.gz --no-check-certificate", $year, $month, $day);

  printf("%s\n", $wget);

  system $wget;

  $wet_file     = sprintf("%s/%04d-%02d-%02d.tar.gz", $work_dir, $year, $month, $day);
  $wet_tar_file = sprintf("%s/%04d-%02d-%02d.tar", $work_dir, $year, $month, $day);

  if (-e $wet_file)
    {
    $num_transferred++;

    # Gunzip and Untar the tarball

    system "gunzip " . $wet_file;
    system "tar -xf " . $wet_tar_file . " --strip-components 1";

    # Delete the tar file

    unlink($wet_tar_file);
    }


  # Transfer the atm files

  printf("\nTransferring the atmPrf files from Umd...\n\n");

  $wget = sprintf("wget -q https://gpsmet.umd.edu/star_gnssro/data/cosmic2/nrt/atmPrf/%04d-%02d-%02d.tar.gz --no-check-certificate", $year, $month, $day);

  printf("%s\n", $wget);

  system $wget;

  $atm_file     = sprintf("%s/%04d-%02d-%02d.tar.gz", $work_dir, $year, $month, $day);
  $atm_tar_file = sprintf("%s/%04d-%02d-%02d.tar", $work_dir, $year, $month, $day);

  if (-e $atm_file)
    {
    $num_transferred++;

    # Gunzip and Untar the tarball

    system "gunzip " . $atm_file;
    system "tar -xf " . $atm_tar_file . " --strip-components 1";

    # Delete the tar file

    unlink($atm_tar_file);
    }


  # Copy the transferred data to the orbital archive

  $archive_file = sprintf("%s/cosmic2_star_%d.tar.gz", $orbital_archive, $date);

  printf("\nTarring amd Gzipping the files for the orbital archive\n");
  $tar_command = sprintf("tar -czf %s *%d*.nc", $archive_file, $date);
  system $tar_command;
  }



sub copyFilesFromOrbitalArchive
  {
  my ($date) = @_;

  $archive_file = sprintf("%s/cosmic2_star_%d.tar.gz", $orbital_archive, $date);
  $gzip_file = sprintf("%s/cosmic2_star.tar.gz", $work_dir);

  printf("   Transferring the file from the orbital archive...\n");
  copy ($archive_file, $gzip_file);

  printf("   Untarring and Unzipping the file...\n");
  system "tar -xzf " . $gzip_file;

  unlink $gzip_file;

  printf("\n");
  }

# end of file
