#!/usr/bin/perl
#
# Compare the instrumental magnitudes of stars in a fiducial filter
#   to their magnitudes in shifted filters.
#
#   Place the DIFFERENCES in magnitude into files with lines line
#
#              offset_in_Angstroms       diff_in_mag
#
#
# MWR 3/14/2008
#
# Modified to work on the "8B" set of filters.
#    Output goes 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;
  }

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

    $num_shift = 0;

    # this is the unshifted magnitude
    $unshift_file = sprintf "./conv_%s_b8_%s.dat", $star, $filter;
    $unshift_mag = get_synth_mag($unshift_file);
    $mag_array[$num_shift] = $unshift_mag;
    $shift_array[$num_shift] = 0.0;
    $num_shift++; 


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

      # first, do positive shift
      $output_file = sprintf "shifted_8b/conv_%s_b8_%s_p%02d.dat", $star, $filter, $shift;
      if ($debug > 0) {
        printf "next is output_file %s\n", $output_file;
      }
      $shift_mag = get_synth_mag($output_file);
      $mag_array[$num_shift] = $shift_mag;
      $shift_array[$num_shift] = 0.0 + $shift;
      $num_shift++; 

      # next, do negative shift
      $output_file = sprintf "shifted_8b/conv_%s_b8_%s_m%02d.dat", $star, $filter, $shift;
      if ($debug > 0) {
        printf "next is output_file %s\n", $output_file;
      }
      $shift_mag = get_synth_mag($output_file);
      $mag_array[$num_shift] = $shift_mag;
      $shift_array[$num_shift] = 0.0 - $shift;
      $num_shift++; 

      if ($debug > 0) {
        for ($i = 0; $i < $num_shift; $i++) {
          printf " %8.4f  %8.4f \n", $shift_array[$i], $mag_array[$i];
        }
      }



    }

    # now we normalize the shifts by subtracting the magnitude of the
    #   unshifted star
    for ($i = 0; $i < $num_shift; $i++) {
      $mag_array[$i] -= $unshift_mag;
      if ($debug > 0) {
        printf " norm  %8.4f  %8.4f \n", $shift_array[$i], $mag_array[$i];
      }
    }

    # and we finally write the results to an output file, with a name
    #   of the form
    # 
    #          result_a0v_b8_1.dat
    #
    $result_file = sprintf "result_%s_b8_%s.dat", $star, $filter;
    if ($debug > 0) {
      printf " writing results to file %s \n", $result_file;
    }
    open(RESULT_FILE, ">$result_file") || die("can't open file $result_file");
    for ($i = 0; $i < $num_shift; $i++) {
      printf RESULT_FILE " %8.4f  %8.4f \n", $shift_array[$i], $mag_array[$i];
    }
    close(RESULT_FILE);
    


  }


}


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



##############################################################################
# PROCEDURE: get_synth_mag
#
# DESCRIPTION: Given a file name, read the single line from it, which 
#              should look like this:
#
#                ergs   8.62317e+02  photons 2.25354e+14   'mag'   -10.8822 
#
#              Pick out the final value, the synthetic mag, and return it.
#
# RETURNS:
#              a magnitudee
#
#
sub get_synth_mag {

  my($filename, $mag);
  my($line, @words, $num);
  my($i);

  $filename = $_[0];

  open(FILENAME, "$filename") || die("get_synth_mag: can't open file $filename");
  $line = <FILENAME>;
  @words = split(/\s+/, $line);
  $num = $#words;
  if ($#words != 6) {
     printf " error: file %s has %d words, not 6 \n", $filename, $num;
     die("ending now");
  }
  
  if ($debug > 0) {
    for ($i = 0; $i < $num; $i++) {
      printf "  get_synth_mag:  word %4d is ..%s.. \n", $i, $words[$i];
    }
  }

  $mag = $words[6];
  if ($debug > 0) {
    printf " get_synth_mag: going to return mag %8.4f \n", $mag;
  }


  return($mag);
}



