#!/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\"),
Provide full paths for images (at the end of step 1, the lev1.pad.cubs will have been stored in subdirectories named after each image)
";

   $| = 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,-13); #Strip trailing '.lev1.pad.cub'
   $img_name2 = substr($ctx2,0,-13); #Strip trailing '.lev1.pad.cub'


#------------------------------------------------------------------------------------------------------------
# Step 8.1 - get stretched values from .txt files
#------------------------------------------------------------------------------------------------------------

#Import results of 'percent' for ctx image 1
$lowstretch=$ctx1."."._p0005.".".txt;
open(DAT,$lowstretch) || die("Could not open file! $lowstretch");
@lowdata1=<DAT>;
close(DAT);


$highstretch=$ctx1."."._p9995.".".txt;
open(DAT,$highstretch) || die("Could not open file! $highstretch");
@highdata1=<DAT>;
close(DAT);


#Import results of 'percent' for ctx image 2
$lowstretch=$ctx2."."._p0005.".".txt;
open(DAT,$lowstretch) || die("Could not open file! $lowstretch");
@lowdata2=<DAT>;
close(DAT);


$highstretch=$ctx2."."._p9995.".".txt;
open(DAT,$highstretch) || die("Could not open file! $highstretch");
@highdata2=<DAT>;
close(DAT);

#Grab the values and store them in array stretchval, as follows:
#[0] <--> highdata1, [1] <--> lowdata1, [2] <--> highdata2, [3] <--> lowdata2
  $value_line = @highdata1[2];
          @stretchvalue = split(/ = /,$value_line);
          @stretchval[0] = @stretchvalue[1];
          print "High stretch for \t $ctx1 \t is @stretchval[0]\n";


  $value_line = @lowdata1[2];
          @stretchvalue = split(/ = /,$value_line);
          @stretchval[1] = @stretchvalue[1];
          print "Low stretch for \t $ctx1 \t is @stretchval[1]\n";


  $value_line = @highdata2[2];
          @stretchvalue = split(/ = /,$value_line);
          @stretchval[2] = @stretchvalue[1];
          print "High stretch for \t $ctx2 \t is @stretchval[2]\n";

  $value_line = @lowdata2[2];
          @stretchvalue = split(/ = /,$value_line);
          @stretchval[3] = @stretchvalue[1];
          print "Low stretch for \t $ctx2 \t is @stretchval[3]\n";

#Strip spaces and newlines from stretchval array
          foreach (@stretchval) {
              $_ =~ s/ // 	;
   	      $_ =~ s/\n// 	;
          }

#------------------------------------------------------------------------------------------------------------
# Step 8.2 - Convert to 8-bit using values stored in stretchval
#------------------------------------------------------------------------------------------------------------
   #Run stretch on image 1
   $pairs1 = "0:0 ".@stretchval[1].":1 ".@stretchval[0].":254";
   print "$pairs1\n";
   $cmd = qq(stretch from=$ctx1 to=$img_name1.lev1.8bit.cub+8bit+1:254 pairs=\\"$pairs1\\" lis=1.0 lrs=1.0 his=254 hrs=254);
   print "$cmd\n";
   system($cmd) == 0 || ReportErrAndDie ("stretch failed on $ctx1");


   #Run stretch on image 2
   $pairs2 = "0:0 ".@stretchval[3].":1 ".@stretchval[2].":254";
   print "$pairs2\n";
   $cmd = qq(stretch from=$ctx2 to=$img_name2.lev1.8bit.cub+8bit+1:254 pairs=\\"$pairs2\\" lis=1.0 lrs=1.0 his=254 hrs=254);
   print "$cmd\n";
   system($cmd) == 0 || ReportErrAndDie ("stretch failed on $ctx2");


#------------------------------------------------------------------------------------------------------------
# Step 9 - Convert images to raw (bring these to Socet Set)
#------------------------------------------------------------------------------------------------------------

     $cmd = "isis2raw from=$img_name1.lev1.8bit.cub to=$img_name1.raw bittype=8bit stretch=none";
     system($cmd) == 0 || ReportErrAndDie ("isis2raw failed on $ctx1");

     $cmd = "isis2raw from=$img_name2.lev1.8bit.cub to=$img_name2.raw bittype=8bit stretch=none";
     system($cmd) == 0 || ReportErrAndDie ("isis2raw failed on $ctx2");

#------------------------------------------------------------------------------------------------------------
# Step 10 - Get Generic Pushbroom keywords list files (bring these to Socet Set)
#------------------------------------------------------------------------------------------------------------

#NOTE TO USER: If calc_pushbroom_keywords is not on your $PATH, replace with the full path to this file.

     $cmd = 	"/data/dyn01/kite/SOFTWARE_JUNE2009/ISIS_3.1.20/BIN/LINUX_X86_64BIT/calc_pushbroom_keywords from=$ctx1 pushkey=$img_name1._keywords.lis";
     system($cmd) == 0 || ReportErrAndDie ("calc_pushbroom_keywords failed on $ctx1");


     $cmd = 	"/data/dyn01/kite/SOFTWARE_JUNE2009/ISIS_3.1.20/BIN/LINUX_X86_64BIT/calc_pushbroom_keywords from=$ctx2 pushkey=$img_name2._keywords.lis";
     system($cmd) == 0 || ReportErrAndDie ("calc_pushbroom_keywords failed on $ctx2");

exit;


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

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

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


# End of script
                                                   

#end of script

