
package cal_pipe2;

require Exporter;

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

#use strict;

sub flat_ir2 {

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

  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_flat, $cmd, $bands);

  # Divide this cube by the appropriate piece of the appropriate flat.
  # Pick a flat, divide. In cases of POINT mode or OCCULTATION mode, use
  # the appropriate single pixel from the flat.
  my $irsm = $head_vals{ir_sampling_mode};
  my $cube_mode = $head_vals{instrument_mode_id};

  my $temptbl = "temp_table";
  my $temptbl2 = "temp_table.tbl";

  $bands = "1-352";

  # Set up the IR flat fields, special set for spectrally summed cubes.
  my $norm_flat = "$ENV{caldir}/cal_files/ir_flat_3_2009.cub";
  my $high_flat = "$ENV{caldir}/cal_files/ir_highflat_3_2009.cub";
  my $nyqu_flat = "$ENV{caldir}/cal_files/ir_nyquistflat_3_2009.cub";

  if ($cmds{ir_flat_field} eq "oldflat") {
    $norm_flat = "$ENV{caldir}/cal_files/ir_flat_352.cub";
    $high_flat = "$ENV{caldir}/cal_files/ir_highflat_352.cub";
    $nyqu_flat = "$ENV{caldir}/cal_files/ir_nyquistflat_352.cub";
  }

  if ($head_vals{ssf} eq "ON") {
    $norm_flat = "$ENV{caldir}/cal_files/ir_ss_flat_3_2009.cub";
    $high_flat = "$ENV{caldir}/cal_files/ir_ss_highflat_3_2009.cub";
    $nyqu_flat = "$ENV{caldir}/cal_files/ir_ss_nyquistflat_3_2009.cub";
    if ($cmds{ir_flat_field} eq "oldflat") {
      $norm_flat = "$ENV{caldir}/cal_files/ir_ss_flat_352.cub";
      $high_flat = "$ENV{caldir}/cal_files/ir_ss_highflat_352.cub";
      $nyqu_flat = "$ENV{caldir}/cal_files/ir_ss_nyquistflat_352.cub";
    }
  }

  print "Norm_Flat = $norm_flat\n";

  if ($cube_mode eq "IMAGE" or $cube_mode eq "CAL_SOLAR" or $cube_mode eq "LINE") {
    if ($irsm eq "NORMAL" || $irsm eq "UNK") {
      $the_flat = $norm_flat;
      $cmd = "ratio from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$norm_flat\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  to=$outfile" .
	     "  divband=--";
      print ("$cmd\n");
      system($cmd);
    } elsif ($irsm eq "HIGH" || $irsm eq "HI-RES") {
      $ss = (2*($head_vals{start_sample} - 1)) + ($head_vals{samples}/2);
      $sl = $head_vals{start_line};
      $es = $ss + $head_vals{samples} - 1;
      $el = $sl + $head_vals{lines}   - 1;
      $the_flat = $high_flat;
      $cmd = "ratio from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$high_flat\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  to=$outfile" .
	     "  divband=--";
      print ("$cmd\n");
      system($cmd);
    } elsif ($irsm eq "UNDER" || $irsm eq "NYQUIST") {
      $ss = (2*($head_vals{start_sample} - 1)) + ($head_vals{samples}/2);
      $sl = $head_vals{start_line};
      $es = $ss + $head_vals{samples} - 1;
      $el = $sl + $head_vals{lines}   - 1;
      $the_flat = $nyqu_flat;
      $cmd = "ratio from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$nyqu_flat\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  otype=3" .
	     "  to=$outfile" .
	     "  divband=--";
      print ("$cmd\n");
      system($cmd);
    } else {
      print ("ERROR: unknown sampling_mode, ir flat not done\n");
      $the_flat = "NOT_DONE";
    }
  } elsif ($cube_mode eq "OCCULTATION") {
    $ss = (2*($head_vals{start_sample} - 1));
    $sl = $head_vals{start_line};
    $es = $ss + $head_vals{samples} - 1;
    $el = $sl + $head_vals{lines}   - 1;
    $the_flat = $high_flat;

    # Paste an extra line on the bottom of the cube so we can use ratiocs
    # without getting the multiplier spectrum stuck in the cube.
    $cmd = "cubestack from1=$file" .
       "  sfrom1='\"::\"'" .
       "  from2=$file" .
       "  sfrom2='\":1:\"'" .
       "  to=$outfile" .
       "  otype=3" .
       "  orange=(0.0,0.0)" .
       "  HOPT=1";
    print ("$cmd\n");
    system($cmd);
    system("mv $outfile temp.cub");

    $cmd = "cubespec from=$high_flat" .
	   "  sfrom='\"$ss:$sl:$bands\"'" .
	   "  tblto=$temptbl";
    print ("$cmd\n");
    system($cmd);

    $cmd = "ratiocs from=temp.cub" .
	   "  sfrom='\"::\"'" .
	   "  to=$outfile" .
	   "  otype=3" .
	   "  orange='\"-10000.0,1000000.0\"'" .
	   "  tblfrom=$temptbl";
    print ("$cmd\n");
    system($cmd);
    system("rm $temptbl2");
    system("rm temp.cub");

    # Trim off that extra line we added earlier.
    system("mv $outfile temp.cub");
    my $lns = $head_vals{'lines'};
    $cmd = "transpose from=temp.cub sfrom='(\":1-$lns:\")' to=$outfile " .
           "order=BSQ\n";
    print("$cmd");
    system($cmd);
    system("rm temp.cub");
  } elsif ($cube_mode eq "POINT") {
    if ($irsm eq "NORMAL" || $irsm eq "UNK") {
      $the_flat = $norm_flat;

      # Paste an extra line on the bottom of the cube so we can use ratiocs
      # without getting the multiplier spectrum stuck in the cube.
      $cmd = "cubestack from1=$file" .
         "  sfrom1='\"::\"'" .
         "  from2=$file" .
         "  sfrom2='\":1:\"'" .
         "  to=$outfile" .
         "  otype=3" .
         "  orange=(0.0,0.0)" .
         "  HOPT=1";
      print ("$cmd\n");
      system($cmd);
      system("mv $outfile temp.cub");

      $cmd = "cubespec from=$norm_flat" .
	     "  sfrom='\"$ss:$sl:$bands\"'" .
	     "  tblto=$temptbl";
      print ("$cmd\n");
      system($cmd);

      $cmd = "ratiocs from=temp.cub" .
	     "  sfrom='\"::\"'" .
	     "  to=$outfile" .
	     "  otype=3" .
	     "  orange='\"-10000.0,1000000.0\"'" .
	     "  tblfrom=$temptbl";
      print ("$cmd\n");
      system($cmd);
      system("rm $temptbl2");
      system("rm temp.cub");

      # Trim off that extra line we added earlier.
      system("mv $outfile temp.cub");
      my $lns = $head_vals{'lines'};
      $cmd = "transpose from=temp.cub sfrom='(\":1-$lns:\")' to=$outfile " .
              "order=BSQ\n";
      print("$cmd");
      system($cmd);
      system("rm temp.cub");
    } elsif ($irsm eq "HIGH" || $irsm eq "HI-RES") {
      $ss = (2*($head_vals{start_sample} - 1));
      $sl = $head_vals{start_line};
      $es = $ss + $head_vals{samples} - 1;
      $el = $sl + $head_vals{lines}   - 1;
      $the_flat = $high_flat;

      # Paste an extra line on the bottom of the cube so we can use ratiocs
      # without getting the multiplier spectrum stuck in the cube.
      $cmd = "cubestack from1=$file" .
         "  sfrom1='\"::\"'" .
         "  from2=$file" .
         "  sfrom2='\":1:\"'" .
         "  to=$outfile" .
         "  otype=3" .
         "  orange=(0.0,0.0)" .
         "  HOPT=1";
      print ("$cmd\n");
      system($cmd);
      system("mv $outfile temp.cub");

      $cmd = "cubespec from=$high_flat" .
	     "  sfrom='\"$ss:$sl:$bands\"'" .
	     "  tblto=$temptbl";
      print ("$cmd\n");
      system($cmd);

      $cmd = "ratiocs from=temp.cub" .
	     "  sfrom='\"::\"'" .
	     "  to=$outfile" .
	     "  otype=3" .
	     "  orange='\"-10000.0,1000000.0\"'" .
	     "  tblfrom=$temptbl";
      print ("$cmd\n");
      system($cmd);
      system("rm $temptbl2");
      system("rm temp.cub");

      # Trim off that extra line we added earlier.
      system("mv $outfile temp.cub");
      my $lns = $head_vals{'lines'};
      $cmd = "transpose from=temp.cub sfrom='(\":1-$lns:\")' to=$outfile " .
              "order=BSQ\n";
      print("$cmd");
      system($cmd);
      system("rm temp.cub");

    } elsif ($irsm eq "UNDER" || $irsm eq "NYQUIST") {

      $ss = (2*($head_vals{start_sample} - 1));
      $sl = $head_vals{start_line};
      $es = $ss + $head_vals{samples} - 1;
      $el = $sl + $head_vals{lines}   - 1;
      $the_flat = $nyqu_flat;

      # Paste an extra line on the bottom of the cube so we can use ratiocs
      # without getting the multiplier spectrum stuck in the cube.
      $cmd = "cubestack from1=$file" .
         "  sfrom1='\"::\"'" .
         "  from2=$file" .
         "  sfrom2='\":1:\"'" .
         "  to=$outfile" .
         "  otype=3" .
         "  orange=(0.0,0.0)" .
         "  HOPT=1";
      print ("$cmd\n");
      system($cmd);
      system("mv $outfile temp.cub");

      $cmd = "cubespec from=$nyqu_flat" .
	     "  sfrom='\"$ss:$sl:$bands\"'" .
	     "  tblto=$temptbl";
      print ("$cmd\n");
      system($cmd);

      $cmd = "ratiocs from=temp.cub" .
	     "  sfrom='\"::\"'" .
	     "  to=$outfile" .
	     "  otype=3" .
	     "  orange='\"-10000.0,1000000.0\"'" .
	     "  tblfrom=$temptbl";
      print ("$cmd\n");
      system($cmd);
      system("rm $temptbl2");
      system("rm temp.cub");

      # Trim off that extra line we added earlier.
      system("mv $outfile temp.cub");
      my $lns = $head_vals{'lines'};
      $cmd = "transpose from=temp.cub sfrom='(\":1-$lns:\")' to=$outfile " .
              "order=BSQ\n";
      print("$cmd");
      system($cmd);
      system("rm temp.cub");

    } else {
      print ("ERROR: unknown sampling_mode, ir flat not done\n");
      $the_flat = "NOT_DONE";
    }
  }

  if ($cmds{ir_flat_field} eq "newflat") {
    $keyval = $the_flat;
    $cmd = "labels from=$outfile" .
	   "  list=NONE" .
           "  dest=FILE" .
           "  listfile=listfile" .
	   "  grpnam=CALIBRATION" .
	   "  keyword=IR_FLAT" .
	   "  keyvalue=$keyval" .
	   "  keytype=STRING";
    print ("$cmd\n");
    system($cmd);
    system("rm listfile.lis");
  } else {
    $keyval = $the_flat . '-29-JUL-2005';
    $cmd = "labels from=$outfile" .
	   "  list=NONE" .
           "  dest=FILE" .
           "  listfile=listfile" .
	   "  grpnam=CALIBRATION" .
	   "  keyword=IR_FLAT" .
	   "  keyvalue=$keyval" .
	   "  keytype=STRING";
    print ("$cmd\n");
    system($cmd);
    system("rm listfile.lis");
  }

  system("rm isis_param.out");

}
