package cal_pipe2;

require Exporter;

@ISA      = qw(Exporter);
@EXPORT   = qw(isis_geo);

use Getopt::Long;

sub isis_geo {

  my ($infile, $with_back, $back_only, $stat) = @_;

  $isisbin  = $ENV{ISISEXE};
  $kerdefdir= $ENV{ISISCSSDATA};
  @words    = split/\//, $infile;
  $cub      = $words[$#words];
  $pos      = length($cub) - 4;
  $ir       = $cub;
  $vis      = $cub;
  $visbp    = $cub;
  $irbp     = $cub;
  $cvt      = "geo/${cub}.cvt";
  $bsq      = "geo/${cub}";
  $stats    = "geo/cube_stats.txt";

  substr($ir, $pos)    = "_ir.cub";
  substr($vis, $pos)   = "_vis.cub";
  substr($irbp, $pos)  = "_ir_geo.cub";
  substr($visbp, $pos) = "_vis_geo.cub";

  $ir    = "geo/$ir";
  $vis   = "geo/$vis";
  $irbp  = "geo/data/$irbp";
  $visbp = "geo/data/$visbp";

  open INFILE, "$infile" or die "Can not open $infile ...\n";

  # Check to make sure directories exist
  if(!(-d "geo")){
    mkdir "geo";
  }
  if(!(-d "geo/data")){
    mkdir "geo/data";
  }

  $sys=`uname`;
  chomp $sys;

  # Read input file header to get necessary keywords and values.
  INPUT: while(<INFILE>){
    ($key,$value)=split/ = /;
    ($key,$junk)=split " ",$key;
    chop $value;
    chop $value;

    if($key eq "CORE_ITEMS"){
      $value =~ s/\(//;
      $value =~ s/\)//;
      $core = $value;
      ($sample, $band, $line)=split/,/,$value;
    }elsif($key eq "CORE_ITEM_TYPE"){
      ($core_type,$junk)=split/_/, $value;
    }
    elsif($key eq "TARGET_NAME"){
      $target=$value;
      $target =~ s/"//g;
    }
    elsif($key eq "NATIVE_START_TIME"){
      $st_time=$value;
      $st_time =~ s/"//g;
    }
    elsif($key eq "NATIVE_STOP_TIME"){
      $sp_time=$value;
      $sp_time =~ s/"//g;
    }
    elsif($key eq "INTERLINE_DELAY_DURATION"){
      $interline=$value;
    }
    elsif($key eq "EXPOSURE_DURATION"){
      $exp=$value;
      $value =~ s/\(//;
      $value =~ s/\)//;
      ($irexp, $visexp)=split/,/,$value;
    }
    elsif($key eq "SEQUENCE_ID"){
      $seq=$value;
      $seq =~ s/"//g;
      $ker = "${kerdefdir}/${seq}_kernels.def";
    }
    elsif($key eq "BAND_BIN_CENTER"){
      last INPUT;
    }
  }

  #print "infile - $infile\n";
  #print "cvt - $cvt\n";
  #print "ker - $ker\n";
  #print "target - $target \n";
  #print "start - $st_time \n";
  #print "stop - $sp_time \n";
  #print "interline - $interline \n";
  #print "exp -  $exp\n";
  #print "irexp - $irexp \n";
  #print "visexp - $visexp \n";
  #print "core - $core\n";
  #print "sample - $sample \n";
  #print "line - $line \n";
  #print "sys - $sys\n";
  #print "core_type - $core_type\n";

  # Check the core type to make sure it is compatible with the current platform 
  # and convert to platform type if necessary prior to converting from BIL to BSQ
  if(($sys eq "Linux" && $core_type eq "SUN") ||
     ($sys ne "Linux" && $core_type eq "PC")){
    $cmd="$isisbin/convert 'from=$infile to=$cvt'; # >> /dev/null";
    system($cmd);
    $cmd="$isisbin/transpose 'from=$cvt to=$bsq order=BSQ' >> /dev/null";
    system($cmd);

  }
  else{
    $cmd="$isisbin/transpose 'from=$infile to=$bsq order=BSQ'"; # >> /dev/null;
    system($cmd);
  }

  # Split the cubes into ir and vis
  $cmd="$isisbin/dsk2dsk 'from=$bsq to=$vis sfrom=\"::1-96\"'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/dsk2dsk 'from=$bsq to=$ir sfrom=\"::97-352\"'"; # >> /dev/null;
  system($cmd);

  # remove the converted and bsq cubes
  $cmd="rm $cvt $bsq > /dev/null 2>&1";
  system($cmd);

  # Put ISIS required labels in the vis cube
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_TARGET\" keyword=\"TARGET_NAME\" keyval=$target keytype=string'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"SPACECRAFT_NAME\" keyval=\"CASSINI\"'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"INSTRUMENT_ID\" keyval=\"VIMS\"'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"CHANNEL\" keyval=\"VIS\" keytype=string' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"NATIVE_START_TIME\" keyval=$st_time keytyp=string' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"NATIVE_STOP_TIME\" keyval=$sp_time keytyp=string' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"EXPOSURE_DURATION\" keyval=$visexp keytyp=double' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"INTERLINE_DELAY_DURATION\" keyval=\"$interline\" keytyp=double' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"LINES\" keyval=$line keytyp=integer' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$vis list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"SAMPLES\" keyval=$sample keytyp=integer' >> /dev/null";
  system($cmd);

  # Put ISIS required labels in the ir cube
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_TARGET\" keyword=\"TARGET_NAME\" keyval=$target keytype=string'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"SPACECRAFT_NAME\" keyval=\"CASSINI\"'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"INSTRUMENT_ID\" keyval=\"VIMS\"'"; # >> /dev/null;
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"CHANNEL\" keyval=\"IR\" keytype=string' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"NATIVE_START_TIME\" keyval=$st_time keytyp=string' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"NATIVE_STOP_TIME\" keyval=$sp_time keytyp=string' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"EXPOSURE_DURATION\" keyval=$irexp keytyp=double' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"INTERLINE_DELAY_DURATION\" keyval=\"$interline\" keytyp=double' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"LINES\" keyval=$line keytyp=integer' >> /dev/null";
  system($cmd);
  $cmd="$isisbin/labels 'from=$ir list=none grpnam=\"ISIS_INSTRUMENT\" keyword=\"SAMPLES\" keyval=$sample keytyp=integer' >> /dev/null";
  system($cmd);


  # Run levinit on vis and ir cubes
  print("about to run levinit, ker = $ker, isisbin = $isisbin\n");
  system("ls");
#  $cmd="$isisbin/levinit vims2isis 'from=$vis kernlst=\"$ker\"'"; # >> /dev/null;
  $cmd="$isisbin/levinit 'from=$vis kernlst=\"$ker\"'"; # >> /dev/null;
  print("$cmd\n");
  system($cmd);
#  $cmd="$isisbin/levinit vims2isis 'from=$ir kernlst=\"$ker\"'"; # >> /dev/null;
  $cmd="$isisbin/levinit 'from=$ir kernlst=\"$ker\"'"; # >> /dev/null;
  print("$cmd\n");
  system($cmd);

  if($with_back){
    # Generate the geometric info as backplanes for both cubes
    $cmd="$isisbin/levgeoplane 'from=$vis lat=yes lon=yes sampres=yes lineres=yes phase=yes inc=yes ema=yes noraz=yes'"; # >> /dev/null;
    system($cmd);
    $cmd="$isisbin/levgeoplane 'from=$ir lat=yes lon=yes sampres=yes lineres=yes phase=yes inc=yes ema=yes noraz=yes'"; # >> /dev/null;
    system($cmd);
  }

  if($back_only){
    # Generate the geometric info as seperate cubes for both cubes
    $cmd="$isisbin/levgeoplane 'from=$vis to=$visbp lat=yes lon=yes sampres=yes lineres=yes phase=yes inc=yes ema=yes noraz=yes'"; # >> /dev/null;
    system($cmd);
    $cmd="$isisbin/levgeoplane 'from=$ir to=$irbp lat=yes lon=yes sampres=yes lineres=yes phase=yes inc=yes ema=yes noraz=yes'"; # >> /dev/null;
    system($cmd);
  }

  if($stat){
    # Generate the statics for the cube
    $cmd="$isisbin/lev1stats 'from=$ir to=$stats'"; # >> /dev/null;
    system($cmd);
    $cmd="$isisbin/lev1stats 'from=$vis to=$stats'"; # >> /dev/null;
    system($cmd);
  }

  # Clean up the mess you made
  if(!$with_back){
    $cmd="rm $vis >> /dev/null";
    system($cmd);
    $cmd="rm $ir >> /dev/null";
    system($cmd);
  }

  if(!$back_only){
    $cmd="rm -rf geo/data >> /dev/null";
    system($cmd);
  }

}
