;PHASE_OUT_PLOT
;
;PURPOSE:
;	This script will take in a phase screen generated by turb2d.c (written
;	by Garret Jernigan) and generate a color image of that screen.  It also
;	takes the slope of the wavefronts in the x and y direction as listed
;	in the see.x and see.y files and plots them as a field.
;
;	It will also compute the auto-correlation for the available angles on 
;	the ShackHartman grid from the see.x and see.y wavefront slopes.  
;	A plot will be generated of the correlations.
;
;CALLING SEQUENCE:
;	phase_out_plot,[date,[windspeed,[depth]]]
;
;KEYWORDS:
;	Date: The date (10,11, or 12) of the directory to which the stats will
;	      be written
;	Windspeed: The speed at which the frozen screen will be dragged across 
;		   the screen.  The units 


pro seeing_backwards_tv,Date=Date,xwindspeed=xwindspeed,ywindspeed=ywindspeed,$
                   depth=depth,k=k,phase_dim=phase_dim

If (not keyword_set(Date)) then Date='11'
If (not keyword_set(xwindspeed)) then xwindspeed=0
If (not keyword_set(ywindspeed)) then ywindspeed=0
If (not keyword_set(depth)) then depth=1
If (not keyword_set(k)) then k=100
If (not keyword_set(phase_dim)) then phase_dim=2048

max_windspeed=max([xwindspeed,ywindspeed])
;DIMENSIONS OF PHASE SCREEN AND SUB_SECTION OF SCREEN
sub_sec=256
offset=sub_sec/2-12
grid_spots=625
num=25
num_of_cor=(phase_dim-sub_sec)/max_windspeed

;FILE IO
color_screen_sub=indgen(sub_sec,sub_sec,3)
stats_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
	'/ShackHartman/coo/'
atmosphere_dir='/afs/slac.stanford.edu/u/ki/lances/lsst_sim/atmosphere/'
image_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/ShackHartmann_Cal/'
Postscript_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
        '/ShackHartman/postscripts/'

color_screen=indgen(phase_dim,phase_dim,3)
openr,phase,atmosphere_dir+'phase.out',/get_lun
;screen=read_binary(phase,data_dims=[phase_dim,phase_dim],data_type=4)
screen=fltarr(phase_dim,phase_dim)
readu,phase,screen
close,phase

;GET THE WAVEFRONT SLOPES. 
;They are stored as Columns in see.x and see.yN
readcol,atmosphere_dir+'see_'+strtrim(phase_dim,2)+'_'+strtrim(k,2)+'.x',x_slopes,format='f'
readcol,atmosphere_dir+'see_'+strtrim(phase_dim,2)+'_'+strtrim(k,2)+'.y',y_slopes,format='f'
readcol,image_dir+'TestCalImage_stats.txt',x_grid_coor,y_grid_coor,$
				        format='x,f,f'

;TURN THESE SLOPES INTO 2-d arrays
see_length=n_elements(x_slopes)
see_index=lindgen(see_length)
see_sub_dim=sqrt(see_length)
x_slopes_arr=fltarr(see_sub_dim,see_sub_dim)
y_slopes_arr=fltarr(see_sub_dim,see_sub_dim)
x_slopes_arr(see_index mod see_sub_dim, see_index/see_sub_dim)=$
	x_slopes(see_index)
y_slopes_arr(see_index mod see_sub_dim, see_index/see_sub_dim)=$
	y_slopes(see_index)

;GET MIN AND MAX OF SCREEN AND CONVERT TO 0-255 FOR COLOR PURPOSES
max_screen=max(screen)
min_screen=min(screen)
screen_val=floor(255*((screen-min_screen)/(max_screen-min_screen)))

;PULL OUT THE FIDUCIAL SPOTS ACROSS THE SOAR APERTURE
readcol,stats_dir+'fiducial_reference.txt',spots,format='x,x,f'
spot_arr=intarr(25,25)
spot_arr(spots)=130
complement,findgen(625),spots,non_spots

;PLOTTING PARAMS
set_plot,'X'
white='FFFFFF'x
black='000000'x
!P.CHARSIZE=.7
!P.THICK=.8
loadct,36
tvlct,R,G,B,/get
window,xsize=1024,ysize=536

;GENERATE COLOR VALUES FOR THE PHASE SCREEN 
color_screen(*,*,0)=r(screen_val)
color_screen(*,*,1)=g(screen_val)
color_screen(*,*,2)=b(screen_val)

;MAKE SUB ARRAY FOR SLOPES ACROSS SUBAPERTURE
x_slopes_sub_arr=fltarr(25,25)
y_slopes_sub_arr=fltarr(25,25)

;WINDSPEED TO MOVE PHASE SCREEN ACROSS SUBAPERTURE
windspeed=5

for increment=0,num_of_cor-1 do begin
  
  ;CORRELATIONS 
  x_segment=increment*xwindspeed
  y_segment=increment*ywindspeed
  erase
  !p.noerase=1
  
  ;PHASE SCREEN
   color_screen_sub[*,*,0]=color_screen[$
	           phase_dim-sub_sec-x_segment:phase_dim-x_segment-1,$
    		   phase_dim-sub_sec-y_segment:phase_dim-y_segment-1,0]
  color_screen_sub[*,*,1]=color_screen[$
	 	   phase_dim-sub_sec-x_segment:phase_dim-x_segment-1,$
                   phase_dim-sub_sec-y_segment:phase_dim-y_segment-1,1]
  color_screen_sub[*,*,2]=color_screen[$
	           phase_dim-sub_sec-x_segment:phase_dim-x_segment-1,$
                   phase_dim-sub_sec-y_segment:phase_dim-y_segment-1,2]

  ;SOAR APERTURE
  color_screen_sub[offset:offset+24,offset:offset+24,0]=$
    color_screen_sub[offset:offset+24,offset:offset+24,0]+spot_arr
  tv,congrid(color_screen_sub,512,512,3,/center),0,0,true=3,/normal

  ;SLOPES
  x_slopes_sub_arr(*,*)=x_slopes_arr[$
	  phase_dim-offset-x_segment:phase_dim-x_segment-1-offset+num,$
          phase_dim-offset-y_segment:phase_dim-y_segment-1-offset+num]
  y_slopes_sub_arr(*,*)=y_slopes_arr[$
          phase_dim-offset-x_segment:phase_dim-x_segment-1-offset+num,$
          phase_dim-offset-y_segment:phase_dim-y_segment-1-offset+num]

  
  partvelvec,x_slopes_sub_arr(spots),y_slopes_sub_arr(spots),$
	     x_grid_coor(spots),y_grid_coor(spots),$
	     yrange=[100,820],xrange=[180,880],position=[0.52,.04,.96,.96]

  wait,1.5 
endfor

                                                                                 
stop
                                                                                 
end


