;pro examine_leaky_pixels
;
;PURPOSE:
;	To produce some plots of the bad types of pixels in dark and flat 
;	frames.
;
;INPUTS:
;	x - The center x-coordinate of a 3x3 region
;       y - The center y-coordinate of a 3x3 region
;KEYWORDS:
;	FROMMASK
;	    If this keyword is set, the Leaky and Hot Pixel masks will be 
;	    read in and the coordinates for the corresponding pixels will 
;	    be held in an array to be used.
;	AVGS - 
;	   0 - Plot individual pixels as we go through the for loop
;	   1 - Plot histograms at the end of the file
;***********************************************************************
pro Examine_Leaky_Pixels, x, y, ThisFilter=ThisFilter, FromMask = FromMask,$
	TvFlag = TvFlag, Avgs = Avgs, TestSlopeFit = TestSlopeFit, $
	SubtractDark = SubtractDark

;Get the keywords from the structure file
@KeywordStruct.pro
cd, RootDir+Date

;Get plotting configuration
@PlotSettings.pro

;Only using 2 read files
NReads = 2
If SubtractDark eq 0 then DarkSubStr = 'NoDarkSubtracted'

;Search the given directory for the darks and the flats
DarkFiles = File_Search(DarkDir+'MedianDark*_2Reads*.fits')
FlatFiles = File_Search(FlatDir+'MedianFlat*2*Reads*'+DarkSubStr+'.fits')
DarkFile = DarkFiles(0)
DarkPos  = Fits_Open_DataCube(DarkFile, DarkFCB, DarkHeader, 'Read')
DarkFileBase = (strsplit(DarkFile, '.', /extract))[0]
DarkFileParts= strsplit(DarkFileBase, PathDelim, /extract,count=numparts)
DarkFileKey  = strmid(DarkFileParts(numparts-1), stregex(DarkFile,'Reads_')+1)
print,DarkFile
FlatFile = FlatFiles(0)
FlatPos  = Fits_Open_DataCube(FlatFile, FlatFCB, FlatHeader, 'Read')
FlatFileBase = (strsplit(FlatFile, '.', /extract))[0]
FlatFileParts= strsplit(FlatFileBase, PathDelim, /extract, count=numparts)
FlatFileKey = strmid(FlatFileParts(numparts-1), stregex(FlatFile,'Reads_')+1)
print,FlatFile

;Open the correct file if postscript was chosen
If TvFlag eq 2 then begin 
   If Avgs then begin
     PSFileName = LeakyDir + FlatFileKey+'LeakyAndHotPixels'+DarkSubStr+'Histogram.ps'
   EndIf Else begin
     PSFileName = LeakyDir + FlatFileKey+'LeakyAndHotPixels'+DarkSubStr+'Ex.ps'
   EndElse
     device, filename = PSFilename
EndIf

;If FromMask = 1, take the mask files and get all of the indices
If FromMask then begin

 Masks = Return_Pixel_Masks()
 LeakyPixInd = Masks.LeakyInd
 LeakyPixX   = LeakyPixInd mod Naxis1
 LeakyPixY   = LeakyPixInd / Naxis2

EndIf

;Form arrays for some averages
NLeaky = N_Elements(LeakyPixInd)
NNFracLeakyD = fltarr(NLeaky)
NNFracLeakyF = fltarr(NLeaky)
MaxPixInd = NLeaky

Print, 'Beginning To Go through '+strtrim(NLeaky,2) + 'Pixels'

;Start looking through the pixels and getting the fractions  
For PixInd = 0L, MaxPixInd-1 do begin

  ;Determine what type of pixels are being examined here 
  X = LeakyPixX(PixInd)
  Y = LeakyPixY(PixInd)
  CommentStr = strtrim(abs(0.2),2) + ' counts below'

  ;Read the Region of the Dark Cube - Dark Diff(DD), Flat Diff(FD)
  DarkIm = Fits_Read_DataCube_Region(DarkFiles(0), DarkFCB, DarkHeader,$
	 	x-1, x+1, y-1, y+1, 1, 1)
  DD12 = float(DarkIm(*,*,0)) 
  DDNNAvg = mean([DD12(0,1),DD12(1,0),DD12(2,1),DD12(1,2)])
   
  FlatIm = Fits_Read_DataCube_Region(FlatFiles(0), FlatFCB, FlatHeader, $
		x-1, x+1, y-1, y+1, 1, 1)
  FD12 = float(FlatIm(*,*,0)) 
  FDNNAvg = mean([FD12(0,1),FD12(1,0),FD12(2,1),FD12(1,2)])

  NNFracLeakyD(PixInd) = float(DD12(1,1))/DDNNAvg
  NNFracLeakyF(PixInd) = float(FD12(1,1))/FDNNAvg

EndFor
  
DarkClose = Fits_Close_DataCube(DarkFCB)
FlatClose = Fits_Close_DataCube(FlatFCB)

;Plot Histograms if TvFlag eq 2 and Avgs = 1
If TvFlag eq 2 and Avgs eq 1 then begin 

  !p.noerase = 1
  ;Plot the histogram of the leaky ratio
  LeakyDHist = histogram(NNFracLeakyD, locations = LeakyDLocs, $
	nbins = 2000)
  Plot, LeakyDLocs, LeakyDHist, psym = 10, xrange = [0, 2], $
	Title = 'Ratios for the Leaky Pixels in Dark Frame ' $
	        +DarkFileKey, $
	Position = [0.05, 0.075, 0.95, 0.45], /normal

  LeakyFHist = histogram(NNFracLeakyF, locations = LeakyFLocs, $
	nbins = 2000)
  Plot, LeakyFLocs, LeakyFHist, psym = 10, xrange = [ 0, .2], $
	Title = 'Ratios for the Leaky Pixels in Flat Frame ' $
		+FlatFileKey, $
	position = [0.05, 0.525, 0.95, 0.95], /normal

EndIf

If TvFlag eq 2 then device,/close

set_plot,'x'
stop

end

