#!/usr/bin/perl
#
# Run through all "8B" filters, making a number of shifted copies
#   of each one.  We shift by offsets of 
#
#            +/-  4, 8, 12, 16, 20, 50, 100, 200  Angstroms
#
# Place the shifted versions of the filter files into the subdir "shifted_8b",
#   with names like
#
#            shift_8b1_p04.filter            filter 1, shifted +4 Angstroms
#            shift_8b2_m08.filter            filter 2, shifted -8 Angstroms
#            shift_8b4_m16.filter            filter 4, shifted -16 Angstroms
#
# MWR 3/14/2008

$debug = 1;

@filter_list = ("2", "6", "8");
@shift_list = (4, 8, 12, 16, 20, 50, 100, 200, 300);

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

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

    $filter_file = sprintf "../b8_filters/b8_%s.filter", $filter;
    if ($debug > 0) {
      printf "next is filter_file %s\n", $filter_file;
    }

    # positive shift
    $output_file = sprintf "shifted_8b/shift_8b%s_p%02d.dat", $filter, $shift;

    open(OUTPUT_FILE, ">$output_file") || die("can't open file $output_file");
    open(INPUT_FILE, "$filter_file") || die("can't open file $filter_file");
    while (<INPUT_FILE>) {
      $line = " " . $_;
      @words = split(/\s+/, $line);
      $lambda = $words[1];
      $trans = $words[2];
      $lambda += $shift;
      printf OUTPUT_FILE "%8.3f  %8.4f \n", $lambda, $trans;
    }
    close(INPUT_FILE);
    close(OUTPUT_FILE);

    # negative shift
    $output_file = sprintf "shifted_8b/shift_8b%s_m%02d.dat", $filter, $shift;

    open(OUTPUT_FILE, ">$output_file") || die("can't open file $output_file");
    open(INPUT_FILE, "$filter_file") || die("can't open file $filter_file");
    while (<INPUT_FILE>) {
      $line = " " . $_;
      @words = split(/\s+/, $line);
      $lambda = $words[1];
      $trans = $words[2];
      $lambda -= $shift;
      printf OUTPUT_FILE "%8.3f  %8.4f \n", $lambda, $trans;
    }
    close(INPUT_FILE);
    close(OUTPUT_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);
}


