#!/usr/bin/perl
#
# Compute simple numbers pertaining to signal and noise
#   for an exposure of a star as a function of exposure time.
#
# MWR 4/11/2006


$debug = 1;


# how long is the exposure (seconds)?
$exptime = 100.0;
# compute in steps of how much (seconds)?
$dt = 1.0;

# how bright is star (electrons per second)?
$star_rate = 10.0;

# at what rate do thermal (or sky) electrons appear (electrons per second)?
$dark_rate = 50.0;

# what is the readout noise (electrons)?
#   we must square this to get variance due to readout, of course
$readout_noise = 20.0;


$num_star_total = 0;
$num_dark_total = 0;

for ($t = 0.0; $t < $exptime; $t += $dt) {

  # calculate number of electrons from star 
  #    collected in this block of time 
  $num_star = $star_rate * $dt;
  $num_star_total += $num_star;
  if ($debug > 0) {
    printf " t %6.2f   star %7.2e %7.2e  ", 
               $t, $num_star, $num_star_total;

  }

  # compute the noise from the star, and the signal-to-noise ratio
  #   due to electrons from the star
  $noise_star = sqrt($num_star_total);
  $sn_star = ($num_star_total)/($noise_star);
  if ($debug > 0) {
    printf " starnoise %7.2e %7.2e  ", 
               $noise_star, $sn_star;
  }

  # calculate number of electrons due to thermal noise
  #    collected in this block of time  and total
  $num_dark = $dark_rate * $dt;
  $num_dark_total += $num_dark;
  if ($debug > 0) {
    printf " dark %7.2e %7.2e %7.2e ", 
                $num_dark, $num_dark_total, sqrt($num_dark_total);

  }

  # readout noise -- which does not change with time
  $noise_readout = $readout_noise;
  if ($debug > 0) {
    printf " readout %7.2e ", 
                $noise_readout;
  }


  # compute total noise from all sources,
  #   plus signal-to-noise ratio
  #   due to all sources
  $noise_tot = sqrt($num_star_total + $num_dark_total + 
                       $noise_readout*$noise_readout);
  $sn_tot = ($num_star_total)/($noise_tot);
  if ($debug > 0) {
    printf " totnoise %7.2e %7.2e  ", 
               $noise_tot, $sn_tot;
  }





  printf "\n";
}



exit 0;
