;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 cmos_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/CMOSSimple.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

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'
;  device,set_resolution=[1000,768]
  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

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

;3 phase CCD
NumChannels   = Naxis1
PixWidth      = 500/NumChannels
DetWidth      = NumChannels*PixWidth
YDetOff       = 150
XDetOff       = 50
;Set up the max time and shift register array
ImMax = Max(ImF)
FrameNo=0L
FrameGap=4.
tmax=float(Naxis1*Naxis2*2)
ShiftReg=fltarr(1,Naxis2)

for ReadNum = 0, 1 do begin
 ;EXPOSURE
 for Row = 0, Naxis2 - 1 do begin
  for Col = 0, Naxis1 - 1 do begin 
    ;Increase the time
    t = ReadNum*Naxis1*Naxis2+Naxis1*Row + Col
    erase,!p.color
    !p.noerase=1
    ;ClockLines
    Plot, [0],[0],/nodata,xrange=[0,800],yrange=[0,800],xstyle=4,ystyle=4,$
      background = fsc_color('white'), $
      /normal, position = [0,0,1,1]
    NumClockLines = 3*Naxis1
    For CL = 0, Naxis1 - 1 do begin
      XHClock = (Fltarr(PixWidth)+1)*CL*PixWidth+XDetOff+.5*PixWidth
      YHClock = Findgen(PixWidth)+DetWidth+YDetOff
      YHSel   = Findgen(PixWidth)+YDetOff-PixWidth
      XVClock = Findgen(PixWidth)+DetWidth+XDetOff
      YVClock = (Fltarr(PixWidth)+1)*CL*PixWidth+YDetOff+.5*PixWidth
   
      oplot, XHClock, YHSel, $
            color = Colors(Cl mod naxis1 eq Col), thick = 2
      oplot, XVClock, YVClock, $
	    color = Colors(2*(Cl mod naxis2 eq Row)), thick = 2

      OutBusX = [XDetOff, XDetOff+DetWidth+1.5*PixWidth]
      OutBusY = [YDetOff-PixWidth, YDetOff-PixWidth]
      oplot, OutBusX, OutBusY, color = fsc_color('black'), thick = 2
    EndFor

    Im(*,*)=(float(t)/tmax)*ImF
    ShiftReg(*)=0
    tvimage,bytscl(congrid(ceil(Im),DetWidth,DetWidth,/center),min=0,max=ImMax),$
    XDetOff,YDetOff,/device,/tv 
    xyouts,50,750,'Exposing and Reading...',/device, $
	color=fsc_color('black')
    xyouts,50,700,'Read Number: ' + Strtrim(ReadNum,2), $ 
	/device,color=fsc_color('black')
    xyouts,DetWidth+PixWidth,700,'Row Select'  , $
	/device, color=fsc_color('green')
    xyouts,650,80,'ADU='+strtrim(long(Im(Col,Row)),2),/device,$
        color=fsc_color('black')
    xyouts,DetWidth+PixWidth,750,'Column Select',$
	/device,color=fsc_color('blue')

    if TvFlag eq 1 then wait, .2
    if TvFlag eq 2 then begin
      Scene=tvrd(true=1)
      for i=0,FrameGap do begin
        mpeg_put,Movie,frame=(FrameNo*FrameGap+i),$
        /order,window=0
      endfor
    endif
    FrameNo = FrameNo + 1
  endfor
 endfor
endfor

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

stop   
end

