#!/usr/bin/perl
# 
# Compute the expected scintillation noise for a given 
#    instrumental and observational setup.
#
# Refs: see 
#    Dravins et al., PASP 110, 610 (1998)
#    http://www.journals.uchicago.edu/PASP/journal/issues/v110n747/980019/980019.html
#
#
# 
#                     -0.67           1.75                       -0.5
#  sig  =  0.09  * (D)      *  (sec Z)      *  exp(-h/h0)  * (2T)
#
#in which
#
#    sig is the root-mean-square deviation from the mean intensity, in fractional units; in other words, if the RMS is half the signal, sig = 0.5.
#    D is the diameter of the telescope aperture in centimeters; in this case, 50 mm = 5 cm
#    sec(Z) is the airmass; in this case, the altitude was 27 degrees, so the angle Z = 90 - alt = 63 degrees, so the airmass sec(Z) was about 2.20
#    h is the altitude of the site, and h0 is the scale height of the atmosphere (about 8000 m); in this case, h = 610 m
#    T is the exposure time, in seconds; in this case, since two fields of 1/60 second were combined onto each frame of the movie file, I'll say that T = 1/30 sec = 0.033 sec 
##
#
# MWR 3/18/2015

use POSIX;

$debug = 1;

# scale height of atmosphere (m)
$h0 = 8000.0;

# telescope diameter (cm)
$tel_diam = 25.0;
# airmass = sec z (airmasses)
$airmass = 1.3;
#$airmass = 1.0;
# altitude of site (m)
$altitude = 168.0;
#$altitude = 0.0;
# exposure time (s)
$exptime = 10.0;


$min_exptime = 1.0;
$max_exptime = 100.0;
$d_exptime = 1.0;

$min_diam = 10.0;
$max_diam = 100.0;
$d_diam = 5.0;

for ($tel_diam = $min_diam; $tel_diam <= $max_diam; $tel_diam += $d_diam) {

    for ($exptime = $min_exptime; $exptime <= $max_exptime; $exptime += $d_exptime) {


      $rms = 0.09 * pow($tel_diam, -0.67) * pow($airmass, 1.75)
                  * exp(-($altitude/$h0)) * pow((2.0*$exptime), -0.5);

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



    }
    printf "\n\n";

}



exit 0;



