;pro Examine_DarkCurrent
;
;PURPOSE:
;	To take an median dark frame and try to fit an exponential 
;	function to the ramp.
;
;INPUTS:
;	FITSFILENAME:
;	  The File Name for which the analysis should be done
; 
;KEYWORDS:
;       ROOTDIR: 
;         The Directory holding the directories of Raw files
;       TVFLAG 
;         0 - Don't Plot anything
;         1 - Plot to the X Window
;         2 - Plot a postscript file that was opened by calling script
;         3 - Plot a png file tha can be converted for a paper 
;	MODE:
;	  0 - Only plot the slopes for bright stars
;	  1 - Plot the stars for every pixel on the array with the exception
;	      of the pixels in the bad pixel mask.
;OUTPUTS:
;
;	  TvFlag = 2, 
;	   A postscript file will be written with all of the Fourier Plots
;	  TvFlag = 3
;	   A .png file will be written with the fourier spectrum for channel 0
;CALLING SEQUENCE:
;
;Examine_Linearity, '/nfs/slac/g/ki/ki09/lances/07Apr26/M13_Dither0_0ArcSecondsEast_0ArcSecondsSouth_H4RG_SIPIN_30_Reads_Apr27_2007_03_15_28.fits'
;Examine_DarkCurrent,'/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/Darks/MedianDark_Blank_ForEachOf_30Reads.fits'
pro Examine_DarkCurrent, FitsFiles, RootDir = RootDir, Date=Date, $
	ReducedDir = ReducedDir, $
	ObjectName=ObjectName, NReads = NReads, $
	ReadNum = ReadNum, ThisFilter = ThisFilter,$
	OverRide = OverRide, TvFlag = TvFlag, AvgEntireFrame = AvgEntireFrame, $
	ExamineEachChannel = ExamineEachChannel, SubtractDark = SubtractDark, $
	BiasSub = BiasSub, SkipRow = SkipRow, Mode = Mode

  If N_Elements(SkipRow)      eq 0 then SkipRow = 0
  If N_Elements(SubtractDark) eq 0 then SubtractDark = 1
  If N_Elements(Mode)         eq 0 then Mode = 0

  Common KeyParams, KeyStr
  Common ColorStr, Color, ColorNum, ColorTri, ColorNumTri
  Common FuncXY, T, S

  ;Get all of the defaults for the keywords from the KeywordStruct file
  @KeywordStruct.pro
  @PlotSettings.pro

  For X=200, 2500 do begin
    Y = 1400
    Fits_Read_DataCube, FitsFiles, Im, FitsHeader, $
	XStart=X, XStop = X, $
	YStart=Y, YStop = Y  
    FileParams    = Return_File_Params(FitsFiles)
    SlopeStr      = Return_Slope_Params(FitsHeader)
  
    T = SlopeStr.ReadTimes
    S = Im(1:KeyStr.NReads-1)+Im(0)  

    P = Amoeba(1.0e-5,scale=[15000.,15000.,0.04],p0=[35000.,20000.,0.005],$
        function_value=fval,function_name='dark_current_fit_function',$
    	ncalls=tries,nmax=1000000.)
    plot,  T, S, yrange = [min(S),max(S)], background=fsc_color('white'), $
           color=fsc_color('black')
    oplot, T, p(0)-p(1)*exp(-p(2)*T), color=fsc_color('black')
    stop

    ADUMax  = 40000.
    ValidX  = (ADUMax-S(0:KeyStr.NReads-3))
    ValidY  = (S(1:KeyStr.NReads-2)-S(0:KeyStr.NReads-3))/SlopeStr.FrameTime
   
    SN   = double(N_Elements(ValidX))
    Sx  = Total(ValidX)
    Sy  = Total(ValidY)
    Sxx = Total(ValidX^2)
    Sxy = Total(ValidX*ValidY)

    ;Assume bright objects will have a large difference for all of 
    ;the consecutive reads in the linear A/D range and don't reject
    ;Get the slope values and the difference of each point from the fit
    Delta  = SN*Sxx - Sx*Sx
    m      = (SN*Sxy - Sx*Sy)/Delta
    b      = (Sxx*Sy-Sx*Sxy)/Delta
 
    plot, ValidX, ValidY, xtitle = Strtrim(ADUMax)+' - Signal(ADU)', $
	  ytitle = 'S!Li+1!N -S!Li!N', background=fsc_color('white'), $
	  color = fsc_color('black')
    oplot, ValidX, M*ValidX+B, color=fsc_color('black')
   
    xyouts, 0.2,0.8,'M = '+strtrim(M,2), color=fsc_color('black'), $
	  /normal
    xyouts, 0.2,0.7,'B = '+strtrim(B,2), color=fsc_color('black'), $
          /normal

    stop
   EndFor
End 
