#!/usr/bin/perl
#
# Compute some sample K-corrections using a galaxy spectrum
#    and the Bessell V-band.
#
# MWR 5/2/2011

$debug = 0;
$dd = "~/z1/snap/zero_point";

$gal_spec = "./gal_spectrum_ang.txt";
$passband = "./bess_i.pass";
$tempfile = "./calc_kcor.tmp";

# compute the "magnitude" when gal spectrum at redshift 0
$cmd = "$dd/progs/flam_mag ";
$cmd .= " $gal_spec $passband 0 0 strict ";
$ret = exec_cmd($cmd);
@words = split(/\s+/, $ret);
$mag_zero = $words[6];



for ($z = 0.0; $z < 0.20; $z += 0.01) {
  if ($debug > 0) {
    printf " z is %6.2f \n", $z;
  }

  # shift the spectrum by the redshift $z
  $cmd = "$dd/progs/redshift_passband.pl ";
  $cmd .= " $gal_spec $z > $tempfile ";
  $ret = exec_cmd($cmd);

  # run the synthetic photometry program
  $cmd = "$dd/progs/flam_mag ";
  $cmd .= " $tempfile $passband 0 0 ";
  $ret = exec_cmd($cmd);
  @words = split(/\s+/, $ret);
  $mag = $words[6];

  # compute difference delta = mag_at_z - mag_at_rest
  $delta_mag = $mag - $mag_zero;

  # print out the result
  printf " z %6.3f   delta_mag %6.3f \n",
       $z, $delta_mag;

  


  

}





exit 0;



##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


