#!/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/NucapsOzonesondeCollocator";
$work_dir         = "/data/data065/nprovs/gas/ozone/NucapsOzonesondeCollocator";
$collocations_dir = "/data/data599/gas/ozone/collocations";
$archive_dir      = "/data/data599/gas/ozone/archive/nucaps_noaa20";
$temp_data_dir    = "/data/data065/nprovs/gas/ozone/NucapsOzonesondeCollocator/temp_data";

chdir($work_dir);


# Date range

#$first_date = 20230401;
#$last_date  = 20230601;
$first_date = 20230717;
$last_date  = 20230717;


$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);

    # Look for the data in archive first

    $archive_file = sprintf("%s/nucaps_noaa20_%d.tar.gz", $archive_dir, $day_of_data);
    $zipped_file  = sprintf("%s/nucaps_noaa20_%d.tar.gz", $temp_data_dir, $day_of_data);
    $tarred_file  = sprintf("%s/nucaps_noaa20_%d.tar", $temp_data_dir, $day_of_data);

    if (-e $archive_file)
      {
      chdir $temp_data_dir;

      printf("Copying the data from the archive...\n");
      copy($archive_file, $zipped_file);

      printf("Unzipping the archive file...\n");
      system "gunzip " . $zipped_file;

      printf("Untarring the archive file...\n");
      system "tar -xvf " . $tarred_file;

      unlink $tarred_file;
      }  # if (-e $archive_file)...
    else
      {
      chdir $temp_data_dir;

      printf("\nCopying the data from SCDR...\n\n");

      system "/data/starfs1/bin/scdr-files -t NUCAPS-EDR --re-file _j01_ -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $temp_data_dir;

      printf("Tarring the granules...\n");
      system "tar -cvf " . $tarred_file . " *.nc";

      printf("Zipping the tarred file...\n");
      system "gzip " . $tarred_file;

      printf("Copying the zipped file to the archive...\n");
      copy ($zipped_file, $archive_file);

      unlink $zipped_file;
      }


    # Process all subdirectories within the collocation date directory

    chdir($work_dir);

    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, $temp_data_dir);
        }
      }

    closedir $dh;


    # Remove the NUCAPS granules

    my @tempfiles = glob $temp_data_dir . "/*.nc";

    foreach $file (@tempfiles)
      {
      unlink $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, $nucaps_data_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);

    $nucaps_netcdf_file = sprintf("%s/%s/nucaps_noaa20.nc", $main_dir, $collocation_dir);

    symlink $file, "ozonesonde.in" or warn "Cannot link %file to ozonesonde.in";
    symlink $nucaps_netcdf_file, "nucaps.out" or warn "Cannot link $nucaps_netcdf_file to nucaps.out";
    symlink $nucaps_data_dir, "nucaps.dir" or warn "Cannot link $file to in.file";

    system $source_dir . "/NucapsOzonesondeCollocator.x 605 'NUCAPS NOAA-20'";

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

# end of file
