#!/usr/bin/perl
#
# Quick and dirty numerical integration of EW
#    given some datafile with absorption-line spectrum
#    
# MWR 9/18/2020

$debug = 1;

if (0 == 1) {
  #  for sodium 5889 line
  $start_lambda = 5888.0;
  $end_lambda = 5892.0;
  $continuum_level = 1.00;
}
else {
  #  for sodium 3302 line
  $start_lambda = 3302.22;
  $end_lambda = 3302.52;
  $continuum_level = 1.00;
}

# first, read in all the data
$num = 0;
while (<STDIN>) {
  $line = $_;
  if ($line =~ /^#/) {
    next;
  }
  
  @words = split(/\s+/, " " . $line);
  $lambda_array[$num] = $words[1]*10.0;
  $inten_array[$num] = $words[2];
  $num++;

}
if ($debug > 0) {
  for ($i = 0; $i < $num; $i++) {
     printf " i %6d  lambda %9.3f  inten %7.3f \n", 
           $i, $lambda_array[$i], $inten_array[$i];
  }
}


# now, compute EW
$sum = 0;
for ($i = 0; $i < $num; $i++) {

  if (($lambda_array[$i] < $start_lambda) || ($lambda_array[$i] > $end_lambda)) {
    next;
  }

  if ($i > 0) {
    $width = $lambda_array[$i] - $lambda_array[$i - 1];
  }
  else {
    $width = $lambda_array[$i+1] - $lambda_array[$i];
  }

  $depth = $continuum_level - $inten_array[$i];
  $area = $width*$depth;
  $sum += $area;

  if ($debug > 0) {
    printf "  i %6d  lam %8.3f width %8.3f  depth %8.3f  area %8.3f  sum %8.3f \n",
          $i, $lambda_array[$i], $width, $depth, $area, $sum;
  }

}
printf "Sum is %10.4f \n", $sum;



exit 0;

