#!/usr/bin/perl -s


use File::Copy;
use Cwd;

my ($progname) = ($0 =~ m#([^/]+)$#);  # get the name of this program

$email = "kite\@berkeley.edu";

my $usage = "
This is a script to prepare CTX stereo pairs for import to SOCET SET.
Author = Edwin Kite ($email)
The neccessary steps are from a recipe kindly supplied by Annie Howington-Kraus,
and the script is adopted from ctx4socet_kite.pl also written by Annie Howington-Kraus.
The script should be launched from within the project directory (e.g. \"Tempe1\"),
and uses IMG files from the PDS as its starting material.
";

   $| = 1;

#---------------------------------------------------------------------
# Check the argument list
#---------------------------------------------------------------------
   if ($#ARGV < 1 || $#ARGV > 2)
      {
      print "$usage\n";
      exit 1;
      }

#---------------------------------------------------------------------
# Obtain the input file name
#---------------------------------------------------------------------

   $ctx1 = $ARGV[0];
   $ctx2 = $ARGV[1];

#---------------------------------------------------------------------
# Open LOG file
#---------------------------------------------------------------------

   $log = "ctx4socet_kite.err";
   open (LOG,">$log") or die "\n Cannot open $log\n";

#---------------------------------------------------------------------
# Make sure input file exists
#---------------------------------------------------------------------

   if (!(-e $ctx1))
      {
      print "*** ERROR *** Input CTX file no. 1 does not exist: $ctx1\n";
      print "ctx4socet_kite.pl will terminate\n";
      exit 1;
      }


   if (!(-e $ctx2))
      {
      print "*** ERROR *** Input CTX file no. 2 does not exist: $ctx2\n";
      print "ctx4socet_kite.pl will terminate\n";
      exit 1;
      }

#------------------------------------------------------------------------------------------------------------
# Run CTX pre-processing steps (from notes supplied by Annie Howington-Kraus stapled in to 'Stereo' notebook)
#------------------------------------------------------------------------------------------------------------

   $img_name1 = substr($ctx1,0,-3); #Strip trailing 'IMG'
   $img_name2 = substr($ctx2,0,-3); #Strip trailing 'IMG'

#------------------------------------------------------------------------------------------------------------
# Step 2 - convert to isis format
#------------------------------------------------------------------------------------------------------------

#First make backups of the IMGs downloaded from the PDS
   $cmd = "cp $ctx1 $ctx1.backup";
   system($cmd);
   $cmd = "cp $ctx2 $ctx2.backup";
   system($cmd);


#Now convert to isis format
   $ctx1cub = $img_name1 . ".cub";
   $cmd = "mroctx2isis from=$ctx1 to=$ctx1cub";
   system($cmd) == 0 || ReportErrAndDie ("mroctx2isis failed on $ctx1"); 


   $ctx2cub = $img_name2 . ".cub";
   $cmd = "mroctx2isis from=$ctx2 to=$ctx2cub";
   system($cmd) == 0 || ReportErrAndDie ("mroctx2isis failed on $ctx2"); 

#------------------------------------------------------------------------------------------------------------
# Step 3 - ctxcal and ctxevenodd
#------------------------------------------------------------------------------------------------------------

   #Run ctxcal
   $ctx1calcub = $img_name1 . ".cal.cub";
   $cmd = "ctxcal from=$ctx1cub to=$ctx1calcub";
   system($cmd) == 0 || ReportErrAndDie ("ctxcal failed on $ctx1"); 

   #Run ctxevenodd
   $ctx1lev1cub = $img_name1 . ".lev1.cub";
   $cmd = "ctxevenodd from=$ctx1calcub to=$ctx1lev1cub";
   system($cmd) == 0 || ReportErrAndDie ("ctxevenodd failed on $ctx1"); 


   #Run ctxcal
   $ctx2calcub = $img_name2 . ".cal.cub";
   $cmd = "ctxcal from=$ctx2cub to=$ctx2calcub";
   system($cmd) == 0 || ReportErrAndDie ("ctxcal failed on $ctx2"); 

   #Run ctxevenodd
   $ctx2lev1cub = $img_name2 . ".lev1.cub";
   $cmd = "ctxevenodd from=$ctx2calcub to=$ctx2lev1cub";
   system($cmd) == 0 || ReportErrAndDie ("ctxevenodd failed on $ctx2"); 


#------------------------------------------------------------------------------------------------------------
# Step 4 - spiceinit
#------------------------------------------------------------------------------------------------------------
 
   $cmd = "spiceinit from=$ctx1lev1cub attach=no";
   system($cmd) == 0 || ReportErrAndDie ("spiceinit failed on $ctx1"); 


   $cmd = "spiceinit from=$ctx2lev1cub attach=no";
   system($cmd) == 0 || ReportErrAndDie ("spiceinit failed on $ctx2"); 


#------------------------------------------------------------------------------------------------------------
# Step 4B - Run hidata4socet.pl to gather required information for SOCET SET.
#------------------------------------------------------------------------------------------------------------

#Part 1: Set up the directory structure required by hidata4socet.pl, move files to their appropriate places

   $cmd = "mkdir $img_name1";
   system($cmd) == 0 || ReportErrAndDie ("Failed to create directory $img_name1");

   $cmd = "mkdir $img_name2";
   system($cmd) == 0 || ReportErrAndDie ("Failed to create directory $img_name2");

   $cmd = "mv $ctx1lev1cub $img_name1";
   system($cmd) == 0 || ReportErrAndDie ("Failed to move $ctx1lev1cub to directory $img_name1");
 
   $cmd = "mv $ctx2lev1cub $img_name2";
   system($cmd) == 0 || ReportErrAndDie ("Failed to move $ctx2lev1cub to directory $img_name2");

#Part 2: Run hidata4socet.pl.

   $name_of_this_directory = "Tempe1";      
   $pti1 = $img_name1 . "/" . $ctx1lev1cub; #Path to image 1
   $pti2 = $img_name2 . "/" . $ctx2lev1cub; #Path to image 2
   $cmd = "hidata4socet.pl $name_of_this_directory  $pti1 $pti2";
   system($cmd) == 0 || ReportErrAndDie ("Hidata4socet.pl encountered problems!");

#------------------------------------------------------------------------------------------------------------
# Step 5 - Pad the images to force boresight at image center (required for Generic Pushbroom sensor model)
#------------------------------------------------------------------------------------------------------------
 
#   $cmd = "spiceinit from=$ctx1lev1cub attach=no";
#   system($cmd) == 0 || ReportErrAndDie ("spiceinit failed on $ctx1"); 

exit; 
  
##############################################################################
#  Error Handling Subroutine
##############################################################################
sub ReportErrAndDie
    {
    my $ERROR=shift;

    print "$ERROR\n";
    print "hi4socet.pl aborted\n";

    print LOG "$ERROR\n";
    close(LOG);
    exit 1;
    }


# End of script
