##PURPOSE:
## To plot 3 linearity curves for H2RG_001 and H2RG-32-147 
## showing the affect of different bias settings.
##
from Return_Linearity import *
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
execfile('/afs/slac/u/ki/lances/python/Thesis_3x1_PlotSettings.py')
PlotDir = '/nfs/slac/g/ki/ki04/lances/LSST/KPNO/Latex/Thesis/Linearity/Figures/'

########################################################################################
##Generate the First Curve FOR H2RG-32-147 WITH LEACH
FitsFileName1  = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/LEACH/Test_Images/H2RG_Test_Ifilter_Increased_Vpullup_0.fits'
SlopeFileName = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec13/Flat/Flat_G_H2RG_SIPIN_15_Reads_Dec13_2007_21_35_50_G_Slope.fits' 
HxRG1 = HxRG_C(FitsFileName=FitsFileName1)
HxRG1.Get_Raw_Header(FitsFileName1)
HxRG1.CGain = 4.00
YStart=1200 
YStop=1600 
XStart=100 
XStop=500
TvFlag=0 
Mode=2 
UseRef=1 
CFac  = 1.10
CGain = 4.00
MedADU_RP_1, DeltasMed_RP_1, DeltasMean_RP_1, DeltasCumMed_RP_1, DeltasCumMean_RP_1 = \
  ReturnLinearity(FitsFileName1, SlopeFileName, YStart=YStart, YStop=YStop, XStart=XStart, \
                  XStop=XStop, Mode=Mode, UseRef=UseRef, CFac=CFac)

UseRef=0
MedADU_NRP_1, DeltasMed_NRP_1, DeltasMean_NRP_1, DeltasCumMed_NRP_1, DeltasCumMean_NRP_1 = \
  ReturnLinearity(FitsFileName1, SlopeFileName, YStart=YStart, YStop=YStop, XStart=XStart, \
                  XStop=XStop, Mode=Mode, UseRef=UseRef, CFac=CFac)

########################################################################################
##Generate the First Curve FOR H2RG-32-147 WITH ASIC
FitsFileName2  = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/07Dec15/Flat/Flat_G_H2RG_SIPIN_30_Reads_Dec15_2007_20_57_03.fits'
HxRG2 = HxRG_C(FitsFileName=FitsFileName2)
HxRG2.Get_Raw_Header(FitsFileName2)
HxRG2.CGain = 5.86
 
UseRef=1
CFac  = 1.45
CGain = 5.86
MedADU_RP_2, DeltasMed_RP_2, DeltasMean_RP_2, DeltasCumMed_RP_2, DeltasCumMean_RP_2 = \
  ReturnLinearity(FitsFileName2, SlopeFileName, YStart=YStart, YStop=YStop, XStart=XStart, \
                  XStop=XStop, Mode=Mode, UseRef=UseRef, CFac=CFac)

UseRef=0
MedADU_NRP_2, DeltasMed_NRP_2, DeltasMean_NRP_2, DeltasCumMed_NRP_2, DeltasCumMean_NRP_2 = \
  ReturnLinearity(FitsFileName2, SlopeFileName, YStart=YStart, YStop=YStop, XStart=XStart, \
                  XStop=XStop, Mode=Mode, UseRef=UseRef, CFac=CFac)

########################################################################################
##Generate the First Curve FOR H2RG-001 WITH ASIC
FitsFileName3  = '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec15/TestDiode/TurnOnDiodeWhileCoolingAt135K_H2RG_SIPIN_10_Reads_Dec15_2008_10_54_34.fits'
HxRG3 = HxRG_C(FitsFileName=FitsFileName3)
HxRG3.Get_Raw_Header(FitsFileName3)
HxRG3.CGain = 1.45

YStart=1690
YStop=1700
XStart=1540
XStop=1600
UseRef=1 
CFac  = 1.45
CGain = 1.45

MedADU_RP_3, DeltasMed_RP_3, DeltasMean_RP_3, DeltasCumMed_RP_3, DeltasCumMean_RP_3 = \
  ReturnLinearity(FitsFileName3, SlopeFileName, YStart=YStart, YStop=YStop, XStart=XStart, \
                  XStop=XStop, Mode=Mode, UseRef=UseRef, CFac=CFac)

UseRef=0
MedADU_NRP_3, DeltasMed_NRP_3, DeltasMean_NRP_3, DeltasCumMed_NRP_3, DeltasCumMean_NRP_3 = \
  ReturnLinearity(FitsFileName3, SlopeFileName, YStart=YStart, YStop=YStop, XStart=XStart, \
                  XStop=XStop, Mode=Mode, UseRef=UseRef, CFac=CFac)
 
 
########################################################################
##MAKE THE PLOT
Fig=mplot.figure(0)
mplot.clf()

#Axis 1-----------------------------------------------------------------
YStart=1200
YStop=1600 
XStart=100 
XStop=500
Ax1=Fig.add_axes([0.100,0.100,0.65,0.290])
mplot.plot(HxRG1.CGain*MedADU_NRP_1[0:-1], DeltasCumMed_NRP_1/(DeltasCumMed_NRP_1[0]), 'r', \
           label=r'\textbf{No Reference Pixels Applied}')
mplot.plot(HxRG1.CGain*MedADU_RP_1[0:-1],  DeltasCumMed_RP_1/(DeltasCumMed_RP_1[0:3].mean()), 'b',\
           label=r'\textbf{Reference Pixels Applied}')
mplot.plot(HxRG1.CGain*MedADU_RP_1[0:-1],  DeltasCumMed_RP_1/(DeltasCumMed_RP_1[0:3].mean()), 'bo')
mplot.plot(HxRG1.CGain*MedADU_NRP_1[0:-1], DeltasCumMed_NRP_1/(DeltasCumMed_NRP_1[0]), 'ro')
mplot.grid(True, which='major')

mplot.figtext(0.78, 0.35, r'V$_{SUB}$ = ' + "%.2f" % HxRG1.VSUB)
mplot.figtext(0.78, 0.32, r'D$_{SUB}$ = ' + "%.2f" % HxRG1.DSUB)
mplot.figtext(0.78, 0.29, r'V$_{RESET}$ = ' + "%.2f" % HxRG1.VRESET)
mplot.figtext(0.78, 0.26, r'V$_{BIASGATE}$ = ' + "%.2f" % HxRG1.VBIASGAT)
mplot.figtext(0.78, 0.23, r'V$_{BIASPWR}$ = ' + "%.2f" % HxRG1.VBIASPWR)
mplot.figtext(0.78, 0.20, r'V$_{DDA}$ = ' + "%.2f" % HxRG1.VDDA)
mplot.figtext(0.78, 0.17, r'V$_{DD}$ = ' + "%.2f" % HxRG1.VDD)
mplot.figtext(0.78, 0.14, r'V$_{CELLDRAIN}$ = ' + "%.2f" % HxRG1.CELLDRAI)
mplot.figtext(0.78, 0.11, r'V$_{DRAIN}$ = ' + "%.2f" % HxRG1.DRAIN)
mplot.figtext(.15,.120,(r'\textbf{Region: [' +
                        str(int(XStart)) + ':' + str(int(XStop)) + ',' +
                        str(int(YStart)) + ':' + str(int(YStop)) + ']}'),size=11, \
              backgroundcolor='w')
mplot.figtext(.15,.150,r'\textbf{Detector   : H2RG-32-147}' , color='k', size=11, \
              backgroundcolor='w')
mplot.figtext(.15,.180,r'\textbf{Electronics: LEACH}'       , color='k', size=11,  \
              backgroundcolor='w')

mplot.xlabel(r'\textbf{Mean Signal ($e^{-}$ in well)}')
xlim(0,90000)
ylim(0.95,1.05)
yt1 = Ax1.get_yticklabels()
yt1[size(yt1)-1].set_visible(False)

#Axis 2--------------------------------------------------------------------
YStart=1200
YStop=1600 
XStart=100 
XStop=500
Ax2=Fig.add_axes([0.100,0.390,0.65,0.290])
mplot.plot(HxRG2.CGain*MedADU_NRP_2[0:-1], DeltasCumMed_NRP_2/(DeltasCumMed_NRP_2[0]), 'r')
mplot.plot(HxRG2.CGain*MedADU_RP_2[0:-1],  DeltasCumMed_RP_2/(DeltasCumMed_RP_2[0:3].mean()), 'b')
mplot.plot(HxRG2.CGain*MedADU_RP_2[0:-1],  DeltasCumMed_RP_2/(DeltasCumMed_RP_2[0:3].mean()), 'bo',\
           label=r'\textbf{No Reference Pixels Applied}')
mplot.plot(HxRG2.CGain*MedADU_NRP_2[0:-1], DeltasCumMed_NRP_2/(DeltasCumMed_NRP_2[0]), 'ro',\
           label=r'\textbf{Reference Pixels Applied}')

mplot.figtext(0.78, 0.65, r'V$_{SUB}$ = ' + "%.2f" % HxRG2.VSUB)
mplot.figtext(0.78, 0.62, r'D$_{SUB}$ = ' + "%.2f" % HxRG2.DSUB)
mplot.figtext(0.78, 0.59, r'V$_{RESET}$ = ' + "%.2f" % HxRG2.VRESET)
mplot.figtext(0.78, 0.56, r'V$_{BIASGATE}$ = ' + "%.2f" % HxRG2.VBIASGAT)
mplot.figtext(0.78, 0.53, r'V$_{BIASPWR}$ = ' + "%.2f" % HxRG2.VBIASPWR)
mplot.figtext(0.78, 0.50, r'V$_{DDA}$ = ' + "%.2f" % HxRG2.VDDA)
mplot.figtext(0.78, 0.47, r'V$_{DD}$ = ' + "%.2f" % HxRG2.VDD)
mplot.figtext(0.78, 0.44, r'V$_{CELLDRAIN}$ = ' + "%.2f" % HxRG2.CELLDRAI)
mplot.figtext(0.78, 0.41, r'V$_{DRAIN}$ = ' + "%.2f" % HxRG2.DRAIN)
mplot.figtext(.15,.400,(r'\textbf{Region: [' +
                        str(int(XStart)) + ':' + str(int(XStop)) + ',' +
                        str(int(YStart)) + ':' + str(int(YStop)) + ']}'),size=11, \
              backgroundcolor='w')
mplot.figtext(.15,.430,r'\textbf{Detector   : H2RG-32-147}' , color='k', size=11, \
              backgroundcolor='w')
mplot.figtext(.15,.460,r'\textbf{Electronics: ASIC}'       , color='k', size=11,  \
              backgroundcolor='w')
#LEGEND
l=mplot.legend(loc=6, numpoints=1)
t=l.get_texts()
t[0].set_fontsize(10)
draw()

xlim(0,90000)
ylim(0.8, 1.10)
mplot.ylabel(r'\textbf{Fractional Countrate}')
mplot.grid(True, which='major')
yt2 = Ax2.get_yticklabels()
yt2[size(yt2)-2].set_visible(False)
draw()

xt2 = Ax2.get_xticklabels() 
for i in xt2:
  i.set_visible(False)
draw()

#Axis 3------------------------------------------------------------------------
YStart=1690
YStop=1700
XStart=1540
XStop=1600
Ax3=Fig.add_axes([0.100,0.680,0.65,0.290])
mplot.plot(HxRG3.CGain*MedADU_NRP_3[0:-1], DeltasCumMed_NRP_3/(DeltasCumMed_NRP_3[0]), 'r', \
           label=r'\textbf{No Reference Pixels Applied}')
mplot.plot(HxRG3.CGain*MedADU_RP_3[0:-1],  DeltasCumMed_RP_3/(DeltasCumMed_RP_3[0:3].mean()), 'b',\
           label=r'\textbf{Reference Pixels Applied}')
mplot.plot(HxRG3.CGain*MedADU_RP_3[0:-1],  DeltasCumMed_RP_3/(DeltasCumMed_RP_3[0:3].mean()), 'bo')
mplot.plot(HxRG3.CGain*MedADU_NRP_3[0:-1], DeltasCumMed_NRP_3/(DeltasCumMed_NRP_3[0]), 'ro')
mplot.figtext(0.78, 0.94, r'V$_{SUB}$ = ' + "%.2f" % HxRG3.VSUB)
mplot.figtext(0.78, 0.91, r'D$_{SUB}$ = ' + "%.2f" % HxRG3.DSUB)
mplot.figtext(0.78, 0.88, r'V$_{RESET}$ = ' + "%.2f" % HxRG3.VRESET)
mplot.figtext(0.78, 0.85, r'V$_{BIASGATE}$ = ' + "%.2f" % HxRG3.VBIASGAT)
mplot.figtext(0.78, 0.82, r'V$_{BIASPWR}$ = ' + "%.2f" % HxRG3.VBIASPWR)
mplot.figtext(0.78, 0.79, r'V$_{DDA}$ = ' + "%.2f" % HxRG3.VDDA)
mplot.figtext(0.78, 0.76, r'V$_{DD}$ = ' + "%.2f" % HxRG3.VDD)
mplot.figtext(0.78, 0.73, r'V$_{CELLDRAIN}$ = ' + "%.2f" % HxRG3.CELLDRAI)
mplot.figtext(0.78, 0.70, r'V$_{DRAIN}$ = ' + "%.2f" % HxRG3.DRAIN)
mplot.figtext(.15,.700,(r'\textbf{Region: [' +
                        str(int(XStart)) + ':' + str(int(XStop)) + ',' +
                        str(int(YStart)) + ':' + str(int(YStop)) + ']}'),size=11, \
              backgroundcolor='w')
mplot.figtext(.15,.730,r'\textbf{Detector   : H2RG-001}' , color='k', size=11, \
              backgroundcolor='w')
mplot.figtext(.15,.760,r'\textbf{Electronics: ASIC}'       , color='k', size=11,  \
              backgroundcolor='w')

xlim(0,90000)
ylim(0.8, 1.05)
mplot.grid(True, which='major')
yt3 = Ax3.get_yticklabels()
yt3[size(yt3)-1].set_visible(False)
xt3 = Ax3.get_xticklabels() 
for i in xt3:
  i.set_visible(False)
draw()

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