;new_xy_offsets_cn_depth
;
;PURPOSE:
;	This script will write the correlations to files in the stats_directory
;	
;INPUTS:
;
;	[stats_directory]/*5132*offsets.txt
;
;OUTPUTS:
;
;	[stats_directory]/x_sub_correlations.txt
;	[stats_directory]/y_sub_correlations.txt
;

pro new_cn_avg_sub_output_files,Date=Date,stats_directory=stats_directory,$
	x_center_file=x_center_file,y_center_file=y_center_file,$
	depth=depth


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats'
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=1
If (not keyword_set(postscript_dir)) then postscript_dir='postscripts'

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/'
;ARRAYS OF FILES
offset_files=file_search(stats_directory+'*5132*offsets.txt',count=num_stats_files)
average_offset_file=stats_directory+'average_offsets.txt'
readcol,average_offset_file,x_off_avg,y_off_avg,format='x,f,f,'

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

;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,depth)
Cxl_full=fltarr(num_stats_files,num,depth)
Cyt_full=fltarr(num_stats_files,num,depth)
Cyl_full=fltarr(num_stats_files,num,depth)


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

	;Take the results from the offset.txt files and fill the cube, subtracting the average 
	;offset
	readcol,offset_files(file_number),x_offset,y_offset,format='X,X,f,X,f'
	not_fitted=where(x_offset eq -10,complement=fitted)
	;x_offset(fitted)=x_offset(fitted)-x_off_avg(fitted)
	;y_offset(fitted)=y_offset(fitted)-y_off_avg(fitted)

	;Obtain the difference ordered in COLUMN, ROW between centroid and grid 	point
	x_offset_hor(file_number,*)=x_offset
	y_offset_hor(file_number,*)=y_offset

	;Obtain the difference ordered in ROW, COLUMN
	x_offset_ver(file_number,*)=x_offset((index mod 25)*25+index/25)
	y_offset_ver(file_number,*)=y_offset((index mod 25)*25+index/25)

		
endfor
	
;DATA CUBEs are now in place. Begin the covariance

;Loop through all of the files in the DATE directory
for file_number=0,num_stats_files-depth do begin

   ;Cut off the last part of the file name so we have file name for the correlation files
   offset_path_parts=strsplit(offset_files(file_number),'/',/extract)
   offset_file_ref=strsplit(offset_path_parts(N_elements(offset_path_parts)-1),'offsets.txt',$
                /extract,/regex)
   pre_letter=strsplit(offset_file_ref,'_',/extract)
   pre_number=pre_letter(1)
   pre_letter=pre_letter(0)

   ;Lable the correlation files
   xcor_name=Cor_Directory+offset_file_ref+'x_avg_sub_correlations.txt'
   ycor_name=Cor_Directory+offset_file_ref+'y_avg_sub_correlations.txt'

  ;OPEN FILES TO PRINT THE CORRELATIONS TO
  get_lun,xcor
  openw,xcor,xcor_name
  get_lun,ycor
  openw,ycor,ycor_name

  printf,xcor,FORMAT='(%"%s\t%s\t%s")','Long. Cor.','Trans. Cor.','Number of Pairs'
  printf,ycor,FORMAT='(%"%s\t%s\t%s")','Long. Cor.','Trans. Cor.','Number of Pairs'

  ;CORRELATIONS
  Cxl=fltarr(num,depth)
  Cxt=fltarr(num,depth)
  Cyl=fltarr(num,depth)
  Cyt=fltarr(num,depth)

     for time=0, depth-1 do begin

	for spacing=0, num-1 do begin
	
	  Cxl_arr=fltarr(num*num)
       	  Cxt_arr=fltarr(num*num)
	  Cyl_arr=fltarr(num*num)
	  Cyt_arr=fltarr(num*num)

	  ;SCAN FIRST ALONG THE ROWS
	
	  for column_index=0, num-1 do begin
	     for row_index=0, num -1 do begin
		 spot_index=column_index*num+row_index
			
		 ;If we have reached the edge, start on the next row
		 If (row_index+spacing) ge num then break
		 ;Check to see if both have valid values
		 If (x_offset_hor(file_number,spot_index) ne -10) and $
		    (x_offset_hor(file_number+time,spot_index+spacing) ne -10) $
		    then begin

		    Cxl_arr(spot_index)=x_offset_hor(file_number,spot_index)*$
		    x_offset_hor(file_number+time,spot_index+spacing)
		    Cxt_arr(spot_index)=y_offset_hor(file_number,spot_index)*$
                    y_offset_hor(file_number+time,spot_index+spacing)
		    ;print,FORMAT='(%"%i\t%i\t")',spot_index,spot_index+spacing
	
		 endif

	      endfor
	   endfor
		
	   correlated=where(Cxl_arr ne 0)
	   If correlated(0) ne -1 then begin
		Cxl(spacing,time)=mean(Cxl_arr(correlated))
		Cxt(spacing,time)=mean(Cxt_arr(correlated))
	   endif else begin
		Cxl(spacing,time)=0
		Cxt(spacing,time)=0
	   endelse
		
	   printf,xcor,FORMAT='(%"%f\t%f\t%i")',Cxl(spacing,0),Cxt(spacing,0),N_elements(correlated)
		
	
           ;SCAN SECOND ALONG Y

           for column_index=0, num-1 do begin
               for row_index=0, num -1 do begin
                   spot_index=column_index*num+row_index
	
        	   ;If we have reached the edge, start on the next row
                   If (row_index+spacing) ge num then break
                   ;Check to see if both have valid values
                   If (y_offset_ver(file_number,spot_index) ne -10) and $
                   (y_offset_ver(file_number+time,spot_index+spacing) ne -10) $
		   then begin

                   Cyl_arr(spot_index)=y_offset_ver(file_number,spot_index)*$
                   y_offset_ver(file_number+time,spot_index+spacing)
                   Cyt_arr(spot_index)=x_offset_ver(file_number,spot_index)*$
                   x_offset_ver(file_number+time,spot_index+spacing)
                   ;print,FORMAT='(%"%i\t%i\t")',spot_index,spot_index+spacing

          	   endif

                   endfor
            endfor

            correlated=where(Cyl_arr ne 0)
            If correlated(0) ne -1 then begin
                 Cyl(spacing,time)=mean(Cyl_arr(correlated))
                 Cyt(spacing,time)=mean(Cyt_arr(correlated))
            endif else begin
                 Cyl(spacing,time)=0
                 Cyt(spacing,time)=0
            endelse

            printf,ycor,FORMAT='(%"%f\t%f\t%i")',Cyl(spacing,0),Cyt(spacing,0),N_elements(correlated)

     	  endfor
	
	endfor	

	close,xcor
	free_lun,xcor		
	close,ycor
	free_lun,ycor
	

endfor

end


