#!/usr/bin/perl
# 
# Compute the expected photon noise for a given
#    instrumental and observational setup.
#
# Refs: see my CCD signal-to-noise calculator, I guess.
#
# MWR 3/19/2015

use POSIX;

$debug = 1;

$mypi = 3.141592654;

# number of photons per sec per sq. cm. for a star of mag=0
#    (this is roughly correct for V or R)
$mag0 = 1.0E6;

# telescope diameter (cm)
$tel_diam = 25.0;
# extinction coefficient  (mag/airmass) 
#    (this is roughly correct for V or R)
$air_k = 0.15;
# airmass = sec z (airmasses)
$airmass = 1.3;
# exposure time (s)
$exptime = 10.0;


$min_exptime = 1.0;
$max_exptime = 1000.0;
$mul_exptime = sqrt(10.0);

$min_mag = 5.0;
$max_mag = 15.0;
$d_mag = 1.0;

for ($mag = $min_mag; $mag <= $max_mag; $mag += $d_mag) {

    for ($exptime = $min_exptime; $exptime <= $max_exptime; $exptime *= $mul_exptime) {

      $star_mag = $mag;

      $area = $mypi*($tel_diam/2.0)*($tel_diam/2.0);
      $nphoton = $area*$exptime*$mag0;
      $ext_mag = $air_k*$airmass;
      $effective_mag = $star_mag + $ext_mag;
      $nphoton *= pow(10.0, (-0.4*$effective_mag));

      # RMS in fractional terms
      $rms = 1.0 / sqrt($nphoton);

      # compute RMS in magnitude units, rather than fractional units
      $rms_mag = 2.5*log10(1.0 + $rms);
      printf " mag %6.3f  D %5.0f  exp %6.1f  rms %10.6f  rms_mag %10.6f \n", 
               $star_mag, $tel_diam, $exptime, $rms, $rms_mag;



    }
    printf "\n\n";

}



exit 0;



