#!/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 copies data from multiple NUCAPS granule files into an IDDF

print "\n";
print "Processing the EUMETSAT IASI MetOp-C data\n";
print "\n";
system "date";
print "\n\n";

$source_dir   = "/data/data065/nprovs/source/data_transfer/eu_iasi";
$nprovs_dir   = "/data/data065/nprovs/data/matchup/DailyFiles_incoming";
$raw_data_dir = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/raw_data/euiasi_metopc";
$log_dir      = "/data/data065/nprovs/source/data_transfer/eu_iasi/logs";

chdir $raw_data_dir;


# Search the incoming directory and remove any file that was created
# more than 2 days ago

#my $incoming_template = "IASI_SND_";

#my @tempfiles = glob $raw_data_dir . "/" . $incoming_template . "*.nc";

#foreach $file (@tempfiles)
#  {
#  if (-M $file > 1)
#    {
#    unlink $file;
#    }
#  }



# Calculate the date of the data to be processed. By default, this
# is usually yesterday. To override, replace $data_date with the
# date in the form YYYYMMDD

$data_day = time();
$data_day -= (2 * 86400);

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

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

printf("Date to be processed:  %d\n\n", $date_to_process);



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

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

#system "/data/starfs1/bin/scdr-files -t IASI_EUL2 -satid m03 -d " . $year . "-" . $mon . "-" . $mday . " | xargs -L100 -P2 cp -t " . $raw_data_dir;


#my $iddf_file = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/eu_iasiV06.metopC.eidf";
my $iddf_file = "/data/data065/nprovs/data/matchup/DailyFiles_incoming/eu_iasiV06.metopC_test.eidf";



# Remove the previous IDDF

##unlink $iddf_file or warn "Cannot unlink the iddf file";
unlink $iddf_file;





# Set up the log file

$log_file = "daily_log_file.metopc";
open (LOGFILE, ">>$log_dir/$log_file");
print LOGFILE "\n=======================================================================\n";
print LOGFILE "\nStarting the script to process EUMETSAT IASI files for MetOp-C\n";
print LOGFILE `date`;
print LOGFILE "  \n";
print LOGFILE "\nDate to process:  $date_to_process\n";


# Goto exec directory
#chdir ("$source_dir") or die "Cannot cd to $source_dir:$!\n";


# Run the program to process the data

my $eidf_file1 = "$raw_data_dir/eu_iasiV06.M03.eidf1";
my $eidf_file2 = "$raw_data_dir/eu_iasiV06.M03.eidf2";
my $eidf_file = "eu_iasiV06.metopC_test.eidf";


# Remove the previous 2 eidf files
unlink $eidf_file1 or warn "Cannot unlink the eidf1 file";
unlink $eidf_file2 or warn "Cannot unlink the eidf2 file";


# find the total number of granule files
my @iasifiles = glob "$raw_data_dir/IASI_SND_02_M03_$date_to_process*";

$tot = @iasifiles;
        printf("tot= %d\n", $tot);
$mod = $tot%2;
        printf("residual mod= %d\n", $mod);

#split the total number into two halfs
$num_half01 = ($tot -$mod)/2;
$num_half02 = ($tot -$mod)/2 + $mod;
        printf("num_half01= %d\n", $num_half01);
        printf("num_half02= %d\n", $num_half02);


my @iasifiles_01;
my @iasifiles_02;
$num=0;

foreach $file (@iasifiles)
  {
  $num=$num+1;

  if ($num <= $num_half01)
    {
    push @iasifiles_01, $file;
    }

  if ($num >$num_half01)
    {
    push @iasifiles_02, $file;
    }
  }


$half_01 = @iasifiles_01;
        printf("half_01= %d\n", $half_01);
$half_02 = @iasifiles_02;
        printf("half_02= %d\n", $half_02);



$num=0;

foreach $file (@iasifiles_01)
  {
  #print "\n-------------------------------\n";
  print "Processing file 1: ". $file, "\n\n";

  # Link the files

  $num = $num + 1;
		
  #printf("number= %d\n", $num);
  print LOGFILE "number= ". $num,$file,"\n";

  symlink $eidf_file1, "out.file" or warn "Cannot link $eidf_file1 to out.file";
  system "$source_dir/EUIASItoEUIDDFnew.x $file >> $log_dir/$log_file";
#  system "$source_dir/EUIASItoEUIDDFnew.x $file";

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

  if ($num >350)
    {
    last;
    }
  }


##system ("rm in.file*");
##system ("rm out.file");

$num = 0;

foreach $file (@iasifiles_02)
  {
  #print "\n-------------------------------\n";
  print "Processing file 2: ". $file, "\n\n";

  # Link the files

  $num = $num + 1;
		
  #printf("number= %d\n", $num);
  print LOGFILE "number= ". $num,$file,"\n";

  symlink $eidf_file2, "out.file" or warn "Cannot link $eidf_file2 to out.file";
  system "$source_dir/EUIASItoEUIDDFnew.x $file >> $log_dir/$log_file";

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

  if ($num > 350)
    {
    last;
    }
  }

##system ("rm in.file*");
##system ("rm out.file");

#system "$source_dir/iasiv06_m03_combine.exe >> $log_dir/$log_file";
system "$source_dir/combine_2eidf_M03.x >> $log_dir/$log_file";

# Move the file to the incoming daily directory

system ("mv $raw_data_dir/$eidf_file  $nprovs_dir");


#remove all old files that are currently in the local directory
#@files = glob "$raw_data_dir/IASI_SND*";
#foreach $file (@files)
#	{
#  		if (-M $file > 2)
#      		{
#          		unlink $file;
#         	}
#         }



# end of file
