;pro Examine_Ref_Pixels
;
;PURPOSE:
;	To take a collection of dark images and produce quantitative measure-
;	ments of the reference pixels
;
;INPUTS:
;
;OUTPUTS:
;
pro Examine_Dark_Ref_Pixels, RootDir = RootDir, Date=Date, $
	ReducedDir = ReducedDir, $
	ObjectName=ObjectName, NReads = NReads, ThisFilter = ThisFilter,$
	OverRide = OverRide, TvFlag = TvFlag, AvgEntireFrame = AvgEntireFrame

  Common FitsCube, FCB, unit

  If not(keyword_set(date)) then Date='07Apr30'
  If not(keyword_set(RootDir)) then RootDir=$
	   '~/Desktop/LSST/KPNO/Images/'
  If not(keyword_set(ObjectName)) then ObjectName = 'Dark'
  If not(keyword_set(NReads)) then NReads = 20
  If not(keyword_set(ThisFilter)) then ThisFilter = 'G'
  If not(keyword_set(OverRide)) then OverRide = 0
  If not(keyword_set(TvFlag)) then TvFlag = 0
  If not(keyword_set(AvgEntireFrame)) then AvgEntireFrame = 0

  cd,RootDir
	
  ;Block Length for Offset
  if not(keyword_set(BlockLength)) then BlockLength = 0

  ;***************************************************************************************
  ;Get all of the Dark Files for this Number of Reads
  Dark_Files = file_search(ObjectName+'*'+strtrim(NReads,2)+'_Reads*.fits')
  Dummy = 0 
  Valid_Indices = 0

  ;Get a few kewyords
  Fits_Read,  Dark_Files(0), NoData, FitsHeader_Dark, /header_only       
  Naxis1     = long(sxpar(FitsHeader_Dark, 'NAXIS1'))          
  Naxis2     = long(sxpar(FitsHeader_Dark, 'NAXIS2'))         
  TotalReads = sxpar(FitsHeader_Dark ,'NAXIS3')      
  ArrSize = long(Naxis1*Naxis2)
	
  ;Only use the images that are certain to have Blank as the filter
  for i = 0, n_elements(Dark_Files) - 1 do begin
      Fits_Read, Dark_Files(i), NoData, FitsHeader, /header_only
      Filter = sxpar(FitsHeader ,'FILTER')
      print, i, ' ' , Filter
      If stregex(Filter, 'Blank' , /boolean) and $
	 not stregex(Dark_Files(i), '42_44', /boolean) then $
          valid_indices = [valid_indices, i]
  endfor

  ;Form an Array big enough for all of the darks of a given Naxis3
  NumDark_Files = N_elements(Valid_Indices)
  FullIms = LonArr(Naxis1, Naxis2, NumDark_Files-1)
	
  ;Form Cube of Darks and then Take Median at the End 
  for DarkNum = 1, NumDark_Files - 1 do begin
      index = Valid_Indices(DarkNum)

      ;Get one frame
      DarkIm = Fits_Read_DataCube_Region(Dark_Files(index), $
				FitsHeader, 0, Naxis1-1, 0, Naxis2-1, $
				0, 0, Naxis1, Naxis2)

      ;Write a fits file with the unfiltered image
      Fits_Write, 'UnFilteredDark.fits', DarkIm, FitsHeader
      DarkImFil = fltarr(Naxis1,Naxis2)

      Filter = sxpar(FitsHeader ,'FILTER')
      print, index, ' ', Filter, ' ', Dark_Files(index) 

      ;Filter the Image
     
      Filter = reffilter(DarkIm, TvFlag=TvFlag, AvgEntireFrame = AvgEntireFrame)
      for Col = 0, Naxis2 - 1 do begin
          FFTDarkImCol  = fft(DarkIm(Col, *))
          FFTDarkImCol  = Filter*FFTDarkImCol
	  DarkImFil(Col,*) = fft(FFTDarkImCol,/inverse)  
      endfor
      
      Filtered = reffilter(DarkIm, TvFlag=TvFlag, AvgEntireFrame = AvgEntireFrame)

      SxAddPar, FitsHeader, 'REFTYPE', 'Fixed Pattern Filter'
      Fits_Write, 'FilteredDark.fits', DarkImFil, FitsHeader

      Fits_Write, 'FilteredDiff.fits', DarkIm - DarkImFil, FitsHeader
 
      ;Get Difference of Dark Frames
      FullIms(*,*, DarkNum - 1) = return_cube_diff(Dark_Files(index), $
	     ArrSize, Naxis1, Naxis2, 1, TotalReads)

endfor

stop

end
