;spot_drift
;
;PURPOSE:
;	This script will read in all of the shackhartman data from a series 
;	of images with path given by
;	
;	[Directory]hr5132[letter]_file_number_stats.txt
;
;	and record into a data cube the centroids of the spots
;
;CALLING SEQUENCE
;
;	spot_drift,[Directory=Directory,[letter=letter],[id_array]]]
;
;INPUTS
;
;	Directory	The path to the directory where the Shackhartman data
;			are located
;	letter		The letter included in the files. This ranges from 
;			a-j, corresonding to early-late times
;	id_array	The id numbers (stored in a one-dimensional array)
;			of the points that should be plotted

pro spot_drift_correct, Directory=Directory,letter=letter,id_array=id_array


;set directory path delimiter
    if (!version.os_family eq 'unix') then begin
       delim='/'
    endif else begin
       delim='\'
    endelse

If (not keyword_set(Directory)) then Directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-10/ShackHartman/'
If (not keyword_set(id_array)) then id_array=[300,301,302]
If (not keyword_set(letter)) then letter='a'

threshold=3
spot_count=625

;create an array that has the write size in 1_dimension
;d_size=N_elements(id_array)
;x_offset_array=findgen(d_size)
stack_x=fltarr(spot_count,99)
stack_y=fltarr(spot_count,99)
mag_arr=fltarr(spot_count,99)
sharp_arr=fltarr(spot_count,99)
x_err_arr=fltarr(spot_count,99)
y_err_arr=fltarr(spot_count,99)

for file_number=1,99 do begin                                                                              
	if (file_number lt 10) then filename=Directory+'stats/hr5132'+letter+'_'+$
	string(file_number,format='(I1)')+'_stats.txt'
	if file_number ge 10 and file_number lt 100 then filename=$
	Directory+'stats/hr5132'+letter+'_'+string(file_number,format='(I2)')+'_stats.txt'
	if file_number ge 100 and file_number lt 1000 then filename=$
	Directory+'stats/hr5132'+letter+'_'+string(file_number,format='(I3)')+'_stats.txt'

 
	;Mag.txt has the following
	;,id,x_coord,y_coord,x_err,y_err,mag,sharp,roundness,x_offset,y_offset,
	;format='X,X,X,X,X,X,X,X,f,f'
	readcol,filename,x_coord,y_coord,x_err,y_err,mag,sharp,format='X,f,f,f,f,f,f'

	;create data cube of (x,y) coordinate pairs
	stack_x(*,file_number-1)=x_coord
	stack_y(*,file_number-1)=y_coord
	mag_arr(*,file_number-1)=mag
	sharp_arr(*,file_number-1)=sharp
	x_err_arr(*,file_number-1)=x_err
	y_err_arr(*,file_number-1)=y_err
	
endfor

;Set up arrays to hold data for the mean and variance of each spot
weighted_x_arr=fltarr(spot_count)
weighted_y_arr=fltarr(spot_count)
variance_x_arr=fltarr(spot_count)
variance_y_arr=fltarr(spot_count)
sharp_arr=sharp_arr(where(sharp_arr ne -1))
mag_arr=mag_arr(where(mag_arr ne -1))
x_err_arr_1f=x_err_arr(where(x_err_arr ne -1))
y_err_arr_1f=y_err_arr(where(y_err_arr ne -1))
x_err_arr_2f=[0]
y_err_arr_2f=[0]


;Magnitude Histogram
mag_max=floor(max(mag_arr))
mag_min=floor(min(mag_arr))
mag_bins=40
mag_hist=histogram(mag_arr,nbins=mag_bins,max=mag_max,min=mag_min,locations=magvals)

;Sharpness Histogram
sharp_max=floor(max(sharp_arr))+1
sharp_min=floor(min(sharp_arr))
sharp_bins=40
sharp_hist=histogram(sharp_arr,nbins=sharp_bins,max=sharp_max,min=sharp_min,locations=sharpvals)

valid_index=0


;begin plotting
set_plot,'ps'
                                                                                
device,filename=Directory+'hr5132'+letter+'_reference'+'.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=1.2
!P.THICK=4.

!P.MULTI=[0,1,2]                                                                                
plot,magvals,mag_hist,psym=10,title="Magnitudes of fitted spots for hr5132"+$
	letter+" files 1-100",xrange=[mag_min,mag_max],$
	xtitle="Magnitude",ytitle="Number",background=white,color=black,$
	position=[0.10,0.10,0.9,0.4],charsize=1.10*!P.CHARSIZE

plot,sharpvals,sharp_hist,psym=10,title="Sharpness of fitted spots for hr5132"+$
        letter+" files 1-100",xrange=[sharp_min,sharp_max],$
        xtitle="Sharpness",ytitle="Number",background=white,color=black,$
        position=[0.10,0.5,0.9,0.9],charsize=1.10*!P.CHARSIZE


;plot,x_err_arr,y_err_arr

plot,[200,900],[100,900],/NODATA,title='Plot of ShackHartman Focal Plane',xtitle='X coordinate',ytitle='Y coordinate',background=white,color=000,/xstyle,/ystyle,position=[.05,.05,.9,.9]

;For each spot, go through and calculate its average (x,y) coordinate

for spot_number=0,spot_count-1 do begin

	;Any spots where the x_coord and y_coord are -1 will not be 
	;included.  valid_point_indices are the array indices where this
	;is the case
	valid_point_indices=where((stack_x(spot_number,*) gt 0) and (stack_y(spot_number,*) gt 0))
	
	;print,N_elements(valid_point_indices)	
	;For some spots, there will be no valid fits
	if (N_elements(valid_point_indices) gt threshold)  then begin
                                                                                
		for valid_point_index=0, N_elements(valid_point_indices)-1 do begin
			oplot,[stack_x(spot_number,valid_point_index)],$
              			[stack_y(spot_number,valid_point_index)],$
              			psym=3,color=(float(max(valid_point_indices)-valid_point_index)/max(valid_point_indices)*256)
	
		endfor
	
		weighted_x=mean(stack_x(spot_number,valid_point_indices))
		weighted_y=mean(stack_y(spot_number,valid_point_indices))
		
		variance_x=variance(stack_x(spot_number,valid_point_indices))
		variance_y=variance(stack_y(spot_number,valid_point_indices))
	
		x_err_subarr=x_err_arr(spot_number,valid_point_indices)
		y_err_subarr=y_err_arr(spot_number,valid_point_indices)

		
		weighted_x_arr(valid_index)=weighted_x
        	weighted_y_arr(valid_index)=weighted_y
        	variance_x_arr(valid_index)=variance_x
        	variance_y_arr(valid_index)=variance_y
		x_err_arr_2f=[[x_err_arr_2f],[x_err_subarr]]
		y_err_arr_2f=[[y_err_arr_2f],[y_err_subarr]]
	
		valid_index=valid_index+1
		print,N_elements(valid_point_indices)
	endif 	
	

endfor

weighted_x_arr=weighted_x_arr(where(weighted_x_arr gt 0))
weighted_y_arr=weighted_y_arr(where(weighted_y_arr gt 0))
variance_x_arr=variance_x_arr(where(weighted_x_arr gt 0))
variance_y_arr=variance_y_arr(where(weighted_y_arr gt 0))

oplot,weighted_x_arr,weighted_y_arr,psym=3,color=256

!P.multi=[0,1,2]

plot,x_err_arr_1f,y_err_arr_1f,psym=3,title="Fit error Scatter Plot for hr5132"$
        +letter+" files 1-100 Before Filtering",$
        xtitle="XERR",ytitle="YERR",background=white,color=black,$
        position=[0.10,0.10,0.9,0.4],charsize=1.10*!P.CHARSIZE
                                                                               
plot,x_err_arr_2f,y_err_arr_2f,psym=3,title="Fit error Scatter Plot for hr5132"+$
        letter+" files 1-100 After Filtering",$
        xtitle="XERR",ytitle="YERR",background=white,color=black,$
        position=[0.10,0.5,0.9,0.9],charsize=1.10*!P.CHARSIZE

!P.multi=[0,1,2]

plot,x_err_arr_1f,sharp_arr,psym=3,title="Sharpness Scatter Plot for hr5132"$
        +letter+" files 1-100",$
        xtitle="XERR",ytitle="Sharpness",background=white,color=black,$
        position=[0.10,0.10,0.9,0.4],charsize=1.10*!P.CHARSIZE
                                                                                
plot,y_err_arr_1f,sharp_arr,psym=3,title="Sharpness Scatter Plot for hr5132"+$
        letter+" files 1-100",$
        xtitle="YERR",ytitle="Sharpness",background=white,color=black,$
        position=[0.10,0.5,0.9,0.9],charsize=1.10*!P.CHARSIZE

!p.multi=[0,1,2]

plot,x_err_arr_1f,mag_arr,psym=3,title="Magnitude Scatter Plot for hr5132"$
        +letter+" files 1-100",$
        xtitle="XERR",ytitle="Magnitude",background=white,color=black,$
        position=[0.10,0.10,0.9,0.4],charsize=1.10*!P.CHARSIZE
                                                                                
plot,y_err_arr_1f,mag_arr,psym=3,title="Magnitude Scatter Plot for hr5132"+$
        letter+" files 1-100",$
        xtitle="YERR",ytitle="Magnitude",background=white,color=black,$
        position=[0.10,0.5,0.9,0.9],charsize=1.10*!P.CHARSIZE


device,/close

set_plot,'X'                                                                                
end


