package cal_pipe2;

require Exporter;

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

use strict;

sub flat_vis2 {

 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_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 $vissm = $head_vals{vis_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 VIS flat fields, special set for spectrally summed cubes.
  my $norm_flat = "$ENV{caldir}/calib/vis_flatfield.qub";
  my $high_flat = "$ENV{caldir}/calib/vis_hires_flatfield.qub";
  if ($head_vals{ssf} eq "ON") {
    $norm_flat = "$ENV{caldir}/calib/vis_flatfield_ss.qub";
    $high_flat = "$ENV{caldir}/calib/vis_hires_flatfield_ss.qub";
  }

  if ($cube_mode eq "IMAGE" or $cube_mode eq "CAL_SOLAR" or $cube_mode eq "LINE") {
    if ($vissm eq "NORMAL" || $vissm eq "UNK") {
      $the_flat = $norm_flat;
      $cmd = "ratio from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$the_flat\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  to=$outfile" .
	     "  otype=3" .
	     "  divband=--";
      print ("$cmd\n");
      system($cmd);
    } elsif ($vissm eq "HIGH" || $vissm eq "HI-RES") {
      $the_flat = $high_flat;

      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 = "ratio from=$file" .
	     "  sfrom='\"::\"'" .
	     "  from2='\"$the_flat\"'" .
	     "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	     "  to=$outfile" .
	     "  divband=--";
      print ("$cmd\n");
      system($cmd);
    } else {
      print ("ERROR: unknown sampling_mode, vis flat not done\n");
      $the_flat = "NOT_DONE";
    }
  } elsif ($cube_mode eq "OCCULTATION") {
    $the_flat = $high_flat;

    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;

    # 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 ($vissm eq "NORMAL" || $vissm eq "UNK") {

      # 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");

      $the_flat = $norm_flat;
      $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 ($vissm eq "HIGH" || $vissm eq "HI-RES") {
      $the_flat = $high_flat;

      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;

      # 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");
    } else {
      print ("ERROR: unknown sampling_mode, vis flat not done\n");
      $the_flat = "NOT_DONE";
    }
  }

  $cmd = "labels from=$outfile" .
	 "  list=NONE" .
	 "  dest=FILE" .
         "  listfile=listfile" .
	 "  grpnam=CALIBRATION" .
	 "  keyword=VIS_FLAT" .
	 "  keyvalue=$the_flat" .
	 "  keytype=STRING";
  print ("$cmd\n");
  system($cmd);
  system("rm listfile.lis");

  system("rm isis_param.out");

}
