#!/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";
  }



$baseday_minus_one = $ARGV[0];
$baseday           = $ARGV[1];
$baseday_plus_one  = $ARGV[2];

printf("Date to be processed:  %d    %d   %d\n\n", $baseday_minus_one, $baseday, $baseday_plus_one);


$temp_dir   = "/data/data065/nprovs/scripts/data_input/reprocess/special_capture/data";
$target_dir = "/net/www/aftp/pub/smcd/opdb/mpettey";


processDay($baseday);
processDay($baseday_minus_one);
processDay($baseday_plus_one);





# ===========================================================================
# ===========================================================================
# ===========================================================================
# The subroutine processHour extracts data for a specific date

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

  printf("\n\n==========================================================\n");
  printf("Processing the COSMIC-2 UCAR %d data\n\n", $date);

  $year  = $date / 10000;
  $month = ($date % 10000) / 100;
  $day   = $date % 100;

  $yday  = dayofyear($year, $month, $day);

  chdir $temp_dir;


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

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

  system "rm " . $temp_dir . "/atmPrf*";
  system "rm " . $temp_dir . "/wetPf2*";


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

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

  # Build the name of the wet file

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

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

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

    # 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", $temp_dir, $year, $month, $day;
    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", $temp_dir, $year, $month, $day);

    chdir $dir_name;

    system "mv * ../";

    chdir $temp_dir;

    system "rmdir " . $dir_name;
    }


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

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

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

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

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

    # 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", $temp_dir, $year, $month, $day;
    system "tar -xvf " . $tar_file;

    # Delete the tar file

    unlink $tar_file;
    }


  # ----------------------------------------------------
  # Tar the files

  printf("\nTarring the files...\n");

  $tar_file = sprintf("cosmic2_star_%d.tar", $date);

  chdir $temp_dir;

  system "tar -cvf " . $tar_file . " *nc";

  # Gzip the tar file

  printf("\nGzipping the tar file...\n");
  system "gzip " . $tar_file;

  # Copy the tar file to the target directory

  printf("\nCopying the file...\n");
  $from_file = sprintf("%s/cosmic2_star_%d.tar.gz", $temp_dir, $date);
  $to_file   = sprintf("%s/cosmic2_star_%d.tar.gz", $target_dir, $date);
  copy($from_file, $to_file);

  # Remove the files from the temp directory

  system "rm *nc";

  $file = sprintf("%s/cosmic2_ucar_%d.tar.gz", $temp_dir, $date);
  unlink $file;
  }



# =============================================================================
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
