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

# This script copies data from multiple AIRS files into an daily file

BEGIN
  {
  $ENV{LD_LIBRARY_PATH}="LD_LIBRARY_PATH:/data/starfs1/libs/netcdf-4.2/lib:/data/starfs1/libs/hdf5-1.8.7/lib";
  }


$true = 0;
$false = 1;

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


#-------------------------------------------------------------------------
# Variables that can be changed to affect the date to be processed, the location
# of the input data and the location of the output

# Uncomment $year, $mon and $mday to process a specific day. If they are
# commented, then yesterday's data will be processed.

# ***********************
# ***********************
# ***********************
$year = 2025;
$mon  = 3;
$mday = 4;
#$year = 0;
#$mon  = 0;
#$mday = 0;
# ***********************
# ***********************
# ***********************

$yday = dayofyear($year, $mon, $mday);


$date_to_process = ($year*10000) + ($mon*100) + $mday;
printf("Date to process:  %d    Julian Date:  %d\n", $date_to_process, $yday);


# Create a temporary working directory

$data_day = time();
($now_sec, $now_min, $now_hour, $now_mday, $now_mon, $now_year, $now_wday, $now_yday, $now_isdst) = localtime($data_day);
$now_year += 1900;
$now_mon++;

$now_isdst = 0;
$now_yday  = 0;
$now_wday  = 0;

$work_dir = sprintf("/data/data599/work_dirs/airs_%04d%02d%02d_%02d%02d%02d", $now_year, $now_mon, $now_mday, $now_hour, $now_min, $now_sec);

mkdir $work_dir;
chdir $work_dir;

mkdir $work_dir . "/ods";



$source_dir   = "/data/data065/nprovs/source/data_transfer/airs/version6";

$archive_dir  = "/data/data599/orbital_archive/airs";
$archive_dir2 = "/data/data215/nprovs/data/orbital_archive/airs";

$ods_source_dir = "/data/data065/nprovs/source/graphics/ods_capture/airs";
$ods_dir = "/data/data065/nprovs/data/ods/airs";
$ftp_dir = "/data/www/data/nprovs/monitoring/ods";



#-------------------------------------------------------------------------
# Copy the files for the selected date from the NASA server to the incoming directory

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

$wget = sprintf("wget --load-cookies ~/.urs_cookies --save-cookies ~/.urs_cookies --auth-no-challenge=on --keep-session-cookies --level=1 -r -nH -nd -np -A hdf https://discnrt1.gesdisc.eosdis.nasa.gov/data/Aqua_AIRS_NRT/AIRS2SUP_NRT.006/%d/%03d/", $year, $yday);

printf("%s\n", $wget);

system $wget;



#-------------------------------------------------------------------------
# Set up a new EDIF file

$addf_file_name = "airs_" . $date_to_process . ".addf";

my $output_file_name = "$work_dir/" . $addf_file_name;



# Process the granules

@airsfiles = glob "$work_dir/AIRS*.hdf";

foreach $file (@airsfiles)
  {
  printf("Processing file:  %s\n", $file);

  symlink $output_file_name, "out.file" or warn "Cannot link $addf_file to out.file";

  system "$source_dir/AIRStoADDF.x " . $file;

  unlink "out.file" or warn "Cannot unlink the out.file";
  }



#-------------------------------------------------------------------------
# Create an ODS file if $do_ods is set to TRUE

if ($do_ods == $true)
  {
  $ods_file_name = sprintf("airs_%d%02d%02d.ods", $year, $mon, $mday);
  $ods_file = $ods_dir . "/" . $ods_file_name;


  # Run the main program
  symlink $output_file_name, "in.file" or warn "Cannot link to in.file";
  symlink $work_dir . "/ods", "out.dir" or warn "Cannot link to out.dir";
  symlink $ods_source_dir . "/airs.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 . "/createODSFromAirs.x \"AIRS\" " . $date_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 " . $ods_source_dir . "/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 " . $work_dir . "/ods";



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

  # Build the command line executable for ascending and then run it

  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/euiasi_asc.xml");
  $output_file  = sprintf("output:/data/www/smcd/opdb/nprovs/images/coverage/airs_asc_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/data/www/smcd/opdb/nprovs/images/coverage/airs_asc_%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;


  # Build the command line executable for descending and then run it

  $input_file   = sprintf("file:%s", $ods_file);
  $parm_file    = sprintf("parmfile:/data/data065/nprovs/source/graphics/imagemaker/defaults/euiasi_des.xml");
  $output_file  = sprintf("output:/data/www/smcd/opdb/nprovs/images/coverage/airs_des_%d.png", $date_to_process);
  $thumb_file   = sprintf("thumbnail:/data/www/smcd/opdb/nprovs/images/coverage/airs_des_%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

  my $ftp_file = $ftp_dir . "/" . $ods_file_name;

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

  copy($ods_file, $ftp_file);
  }  # if ($do_ods...



#-------------------------------------------------------------------------
# Gzip the daily file and copy it to the orbital archive

printf("Copying the daily file to the archive directories\n\n");

system "gzip " . $output_file_name;

$gzipped_file = $output_file_name . ".gz";

$to_file = $archive_dir . "/" . $addf_file_name . ".gz";
copy($gzipped_file, $to_file);

#$to_file = $archive_dir2 . "/" . $addf_file_name . ".gz";
##copy($gzipped_file, $to_file);



#-------------------------------------------------------------------------
# Remove the temporary working directory

system "rm -r " . $work_dir;





sub dayofyear
  {
  my ($year, $month, $day)=@_;
  my @days_in_month = (0,31,59,90,120,151,181,212,243,273,304,334,365);
  my $julday = $days_in_month[$month-1] + $day;

  my $is_leapyear = 0;

  if (($year % 4) == 0)
    {
    $is_leapyear = 1;
    }

  if (($year % 100) == 0)
    {
    $is_leapyear = 0;
    }

  if (($year % 400) == 0)
    {
    $is_leapyear = 1;
    }

  if (($is_leapyear == 1) && ($month >= 3))
    {
    $julday = $julday + 1;
    }

  return $julday;
  }

# end of file
