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


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

printf("Date to be processed:  %d   %d\n\n", $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";
$source_dir = "/data/data065/nprovs/source/data_transfer/hrrr";


# Copy the files to the temp directory

#------------------------------------------------------------------------
# Call the subroutine processHour which will copy the HRRR Grib file
# and extract the data to a binary file

#for (my $time=0; $time<=23; $time++)
for (my $time=0; $time<=23; $time+=3)
  {
  processHour($baseday, $time, "conus", "");
  processHour($baseday, $time, "alaska", ".ak");

  if ($time == 0)
    {
    processHour($baseday_plus_one, $time, "conus", "");
    processHour($baseday_plus_one, $time, "alaska", ".ak");
    }
  }




# Tar the files

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

chdir $temp_dir;

system "tar -cvf hrrr.tar *.nc";

# Gzip the tar file

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


# Copy the tar file to the target directory

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


# Remove the files from the temp directory

system "rm *.nc";

$file = sprintf("%s/hrrr.tar.gz", $temp_dir);
unlink $file;



# end of main script
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------




# ===========================================================================
# ===========================================================================
# ===========================================================================
# The subroutine processHour extracts data for a specific hour from the
# HRRR grib file and writes the data to a binary file

sub processHour
  {
  my ($date, $hour, $region, $region_ext) = @_;

  printf("\n\n==========================================================\n");
  printf("Processing the RAP/HRRR %d %02dZ %s data\n\n", $date, $hour, $region);

  # Use wget to transfer the file

  $grib_file = sprintf("hrrr_%s_%d_t%02dz.grib2", $region, $date, $hour);

  unlink $grib_file;

  $wget = sprintf("wget -q https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.%d/%s/hrrr.t%02dz.wrfnatf00%s.grib2 --no-check-certificate -O %s", $date, $region, $hour, $region_ext, $grib_file);

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

  system $wget;


  # Only continue if a wgrib file was transferred

  if (-e $grib_file)
    {

    # Convert the wgrib file to a netCDF file

    $netcdf_file = sprintf("%s/hrrr_%s_%d_t%02dz.nc", $temp_dir, $region, $date, $hour);

    unlink $netcdf_file;

    $unpack_to_netcdf = sprintf("/data/starfs1/utils/wgrib2-v2.0.8/wgrib2 %s -nc_table %s/soundings_nc.table -netcdf %s", $grib_file, $source_dir, $netcdf_file);

    system $unpack_to_netcdf;

    # Remove the grib file

    unlink $grib_file;
    }  # if (-e $grib_file...
  else
    {
    printf("The %s data from %d %02dz was not transferred.\n", $region, $date, $hour);
    printf("The data will be skipped.\n\n");
    }
  }

# end of file
