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

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

printf("\nCapturing ECMWF Analysis data and creating an ECMWF 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 = 20190731;

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

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


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

$source_dir = "/data/data065/nprovs/source/data_transfer/ecmwf";
$work_dir   = "/data/data065/nprovs/source/data_transfer/ecmwf";
#$ecmwf_dir  = "/data/data300/pub/ecmwf";
$ecmwf_dir  = "/data/smcd5/pkeehn/ecmwf";
$nprovs_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";

chdir $work_dir;


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

$file_00_00z = sprintf("UAD%04d0000%04d00001", $mmdd, $mmdd);
$file_00_03z = sprintf("UAD%04d0000%04d03001", $mmdd, $mmdd);
$file_06_06z = sprintf("UAD%04d0600%04d06001", $mmdd, $mmdd);
$file_00_09z = sprintf("UAD%04d0000%04d09001", $mmdd, $mmdd);
$file_12_12z = sprintf("UAD%04d1200%04d12001", $mmdd, $mmdd);
$file_12_15z = sprintf("UAD%04d1200%04d15001", $mmdd, $mmdd);
$file_18_18z = sprintf("UAD%04d1800%04d18001", $mmdd, $mmdd);
$file_12_21z = sprintf("UAD%04d1200%04d21001", $mmdd, $mmdd);

processHour("00", $file_00_00z);
processHour("03", $file_00_03z);
processHour("06", $file_06_06z);
processHour("09", $file_00_09z);
processHour("12", $file_12_12z);
processHour("15", $file_12_15z);
processHour("18", $file_18_18z);
processHour("21", $file_12_21z);




#------------------------------------------------------------------------
# Run the program that copies the binary data into an ECMWF Daily File

# Remove the previous ECMWF Daily Data File if it exists

$ecmwf_daily_file = $nprovs_dir . "/ecmwfo3.eadf";

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


# Remove the previous log file

unlink $work_dir . "/ecmwfToBinaryToDailyFile.log";


# Run the program

symlink $ecmwf_daily_file, "fort.11";
symlink $work_dir . "/ecmwfToBinary_output.00.bin", "fort.20";
symlink $work_dir . "/ecmwfToBinary_output.03.bin", "fort.21";
symlink $work_dir . "/ecmwfToBinary_output.06.bin", "fort.22";
symlink $work_dir . "/ecmwfToBinary_output.09.bin", "fort.23";
symlink $work_dir . "/ecmwfToBinary_output.12.bin", "fort.24";
symlink $work_dir . "/ecmwfToBinary_output.15.bin", "fort.25";
symlink $work_dir . "/ecmwfToBinary_output.18.bin", "fort.26";
symlink $work_dir . "/ecmwfToBinary_output.21.bin", "fort.27";


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

#$exec = sprintf("%s/ecmwfBinaryToDailyFile.x", $source_dir);

system $exec;

unlink "fort.11";
unlink "fort.20";
unlink "fort.21";
unlink "fort.22";
unlink "fort.23";
unlink "fort.24";
unlink "fort.25";
unlink "fort.26";
unlink "fort.27";


# Remove the binary files

unlink $work_dir . "/ecmwfToBinary_output.00.bin";
unlink $work_dir . "/ecmwfToBinary_output.03.bin";
unlink $work_dir . "/ecmwfToBinary_output.06.bin";
unlink $work_dir . "/ecmwfToBinary_output.09.bin";
unlink $work_dir . "/ecmwfToBinary_output.12.bin";
unlink $work_dir . "/ecmwfToBinary_output.15.bin";
unlink $work_dir . "/ecmwfToBinary_output.18.bin";
unlink $work_dir . "/ecmwfToBinary_output.21.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_dir  = "/data/data599/orbital_archive";
$archive_dir2 = "/data/data215/nprovs/data/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 = $ecmwf_daily_file;
$to_file = $nprovs_dir . sprintf("/ecmwfo3_%d.eadf", $date_to_process);
copy($from_file, $to_file);

printf("Gzipping the file...\n\n");
$gzip_file = sprintf("ecmwfo3_%d.eadf", $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 . "/ecmwf/" . $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 . "/ecmwf/" . $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_ecmwf_ods_capture";


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




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

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

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

  $from_file = sprintf("%s/%04d/%02d/%s.gz", $ecmwf_dir, $year, $month, $file);
  $to_file   = sprintf("%s/%s.gz", $work_dir, $file);

  if (-e $from_file)
    {

    # Copy the ECMWF grib file

    printf("Copy from: %s\n", $from_file);
    printf("       to: %s\n\n", $to_file);

    copy($from_file, $to_file);

    # Unzip the file

    printf("Unzipping the file\n\n");

    system "gunzip " . $to_file;

    # Set the names of the input ECMWF grib file and the tempoary output files

    $ecmwf_file = sprintf("%s/%s", $work_dir, $file);

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

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

    printf("\nRunning WGRIB to extract data from the ECMWF file\n\n");

    system $source_dir . "/unpack_from_grib " . $ecmwf_file . " " . $temp_binary_file;

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

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

    symlink $temp_binary_file, "fort.11";
    symlink $work_dir . "/ecmwfToBinary_output." . $hour . ".bin", "fort.20";

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

    system $exec;

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

    # Delete the ECMWF grib file and the temporary file that was
    # generated by the WGRIB step

    unlink $ecmwf_file;
    unlink $temp_binary_file;
    }
  else
    {
    printf("The %sz file could not be found.\n", $hour);
    printf("File: %s\n", $from_file);
    }


  #$ods_exec = "/data/data065/nprovs/scripts/graphics/run_ecmwfo3_ods_capture";
  #system $ods_exec;
  }

# end of file
