#Make a read noise map of H2RG-001

from ReadNoise import *
execfile('/afs/slac/u/ki/lances/python/ThesisImageFrameSettings.py')
PlotDir = '/nfs/slac/g/ki/ki04/lances/LSST/KPNO/Latex/Thesis/ReadNoise/Figures/'
CDSMap, BinsCor, N, FWHM, HalfMax, StdDev = ReadNoise('H2RG-001', 'ASIC', 1, 'H2RG_001_ASICSpatialReadNoiseFiles100Reads_20V_100K.txt', TvFlag=0)

mplot.hold(True)
mplot.hot()
ImFig   = mplot.figure(0)
mplot.clf()
SecondAx=ImFig.add_axes([0.00, 0.08, .990, 0.900])
Im1=mplot.matshow(CDSMap, vmin=-40, vmax=40, fignum=0)
Im1.axes.yaxis.set_ticks('')
Im1.axes.xaxis.set_ticks('')
#Add the colorbar
pos = SecondAx.get_position()
l, b, w, h = getattr(pos, 'bounds', pos)
cax=axes([l+0.00,b-0.03,0.8,.02])
mplot.colorbar(cax=cax, orientation='horizontal', format='%.0f')
mplot.figtext(0.89, 0.035, r'\textbf{e$^{-}$}', size=14)
mplot.savefig(PlotDir+'CDSReadNoiseMap_100K_H2RG.png')
mplot.savefig(PlotDir+'CDSReadNoiseMap_100K_H2RG.eps')

#Find the FWHM
mplot.figure(1)
mplot.clf()
mplot.plot(BinsCor, N, 'k')
mplot.axhline(HalfMax, color='k', linestyle='--')
xlim(-3*FWHM, 3*FWHM)
mplot.xlabel(r'\textbf{Science Pixels (electrons)}')
mplot.ylabel(r'\textbf{Occurences}')

#Electrons
FWHMStr = "%.2f" % FWHM
StdDevStr = "%.2f" % StdDev
mplot.figtext(0.7, 0.80, r'\textbf{FWHM : ' + FWHMStr +' $e^{-}$}')
mplot.figtext(0.7, 0.74, r'\textbf{Std. : ' + StdDevStr + '$e^{-}$}')

#Microvolts
FWHMuVStr   = "%.2f" % (FWHM*12)
StdDevuVStr = "%.2f" % (StdDev*12)
mplot.figtext(0.7, 0.68, r'\textbf{FWHM : ' + FWHMuVStr +' $\mu V$}')
mplot.figtext(0.7, 0.62, r'\textbf{Std. : ' + StdDevuVStr + '$\mu V$}')

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


CDSMap, BinsCor, N, FWHM, HalfMax, StdDev = ReadNoise('H2RG-001', 'ASIC', 1, 'H2RG_001_ASICSpatialReadNoiseFiles100Reads_20V_160K.txt', TvFlag=0, XStart=0, XStop=4)

FWHM    = FWHM/1.2 * 16
BinsCor = BinsCor/1.2 * 16
StdDev  = StdDev/1.2 * 16

#Find the FWHM
mplot.figure(2)
mplot.clf()
mplot.plot(BinsCor, N, 'k')
mplot.axhline(HalfMax, color='k', linestyle='--')
xlim(-3*FWHM, 3*FWHM)
mplot.xlabel(r'\textbf{Reference Pixels ($\mu V$)}')
mplot.ylabel(r'\textbf{Occurences}')

#Microvolts
FWHMuVStr   = "%.2f" % (FWHM)
StdDevuVStr = "%.2f" % (StdDev)
mplot.figtext(0.7, 0.68, r'\textbf{FWHM : ' + FWHMuVStr +' $\mu V$}')
mplot.figtext(0.7, 0.62, r'\textbf{Std. : ' + StdDevuVStr + '$\mu V$}')

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

print 'CDS Noise ' + str(FWHM/2.354)

CDSMap, BinsCor, N, FWHM, HalfMax, StdDev = ReadNoise('H2RG-001', 'ASIC', 1, 'H2RG_001_ASICSpatialReadNoiseFiles100Reads_20V_100K.txt', TvFlag=0)

