;pro photon_transfer
;
;PURPOSE:
;	To reduce the data taken with the photon transfer test
;
;CALLING SEQUENCE:
;   photon2_reduce,indir [,outdir[,filelist]]
;
;INPUTS:
;   indir       - input directory where data and filelist are located
;
;KEYWORD PARAMETERS:
;   filelist    - list of images to be processed without full path names
;   outdir      - directory where output will be placed
;   refsub      - turns on reference pixel correction

 
pro photon_transfer, RootDir = RootDir

common FitsCube, FCB, unit

If Not Keyword_Set(RootDir) then RootDir = '/u1/ki/PhotonTransferImages/'
If Not Keyword_Set(InDir) then InDir = RootDir
If Not Keyword_set(OutDir) then Outdir = RootDir
If Not Keyword_set(FileList) then FileList = ''

RefSubBersion = 'dummy'
If Not keyword_set(refsubon) then Refsubversion = 'None'
RefSubType = 'None'
If keyword_set(refsubon) then begin
       RefSubType = 'Spatial averaging'
Endif
   
; Move to the directory where the files are 
cd, RootDir

; determine path directory delimiter from OS type
If (!version.os_family eq 'unix') then begin
        delim = '/'
Endif else begin
        delim = '\'
Endelse

; check to see if input and outdirectory names end in path delimiter--if
; not, add it to the end
If (strmid(indir, strlen(indir) - 1, 1) ne delim) then begin
        indir = indir + delim
Endif

If (strmid(outdir, strlen(outdir)-1, 1) ne delim) then begin
        outdir = outdir + delim
Endif

; set histogram bin size
Binsize = 100.

; define upper limit and lower limit of signal range for use in linear fit
lower = 1e3         ; ADU
upper = 1e4         ; ADU
lowerbin = fix(lower / binsize)
upperbin = fix(upper / binsize)

; set flag to control whether the line fit is done to the binned data
fittobinned = 1

; save current device name so it can be reset later
dname = !D.NAME

; set plot colors
black = 0
white = 255

;Only Look for Files in FileList.txt if specified
If Keyword_Set(FileList) then begin  
  ; exit gracefully if filelist does not exist
  If (file_test(indir + filelist) eq 0) then begin
    print,'File ' + indir + filelist + '  does not exist'
    stop 
  Endif 
 
  ; read file list 
  readcol, indir + filelist, AllFiles, format = '(A)'

Endif Else begin
  ; Check for all of the PhotonTransfer files that will allow for subtraction
  NReads = 2 
  XFerFiles = file_search('PhotonXFer*'+Strtrim(NReads,2)+'*.fits')
EndElse

; Figure out how many images of each Cadence there are in the list
XFerDrops = BytArr(N_Elements(XFerFiles))

for XFerFile = 0, N_Elements(XFerFiles)-1 do begin
    fits_read, XFerFiles(XFerFile), NoData, XFerHeader, /header_only
    XFerDrops(XFerFile) = Byte(SxPar(XFerHeader,'NDROPS'))
    print, long( SxPar(XFerHeader, 'NDROPS'))
endfor
stop

; retrieve header information
im = readfits(InDir + Files[0], h)

stop

end
