;fits_star_movie
;
;PURPOSE:
;	This script will return the difference in coordinates between a 
;	ShackHartman image centroid and the corresponding point on the 
;	reference grid
;
;	Since the center of the grid seems to be drifting 

pro fits_star_movie, FitsFileName= FitsFileName, FitsDir = FitsDir,$
		     MPEGDir = MPEGDir

;DIRECTORIES
If not(keyword_set(FitsDir)) then FitsDir = '/u1/kpno/'
If not(keyword_set(MPEGDir)) then MPEGDir = '/u1/kpno/'
If not(keyword_set(FitsFileName)) then FitsFileName = $
	'84737__H4RG_SIPIN_OneWindow400_Reads_Apr25_2007_21_31_07.fits'
FitsPath = FitsDir+FitsFileName
fits_read, FitsPath, im , h, /header_only
NAXIS1=sxpar(h,'NAXIS1')
NAXIS2=sxpar(h,'NAXIS2')
NAXIS3=sxpar(h,'NAXIS3')
NumPixPerFrame = NAXIS1*NAXIS2

;MOVIE PARAMETERS
;*****************                 
FitsFileBase = (strsplit(FitsFileName, '.',/extract))[0]               
Mpegname=MPEGDIR+FitsFileBase+'.mpeg'
Movie=mpeg_open([512,512], quality = 100)
FrameNo=0
FrameGap=10

;CONSTANTS
;*****************

set_plot,'X'

for ReadNum = 0, NAXIS3-1 do begin

  print, 'Procesing File Number ' + strtrim(ReadNum, 2)

  ;Read in the Image Frame
  Fits_Read, FitsPath, im ,$
	first=long(ReadNum*NumPixPerFrame), $
	last=long((ReadNum+1)*NumPixPerFrame)-1

	;Make the image viewable by doing +/- 3 sigma
	StdDevIm = stddev(Im)

	Im = reform(float(Im), NAXIS1, NAXIS2)
	MovieIm = bytscl(congrid(Im,512,512), $
		  min = min(Im), $
		  max = max(Im))

	;Fill in the Gaps
	for i = 0 , FrameGap do begin
	  Mpeg_Put, Movie, IMAGE = MovieIm, Frame = ReadNum + i
	endfor
		
endfor

mpeg_save, Movie, FILENAME= MpegName
mpeg_close, Movie

end

