;pro return_Leaky_Pixels 
;
;PURPOSE:
;	To try to find the pixels that appear to leak in flat frame images 
;	and 'get leaked into' in dark images.  These pixels are usually at the
;	center of a cross pattern and have a very large difference with respect
;	to the pixels above, below, to the left, and to the right of them.
;
;	The method of finding them will be to take the difference between
;	a frame and the same frame shifted up, down, left, and right by 1 pixel
;	and make a cut on the difference of all values.
;
;INPUTS:
;	Im - The image that will be used to find the leaky pixels
;
;OUTPUTS:
;
;KEYWORDS:
;       ROOTDIR: 
;         The Directory holding the directories of Raw files
;       DATE:
;         The Date during which they were taken.  Also the subdirectory that 
;         the files are in.
;       REDUCEDDIR:
;         The directory where the processed images and plots will be placed in.
;       BLOCKLENGTH:
;         An offset for files that were written improperly.  The readu 
;         function reads the values with this offset in place.
;       OBJECTNAME:
;         The string that indicates what files are being sought.  The filenames
;         should begin with this string ('Dark', 'Flat', etc.).
;       NREADS:
;         The number of reads (NAXIS3) that are present in the file.  This
;         is also a string in the filename that will be used as a regular
;         expression in searching for matched files.
;       FIRSTREAD:
;         The first read to be used in the difference 
;             Diff = LastRead - FirstRead
;       LASTREAD: 
;         The last read to be used in the difference
;       THISFILTER:
;         The filter through which the files were taken. ('I', 'G', or 'Y')
;	OVERRIDE: 
;	  Do the processing even if the output file already exists
;	ALLFILES:
;	  1 - Process all files matching the #Reads, Filter, and Object
;	  0 - Only process one file (the first by default)
;**************************************************************************

function Return_Leaky_Pixels, Im, RootDir=RootDir, Date=Date, $
         ReducedDir = ReducedDir, BlockLength = BlockLength, $
         ObjectName=ObjectName, NReads = NReads, ThisFilter = ThisFilter,$
         OverRide = OverRide, FirstRead = FirstRead, LastRead = LastRead, $
	 Threshold = Threshold

  ;Include KeywordStruct.pro file with all the defaults for keywords
  @KeywordStruct.pro

  ;Only look at area that excludes channel 0 and the reference pixels
  RPix = 8
  Ch0W = 128
  PosThreshold = 10000.
  NegThreshold = -7000.
  XSizeM = Naxis1-Ch0W-RPix/2-2
  YSizeM = Naxis2-RPix-2

  ;Form difference frames. Waste of memory, but ease of use with where function
  FrameUpOne    = LonArr(XSizeM, YSizeM)
  FrameDownOne  = LonArr(XSizeM, YSizeM)
  FrameRightOne = LonArr(XSizeM, YSizeM)
  FrameLeftOne  = LonArr(XSizeM, YSizeM) 

  ;Do the differences
  FrameUpOne(*,*) = $
	long(Frame(1+Ch0W:Naxis1-RPix/2-2, 1+RPix/2:Naxis2-RPix/2-2)) - $
	long(Frame(1+Ch0W:Naxis1-RPix/2-2, 2+RPix/2:Naxis2-RPix/2-1))

  FrameDownOne(*,*) = $
	long(Frame(1+Ch0W:Naxis1-RPix/2-2, 1+Rpix/2:Naxis2-RPix/2-2)) - $
	long(Frame(1+Ch0W:Naxis1-RPix/2-2, 0+RPix/2:Naxis2-RPix/2-3))

  FrameLeftOne(*,*) = $
	long(Frame(Ch0W+1:Naxis1-RPix/2-2 , 1+RPix/2:Naxis2-RPix/2-2)) - $
	long(Frame(Ch0W+2:Naxis1-RPix/2-1 , 1+RPix/2:Naxis2-RPix/2-2))
 
  FrameRightOne(*,*) = $
	long(Frame(Ch0W+1:Naxis1-RPix/2-2 , 1+RPix/2:Naxis2-RPix/2-2)) - $
        long(Frame(Ch0W+0:Naxis1-RPix/2-3 , 1+RPix/2:Naxis2-RPix/2-2))

  ;Form a mask showing the number of differences that are greater than threshold
  FrameBadMask = IntArr(XSizeM, YSizeM)

  ;Find pixels above neighbors
  FrameBadMask(where(FrameUpOne gt PosThreshold))    += 1
  FrameBadMask(where(FrameDownOne gt PosThreshold))  += 1
  FrameBadMask(where(FrameLeftOne gt PosThreshold))  += 1
  FrameBadMask(where(FrameRightOne gt PosThreshold)) += 1

  ;Find pixels below neighbors
  FrameBadMask(where(FrameUpOne lt NegThreshold))    -= 1
  FrameBadMask(where(FrameDownOne lt NegThreshold))  -= 1
  FrameBadMask(where(FrameLeftOne lt NegThreshold))  -= 1
  FrameBadMask(where(FrameRightOne lt NegThreshold)) -= 1

  PosCrPix = where(FrameBadMask eq 4)
  XPosCrPix = (PosCrPix mod XSizeM) + Ch0W + 2 
  YPosCrPix = (PosCrPix/YSizeM)+RPix/2 + 2

  NegCrPix = where(FrameBadMask eq -4)
  XNegCrPix = (NegCrPix mod XSizeM) + Ch0W + 2             
  YNegCrPix = (NegCrPix/YSizeM)+RPix/2 + 2

  return, 1

end
