  
package cal_pipe2;

require Exporter;

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

use strict;

sub solar_remove {

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

  my $kilometers_per_AU = 149597870.691;
  my $sat_dist_AU = 9.033;
  my $jup_dist_AU = 5.2;
  my $moon_dist_AU = 1.0;
  my $venus_dist_AU = 0.72;

  # Determine the distance from the sun at the time this cube was taken.
  my $sun_dist = $sat_dist_AU;
  
  my $target = $head_vals{target};
  if ($target eq "GANYMEDE" or
      $target eq "EUROPA" or
      $target eq "IO" or
      $target eq "CALLISTO" or
      $target eq "HIMALIA" or
      $target eq "JUPITER") {
    my $sun_dist = $jup_dist_AU;
  }
  if ($target eq "MOON") {
    my $sun_dist = $moon_dist_AU;
  }
  if ($target eq "VENUS") {
    my $sun_dist = $venus_dist_AU;
  }

  my $ir_state = $head_vals{ir_power_state} eq "ON";
  my $vis_state = $head_vals{vis_power_state} eq "ON";
  my $cube_mode = $head_vals{instrument_mode_id};
  my $temptbl = "temp_table";
  my $temptbl2 = "temp_table.tbl";

  my ($bands, $this_function, $cmd, $solspec);

  $bands = "1-352";

  $solspec  = "$ENV{caldir}/calib/solar.qub";
  if ($head_vals{ssf} eq "ON") {
    $solspec  = "$ENV{caldir}/calib/solar_ss.qub";
  }

  # Divide the solar_spec cube by the square of this distance.
  my $sun_dist_squared = $sun_dist * $sun_dist;
  if ($times_pi eq "yes") {
    # Stick in the factor of PI here because it is a handy place to do it.
    $sun_dist_squared = $sun_dist_squared * 4 * atan2(1.0,1.0);
  }
  $cmd = "fx from=($solspec)" .
	 " sfrom='(\"::\")'" .
	 " function='(\"F1/$sun_dist_squared\")'" .
	 " to=tempsolar.cub otype=3";
  system($cmd);

  # Divide data cube by solar spectrum cube.

  if ($cube_mode eq "IMAGE" or $cube_mode eq "CAL_SOLAR" or $cube_mode eq "LINE") {
    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;

    $cmd = "ratio from=$file" .
	 "  sfrom='\"::\"'" .
	 "  from2=tempsolar.cub" .
	 "  sfrom2='\"$ss-$es:$sl-$el:$bands\"'" .
	 "  to=$outfile" .
	 "  divband=--" .
	 "  otype=3";
    system($cmd);

    $cmd = "labels from=$outfile" .
	   "  list=NONE" .
	   "  dest=FILE" .
	   "  listfile=listfile" .
	   "  grpnam=CALIBRATION" .
	   "  keyword=RM_SOLAR" .
	   "  keyvalue=solar.qub" .
	   "  keytype=STRING";
    system($cmd);
    system("rm listfile.lis");
  } elsif ($cube_mode eq "POINT") {
    $cmd = "cubespec from=tempsolar.cub" . 
           "  sfrom='\"1:1:$bands\"'" .
           "  tblto=$temptbl";
    print ("$cmd\n");
    system($cmd);
        
    # 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 = "ratiocs from=temp.cub" .
           "  sfrom='\"::\"'" .
           "  to=$outfile" .
           "  otype=3" .
           "  orange='\"-10000.0,100000000.0\"'" .
           "  tblfrom=$temptbl";
    print ("$cmd\n");
    system($cmd);
    system("rm $temptbl2");

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

    $cmd = "labels from=$outfile" .
	   "  list=NONE" .
	   "  dest=FILE" .
	   "  listfile=listfile" .
	   "  grpnam=CALIBRATION" .
	   "  keyword=RM_SOLAR" .
	   "  keyvalue=solar_ss.qub" .
	   "  keytype=STRING";
    system($cmd);
    system("rm listfile.lis");

  }

  system("rm isis_param.out");
  system("rm tempsolar.cub");
}
