#!/usr/bin/perl
#
# Given a datafile with two columns,
#
#    wavelength    intensity
#
# we shift all the wavelength values to the values they would have
# if the star were moving at a speed "v" from us, in m/s.  Positive "v"
# means motion away, negative "v" means motion towards.
#
# usage:       perl shift_spec.pl  file  v
#
# MWR 3/21/2011

$debug = 0;

$c = 2.9979e8;

if ($#ARGV != 1) {
  printf STDERR "usage: perl shift_spec.pl datafile velocity \n";
  exit(1);
}
$infile = $ARGV[0];
$vel = $ARGV[1];
if ($debug > 0) {
  printf " infile %s   vel %s \n", $infile, $vel;
}

# we will multiply all wavelengths by this factor
$factor = ( sqrt(1.0 + $vel/$c) / sqrt(1.0 - $vel/$c) );

open(INFILE, "$infile") || die("can't open file $infile");
while (<INFILE>) {
  $line = $_;
  if ($line =~ /^#/) {
    printf "%s", $line;
  }
  if ($line =~ /^$/) {
    printf "%s", $line;
  }
  
  $line = " " . $line;
  @words = split(/\s+/, $line);
  $lambda = $words[1];
  $inten = $words[2];
  if ($debug > 0) {
    printf " lambda %s   inten %s \n", $lambda, $inten;
  }

  $new_lambda = $lambda*$factor;
  
  printf " %8.1f %12.5e \n", $new_lambda, $inten;
}
close(INFILE);




exit 0;
