;xy_offsets
;
;PURPOSE:
;	This script will obtain the correlations in each image from the 
;	files containing them and plot them up

pro correlation_plots,Date=Date,stats_directory=stats_directory,$
	x_center_file=x_center_file,y_center_file=y_center_file,$
	depth=depth,x_off=x_off,y_off=y_off


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats_temp'
If (not keyword_set(x_center_file)) then x_center_file='x_centers.txt'
If (not keyword_set(y_center_file)) then y_center_file='y_centers.txt'
If (not keyword_set(depth)) then depth=2
If (not keyword_set(postscript_dir)) then postscript_dir='postscripts'
If (not keyword_set(x_off)) then x_off=0
If (not keyword_set(y_off)) then y_off=0

stats_directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
	  '/ShackHartman/'+stats_directory+'/'
postscript_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/'+postscript_dir+'/'
Cor_directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/correlations/'
Raw_Directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/'+'raw/'

;ARRAYS OF FILES
offset_files=file_search(stats_directory+'*5132*offsets.txt',count=num_stats_files)
x_cor_files=file_search(cor_directory+'*x_avg_sub_correlations.txt',count=num_cor_files)
y_cor_files=file_search(cor_directory+'*y_avg_sub_correlations.txt',count=num_cor_files)
raw_files=file_search(Raw_directory+'*5132*.fits',count=num_r_files)

;CONSTANTS
num=25
grid_spots=625
index=findgen(625)

;ARRAY REFERENCE 25x25=625 FOR GRID
subx=2*indgen(num)-25           ;The x-reference numbers
suby=indgen(num)-12             ;The y-reference numbers

;HUGE DATA CUBE
x_offset_hor=fltarr(num_stats_files,grid_spots)
y_offset_hor=fltarr(num_stats_files,grid_spots)
x_offset_ver=fltarr(num_stats_files,grid_spots)
y_offset_ver=fltarr(num_stats_files,grid_spots)

;Correlations
Cxt_full=fltarr(num_stats_files,num)
Cxl_full=fltarr(num_stats_files,num)
Cyt_full=fltarr(num_stats_files,num)
Cyl_full=fltarr(num_stats_files,num)
x_numbers=fltarr(num_stats_files,num)
y_numbers=fltarr(num_stats_files,num)
times=dblarr(num_stats_files)

;FILL THE DATA CUBES
for file_number=0,num_cor_files-1 do begin

	;Take the results from the offset.txt files and fill the cube
        readcol,x_cor_files(file_number),xlong,xtrans,number_xpoints,$
	format='f,f,i'
	readcol,y_cor_files(file_number),ylong,ytrans,number_ypoints,$
	format='f,f,i' 	
	fits_read,raw_files(file_number),unshifted_image,header,/header_only
	uttime=SXPAR(header,'DATE-OBS')
        time_with_year=double(DATE_CONV(uttime,'R'))-double(2005000)

	cxt_full(file_number,*)=xtrans
	cxl_full(file_number,*)=xlong
	cyl_full(file_number,*)=ylong
        cyt_full(file_number,*)=ytrans
	x_numbers(file_number,*)=number_xpoints
	y_numbers(file_number,*)=number_ypoints
	times(file_number)=time_with_year	
	
endfor

sorted_times=sort(times)
cxt_full=cxt_full(sorted_times,*)
cxl_full=cxl_full(sorted_times,*)
cyl_full=cyl_full(sorted_times,*)
cyt_full=cyt_full(sorted_times,*)
x_numbers=x_numbers(sorted_times,*)
y_numbers=y_numbers(sorted_times,*)


set_plot,'ps'
device,filename=Postscript_dir+'Correlations.ps'

loadct,39                       ;load color table 39
device,/color                   ;allow color on the postscript
device,ysize=8.5,/inches        ;Height of plot in y
device,xsize=10.0,/inches        ;Width of plot in x
device,yoffset=1.0,/inches      ;Y position of lower left corner

white='FFFFFF'x
black='000000'x
!P.CHARSIZE=.7
!P.THICK=4.


;**************FOURTH PLOT
!p.multi=[0,5,5]

for n=0,num-2 do begin

        plot,Cxt_full(*,0),Cxt_full(*,n+1),$
	psym=3,title='X Transverse',$
        xtitle="Correlation in "+string(strtrim(0,2)),$
	ytitle="Correlation in "+string(strtrim(n+1,2)),$
        background=white,color=black


endfor

;***************EIGTH PLOT
!p.multi=[0,5,5]

for n=0,num-2 do begin

        plot,Cxl_full(*,0),Cxl_full(*,n+1),$
        psym=3,title='X Longitudinal',$
        xtitle="Correlation in "+string(strtrim(0,2)),$
        ytitle="Correlation in "+string(strtrim(n+1,2)),$
        background=white,color=black

endfor


;***************TWELTH PLOT
!p.multi=[0,5,5]

for n=0,num-2 do begin

        plot,Cyt_full(*,0),Cyt_full(*,n+1),$
        psym=3,title='Y Transverse',$
        xtitle="Correlation in "+string(strtrim(0,2)),$
        ytitle="Correlation in "+string(strtrim(n+1,2)),$
        background=white,color=black

endfor


;**************SIXTEENTH PLOT
!p.multi=[0,5,5]

for n=0,num-2 do begin

        plot,Cyl_full(*,0),Cyl_full(*,n+1),$
        psym=3,title='Y Longitudinal',$
        xtitle="Correlation in "+string(strtrim(0,2)),$
        ytitle="Correlation in "+string(strtrim(n+1,2)),$
        background=white,color=black

endfor


;*************FIFTH PLOT

!p.multi=[0,1,1]
plot,[0,num_stats_files],[-4,10],/nodata,xrange=[0,num_stats_files],$
        xtitle="File Number",ytitle="Average X Transverse Correlation",$
	title='Correlations for 5/'+Date+'/05'
                                                                                         
                                                                                         
for n=0, num-1 do begin
       oplot,findgen(num_stats_files),Cxt_full(*,n),color=10*n,psym=3
endfor

;*************SIXTH PLOT
                                                                                         
!p.multi=[0,1,1]
plot,[0,num_stats_files],[-4,10],/nodata,xrange=[0,num_stats_files],$
        xtitle="File Number",ytitle="Average X Longitudinal Correlation",$
        title='Correlations for 5/'+Date+'/05'

                                                                                         
                                                                                         
for n=0, num-1 do begin
       oplot,findgen(num_stats_files),Cxl_full(*,n),color=10*n,psym=3
endfor

;*************SEVENTH PLOT
                                                                                         
!p.multi=[0,1,1]
plot,[0,num_stats_files],[-4,10],/nodata,xrange=[0,num_stats_files],$
        xtitle="File Number",ytitle="Average Y Transverse Correlation",$
        title='Correlations for 5/'+Date+'/05'

                                                                                         
                                                                                         
for n=0, num-1 do begin
       oplot,findgen(num_stats_files),Cyt_full(*,n),color=10*n,psym=3
endfor

;*************EIGTH PLOT
                                                                                         
!p.multi=[0,1,1]
plot,[0,num_stats_files],[-4,10],/nodata,xrange=[0,num_stats_files],$
        xtitle="File Number",ytitle="Average Y Longitudinal Correlation",$
        title='Correlations for 5/'+Date+'/05'

                                                                                         
                                                                                         
for n=0, num-1 do begin
       oplot,findgen(num_stats_files),Cyl_full(*,n),color=10*n,psym=3
endfor


;*************SIXTH PLOT
!p.multi=[0,1,1]

plot,[0,num_stats_files],[0,440],/nodata,xrange=[0,num_stats_files],$
	xtitle="File Number",ytitle="Number of Points used in Correlation"
	

for n=0, num-1 do begin
       oplot,findgen(num_stats_files),x_numbers(*,n),color=10*n,psym=3
endfor

;************SIXTH PLOT

!p.multi=[0,1,1]
plot,[0,num_stats_files],[0,440],/nodata,xrange=[0,num_stats_files],$
	xtitle="File Number",ytitle="Number of Points used in Correlation"
                                                                                         
for n=0, num-1 do begin
       oplot,findgen(num_stats_files),y_numbers(*,n),color=10*n,psym=3
endfor


device,/close
set_plot,'X'



end


