
;r=tri_surf(z,x,y,gs=[grd_sz,grd_sz],/extrapolate,bounds=bounds)
;r=min_curve_surf(z,x,y,gs=[grd_sz,grd_sz],bounds=bounds)
;x_mx_idx=where(x eq x_mx)
;x_mn_idx=where(x eq x_mn)
;y_mx_idx=where(y eq y_mx)
;y_mn_idx=where(y eq y_mn)
;x_plot=findgen(s_r(1))*grd_sz+x_mn
;y_plot=findgen(s_r(2))*grd_sz+y_mn

;w_x=x_mx-x_mn
;w_y=y_mx-y_mn
;width=fix(10*max(w_x,w_y))/10.
;up_x=fix(10*x_mx)/10.
;up_y=fix(10*y_mx)/10.
;down_x=up_x-width
;down_y=up_y-width

;l_px=min(py)
;l_py=s_p(1)-max(px)
;h_px=max(py)
;h_py=s_p(1)-min(px)

;image_rp=rotate(image_p,3)
;image_rs=rotate(image_s,3)

;contour,r,x_plot,y_plot,xrange=[down_x,up_x],yrange=[down_y,up_y],xstyle=1,ystyle=1,$
;  levels=[-0.8,-0.6,-0.4,-0.2,0.0,0.2,0.4,0.6,0.8,1.0,1.2],c_labels=[1,1,1,1,1,1,1,1,1,1,1],/nodata

;contour,r,x_plot,y_plot,xrange=[down_x,up_x],yrange=[down_y,up_y],xstyle=1,ystyle=1,$
;  levels=[-0.8,-0.6,-0.4,-0.2,0.0,0.2,0.4,0.6,0.8,1.0,1.2],c_labels=[1,1,1,1,1,1,1,1,1,1,1],/noerase

;tvscl,image_rp(l_px:h_px,l_py:h_py),!x.window(0),!y.window(0),xsize=!x.window(1)-!x.window(0),ysize=!y.window(1)-!y.window(0),/norm

;   c(i,j)=total(d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)*d_a)
;wset,3
;     image(state.oldx-state.master_col_range,state.oldy-state.master_row_range:state.oldy+state.master_row_range)=max(image)
;     image(state.oldx+state.master_col_range,state.oldy-state.master_row_range:state.oldy+state.master_row_range)=max(image)
;     image(state.oldx-state.master_col_range:state.oldx+state.master_col_range,state.oldy-state.master_row_range)=max(image)
;     image(state.oldx-state.master_col_range:state.oldx+state.master_col_range,state.oldy+state.master_row_range)=max(image)
;   c_b(i:i+s_a(1)-1,j:j+s_a(2)-1)=a
;   print,i,'  ',j,'  ', total(b(i:i+s_a(1),j:j+s_a(2))*a),total(b(i:i+s_a(1),j:j+s_a(2))^2),c(i,j)
;   wset,0
;   tvscl,rebin(b,5*s_b(1),5*s_b(2))
;   wset,1
;   tvscl,rebin(c_b,5*s_b(1),5*s_b(2))
;   wset,2
;   tvscl,rebin(c,5*s_c(1),5*s_c(2))

;   c(i,j)=total(d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)*d_a)
;   if j eq 19 then begin
;     display_image=d_b
;     display_image(i:i+s_a(1)-1,j:j+s_a(2)-1)=display_image(i:i+s_a(1)-1,j:j+s_a(2)-1)+d_a
;     tvscl,rebin(display_image,460,460)
;   xyouts,0.1,0.95,'c = '+strcompress(string(c(i,j)),/remove_all),/normal,charsize=2.0
;     print,c(i,j)
;   endif
;secondary_x=primary_x+i_max+0.5d0
;secondary_y=primary_y+j_max+0.5d0
;print,'get_control:',primary_x,i_min,secondary_x
;print,primary_y,j_min,secondary_y
;   c(i,j)=total((d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)-d_a)^2)
;   c(i,j)=total((d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)-d_a)^2/(d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)+d_a)^2)
;   if denom ne 0d0 then c(i,j)=total(num/denom) else c(i,j)=high_number
;   denom=(d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)+d_a)^2
;   num = (d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)-d_a)^2
;   n_idx=where(num eq 0d0,n_num)
;   if n_num ne 0d0 then num(n_idx) = high_number
;   new_d_b=d_b(i:i+s_a(1)-1,j:j+s_a(2)-1)
;  c_b=dblarr(s_b(1),s_b(2))
;   c(i,j)=total(num/denom)
;xyouts,0.2,0.8,strcompress(string(k),/remove_all),charsize=2.0,charthick=2.0
;xyouts,0.2,0.8,strcompress(string(k),/remove_all),charsize=2.0,charthick=2.0
;xyouts,0.2,0.8,strcompress(string(k),/remove_all),charsize=2.0,charthick=2.0
