#PlotHD53791_RadialPersistence.py
#
#PURPOSE:
#  To plot the persistence for the saturation caused by HD53791 in window mode.
#  The regular plotradial persistence in not used because there are very large 
#  diffraction spikes on the diagonals that ruin the symmetry

execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
from HxRG_Class import *
from pylab import *
from matplotlib.finance import quotes_historical_yahoo
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
import datetime

#Set Up for plotting
PlotFile='/afs/slac/u/ki/lances/python/ThesisPlotSettings.py'
if os.path.exists(PlotFile) : execfile(PlotFile)
PlotDir ='/nfs/slac/g/ki/ki04/lances/LSST/KPNO/Latex/Thesis/NegPersistence/Figures/'
SaveFig = 1
#PLOT SETUP
mplot.figure(0)
mplot.clf()

#FILE AT 100 K
FitsFileName = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/07Dec19/HD53791/HD53791_Dark_H2RG_SIPIN_OneWindow2000_Reads_Dec20_2007_00_54_25.fits'
HxRG = HxRG_C(FitsFileName=FitsFileName)
HxRG.Get_Raw_Header(FitsFileName)
Im   = pyfits.getdata(FitsFileName)
Im   = Im.astype(float32) 
#Average along the three middle rows
LMF    = Im[1399,:,:]-Im[0,:,:]
Off    = +1
FirstCol = 28
LastCol  = 128
MidCol   = FirstCol + (LastCol-FirstCol)/2
Row1Num  = 66
Row2     = LMF[Row1Num, FirstCol+Off:MidCol+Off]
Row1     = LMF[Row1Num, MidCol:LastCol]
Row2Num  = 67
Row4     = LMF[Row2Num, FirstCol+Off:MidCol+Off]
Row3     = LMF[Row2Num, MidCol:LastCol]
Row3Num  = 65
Row6     = LMF[Row3Num, FirstCol+Off:MidCol+Off]
Row5     = LMF[Row3Num, MidCol:LastCol] 
Row4Num  = 64
Row8     = LMF[Row4Num, FirstCol+Off:MidCol+Off]
Row7     = LMF[Row4Num, MidCol:LastCol]
Row5Num  = 68
Row10     = LMF[Row5Num, FirstCol+Off:MidCol+Off]
Row9     = LMF[Row5Num, MidCol:LastCol]
RowAvg   = (Row1+Row2[::-1]+Row3+Row4[::-1]+Row5+Row6[::-1]+Row7+Row8[::-1]+Row9+Row10[::-1])/10
 
mplot.plot(RowAvg, 'bo')
mplot.plot((Row1)/1,'ro')
mplot.plot((Row2[::-1])/1,'go')

FitsFileName = '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/07Dec19/HD53791/HD53791_Dark_H2RG_SIPIN_OneWindow500_Reads_Dec20_2007_02_57_22.fits'
RampOff = 100. #All pixels in this file see a quick increase of about 100 counts
HxRG = HxRG_C(FitsFileName=FitsFileName)
HxRG.Get_Raw_Header(FitsFileName)
Im   = pyfits.getdata(FitsFileName)
Im   = Im.astype(float32)         
#Average along the three middle rows
LMF    = Im[499,:,:]-Im[0,:,:]+RampOff
Off    = +1
FirstCol = 25
LastCol  = 125
MidCol   = FirstCol + (LastCol-FirstCol)/2
Row1Num  = 66
Row2     = LMF[Row1Num, FirstCol+Off:MidCol+Off]
Row1     = LMF[Row1Num, MidCol:LastCol]
Row2Num  = 67
Row4     = LMF[Row2Num, FirstCol+Off:MidCol+Off]
Row3     = LMF[Row2Num, MidCol:LastCol] 
Row3Num  = 65 
Row6     = LMF[Row3Num, FirstCol+Off:MidCol+Off]
Row5     = LMF[Row3Num, MidCol:LastCol]
Row4Num  = 64 
Row8     = LMF[Row4Num, FirstCol+Off:MidCol+Off]
Row7     = LMF[Row4Num, MidCol:LastCol]
Row5Num  = 68
Row10     = LMF[Row5Num, FirstCol+Off:MidCol+Off]
Row9     = LMF[Row5Num, MidCol:LastCol]
RowAvg   = (Row1+Row2[::-1]+Row3+Row4[::-1]+Row5+Row6[::-1]+Row7+Row8[::-1]+Row9+Row10[::-1])/10

Fig1 = mplot.figure(1)
mplot.clf()
mplot.plot(RowAvg, 'ro')
mplot.figtext(0.44, 0.92, r'\textbf{Real Data}', fontsize=16)
mplot.xlabel(r'\textbf{Radius (Pixels)}', fontsize=12)
mplot.ylabel(r'\textbf{ADU in Last Read (Bias Subtracted)}', fontsize=12)
Ax0     = Fig1.add_axes([0.5, 0.45, 0.4, 0.4])
HalfPX  = LMF.shape[0]/2
HalfPY  = 65
BS      = 25
Im0     = imshow(LMF[HalfPY-BS:HalfPY+BS,HalfPX-BS:HalfPX+BS])
ax      = axis('image')
axis('off')
if SaveFig == 1:
  mplot.savefig(PlotDir+'HD53791_Dark_H2RG_SIPIN_OneWindow500_Reads_Dec20_2007_02_57_22_RadialProfile.png')
  mplot.savefig(PlotDir+'HD53791_Dark_H2RG_SIPIN_OneWindow500_Reads_Dec20_2007_02_57_22_RadialProfile.eps')
