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

# This script transfers COSMIC files


#-------------------------------------------------------------------------
# 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 = 2017;
$mon  = 8;
$mday = 12;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************


$incoming_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/cosmic";
$avn_template = "avnPrf_";
$atm_template = "atmPrf_";
$wet_template = "wetPrf_";


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

my $epoch  = timegm(0, 0, 0, $mday, $mon-1, $year-2000);
my $julday = (gmtime($epoch))[7] + 1;

printf("Date to be transferred:  %d  (julian day %d)\n\n", $data_date, $julday);


#-------------------------------------------------------------------------
# Remove all previous files from the input raw data directory

my @avn_tempfiles = glob $incoming_dir . "/" . $avn_template . "*";

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

my @atm_tempfiles = glob $incoming_dir . "/" . $atm_template . "*";

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

my @wet_tempfiles = glob $incoming_dir . "/" . $wet_template . "*";

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



#-------------------------------------------------------------------------
# Copy the files for the selected date from SCDR to the incoming directory

chdir $incoming_dir;

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

# Run the wget line

$wget = sprintf("wget -q --http-user=bsun --http-passwd=cosmic http://cdaac-www.cosmic.ucar.edu/cdaac/rest/tarservice/data/cosmicrt/wetPrf/%d.%03d", $year, $julday);

system $wget;

# Untar the tarball

$untar = sprintf("tar -xvf %d.%03d", $year, $julday);

system $untar;

# Move the individual files to the main directory

my $changedir = sprintf("%s/cosmicrt/wetPrf/%d.%03d", $incoming_dir, $year, $julday);

chdir $changedir;

system "mv * " . $incoming_dir;

# Delete the extra directories that were created during the transfer and untarring

chdir $incoming_dir;

system "rm -r cosmicrt";

$rm_tarball = sprintf("rm %d.%03d", $year, $julday);

system $rm_tarball;



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

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

# Run the wget line

$wget = sprintf("wget -q --http-user=bsun --http-passwd=cosmic http://cdaac-www.cosmic.ucar.edu/cdaac/rest/tarservice/data/cosmicrt/avnPrf/%d.%03d", $year, $julday);

system $wget;

# Untar the tarball

$untar = sprintf("tar -xvf %d.%03d", $year, $julday);

system $untar;

# Move the individual files to the main directory

$changedir = sprintf("%s/cosmicrt/avnPrf/%d.%03d", $incoming_dir, $year, $julday);

chdir $changedir;

system "mv * " . $incoming_dir;

# Delete the extra directories that were created during the transfer and untarring

chdir $incoming_dir;

system "rm -r cosmicrt";

$rm_tarball = sprintf("rm %d.%03d", $year, $julday);

system $rm_tarball;



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

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

# Run the wget line

$wget = sprintf("wget -q --http-user=bsun --http-passwd=cosmic http://cdaac-www.cosmic.ucar.edu/cdaac/rest/tarservice/data/cosmicrt/atmPrf/%d.%03d", $year, $julday);

system $wget;

# Untar the tarball

$untar = sprintf("tar -xvf %d.%03d", $year, $julday);

system $untar;

# Move the individual files to the main directory

$changedir = sprintf("%s/cosmicrt/atmPrf/%d.%03d", $incoming_dir, $year, $julday);

chdir $changedir;

system "mv * " . $incoming_dir;

# Delete the extra directories that were created during the transfer and untarring

chdir $incoming_dir;

system "rm -r cosmicrt";

$rm_tarball = sprintf("rm %d.%03d", $year, $julday);

system $rm_tarball;


# end of file
