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

$source_dir       = "/data/data065/nprovs/gas/ozone/EcmwfOzonesondeCollocator";
$work_dir         = "/data/data065/nprovs/gas/ozone/EcmwfOzonesondeCollocator";
$collocations_dir = "/data/data599/gas/ozone/collocations";
$archive_dir      = "/data/data599/orbital_archive/ecmwf";

chdir($work_dir);


# Date range

#$first_date = 20231007;
#$last_date  = 20231130;

$first_date = 20230515;
$last_date  = 20230802;


$year = int($first_date / 10000);
$mon  = int(($first_date % 10000) / 100) - 1;
$day  = $first_date % 100;

$data_date = timelocal(12, 0, 0, $day, $mon, $year);
$data_date = $data_date - 86400;


$year = int($last_date / 10000);
$mon  = int(($last_date % 10000) / 100) - 1;
$day  = $last_date % 100;

$end_date = timelocal(12, 0, 0, $day, $mon, $year);


# Loop through the date range and process the files that exist and
# are within the range

while ($data_date < $end_date)
  {
  $data_date += 86400;

  ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($data_date);
  $year += 1900;
  $mon++;
  $sec = 0;
  $min = 0;
  $hour = 12;
  $wday = 0;
  $yday = 0;
  $isdst = 0;

  $day_of_data = ($year * 10000) + ($mon * 100) + $mday;


  # If a directory exsists that matches $day_of_data then process it

  $dir_name = sprintf("%s/%d", $collocations_dir, $day_of_data);

  if (-e $dir_name)
    {
    printf("\n============================================================\n");
    printf("Collocation Date: %d\n\n", $day_of_data);

    # Unpack the ECMWF data for this date

    $zipped_file = sprintf("%s/ecmwfo3_%d.eadf.gz", $archive_dir, $day_of_data);
    $temp_file   = sprintf("%s/ecmwfo3_%d.eadf.gz", $work_dir, $day_of_data);
    $ecmwf_file  = sprintf("%s/ecmwfo3_%d.eadf", $work_dir, $day_of_data);

    printf("Copying the ECMWF granules to the temporary data directory...\n");
    copy($zipped_file, $temp_file);

    printf("Gunzipping the ECMWF file...\n");
    system "gunzip $temp_file";


    # Process all subdirectories within the collocation date directory

    opendir my $dh, $dir_name or die "Could not open '$dir_name' for reading '$!'\n";

    my @dir_list = readdir $dh;

    foreach my $dir (@dir_list)
      {
      $char = substr($dir, 0 , 1);

      if ($char ne '.')
        {
        processCollocation($dir_name, $dir);
        }
      }

    closedir $dh;


    # Remove the ECMWF file

    unlink $ecmwf_file;

    }  # if (-e $dir_name)...
  }  # while ($data_date < $end_date...



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

sub processCollocation
  {
  my ($main_dir, $collocation_dir) = @_;

  # Look for the ozonesonde file

  $subdir = sprintf("%s/%s/ozonesonde*", $main_dir, $collocation_dir);

  my @sondefiles = glob $subdir;

  foreach $file (@sondefiles)
    {
    printf("Processing site:  %s\n", $collocation_dir);
    #printf("Ozonesonde file:  %s\n\n", $file);

    $ecmwf_netcdf_file = sprintf("%s/%s/ecmwf.nc", $main_dir, $collocation_dir);

    symlink $file, "ozonesonde.in" or warn "Cannot link %file to ozonesonde.in";
    symlink $ecmwf_netcdf_file, "ecmwf.out" or warn "Cannot link $ecmwf_netcdf_file to ecmwf.out";
    symlink $ecmwf_file, "ecmwf.in" or warn "Cannot link $file to in.file";

    system $source_dir . "/EcmwfOzonesondeCollocator.x 800 ECMWF";

    unlink "ecmwf.in" or warn "Cannot unlink ecmwf.in";
    unlink "ecmwf.out" or warn "Cannot unlink ecmwf.out";
    unlink "ozonesonde.in" or warn "Cannot unlink ozonesonde.in";
    }  # foreach ($file...
  }  # sub processSite...

# end of file
