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


$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 . "/avnPrf*_nc";
  system "rm " . $temp_dir . "/atmPrf*_nc";
  system "rm " . $temp_dir . "/wetPf2*_nc";


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

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

  # Run the wget line

  $wget = sprintf("wget -q http://data.cosmic.ucar.edu/gnss-ro/cosmic2/nrt/level2/%04d/%03d/wetPf2_nrt_%04d_%03d.tar.gz --no-check-certificate", $year, $yday, $year, $yday);

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

  system $wget;

  # Gunzip and Untar the tarball

  $gunzip = sprintf("gunzip wetPf2_nrt_%04d_%03d.tar.gz", $year, $yday);
  system $gunzip;

  $untar = sprintf("tar -xvf wetPf2_nrt_%04d_%03d.tar", $year, $yday);
  system $untar;

  # Delete the tar file

  $deletetar = sprintf("rm wetPf2_nrt_%04d_%03d.tar", $year, $yday);
  system $deletetar;

  # ----------------------------------------------------
  # avnprf

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

  # Run the wget line

  $wget = sprintf("wget -q http://data.cosmic.ucar.edu/gnss-ro/cosmic2/nrt/level2/%04d/%03d/avnPrf_nrt_%04d_%03d.tar.gz --no-check-certificate", $year, $yday, $year, $yday);

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

  system $wget;

  # Gunzip and Untar the tarball

  $gunzip = sprintf("gunzip avnPrf_nrt_%04d_%03d.tar.gz", $year, $yday);
  system $gunzip;

  $untar = sprintf("tar -xvf avnPrf_nrt_%04d_%03d.tar", $year, $yday);
  system $untar;

  # Delete the tar file

  $deletetar = sprintf("unlink avnPrf_nrt_%04d_%03d.tar", $year, $yday);
  system $deletetar;

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

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

  # Run the wget line

  $wget = sprintf("wget -q http://data.cosmic.ucar.edu/gnss-ro/cosmic2/nrt/level2/%04d/%03d/atmPrf_nrt_%04d_%03d.tar.gz --no-check-certificate", $year, $yday, $year, $yday);

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

  system $wget;

  # Gunzip and Untar the tarball

  $gunzip = sprintf("gunzip atmPrf_nrt_%04d_%03d.tar.gz", $year, $yday);
  system $gunzip;

  $untar = sprintf("tar -xvf atmPrf_nrt_%04d_%03d.tar", $year, $yday);
  system $untar;

  # Delete the tar file

  $deletetar = sprintf("rm atmPrf_nrt_%04d_%03d.tar", $year, $yday);
  system $deletetar;


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

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

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

  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_ucar_%d.tar.gz", $temp_dir, $date);
  $to_file   = sprintf("%s/cosmic2_ucar_%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
