##PURPOSE:
## To plot the linearity of H2RG_001 with and without reference pixel corrections.
##
from Return_Linearity import *
from Return_Photon_Transfer import *
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/ki04/lances/LSST/KPNO/Latex/Thesis/Linearity/Figures/'

FitsFileName1 = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/2007Dec15/Flat/Flat_Y_Filter_6.fits' 
FitsFileName2 = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/2007Dec15/Flat/Flat_Y_Filter_7.fits' 
'''
FitsFileName1 = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/2007Dec15/Flat/Flat_G_Filter_6.fits'
FitsFileName2 = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/2007Dec15/Flat/Flat_G_Filter_7.fits'
'''

'''
FitsFileName1 = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/2007Dec15/Flat/Flat_I_Filter_6.fits'
FitsFileName2 = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/2007Dec15/Flat/Flat_I_Filter_7.fits'
'''

YStart    = 1000
YStop     = 1400
XStart    = 600
XStop     = 1000
TvFlag    = 0 
Mode      = 0 
UseRef    = 1 
CFac      = 1.35
CGain     = 5.86
CFac      = 0.35
ReadNoise = 5.0

MeanADUArr1, MeanADUArr2, SigADUArr1, SigADUArr2, SigADUArrD, SNADUArr, FPADUArr, SNRNADUArr = \
Return_Photon_Transfer(FitsFileName1, FitsFileName2, \
  XStart = XStart, XStop = XStop, YStart=YStart, YStop=YStop, UseRef=UseRef, CFac=CFac, ReadNoise=ReadNoise)

mplot.figure(0)
mplot.clf()
mplot.plot(MeanADUArr1,  SigADUArr1**2, 'b')
mplot.plot(MeanADUArr1,  SigADUArr1**2, 'bo')
mplot.plot(MeanADUArr2,  SigADUArr2**2, 'r')
mplot.plot(MeanADUArr2,  SigADUArr2**2, 'ro')

mplot.figure(1)
mplot.clf()
mplot.plot(MeanADUArr1,  SigADUArrD**2, 'b')
mplot.plot(MeanADUArr1,  SigADUArrD**2, 'bo')

m, b, E, CosE = Linefit(MeanADUArr1, SigADUArrD**2, RejectCos=0)
mplot.plot(MeanADUArr1, m*MeanADUArr1+b, 'r--')

Fig=mplot.figure(2)
mplot.clf()
Ax1=Fig.add_axes([0.10,0.14,0.88,0.82])
mplot.loglog(MeanADUArr1,  SNADUArr, 'b', linewidth=2) 
mplot.loglog(MeanADUArr1,  SNADUArr, 'bo', label=r'\textbf{Shot Noise}', \
  markersize=6, markeredgewidth=1)
mplot.loglog(MeanADUArr1,  FPADUArr, 'r', linewidth=2)
mplot.loglog(MeanADUArr1,  FPADUArr, 'ro', label=r'\textbf{Fixed Pattern Noise}', \
  markersize=6, markeredgewidth=1)
mplot.loglog(MeanADUArr1,  SigADUArr1, 'g', linewidth=2)
mplot.loglog(MeanADUArr1,  SigADUArr1, 'go', label=r'\textbf{Total Noise}', \
  markersize=6, markeredgewidth=1)
xvals = arange(100000)+1
mplot.loglog(xvals, .0095*xvals,'k--', label=r'\textbf{Slope =\ 1}', linewidth=3)
mplot.loglog(xvals, (.26*xvals**.5), 'k-.', label=r'\textbf{Slope = 1/2}', linewidth=3)
ylim(4, 300)
xlim(500, 2.5e4)
xlabel(r'\textbf{Signal (ADU)}', fontsize=14)
ylabel(r'\textbf{RMS Noise (ADU)}', fontsize=14)
mplot.grid(True, which='minor')

#LEGEND
l=mplot.legend(loc=2, numpoints=1)
t=l.get_texts()
t[0].set_fontsize(12)
draw()

#ADJUST FONT SIZE FOR TICK LABELS
xt1 = Ax1.get_xticklabels()
for i in xt1:
  i.set_fontsize(14)
yt1 = Ax1.get_yticklabels()
for i in yt1:
  i.set_fontsize(14)
draw()

mplot.figtext(.585,.280,(r'\textbf{Region: [' +
                        str(int(XStart)) + ':' + str(int(XStop)) + ',' +
                        str(int(YStart)) + ':' + str(int(YStop)) + ']}'),size=13, \
              backgroundcolor='w')
mplot.figtext(.585,.230,r'\textbf{Detector   : H2RG-32-147}' , color='k', size=13, \
              backgroundcolor='w')
mplot.figtext(.585,.180,r'\textbf{Electronics: LEACH}'       , color='k', size=13,  \
              backgroundcolor='w')

mplot.savefig(PlotDir+'LEACH_FlatY_2007Dec15_PhotonTransferCurve.png')
mplot.savefig(PlotDir+'LEACH_FlatY_2007Dec15_PhotonTransferCurve.eps')
