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

#$do_ods = "true";
$do_ods = "false";


#------------------------------------------------------------------------
# Set the date to process

$date_to_process = $ARGV[0];

if (not defined $date_to_process)
  {
  $date_to_process = 20211010;
  }

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/netcdf";
$work_dir   = "/data/data065/nprovs/source/data_transfer/ecmwf/netcdf";
$ecmwf_dir  = "/data/data300/pub/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_00z = sprintf("UAD%04d0000%04d00001", $mmdd, $mmdd);
$file_06z = sprintf("UAD%04d0600%04d06001", $mmdd, $mmdd);
$file_12z = sprintf("UAD%04d1200%04d12001", $mmdd, $mmdd);
$file_18z = sprintf("UAD%04d1800%04d18001", $mmdd, $mmdd);

processHour("00", $file_00z);
#processHour("06", $file_06z);
#processHour("12", $file_12z);
#processHour("18", $file_18z);


#------------------------------------------------------------------------
# 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 . "/ecmwf_reprocessed.eadf";

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


# Remove the previous log file

#unlink $work_dir . "/ecmwfToBinary.log";


# Run the program

#symlink $ecmwf_daily_file, "fort.11";
#symlink $work_dir . "/ecmwfToBinary_output.00.bin", "fort.20";
#symlink $work_dir . "/ecmwfToBinary_output.06.bin", "fort.21";
#symlink $work_dir . "/ecmwfToBinary_output.12.bin", "fort.22";
#symlink $work_dir . "/ecmwfToBinary_output.18.bin", "fort.23";


#$exec = sprintf("%s/ecmwfBinaryToDailyFile.x <<EOD > ecmwfBinaryToDailyFile.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 . "/ecmwfToBinary_output.00.bin";
#unlink $work_dir . "/ecmwfToBinary_output.06.bin";
#unlink $work_dir . "/ecmwfToBinary_output.12.bin";
#unlink $work_dir . "/ecmwfToBinary_output.18.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.

###if ($do_ods == "true")
##  generateODS();


#-------------------------------------------------------------------------
# Remove the reprocessed file

#unlink $ecmwf_daily_file;


# 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 temporary 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);
    }
  }




# ===========================================================================
# ===========================================================================
# ===========================================================================
# The subroutine generateODS creates an ODS file from the newly created
# daily file. It then creates images that are used on the NPROVS Daily
# Coverage web page.

sub generateODS
  {
  printf("do ods\n");

  my $hours_to_process = "0 6 12 18";
  #my $hours_to_process = "12";

  # =============================================================================
  # Create a temporary directory into which the pieces of the file 
  # will be written

  my $ods_source_dir = "/data/data065/nprovs/source/graphics/ods_capture/ecmwf_o3";
  my $ods_dir = "/data/data065/nprovs/data/ods/ecmwf";
  my $ftp_dir = "/net/www/aftp/pub/smcd/opdb/nprovs/ods";

  $file_name = "ecmwf_" . $date_to_process . ".ods";
  $ods_file = $ods_dir . "/" . $file_name;


  chdir $ods_source_dir;
  mkdir $ods_source_dir . "/ods";

  # =============================================================================
  # Create an ODS file for the ECMWF

  symlink $ecmwf_daily_file, "in.file" or warn "Cannot link to in.file";
  symlink $ods_source_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
  symlink $ods_source_dir . "/ecmwf_o3.xml", "parm.file" or warn "Cannot link to parm.file";
  symlink $ods_source_dir . "/parm_defs", "parmdefs.dir" or warn "Cannot link to parmdefs.dir";

  system $ods_source_dir . "/createODSFromECMWF.x \"ECMWF Analysis\" " . $date_to_process . " " . $hours_to_process;

  unlink "in.file" or warn "Cannot unlink in.file";
  unlink "out.dir" or warn "Cannot unlink out.dir";
  unlink "parm.file" or warn "Cannot unlink parm.file";
  unlink "parmdefs.dir" or warn "Cannot unlink parmdefs.dir";

  # Copy the filter_options.xml file to the ods directory

  #system "cp filtering_options.xml ods/filtering_options.xml";

  # Zip everything and then delete the temporary directory and everything in it

  system "zip -r " . $ods_file . " ods";
  system "rm -r " . $ods_source_dir . "/ods";

  # =============================================================================
  # Create an image from the ODS file and send the image to the STAR web site
  # for routine monitoring

  # 00Z
  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/ecmwf_00z.xml");
  $output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_00z_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_00z_%d_thumb.png", $date_to_process);
  $thumb_width  = sprintf("thumbwidth:100");
  $thumb_height = sprintf("thumbheight:75");
  $verbose      = sprintf("verbose:no");
  $file_info    = sprintf("file_info:no");

  $exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

  system $exec;


  # 06Z
  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/ecmwf_06z.xml");
  $output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_06z_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_06z_%d_thumb.png", $date_to_process);
  $thumb_width  = sprintf("thumbwidth:100");
  $thumb_height = sprintf("thumbheight:75");
  $verbose      = sprintf("verbose:no");
  $file_info    = sprintf("file_info:no");

  $exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

  system $exec;


  # 12Z
  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/ecmwf_12z.xml");
  $output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_12z_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_12z_%d_thumb.png", $date_to_process);
  $thumb_width  = sprintf("thumbwidth:100");
  $thumb_height = sprintf("thumbheight:75");
  $verbose      = sprintf("verbose:no");
  $file_info    = sprintf("file_info:no");

  $exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

  system $exec;


  # 18Z
  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/ecmwf_18z.xml");
  $output_file  = sprintf("output:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_18z_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/net/www/www/smcd/opdb/nprovs/images/coverage/ecmwf_18z_%d_thumb.png", $date_to_process);
  $thumb_width  = sprintf("thumbwidth:100");
  $thumb_height = sprintf("thumbheight:75");
  $verbose      = sprintf("verbose:no");
  $file_info    = sprintf("file_info:no");

  $exec = sprintf("/usr/bin/java -Djava.awt.headless=true -jar /data/data065/nprovs/source/graphics/imagemaker/ImageMaker.jar %s %s %s %s %s %s %s %s", $input_file, $parm_file, $output_file, $thumb_file, $thumb_width, $thumb_height, $verbose, $file_info);

  system $exec;


  # =============================================================================
  # Copy the ODS file to the FTP site

  $ftp_file = $ftp_dir . "/" . $file_name;

  printf("Copying from: %s\n", $ods_file);
  printf("          to: %s\n\n", $ftp_file);

  copy($ods_file, $ftp_file);
  }

# end of file
