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

#------------------------------------------------------------------------
# This script captures GFS analysis data, extracts selected parameters, and writes
# the selected data into a new GFS Daily Data File.

printf("\nCapturing GFS Analysis data and creating a GFS Daily Data File\n\n");


#------------------------------------------------------------------------
# Set the date to process. By default this will be yesterday. That can be
# overridden by setting $date_to_process to a specific value.

$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 = 0;
$wday = 0;
$yday = 0;
$isdst = 0;

$date_to_process = sprintf("%4d%02d%02d", $year, $mon, $mday);
#$date_to_process = 20211130;

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

$year  = $date_to_process / 10000;
$mmdd  = $date_to_process % 10000;


#------------------------------------------------------------------------
# Set the locations of the various directories

$source_dir = "/data/data065/nprovs/source/data_transfer/gfs";
$work_dir   = "/data/data065/nprovs/source/data_transfer/gfs";
$nprovs_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";

chdir $work_dir;


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

processHour($date_to_process, "00");
processHour($date_to_process, "06");
processHour($date_to_process, "12");
processHour($date_to_process, "18");


#------------------------------------------------------------------------
# Run the program that copies the binary data into a GFS Daily File

# Remove the previous GFS Daily Data File if it exists

$gfs_daily_file = $nprovs_dir . "/gfs_47lvl.gadf";

if (-e $gfs_daily_file)
  {
  unlink $gfs_daily_file;
  }


# Remove the previous log file

unlink $work_dir . "/gfsToBinary.log";


# Run the program

symlink $gfs_daily_file, "fort.11";
symlink $work_dir . "/gfsToBinary_output.00z.bin", "fort.20";
symlink $work_dir . "/gfsToBinary_output.06z.bin", "fort.21";
symlink $work_dir . "/gfsToBinary_output.12z.bin", "fort.22";
symlink $work_dir . "/gfsToBinary_output.18z.bin", "fort.23";

$exec = sprintf("%s/gfsBinaryToDailyFile.x <<EOD > gfsBinaryToDailyFile.log\n%04d\n%04d\nEOD", $source_dir, $year, $mmdd);

system $exec;

unlink "fort.11";
unlink "fort.20";
unlink "fort.21";
unlink "fort.22";
unlink "fort.23";


# Remove the binary files

unlink $work_dir . "/gfsToBinary_output.00z.bin";
unlink $work_dir . "/gfsToBinary_output.06z.bin";
unlink $work_dir . "/gfsToBinary_output.12z.bin";
unlink $work_dir . "/gfsToBinary_output.18z.bin";



#--------------------------------------------------------------------------------
# Gzip the daily data file and copy it the the orbital archive

# Copy the daily file to the orbital archive and the backup archive

#$archive_dir3  = "/data/data215/nprovs/data/orbital_archive";
$archive_dir  = "/data/data599/orbital_archive";
$archive_dir2 = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/archive";


printf("Copying the daily files to the archive directory\n\n");

printf("Making a copy of the daily file...\n\n");
$from_file = $gfs_daily_file;
$to_file = $nprovs_dir . sprintf("/gfs_v2_%d.gadf", $date_to_process);
copy($from_file, $to_file);

printf("Gzipping the file...\n\n");
$gzip_file = sprintf("gfs_v2_%d.gadf", $date_to_process) . ".gz";
system "gzip " . $to_file;

printf("Copying to the main orbital archive...\n");
$from_file = $nprovs_dir . "/" . $gzip_file;
$to_file = $archive_dir . "/gfs_v2/" . $gzip_file;
printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);
copy($from_file, $to_file);

printf("Copying to the backup orbital archive...\n");
$to_file = $archive_dir2 . "/gfs_v2/" . $gzip_file;
printf("Copying from: %s\n", $from_file);
printf("          to: %s\n\n", $to_file);
copy($from_file, $to_file);

#printf("Copying to the backup orbital archive...\n");
#$to_file = $archive_dir3 . "/gfs_v2/" . $gzip_file;
#printf("Copying from: %s\n", $from_file);
#printf("          to: %s\n\n", $to_file);
#copy($from_file, $to_file);

unlink $nprovs_dir . "/" . $gzip_file;


#--------------------------------------------------------------------------------
# Capture data from the MIRS daily data file (MDDF) and write
# the data to ODS files.

system "/data/data065/nprovs/scripts/graphics/run_gfs_v2_ods_capture";


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




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

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

  printf("\n\n==========================================================\n");
  printf("Processing the GFS %s %sZ data\n\n", $date, $hour);

  # Set up the FTP directory and file names

  my $ftp_dir        = sprintf("/pub/data/nccf/com/gfs/prod/gfs.%s/%02d/atmos", $date, $hour);
  my $ftp_file1      = sprintf("%s/gfs.t%02dz.pgrb2.0p25.f000", $ftp_dir, $hour);
  my $ftp_file2      = sprintf("%s/gfs.t%02dz.pgrb2b.0p25.f000", $ftp_dir, $hour);
  my $from_ftp_file1 = sprintf("%s/from_ftp_file_1_%02dz", $work_dir, $hour);
  my $from_ftp_file2 = sprintf("%s/from_ftp_file_2_%02dz", $work_dir, $hour);

  # Copy the GFS data via FTP

  my $host = "ftpprd.ncep.noaa.gov";

  my $ftp = Net::FTP->new($host) or die "Cannot open the ftp server\n";
  $ftp->login() or warn "Cannot log into the ftp server\n";

  $ftp->binary();
  $ftp->cwd($ftp_dir);

  printf("\nFTPing the first %02dz file\n", $hour);
  printf("   From: %s\n", $ftp_file1);
  printf("   To:   %s\n\n", $from_ftp_file1);

  $ftp->get($ftp_file1, $from_ftp_file1) or warn "Could not transfer the file: " . $ftp->code() . ": " . $ftp->message();

  printf("FTPing the second %02dz file\n", $hour);
  printf("   From: %s\n", $ftp_file2);
  printf("   To:   %s\n\n", $from_ftp_file2);

  $ftp->get($ftp_file2, $from_ftp_file2) or warn "Could not transfer the file: " . $ftp->code() . ": " . $ftp->message();

  $ftp->quit();


  # If the files were transferred, then begin processing them

  if ((-e $from_ftp_file1) && (-e $from_ftp_file2))
    {

    # Set the name of the temporary output binary file

    $temp_binary_file = sprintf("%s/gfs_wgrib_output.%02dz.bin", $work_dir, $hour);

    # Run the unpack_from_grib script to extract the grib data into a binary format

    printf("Running WGRIB to extract data from the GFS file\n\n");

    system $source_dir . "/unpack_from_grib " . $from_ftp_file1 . " " . $from_ftp_file2 . " " . $temp_binary_file;

    # Run the program to read the data from the binary files

    printf("\nConverting the WGRIB output to a binary file\n");

    $binary_file = sprintf("%s/gfsToBinary_output.%02dz.bin", $work_dir, $hour);

    symlink $temp_binary_file, "fort.11";
    symlink $binary_file, "fort.20";

    $exec = sprintf("%s/gfsToBinary.x <<EOD > gfs.bin.%s.log\n%04d\n%04d\n%s\nEOD", $source_dir, $hour, $year, $mmdd, $hour);
    #$exec = sprintf("%s/gfsToBinary.x <<EOD \n%04d\n%04d\n%s\nEOD", $source_dir, $year, $mmdd, $hour);

    system $exec;

    unlink $work_dir . "/fort.11";
    unlink $work_dir . "/fort.20";

    # Delete the temporary file that was generated by the WGRIB step

    unlink $temp_binary_file;

    # Delete the files that were transferred from the FTP server

    unlink($from_ftp_file1);
    unlink($from_ftp_file2);
    }
  else
    {
    printf("The files for %02dz could not be processed.\n", $hour);
    printf("The hour will be skipped.\n");
    }
  }


# end of file
