;All_Centroids
;
;This script will obtain every centroid found by IRAF and plot it up

pro routine_All_Centroids_fiducial,Date=Date,coo_dir=coo_dir,exam_date=exam_date

If (not keyword_set(date)) then date='10'
If (not keyword_set(exam_date)) then exam_date='1_18'
If (not keyword_set(coo_dir)) then coo_dir='mag_'+exam_date+'/'

shack_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+date+'/ShackHartman/'
coo_dir=shack_dir+coo_dir
postscript_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/'+exam_date+'_figs/'
image_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/'+exam_date+'_figs/'

coordinate_files=file_search(coo_dir+'*raw.fits.mag.1.txt',count=num_coo_files)
num_of_spots_arr=fltarr(num_coo_files)

;FIDUCIAL-These points will not be included in the analysis
fiducial=[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

;Here is the reference grid I am using to index the spots
num=25
grid_points=625
box=11
x_center=[542]
y_center=[465]
subx=2*indgen(num)-25           ;The x-reference numbers
suby=indgen(num)-12             ;The y-reference numbers

x_ref_col=12.5*subx+x_center[0]
y_ref_col=25.0*suby+y_center[0]

xcoord_arr=[0]
ycoord_arr=[0]

for i=0, num_coo_files-1 do begin 
  xcoords=fltarr(grid_points)
  ycoords=fltarr(grid_points)
  readcol,coordinate_files(i),xcoord,ycoord,format='f,f'
  num_of_spots_arr(i)=N_elements(xcoord)

  for col_num=0,N_elements(y_ref_col)-1 do begin
      for row_num=0,N_elements(x_ref_col)-1 do begin

      ;This index will provide the location on the grid
      ID_index=col_num*N_elements(x_ref_col)+row_num

      ;Check to see if this point is in the fiducial region
      check=where(fiducial eq ID_index)
      If check(0) ne -1 then begin
        found_point=0
      endif else begin
        ;Pick points where x might be near and those where y might be near
        x_index_pos=where(abs(xcoord-x_ref_col(row_num)) lt box)
        y_index_pos=where(abs(ycoord-y_ref_col(col_num)) lt box)
	
        ;Do some indexing gymnastics to determine if they match
        for y_sub_index=0,N_elements(y_index_pos)-1 do begin
          x_sub_index=where(x_index_pos eq y_index_pos(y_sub_index))

          ;The grid coordinates have been matched to a spot
          if ((x_sub_index(0) ne -1) and (y_index_pos(0) ne -1)$
          and (x_index_pos(0) ne -1)) then begin
               x_index=x_index_pos(x_sub_index)
               y_index=y_index_pos(y_sub_index)
               x_ref_dif=xcoord(x_index)-x_ref_col(row_num)
               y_ref_dif=ycoord(y_index)-y_ref_col(col_num)
	       xcoords(ID_index)=xcoord(x_index)
	       ycoords(ID_index)=ycoord(y_index)
	       found_point=1
	       break
	  endif else begin
               found_point=0
          endelse
         endfor
      endelse
      if found_point eq 0 then begin
         xcoords(ID_index)=-1
	 ycoords(ID_index)=-1
      endif
     endfor
  endfor

  xcoords=xcoords(where(xcoords ne -1))
  ycoords=ycoords(where(ycoords ne -1))
  xcoord_arr=[xcoord_arr,xcoords]
  ycoord_arr=[ycoord_arr,ycoords]


endfor

set_plot,'ps'
device,filename=Postscript_Dir+'All_IRAF_centroids_PHOT_'+Date+'new_fiducial.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.


valid_points=where(xcoord_arr ne 0)
N_valid_points=N_elements(valid_points)
xcoord_arr=xcoord_arr(valid_points)
ycoord_arr=ycoord_arr(valid_points)

plot,[0],[0],/nodata,$
       xrange=[140,860],yrange=[100,800],psym=3

for i=float(0),N_valid_points-1 do begin
  oplot,[xcoord_arr(i)],[ycoord_arr(i)],$
       psym=3,color=i/2000
endfor

;Draw the lines
for j=0, num-1 do begin
   left=[x_ref_col(j)-11,x_ref_col(j)-11]
   right=[x_ref_col(j)+11,x_ref_col(j)+11]
   top=[y_ref_col(j)+11,y_ref_col(j)+11]
   bottom=[y_ref_col(j)-11,y_ref_col(j)-11]
   oplot,[0,1000],top
   oplot,[0,1000],bottom
   oplot,left,[0,1000]
   oplot,right,[0,1000]
endfor

plot,num_of_spots_arr,xtitle='File_Number',$
     ytitle='Number Of Spots Found'

device,/close


set_plot,'z'
erase
device, set_font='Courier'
device,set_resolution=[700,600]
!p.charsize=3
!p.charthick=4
!x.thick=2
!y.thick=2
!p.thick=1
!p.noerase=0

device,set_resolution=[1400,1200]

white='FFFFFF'x
black='000000'x
red='FF0000'x
xlow=min(x_ref_col)-box
xhigh=max(x_ref_col)+box
ylow=min(y_ref_col)-box
yhigh=max(y_ref_col)+box

loadct,39

plot,xcoord_arr(where(xcoord_arr ne 0)),ycoord_arr(where(ycoord_arr ne 0)),$
        xrange=[xlow,xhigh],yrange=[ylow,yhigh],psym=3,color=black,$
	background=white,xstyle=1,ystyle=1,$
	xticks=1,yticks=1,xtickv=[220,840],ytickv=[160,780],$
	xtitle='X coordinate (pixels)',ytitle='Y coordinate (pixels)'

;Draw the lines
for j=0, num-1 do begin
   left=[x_ref_col(j)-box,x_ref_col(j)-box]
   right=[x_ref_col(j)+box,x_ref_col(j)+box]
   top=[y_ref_col(j)+box,y_ref_col(j)+box]
   bottom=[y_ref_col(j)-box,y_ref_col(j)-box]
   oplot,[0,1000],top,color=black
   oplot,[0,1000],bottom,color=black
   oplot,left,[0,1000],color=black
   oplot,right,[0,1000],color=black
endfor

jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,image_dir+'All_Centroids_5-'+Date+'_2005_1_06.png',$
jpgimg,reds,greens,blues

stop

end
