;PHASE_GET_SLOPES_FROM_PHASE
;
;PURPOSE:
;	This script will take in a phase screen generated by turb2d.c (written
;	by Garret Jernigan) and obtain the slopes via a finite centered 
;	difference and normalization with the rms of those slopes

;
;CALLING SEQUENCE:
;	phase_out_plot,


pro phase_get_slopes_from_phase,screennum=screennum,low=low,lcut=lcut

if (not keyword_set(screennum)) then screennum=0.0
If (not keyword_set(low)) then low=100.5
If (not keyword_set(lcut)) then lcut=10000.0
phase_dim=1024
slopes=0;

lowstr=strtrim(string(low,format='(%"%10.1f")'),2)
lcutstr=strtrim(string(lcut,format='(%"%10.1f")'),2)
screenumstr=strtrim(string(screennum,format='(%"%4.1f")'),2)

atmosphere_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/lsst_sim/atmosphere/'
color_screen=indgen(phase_dim,phase_dim,3)

openr,phase,atmosphere_dir+'phase_'+strtrim(lowstr,2)+'_'+$ 
      strtrim(lcutstr,2)+'_'+strtrim(screenumstr,2)+$
      '.out',/get_lun


xslopes=fltarr(phase_dim,phase_dim)
yslopes=fltarr(phase_dim,phase_dim)

openr,see,atmosphere_dir+'see_'+strtrim(lowstr,2)+'_'+$
      strtrim(lcutstr,2)+'_'+strtrim(screenumstr,2)+$
      '.out',/get_lun

  readcol,atmosphere_dir+'see_'+strtrim(lowstr,2)+'_'+$
      strtrim(lcutstr,2)+'_'+strtrim(screenumstr,2)+$
      'x.out',x_slopes,format='f'

  readcol,atmosphere_dir+'see_'+strtrim(lowstr,2)+'_'+$
      strtrim(lcutstr,2)+'_'+strtrim(screenumstr,2)+$
      'y.out',y_slopes,format='f'

  slopes=read_binary(see,data_dims=[phase_dim,phase_dim],data_type=4)
  screen=read_binary(phase,data_dims=[phase_dim,phase_dim],data_type=4)

yslopes(1:1022,1:1022)=(screen(2:1023,1:1022)-screen(0:1021,1:1022))/2
xslopes(1:1022,1:1022)=(screen(1:1022,2:1023)-screen(1:1022,0:1021))/2

yslopes=xslopes/sqrt(mean(xslopes^2))
xslopes=yslopes/sqrt(mean(yslopes^2))

max_screen=max(screen)
min_screen=min(screen)

screen_val=floor(255*((screen-min_screen)/(max_screen-min_screen)))

set_plot,'x'

loadct,36
tvlct,R,G,B,/get

color_screen(*,*,0)=r(screen_val)
color_screen(*,*,1)=g(screen_val)
color_screen(*,*,2)=b(screen_val)

tv,congrid(color_screen,1024,1024,3),true=3

stop

end

