#!/usr/bin/perl
#
# Run "flam_mag" over several stars and several SHIFTED filters
#   in order to determine synthetic magnitudes.
#   Place the results into the "shifted" subdir.
#
# MWR 3/14/2008
#
# Modified to work on the "8B" set of filters.
#   Place results into the "shifted_8b" subdir.
#
# MWR 3/17/2008

$debug = 1;

@filter_list = ("2", "6", "8");
@star_list = ("o5v", "b0v", "a0v", "f0v", "a0v", "g0v", "k0v", "m0v", "m5v");
@shift_list = (4, 8, 12, 16, 20, 50, 100, 200, 300);


foreach $star (@star_list) {
  if ($debug > 0) {
    printf "next is star %s\n", $star;
  }
  $star_file = sprintf "uk%s_dat", $star;
  if ($debug > 0) {
    printf "next is star_file %s\n", $star_file;
  }

  foreach $filter (@filter_list) {
    if ($debug > 0) {
      printf "next is filter %s\n", $filter;
    }

    foreach $shift (@shift_list) {
      if ($debug > 0) {
        printf "next is shift %4.1f\n", $shift;
      }

      # first, do positive shift
      $filter_file = sprintf "shifted_8b/shift_8b%s_p%02d.dat", $filter, $shift;
      $output_file = sprintf "shifted_8b/conv_%s_b8_%s_p%02d.dat", $star, $filter, $shift;

      if ($debug > 0) {
        printf "next is filter_file %s\n", $filter_file;
        printf "next is output_file %s\n", $output_file;
      }

      $cmd = sprintf "./flam_mag $star_file $filter_file 0 0 strict ";
      $cmd .= sprintf " > $output_file ";
      exec_cmd($cmd);
if ($debug > 1) {
 printf "here comes results from file $output_file \n";
 $ret = `cat $output_file`;
 printf "%s", $ret;
}


      # now, do negative shift
      $filter_file = sprintf "shifted_8b/shift_8b%s_m%02d.dat", $filter, $shift;
      $output_file = sprintf "shifted_8b/conv_%s_b8_%s_m%02d.dat", $star, $filter, $shift;

      if ($debug > 0) {
        printf "next is filter_file %s\n", $filter_file;
        printf "next is output_file %s\n", $output_file;
      }

      $cmd = sprintf "./flam_mag $star_file $filter_file 0 0 strict ";
      $cmd .= sprintf " > $output_file ";
      exec_cmd($cmd);


    }

  }


}


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


