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


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



print "==============================================================\n";
print "Transfering RAP/HRRR files";
print " ";
system "date";
print "\n\n";


# ----------------------------------------------------
# Determine the date of the data that will be transfered
# (default is yesterday)

my $now = time();
#my $data_date = $now - (1 * 86400);
my $data_date = $now - (8 * 86400);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($data_date);

$year = 1900 + $year;
$yday = $yday + 1;

my $date_to_process = sprintf "%04d%02d%02d", $year, ($mon+1), $mday;

printf("\nDate to process: %d/%02d/%04d\n", ($mon+1), $mday, $year);


my $data_date = $now - (7 * 86400);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($data_date);

$year = 1900 + $year;
$yday = $yday + 1;

my $next_day = sprintf "%04d%02d%02d", $year, ($mon+1), $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 GRIB2 files from the raw data directory\n");
#system "rm " . $raw_data_dir . "/hrrr*";


# ----------------------------------------------------
# GRIB2 transfers

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

# Run the wget line

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t00z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t03z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t06z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t09z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t12z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t15z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t18z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t21z.wrfnatf00.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t00z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t03z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t06z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t09z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t12z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t15z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t18z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t21z.wrfnatf00.ak.grib2 --no-check-certificate", $date_to_process);
printf("%s\n", $wget);
system $wget;


$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/conus/hrrr.t00z.wrfnatf00.grib2 --no-check-certificate -O hrrr.t24z.wrfnatf00.grib2", $next_day);
printf("%s\n", $wget);
system $wget;

$wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/alaska/hrrr.t00z.wrfnatf00.ak.grib2 --no-check-certificate -O hrrr.t24z.wrfnatf00.ak.grib2", $next_day);
printf("%s\n", $wget);
system $wget;


# end of file
