;Pro CCD_Operation
;
;Purpose:
;	This script makes a movie of a CCD shifting it's charge to the 
;	output amplifier.
;
;KEYWORDS:
;	TvFlag:
;		1 - Display on the XWindow
;		2 - Make a Movie
;	REALSTAR
;		0/1- Don't/Do use a real star image
pro ccd_operation,tvflag=tvflag, RealStar = RealStar

If N_Elements(TvFlag) eq 0  then TvFlag = 1
If N_Elements(RealStar) eq 0 then RealStar = 1

If RealStar then begin 
  FitsDir='~/Desktop/IDL/idl_detectors/'
  FitsFileName=FitsDir+'Star2.fits'
  fits_read,FitsFileName,ImF,header1
  Naxis1 = long(SxPar(Header1,'Naxis1'))
  Naxis2 = long(SxPar(Header1,'Naxis2'))
  MpegDir=FitsDir
  MpegName='/Users/lances/Desktop/CCDSimple.mpg'
  Im = Fltarr(Naxis1, Naxis2)
  ImF = Float(ImF)

EndIf Else Begin

  Naxis1=5
  Naxis2=5
  Im=fltarr(Naxis1,Naxis2)
  ImF=fltarr(Naxis1,Naxis2)
  ImF(*)=findgen(Naxis1,Naxis2)

EndElse

Colors = [fsc_color('red'), fsc_color('blue'), fsc_color('green')] 

;Set up the max time and shift register array
ImMax = Max(ImF)
FrameNo=long(0)
FrameGap=4.
tmax=float(Naxis1*Naxis2*2/10)
ShiftReg=fltarr(1,Naxis2)

if TvFlag eq 1 then begin
  set_plot,'X' 
  window,xsize=800,ysize=800
  device,decompose=1
  erase,!p.color
endif else begin
  set_plot,'X'
  erase,!p.color
  window,0,xpos=1024,ypos=500,xsize=800,ysize=800
  device,decompose=1
  Movie=Mpeg_Open([900,900],quality=100)

endelse
!p.charsize=3
!p.charthick=2

;EXPOSURE
for t=0.,tmax do begin
   Im(*,*)=(float(t)/tmax)*ImF
   ShiftReg(*)=0
   erase,!p.color
   ImDisp = congrid(Im, 500, 500)
   tv,bytscl(ImDisp,min=0,max=ImMax),$
      50,100,/device
   tvimage,bytscl(congrid(ShiftReg,50,500,/center),min=0,max=ImMax),$
      650,100,/device,/tv
   xyouts,625,50,'ADU=',/device,color=fsc_color('black')
   xyouts,50,700,'Exposing...',/device,color=fsc_color('black')
   xyouts,50, 650,'Shutter Open',/device,color=fsc_color('black')
   if TvFlag eq 1 then wait, .2
   if TvFlag eq 2 then begin
     Scene=tvrd()
     for i=0,FrameGap do begin
       mpeg_put,Movie,IMAGE=Scene,frame=(FrameNo*FrameGap+i),$
        /order
     endfor
   endif
   FrameNo=FrameNo+1
endfor

;READOUT
for VerShift=0, Naxis1-1 do begin
  if VerShift lt Naxis1-1 then begin
    Im(0:VerShift,*)=0
    Im(VerShift+1:Naxis1-1,*)=ImF(0:Naxis1-VerShift-2,*)
    ShiftReg(0,*)=ImF(Naxis1-Vershift-1,*)
    ShiftRegF=ImF(Naxis1-VerShift-1,*)
  endif else begin
    Im(0:Vershift,*)=0
    ShiftReg(0,*)=ImF(0,*)
    ShiftRegF=ImF(0,*)
  endelse
  for HorShift=0, Naxis2 do begin
   if HorShift lt Naxis2 then begin
     ShiftReg(0,Naxis2-HorShift-1:Naxis2-1)=0
     ShiftReg(0,0:Naxis2-HorShift-1)=ShiftRegF(0,HorShift:Naxis2-1)
   endif else begin
     ShiftReg(*,*)=0
   endelse
   erase,!p.color
   tv,bytscl(congrid(Im,500,500,/center),min=0,max=ImMax),$
      50,100,/device
   tv,bytscl(congrid(ShiftReg,50,500,/center),min=0,max=ImMax),$
      650,100,/device
   xyouts,50,700,'Readout...',/device,color=fsc_color('black')
   xyouts,50,650,'Shutter Closed',/device,color=fsc_color('black')
   if HorShift gt 0 then begin
      xyouts,625,50,'ADU='+strtrim(long(ShiftRegF(0,HorShift-1)),2),/device,$
	color=fsc_color('black')
   endif else begin
      xyouts,625,50,'ADU=',/device,color=fsc_color('black')
   endelse
   if TvFlag eq 1 then wait, .2
   if TvFlag eq 2 then begin
     Scene=tvrd()
     for i=0,FrameGap do begin
       mpeg_put,Movie,IMAGE=Scene,frame=(FrameNo*FrameGap+i),$
        /order
     endfor
   endif
   FrameNo=FrameNo+1.
  endfor
endfor

If TvFlag eq 2 then begin
  mpeg_save,Movie,FILENAME=MpegName
  mpeg_close,Movie
EndIf

stop   
end

