;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
;
;CALLING SEQUENCE:
;	phase_out_plot,


pro phase_plot_png,Date=Date,$
                   xwindspeed=xwindspeed,ywindspeed=ywindspeed,$
                   depth=depth,k=k,phase_dim=phase_dim,$
                   low=low,lcut=lcut

If (not keyword_set(Date)) then Date='11'
If (not keyword_set(Date)) then Date='11'
If (not keyword_set(xwindspeed)) then xwindspeed=25
If (not keyword_set(ywindspeed)) then ywindspeed=25
If (not keyword_set(depth)) then depth=0
If (not keyword_set(k)) then k=100
If (not keyword_set(phase_dim)) then phase_dim=long(1024)
If (not keyword_set(low)) then low=10.0
If (not keyword_set(lcut)) then lcut=10000.0
                                       
lowstr=strtrim(string(low,format='(%"%10.1f")'),2)
lcutstr=strtrim(string(lcut,format='(%"%10.1f")'),2)
lowinmeters=phase_dim*.17/low
highinmeters=phase_dim*.17/lcut
lowinmeters=strtrim(string(lowinmeters,format='(%"%10.2f")'),2)
highinmeters=strtrim(string(highinmeters,format='(%"%10.4f")'),2)

;DIMENSIONS OF PHASE SCREEN
phase_dim=1024
sub_sec=128
offset=sub_sec/2-12

Postscript_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/phase_screen_figs/'
color_screen_sub=indgen(sub_sec,sub_sec,3)
stats_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
	'/ShackHartman/coo/'
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)+'*.out',/get_lun
screen=read_binary(phase,data_dims=[phase_dim,phase_dim],data_type=4)

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

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

readcol,stats_dir+'fiducial_reference.txt',spots,format='x,x,f'
nonfiducial=[1,2,3,4,5,6,7,8,9,13,14,17,18,19,20,21,22,23,24,25,$
          26,27,28,29,30,31,32,38,39,45,46,47,48,49,50,$
          51,52,53,54,55,63,64,71,72,73,74,75,$
          76,77,78,79,88,89,98,98,99,100,$
          101,102,103,113,114,123,124,125,$
          126,127,128,138,139,149,150,$
          151,152,163,164,174,175,$
          176,177,188,189,200,$
          201,210,213,214,225,$
          226,238,239,249,$
          251,261,262,263,264,265,266,$
          276,286,287,288,289,290,291,$
          301,302,303,304,305,306,307,308,309,$
          310,311,312,313,314,315,316,$
          317,318,319,320,321,322,323,324,325,$
          326,327,336,337,338,339,340,341,$
          351,361,362,363,364,365,366,$
          376,388,389,$
          401,413,414,425,$
          426,427,438,439,449,450,$
          451,452,454,463,464,473,474,475,$
          476,477,478,488,489,498,499,500,$
          501,502,503,504,513,514,522,523,524,525,$
          526,527,528,529,530,538,539,546,547,548,549,550,$
          551,552,553,554,555,556,563,564,570,571,572,573,574,575,$
          576,577,578,579,580,581,582,588,589,594,595,596,597,598,599,600,$
          601,602,603,604,605,606,607,608,609,610,613,614,617,618,619,620,621,$
          622,623,624,625]-1

;Extra Index for the scale offset
complement,findgen(625),nonfiducial,fiducial
fitted=fiducial

spot_arr=intarr(25,25)
spot_arr(fitted)=90

;loadct,39	Good One, black and white at ends, color in middle
;loadct,0 	Grey Scale
;loadct,33	Cool rainbow, no white
loadct,2
tvlct,R,G,B,/get
set_plot,'z'
erase
device, set_font='Courier'
device, set_resolution=[768,900]
!p.charsize=.8
!p.charthick=1.2
!x.thick=2
!y.thick=2
!p.thick=.8
!p.noerase=0
!P.CHARSIZE=1.0
!P.THICK=4.

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

test=0
if test eq 1 then begin
set_plot,'x'
window,xsize=768,ysize=900
endif

segment=80

  color_screen_sub[*,*,0]=color_screen[segment:segment+sub_sec-1,$
    segment:segment+sub_sec-1,0]
  color_screen_sub[*,*,1]=color_screen[segment:segment+sub_sec-1,$
    segment:segment+sub_sec-1,1]
  color_screen_sub[*,*,2]=color_screen[segment:segment+sub_sec-1,$
    segment:segment+sub_sec-1,2]
  color_screen_sub[offset:offset+24,offset:offset+24,0]=$
    color_screen_sub[offset:offset+24,offset:offset+24,0]+spot_arr
  
  backdrop=indgen(768,900)
  backdrop(*)=255
  loadct,0
  tv,backdrop
  !p.noerase=1
  loadct,36
  tv,congrid(color_screen_sub,768,768,3,/center),true=3

  colorbar,range=[0,-min(2*!pi*screen/550e-9)+max(2*!pi*screen/550e-9)],$
	format='(f6.2)',$
	position=[0.1,0.92,0.9,0.97],color=0,$
	title='phase'
if test eq 1 then begin
stop
endif

jpgimg=tvrd()
write_png,postscript_dir+'Phase_Screen_'+lowinmeters+'_'+highinmeters+'.png',$
   jpgimg,R,G,B
  
stop

end

