function distgrid_offset, mat, img, xy4x4, iref
;
;
iref = 105
;
imgarr = mat(0,*)
x = mat(3,*)
y = mat(4,*)
ss = where((imgarr eq img) and (x ne 0) and (y ne 0))
ss_ref = where((imgarr eq iref) and (x ne 0) and (y ne 0))
;
xx = x(ss)
yy = y(ss)
nn = n_elements(xx)*1.
;
xx_ref = x(ss_ref)
yy_ref = y(ss_ref)
ss1 = where(abs(xx_ref-min(xx_ref)) lt 2, nn1)
ss2 = where(abs(xx_ref-max(xx_ref)) lt 2, nn2)
ss3 = where(abs(yy_ref-min(yy_ref)) lt 2, nn3)
ss4 = where(abs(yy_ref-max(yy_ref)) lt 2, nn4)
xmin_ref = total(xx_ref(ss1)) / nn1		& xmin_y_ref = total(yy_ref(ss1)) / nn1
xmax_ref = total(xx_ref(ss2)) / nn2		& xmax_y_ref = total(yy_ref(ss2)) / nn2
ymin_ref = total(yy_ref(ss3)) / nn3		& ymin_x_ref = total(xx_ref(ss3)) / nn3
ymax_ref = total(yy_ref(ss4)) / nn4		& ymax_x_ref = total(xx_ref(ss4)) / nn4
xy4x4 = [ymin_x_ref, xmin_y_ref]
;
ss1 = where(abs(xx-min(xx)) lt 2, nn1)
ss2 = where(abs(xx-max(xx)) lt 2, nn2)
ss3 = where(abs(yy-min(yy)) lt 2, nn3)
ss4 = where(abs(yy-max(yy)) lt 2, nn4)
xmin = total(xx(ss1)) / nn1		& xmin_y = total(yy(ss1)) / nn1
xmax = total(xx(ss2)) / nn2		& xmax_y = total(yy(ss2)) / nn2
ymin = total(yy(ss3)) / nn3		& ymin_x = total(xx(ss3)) / nn3
ymax = total(yy(ss4)) / nn4		& ymax_x = total(xx(ss4)) / nn4
;
p_right = n_elements(where(xx gt 800)) / nn	;number of pixels on the right side
p_left  = n_elements(where(xx lt 200)) / nn
p_up    = n_elements(where(yy gt 800)) / nn
p_down  = n_elements(where(yy lt 200)) / nn
;
;print, xmin, xmax, ymin, ymax, xmin_y, xmax_y, ymin_x, ymax_x
;print, xmin_ref, xmax_ref, ymin_ref, ymax_ref, xmin_y_ref, xmax_y_ref, ymin_x_ref, ymax_x_ref
step = 135
out = -1
if (nn1 eq 3) and (p_up lt 0.5) and (p_down lt 0.5) and (xmin gt 150) then begin
    dx = xmin - xmin_ref
    dy = xmin_y - xmin_y_ref
    ;print, img, 'Found Left Edge:   ', dx, dy
end
if (nn2 eq 4) and (p_up lt 0.5) and (p_down lt 0.5) and (xmax lt 850) then begin
    dx = xmax - xmax_ref
    dy = xmax_y - xmax_y_ref
    ;print, img, 'Found right edge:  ', dx, dy
end
if (nn3 eq 3) and (p_left  lt 0.5) and (p_right lt 0.5) and (ymin gt 150) then begin
    dx = ymin_x - ymin_x_ref
    dy = ymin - ymin_ref
    ;print, img, 'Found bottom edge: ', dx, dy
end
if (nn4 eq 4) and (p_left  lt 0.5) and (p_right lt 0.5) and (ymax lt 850) then begin
    dx = ymax_x - ymax_x_ref
    dy = ymax - ymax_ref
    ;print, img, 'Found top edge:    ', dx, dy
end
;
;print, ' '
return, [dx, dy]
end
;------------------------------------------------------------------------------
progver = 'PLOT_GCROSS3B  Ver 1.0
infil = '$MDI_CAL_TXT/distgrid617.gcross
if (n_elements(mat) eq 0) then mat = rd_tfile(infil, 11, 5, /conv)
;
imgarr = mat(0,*)
x = mat(3,*)
y = mat(4,*)
;
u_imgarr = imgarr(uniq(imgarr))
nimg = n_elements(u_imgarr)
;
if (keyword_set(qplot_shifts)) then begin
    range = [-400,400]
    plot,fltarr(2),xrange=range, yrange=range
    for i=1,207,2 do begin
	dxy = distgrid_offset(mat, i)
	oplot, [1,1]*dxy(0), [1,1]*dxy(1), psym=2
    end
    pause
end
;
iref = 105
ss = where(imgarr eq iref)
xref = reform(x(ss), 7, 8)
yref = reform(y(ss), 7, 8)
;
for iimg=1,207,8 do begin
    ss = where(imgarr eq iimg)
    nx = n_elements(where(mat(2,ss) eq mat(2,ss(0))))
    ny = n_elements(where(mat(1,ss) eq mat(1,ss(0))))
    xx = reform(mat(3,ss), nx, ny)
    yy = reform(mat(4,ss), nx, ny)

    ss1 = where((xx ne 0) and (yy ne 0))
    dxy = distgrid_offset(mat, iimg, xy4x4)
    fdx = ((xx(ss1(0)) - dxy(0)) - xy4x4(0) )/135	;coordinate shift to put 2-D array into registration with ref image
    fdy = ((yy(ss1(0)) - dxy(1)) - xy4x4(1) )/135
    idx = fix(fdx + sgn(fdx)*.5) + 3 - (ss1(0) mod nx)
    idy = fix(fdy + sgn(fdy)*.5) + 3 - (ss1(0) /   nx)

    xcomp = shift(xx, idx, idy)
    ycomp = shift(yy, idx, idy)

    dxx = (xcomp(0:6,0:6)-dxy(0)) - xref(0:6,0:6)
    dyy = (ycomp(0:6,0:6)-dxy(1)) - yref(0:6,0:6)
    mag = 100.
    xx2 = xref + dxx*mag
    yy2 = yref + dyy*mag
    tit = infil + ' Image ' + strtrim(iimg,2) + ' Relative to ' + strtrim(iref,2)

    ss2 = where((xcomp ne 0) and (ycomp ne 0))
    plot, xref, yref, psym=3, tit=tit, xtit='X Position', ytit='Y Position'
    oplot, xcomp(ss2)-dxy(0), ycomp(ss2)-dxy(1), psym=2
    for i=0,(nx-1)<6 do for j=0,(ny-1)<7 do begin
	if (xcomp(i,j) ne 0) and (ycomp(i,j) ne 0) then arrow, xref(i,j), yref(i,j), xx2(i,j), yy2(i,j), /data, hsize=-0.45
    end
    plots, [10, mag+10], [40,40], thick=4              & xyouts, 10, 50, 'Size of One Pixel'
    plottime & plottime,0,0,progver & pause
end
;
end
