;PURPOSE:
;
;To make two plots that illustrate the difference between two methods 
;of slope estimation in up-the-ramp-sampling

pro cmos_linearity, TvFlag = TvFlag

If N_Elements(TvFlag) eq 0 then TvFlag = 1
@PlotSettings.pro
erase
screensize= get_screen_size()
If TvFlag eq 1 then window, xsize=screensize(0)-100,$
     ysize=screensize(1)-100
!p.charsize=3.25
!p.charthick=3.25
!p.thick=5
!x.thick=3.25
!y.thick=3.25

Time    = [0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0]
Signal  = [0,0.5,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]
NPoints = N_Elements(Time)

!p.multi=[0,1,2]
plot, Time, Signal, color=fsc_color('black'), $
      background = fsc_color('white'), $
      xtitle = 'Read Number', ytitle = 'Signal'

;Method One: DeltaY=(Y-Y_o)/(T-T_o)
Slopes1 = float(Signal(1:NPoints-1)-Signal(0))/(Time(1:NPoints-1)-Time(0))

;Method Two: DeltaY=(Y_i+1-Y_i)/(T_i+1-T_i) -- Forward Difference
Slopes2 = float(Signal(1:NPoints-1)-Signal(0:NPoints-2))/$
               (Time(1:NPoints-1)-Time(0:Npoints-2))

;Method Three:  -- DeltaY = (Y_i+1-Y_i-1)/(T_i+1-T_i-1) --Centered Difference
;		with Forward and Backward difference near boundaries
Slopes3 = Fltarr(NPoints)
Slopes3(0)           = float(Signal(1)-Signal(0))/(Time(1)-Time(0)) 
Slopes3(1:NPoints-2) = float(Signal(2:NPoints-1)-Signal(0:NPoints-3))/$
               (Time(2:NPoints-1)-Time(0:Npoints-3))
Slopes3(NPoints-1)   = float(Signal(NPoints-1)-Signal(Npoints-2))/$
			    (Time(Npoints-1)-Time(NPoints-2))

;Plot the three methods of estimation
plot,[0],[0],/nodata,xtitle='Read Number', ytitle='Slope',$
     color=fsc_color('black'), yrange = [0,2.5], $
     xrange=[Time(0), Time(NPoints-1)]
oplot, Time(1:Npoints-1),Slopes1, color=fsc_color('red'),psym=1
Slopes2 = float(Signal(1:NPoints-1)-Signal(0:NPoints-2))/$
	       (Time(1:NPoints-1)-Time(0:Npoints-2))
oplot, Time(1:NPoints-1),Slopes2, color=fsc_color('blue'),psym=5
oplot, Time, Slopes3, color=fsc_color('green'),psym=4
SItems = ['Global', 'FFD', 'FBD, FCD, FFD']
SPsym  = [1,5,4]
SCol   = [fsc_color('red'), fsc_color('blue'),fsc_color('green')]
TCol   = [fsc_color('black'), fsc_color('black'),fsc_color('black')]
SThi   = [4,4,4]
SLin   = [1,1,1]
legend, SItems, Psym=SPsym, Colors = SCol, TextColors = TCol, $
	pos=[.75,.3],/norm, thick=SThi, linestyle=SLin

PngImg = tvrd(true=1)
tvlct,reds,greens,blues,/get
write_png, 'SlopeEstimation.png', PngImg, Reds,Greens,Blues

end 
