##PlotPersistenceConservation.py
##
##PURPOSE:
## To plot the persistence from a saturating star as a function of distance
## away from the center
##
##INPUTS:
## FitsFileName: str
##   The full path to the file containing the persistence
##
##CALLING SEQUENCE
##
##run PlotPersistenceConservation.py '/nfs/slac/g/ki/ki04/lances/H1RG-022/ASIC/07Nov19/SAO5417_I_NegPerDark_H1RG_SIPIN_2500_Reads_Nov19_2007_22_45_50.fits'
##
##
##NOTE: The image shown in 
##
##/nfs/slac/g/ki/ki04/lances/H1RG-022/Plots/NegPersistence/SAO5417_I_NegPerDark_22_45_50_PersistenceWithSquareRegions.tif
##
##is centered at 339, 265 and the squares have a length and width of 
##10,20,30,40,50, respectively
##
##The Extra image has a square of length and width 100
import sys
from ReturnRadialProfiles import *
from HxRG_Class import *
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
execfile('/afs/slac/u/ki/lances/python/SPIEPlotSettings.py')

FitsFileName=sys.argv[1]
HxRG = HxRG_C(FitsFileName=FitsFileName)
HxRG.Get_Raw_Header(FitsFileName)
Times = (HxRG.ITime/HxRG.NReads)*frange(HxRG.NReads-1)

Sums, Avgs, IntArr=ReturnRadialProfiles(FitsFileName, 0, YCen=265, XCen=339, \
                   UseRef=1, DoCentroid=0)

SquareSize=IntArr.shape[0]
SquareCen = SquareSize/2

##Plot the squares
mplot.close('all')
mplot.figure(0)
FirstSquare = IntArr[SquareCen-5:SquareCen+5, SquareCen-5:SquareCen+5, :]
FirstSquareSum=FirstSquare.sum(axis=0).sum(axis=0)
mplot.plot(FirstSquareSum, color='c')

SecondSquare = IntArr[SquareCen-10:SquareCen+10, SquareCen-10:SquareCen+10, :]
SecondSquareSum=SecondSquare.sum(axis=0).sum(axis=0)
mplot.plot(SecondSquareSum, color='r') 

ThirdSquare = IntArr[SquareCen-15:SquareCen+15, SquareCen-15:SquareCen+15, :]
ThirdSquareSum=ThirdSquare.sum(axis=0).sum(axis=0)
mplot.plot(ThirdSquareSum, color='g')

FourthSquare = IntArr[SquareCen-20:SquareCen+20, SquareCen-20:SquareCen+20, :]
FourthSquareSum=FourthSquare.sum(axis=0).sum(axis=0)
mplot.plot(FourthSquareSum, color='b')

FifthSquare = IntArr[SquareCen-25:SquareCen+25, SquareCen-25:SquareCen+25, :]
FifthSquareSum=FifthSquare.sum(axis=0).sum(axis=0)
mplot.plot(FifthSquareSum, color='m')

##Plot the absolute value to see if we're near the dark current
mplot.figure(1)
AbsIntArr= abs(IntArr)
AbsIntSum=AbsIntArr.sum(axis=0).sum(axis=0)
mplot.plot(AbsIntSum)
AbsIntMean=AbsIntArr.mean(axis=0).mean(axis=0)
mplot.plot(AbsIntMean)

