;pro Correct_Offseted_Files
;
;PURPOSE:
;  To rightly remove the offset from files captured at the Kitt Peak 2.1m 
;  in April/May of '07 and rewrite them with the offset removed.
; 
;OUTPUTS:
;	Corrected Files that match NREADS, OBJECTNAME, etc.
;
;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.
;	THISFILTER:
;	  The filter through which the files were taken. ('I', 'G', or 'Y')
;
;NOTE:
;	For dark exposures, the median of the zeroth read is taken and 
;	this is the first frame in the cube that is written to disk.  
;	However, for the following reads, the bias is subtracted from
;	each exposure. So the 1st through Nth frames in the cube are 
;	the median of bias-subtracted reads from individual exposures.
;
;CALLING SEQUENCE:
;  Correct_Offseted_Files, Date='07Apr29', nreads=15, objectname='M16',thisfilter='I', offset=1440

;*************************************************************************
pro Correct_Offseted_Files, RootDir=RootDir, Date=Date, $
	 ReducedDir = ReducedDir, BlockLength = BlockLength, $ 
	 ObjectName=ObjectName, NReads = NReads, ThisFilter = ThisFilter,$
	 OverRide = OverRide, Rem60Hz = Rem60Hz, TvFlag = TvFlag, $
	 BiasSub = BiasSub, SubtractDark = SubtractDark, $
	 ThisNDrops = ThisNDrops, Offset=Offset
  
  Common KeyParams, KeyStr

  ;Get all of the defaults for the keywords from the KeywordStruct file
  @KeywordStruct.pro
  @PlotSettings.pro
 
  ;Swithc to the directory with the files
  cd, Rootdir+Date
  
  ;Get all of the Files to be corrected
  RawFiles = File_Search(KeyStr.ObjectName+'*'+strtrim(KeyStr.TotalReads,2)+$
	'_Reads*.fits', count=NumRawFiles)

  ;Loop through all of the files, read them in with offset, and rewrite them
  For FileNum =0, NumRawFiles-1 do begin
    print, 'Rewriting File : ' + RawFiles(FileNum)
    Fits_Read_DataCube, RawFiles(FileNum), Im, H, Offset=Offset
    ThisFileParams = return_file_params(RawFiles(FileNum))    
    MkHdr, H, Im
    HeaderUpdate=Update_Header(H)
    Fits_Write, RawFiles(FileNum), Im, H
  EndFor
  stop
end
