##PlotMag_Vs_ExpTime.py
##
##PURPOSE: 
##  To plot the limiting magnitude as a function of exposure time 
##
##INPUTS: 
##
##KEYWORDS:
##
##CALLING SEQUENCE:
##
##EXAMPLES:
##________
##
##////////////////////////////////////////////////////////////////////////
##SETUP
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
execfile('/afs/slac/u/ki/lances/python/ThesisPlotSettings.py')
PlotDir = '/nfs/slac/g/ki/ki03/lances/KPNO/Latex/Thesis/GuideMode/Figures/'
from HxRG_Class import *
from ReturnCentroid import *
from ReturnRadialAvg import *
import TableIO
import csv
import pdb

#Numbers for calculation
B   = 1.1 		#e-/s from background
D   = 0.1 		#e-/s from dark current
M_o = 0			#Zero point magnitude		
I_o = 8.e4		#e-/s from source
A   = (210./2)**2*pi	#Aperture of telescope in cm^2
R_n = 17./100           #e-/number of reads
r_c = 7.2		#envelope radius of star
s_n = 3                 #signal to noise ratio
Q   = .8		#Quantum Efficiency
T   = .6		#Transmission of atmosphere and telescope

Q   = .7
T   = .9

##VERY NEAT CURVE
#t_exp = s_n**2*(I_o*A*10**(-Mags/2.5)+B+D)/(I_o*A**10**(-2*Mags/2.5))

#Try to generate magnitudes from 0-25
Mags   = frange(250)
Mags   = Mags/10
t_exp1 = s_n**2*(I_o*A*T*Q*10**(-Mags/2.5)+(B+D)*(pi*r_c**2))/((I_o*A*Q*T)**2*10**((-2*Mags)/2.5))

B    = 2.0 
r_c  = 17
t_exp2 = s_n**2*(I_o*A*T*Q*10**(-Mags/2.5)+(B+D)*(pi*r_c**2))/((I_o*A*Q*T)**2*10**((-2*Mags)/2.5))

#Fit a line to the points in the proper range
Fig=mplot.figure(0)
mplot.clf()
Fig.add_axes([0.12,0.12,0.85,0.85])
mplot.plot(Mags, t_exp1, 'r', label='Guiding', lw=1.5)
mplot.plot(Mags, t_exp2, 'b', label='No Guiding', lw=1.5)
mplot.xlabel(r'\textbf{Magnitude}', fontsize=12.5)
mplot.ylabel(r'\textbf{Exposure Time (s)}', fontsize=12.5)
mplot.legend(loc=2)
#Try to plot the star data points
mplot.plot([21.5], [1770], 'bo')
mplot.plot([23.1], [2597], 'ro')
xlim(15,24)
ylim(0,3600)

SaveImg=0
if SaveImg == 1:
  mplot.savefig(PlotDir+'LimitingMagnitudeVsTime.eps')
  mplot.savefig(PlotDir+'LimitingMagnitudeVsTime.png')

