#!/usr/bin/perl
#
# Compute fluxes, number of photons per sec, and mag
#    for a star and a set of bandpasses
#
# MWR 6/12/2016

$debug = 0;

$flam_exe = "~/z1/snap/zero_point/progs/flam_mag";
$filter_exe = "~/z1/snap/grism_study/progs/filter";

@passbands = ("bess-u.pass", "bess-b.pass", "bess-v.pass", "bess-r.pass",
              "bess-i.pass", "2mass_j_angstrom.pass", 
              "2mass_h_angstrom.pass", "2mass_k_angstrom.pass");

@infiles = ("spec_a0v_scaled.dat", "spec_g5v_scaled.dat");



foreach $infile (@infiles) {

  $outfile = $infile;
  $outfile =~ s/dat/photons/;
  open(OUTFILE, ">$outfile") || die("can't open file $outfile");


  foreach $passband (@passbands) {

    $cmd = "$filter_exe $passband";
    $ret = exec_cmd($cmd);
    @words = split(/\s+/, $ret);
    $effec_lambda = $words[3];

    $cmd = "$flam_exe $infile $passband 0 0 ";
    $ret = exec_cmd($cmd);
    @words = split(/\s+/, $ret);
    $ergs = $words[2];
    $photons = $words[4];
    $mag = $words[6];

    printf OUTFILE " %-20s  %12.3f  %6.3e \n", $infile, $effec_lambda, $photons;

  }
 
  close(OUTFILE);

}


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);
}


