package cal_pipe2;

require Exporter;

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

use strict;

sub dark_vis {

 my ($file, $outfile, %head_vals) = @_;

  my $ss = $head_vals{start_sample};
  my $sl = $head_vals{start_line};
  my $es = $ss + $head_vals{samples} - 1;
  my $el = $sl + $head_vals{lines}   - 1;

  my ($the_dark, $cmd, $bands);

  # Subtract from this cube the appropriate piece of the dark.
  # If we have a POINT mode cube, subtract the appropriate pixel.
  my $vissm = $head_vals{vis_sampling_mode};
  my $ir_state = $head_vals{ir_power_state} eq "ON";
  my $cube_mode = $head_vals{instrument_mode_id};
  my $not_done = 0;
  my $temptbl = "temp_table";
  my $temptbl2 = "temp_table.tbl";

  $bands = "1-352";

  if ($cube_mode eq "IMAGE" or $cube_mode eq "CAL_SOLAR" or $cube_mode eq "LINE") {
    if ($vissm eq "NORMAL" or $vissm eq "UNK") {
      $the_dark = "tmp_vis_dark.cub";
      $cmd = "add from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$the_dark\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  to=$outfile" .
	     "  mul2=-1.0" .
	     "  option=c2c";
      print ("$cmd\n");
      system($cmd);
    } elsif ($vissm eq "HIGH" || $vissm eq "HI-RES") {
      $the_dark = "tmp_vis_dark.cub";

      my $samp_center = $ss + ($head_vals{samples}/2);
      my $line_center = $sl + ($head_vals{lines}/2);
      $samp_center = $samp_center * 3;
      $line_center = $line_center * 3;
      $ss = $samp_center - ($head_vals{samples}/2);
      $sl = $line_center - ($head_vals{lines}/2);
      $es = $ss + $head_vals{samples} - 1;
      $el = $sl + $head_vals{lines}   - 1;

      $cmd = "add from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$the_dark\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  to=$outfile" .
	     "  mul2=-1.0" .
	     "  option=c2c";
      print ("$cmd\n");
      system($cmd);
    } else {
      print ("ERROR: unknown sampling_mode, vis dark not done\n");
      $not_done = 1;
    }
  } elsif ($cube_mode eq "POINT") {
    if ($vissm eq "NORMAL" or $vissm eq "UNK") {
      $the_dark = "tmp_vis_dark.cub";
      $cmd = "cubespec from=$the_dark" . 
	     "  sfrom='\"$ss:$sl:$bands\"'" .
	     "  tblto=$temptbl";
      print ("$cmd\n");
      system($cmd);
	  
      $cmd = "addspec from=$file" .
	     "  sfrom='\"::\"'" .
	     "  to=$outfile" .
	     "  otype=3" .
	     "  mul=-1.0" .
	     "  orange='\"-10000.0,1000000.0\"'" .
	     "  tblfrom=$temptbl";
      print ("$cmd\n");
      system($cmd);
      system("rm $temptbl2");
    } elsif ($vissm eq "HIGH" || $vissm eq "HI-RES") {
      $the_dark = "tmp_vis_dark.cub";

      my $samp_center = $ss + ($head_vals{samples}/2);
      my $line_center = $sl + ($head_vals{lines}/2);
      $samp_center = $samp_center * 3;
      $line_center = $line_center * 3;
      $ss = $samp_center - ($head_vals{samples}/2);
      $sl = $line_center - ($head_vals{lines}/2);

      $the_dark = "tmp_vis_dark.cub";
      $cmd = "cubespec from=$the_dark" . 
	     "  sfrom='\"$ss:$sl:$bands\"'" .
	     "  tblto=$temptbl";
      print ("$cmd\n");
      system($cmd);
	  
      $cmd = "addspec from=$file" .
	     "  sfrom='\"::\"'" .
	     "  to=$outfile" .
	     "  otype=3" .
	     "  mul=-1.0" .
	     "  orange='\"-10000.0,1000000.0\"'" .
	     "  tblfrom=$temptbl";
      print ("$cmd\n");
      system($cmd);
      system("rm $temptbl2");
    } else {
      print ("ERROR: unknown sampling_mode, vis dark not done\n");
      $not_done = 1;
    }
  }

  if ($not_done eq 0) {
    $cmd = "labels from=$outfile" .
	   "  list=NONE" .
	   "  dest=FILE" .
	   "  listfile=listfile" .
	   "  grpnam=CALIBRATION" .
	   "  keyword=VIS_BGND" .
	   "  keyvalue=YES" .
	   "  keytype=STRING";
    print ("$cmd\n");
    system($cmd);
    system("rm listfile.lis");
  }

  system("rm isis_param.out");

}
