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



# This script is the start of the NPROVS collocation process. It copies radiosonde data from the
# unified.radiosonde format, creates date directories in the nprovs collocation directory, and
# create netCDF files from the data in the unified radiosonde.


$work_dir       = "/data/data065/nprovs/source/collocate/NprovsCollocator/NucapsCollocator/work_dir";
$source_dir     = "/data/data065/nprovs/source/collocate/NprovsCollocator/NucapsCollocator";
$input_data_dir = "/data/data065/nprovs/source/collocate/NprovsCollocator/NucapsCollocator/work_dir";
#$input_data_dir = "/data/data065/nprovs/source/collocate/NprovsCollocator/NucapsCollocator/temp_dir";
$output_dir     = "/data/data599/collocations/nprovs";


#print "\n\nProcessing the GOES data\n\n";

chdir($work_dir);


# Determine which date to process. This will normally be yesterday, but that
# can be overridden.

$yesterday = time();
#$yesterday -= (1 * 86400);
$yesterday -= (3 * 86400);

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

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


printf("\nProcessing NUCAPS on %d\n", $date_to_process);


# Copy the files for the dates to the working directory

#$year = $date_before / 10000;
#$mon  = ($date_before % 10000) / 100;
#$mday = $date_before % 100;


printf("Transferring data from SCDR...\n");

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




# Run the program to collocate the NUCAPS to the collocation

symlink $input_data_dir, "in.dir" or warn "Cannot link $input_data_dir to in.dir";
symlink $output_dir, "out.dir" or warn "Cannot link $output_dir to out.dir";


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


unlink "out.dir" or warn "Cannot unlink out.dir";
unlink "in.dir" or warn "Cannot unlink in.dir";



# *********************************************************************
# *********************************************************************
# *********************************************************************
# *********************************************************************
# *********************************************************************
# *********************************************************************

#$source_dir         = "/data/data065/nprovs/gas/ozone/NucapsOzonesondeCollocator";
#$binary_source_dir  = "/data/data065/nprovs/gas/ozone/NucapsNOAABinaryOzonesondeCollocator";
#$work_dir           = "/data/data065/nprovs/gas/ozone/NucapsOzonesondeCollocator";
#$collocations_dir   = "/data/data599/gas/ozone/collocations";
#$archive_dir        = "/data/data599/gas/ozone/archive/nucaps_noaa20";
#$binary_archive_dir = "/data/data599/orbital_archive/nucaps_noaa20";
#$temp_data_dir      = "/data/data065/nprovs/gas/ozone/NucapsOzonesondeCollocator/temp_data";

#chdir($work_dir);


# Date range

##$first_date = 20230112;
##$last_date  = 20230803;
#$first_date = 20230111;
#$last_date  = 20230111;


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

#    $data_status = 0;

    # 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;

##      $data_status = 1;
#      }  # if (-e $archive_file)...

    # Attempt to grab the data from SCDR

#    if ($data_status == 0)
#      {
##      chdir $temp_data_dir;

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

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


##      if ($retval == 0)
##        {
##        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;

##        $data_status = 2;
##	}  # if ($retval == 0...
#      }  # if (data_status == 0 (SCDR)...


#$data_status = 1;

    # If the data were not available from the archive or SCDR, then attempt to
    # transfer the binary data from the NPROVS orbital archive

#    $binary_file  = sprintf("%s/nucaps_noaa20_%d.iddf", $temp_data_dir, $day_of_data);

#    if ($data_status == 0)
#      {
#      $archive_file = sprintf("%s/nucaps_noaa20_%d.iddf.gz", $binary_archive_dir, $day_of_data);
#      $zipped_file  = sprintf("%s/nucaps_noaa20_%d.iddf.gz", $temp_data_dir, $day_of_data);

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

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

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

#        $data_status = 3;
#	}
#      }  # if (data_status == 0 (nprovs archive)...


    # Process all subdirectories within the collocation date directory

#    if ($data_status != 0)
#      {
#      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 '.')
#          {
#          if (($data_status == 1) || ($data_status == 2))
#            {
#            processCollocation($dir_name, $dir, $temp_data_dir);
#	    }
#          else
#            {
#	    processBinaryCollocation($dir_name, $dir, $binary_file);
#            }
#          }
#        }

#       closedir $dh;


      # Remove the NUCAPS granules

#      if (($data_status == 1) || ($data_status == 2))
#        {
##        my @tempfiles = glob $temp_data_dir . "/*.nc";

##        foreach $file (@tempfiles)
##          {
##          unlink $file;
##          }
#	}
#      else
#        {
##	unlink $binary_file;
#        }

#      }  # if ($data_status != 0...
#    }  # if (-e $dir_name)...
#  }  # while ($data_date < $end_date...



# ===========================================================================
# ===========================================================================
# ===========================================================================
# The subroutine processCollocation 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("\n\n------------------------------------------------\n");

#    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...



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

#sub processBinaryCollocation
#  {
#  my ($main_dir, $collocation_dir, $nucaps_file) = @_;

  # 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_file, "nucaps.in" or warn "Cannot link $file to in.file";

#    system $binary_source_dir . "/NucapsNOAABinaryOzonesondeCollocator.x 605 'NUCAPS NOAA-20'";

#    unlink "nucaps.in" or warn "Cannot unlink nucaps.in";
#    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
