pro Puffy_Events_star_region_no_double_count4

;This script will attempt to locate the events on the H2RG-015 that are attributed
;to electron-hole pairs in the CdZnTe 800 um substrate

;The program first finds all pixels that have a count greater than THRESHOLD and flags them.  Next it
;looks at symmetric regions (star-shaped regions of radius r, where the star approaches a circle as r
;goes to infinity) and checks to see if all the pixels in that region are greater than THRESHOLD.  If so
;that event is logged in logfile and all of the pixels are set to zero so that it will not be double counted.


;CONSTANTS******************************************************.
;Force some to be floating point integers

numpixrd=2048.*2048.             ;Size of device
Array_Dimension=2048.          ;Array Dimension
THRESHOLD=10               ;THRESHOLD to flag pixel as Cosmic Ray
Gauss_width=14                 ;Width of Area to use for Gaussian Fitting
min_sig=float(.6)                ;Minimum standard deviation required to flag event
read_num=123                 ;Number of read to use for ramp difference
large_cosm_ind_x=[0]          ;X Pixel value Array to be Appended
large_cosm_ind_y=[0]          ;Y Pixel value Array to be Appended

;FILENAMES******************************************************

Directory='dktest.16May03'

file_number_1=4
file_number_str_1=string(file_number_1,format='(I1)')

file_number_2=2
file_number_str_2=string(file_number_2,format='(I1)')

temp=37
temp_str=string(temp,format='(I2)')

read_num_tot=125
readsstr=string(read_num_tot,format='(I3)')

file_name_1='dark_'+temp_str+'K_'+readsstr+'_min_1_0'+file_number_str_1+'.fits'
file_name_2='dark_'+temp_str+'K_'+readsstr+'_min_1_0'+file_number_str_2+'.fits'

logfile='c:\H2RG_15_EVENTS\star.txt'
;************************************************************
;Open the file that statistics will be written to and make table header

openw,1,logfile,/append

;Enter the directory that holds the dark current files for the device
cd,'\\Rabbit\raid1\Rockwell4\H2RG-015-5.0mu\cold1\'+Directory

;Vectors for successive reads for first image
fits_read,file_name_1,x11,first=long(read_num*numpixrd),last=long((read_num+1)*numpixrd)-1

;Vectors for successive reads for second image
fits_read,file_name_2,x21,first=long(read_num*numpixrd),last=long((read_num+1)*numpixrd)-1

;For last read, generate image to use for overall events
;if read_num eq (read_num_tot-3) then begin

im1=reform(float(x11),2048.,2048.)
im2=reform(float(x21),2048.,2048.)
diff=float(im1-im2)
diff(where(diff lt 0))=0.

filename='C:\H2RG_15_EVENTS\'+Directory+'_final_read_diff_'+'_'+temp_str+'K_'+readsstr+'Reads_'+file_number_str_1+'_'+file_number_str_2+'.fits'
Index_string=Directory+'_'+temp_str+'K_'+readsstr+'_'+file_number_str_1+'_'+file_number_str_2
writefits,filename,diff

vals_gr_thresh=where(diff gt THRESHOLD,length_vals)

top=where(diff(vals_gr_thresh) gt THRESHOLD and (diff(vals_gr_thresh+2048) gt THRESHOLD))

stop

end