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
;------------------------------------------------------------------------------
;HISTORY:
;	Written 19-Apr-94
;V1.1	19-Apr-94 (MDM) - Made the output plots square
;V1.2	19-Apr-94 (MDM) - Moved reference location to be the image which is
;			  closest to (512,512)
;
progver = 'PLOT_GCROSS3C  Ver 1.2
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)
;
shift = 1
iref = 105
iref2 = (iref-shift)/8
ss = where(imgarr eq iref)
xref = reform(x(ss), 7, 8)
yref = reform(y(ss), 7, 8)
;
nimg2 = 208/8
;
if (n_elements(iii) eq 0) then begin 		;build fixed 7x8 array
    xmat = fltarr(7, 8, nimg2)
    ymat = fltarr(7, 8, nimg2)
    for iimg=shift,207,8 do begin
	iii = (iimg-shift)/8

	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)

	ix1 = 6 < (nx-1)
	iy1 = 7 < (ny-1)
	xmat(0,0,iii) = xcomp(0:ix1, 0:iy1)
	ymat(0,0,iii) = ycomp(0:ix1, 0:iy1)
    end
end
;
xrange = [0,1000]
yrange=[0,1000]
old_device=!d.(0)
set_plot,'null
plot, fltarr(2), fltarr(2), xrange=xrange, yrange=yrange
set_plot,old_device
mk_square2

qfirst = 1
;for ix=2,2 do for iy=3,3 do begin
for ix=0,5 do for iy=0,6 do begin
    ok = bytarr(nimg2)
    for i=0,nimg2-1 do begin
	dummy1 = min(xmat(ix:ix+1, iy:iy+1, i))
	dummy2 = min(ymat(ix:ix+1, iy:iy+1, i))
	if (dummy1 ne 0) and (dummy2 ne 0) then ok(i) = 1
    end
    ;
    ss = where(ok, nok)
    if (nok gt 5) then begin
	xll = reform(xmat(ix,iy,*))
	xlr = reform(xmat(ix+1,iy,*))
	xul = reform(xmat(ix,iy+1,*))
	xur = reform(xmat(ix+1,iy+1,*))
	yll = reform(ymat(ix,iy,*))
	ylr = reform(ymat(ix+1,iy,*))
	yul = reform(ymat(ix,iy+1,*))
	yur = reform(ymat(ix+1,iy+1,*))
	;
	dx = ((xlr-xll) + (xur-xul) )/2
	dy = ((yul-yll) + (yur-ylr) )/2
	xx = ((xlr+xll)/2 + (xur+xul)/2 )/2
	yy = ((yul+yll)/2 + (yur+ylr)/2 )/2
	tit = infil + ' Box size change relative to ' + strtrim(iref,2) + ' box ' + arr2str([ix,iy])

	plot, xx(ss), yy(ss), psym=3, xrange=xrange, yrange=yrange, $
			tit=tit, xtit='X Position', ytit='Y Position'
	rr = sqrt( (xx(ss)-512)^2 + (yy(ss)-512)^2 )
	dummy = min(rr, iref2)
	iref2 = ss(iref2)
	mag = 20000
	for i=0,nok-1 do begin
	    x0 = xx(ss(i))
	    y0 = yy(ss(i))
	    dx0 = (1- dx(ss(i)) / dx(iref2)) * mag
	    dy0 = (1- dy(ss(i)) / dy(iref2)) * mag
	    if (dx0 gt 0) then begin
		arrow, x0, y0, x0+dx0, y0, /data, hsize=-0.45
		arrow, x0, y0, x0-dx0, y0, /data, hsize=-0.45
	    end else begin
		arrow, x0+dx0, y0, x0, y0, /data, hsize=-0.45
		arrow, x0-dx0, y0, x0, y0, /data, hsize=-0.45
	    end
	    if (dx0 gt 0) then begin
		arrow, x0, y0, x0, y0+dy0, /data, hsize=-0.45
		arrow, x0, y0, x0, y0-dy0, /data, hsize=-0.45
	    end else begin
		arrow, x0, y0+dy0, x0, y0, /data, hsize=-0.45
		arrow, x0, y0-dy0, x0, y0, /data, hsize=-0.45
	    end
	    xcir = cos( findgen(500)/499*2*!pi ) * 20
	    ycir = sin( findgen(500)/499*2*!pi ) * 20
	    if (dx0 eq 0) and (dy0 eq 0) then oplot, x0+xcir, y0+ycir, thick=2
	end
	;
	y0 = 40
	x0 = 10 + mag/100/2.
	dx0 = mag/100/2.
	arrow, x0, y0, x0+dx0, y0, /data, hsize=-0.45
	arrow, x0, y0, x0-dx0, y0, /data, hsize=-0.45
	xyouts, 2*x0, y0, ' == 0.5% change larger'
	y0 = 80
	arrow, x0+dx0, y0, x0, y0, /data, hsize=-0.45
	arrow, x0-dx0, y0, x0, y0, /data, hsize=-0.45
	xyouts, 2*x0, y0, ' == 0.5% change smaller'
	plottime & plottime,0,0,progver & pause

	if (keyword_set(qplot_radial)) then begin
		xxx=xx(ss)
		yyy=yy(ss)
		rrr = sqrt( (xxx-512)^2 + (yyy-512)^2 )
		avgdx = total(dx(ss))/n_elements(ss)
		ddd = (1-dx(ss) / avgdx) * 100
		plot,rrr,ddd,psym=1,xtit='Radius from 512,512 in pixels', ytit='Delta change in %', tit=tit, $
							xrange = [0,500], yrange = [-.3, .3], /ystyle
		coeff = poly_fit(rrr/512., ddd, 1, yfit)
		oplot, rrr, yfit
		xyouts, 10, -.25, 'Slope = ' + string(coeff(1)) + ' % / half CCD'
		plottime & plottime,0,0,progver & pause
		;
		ss0=where(abs(yyy-500) lt 25, nn0)
		if (nn0 gt 3) then begin
			coeff=poly_fit(xxx(ss0), ddd(ss0), 2, yfit)
			cen = -coeff(1)/(2*coeff(2))
			plot, xxx(ss0), ddd(ss0), psym=2, xtit='X position',ytit='Delta change in %', tit=tit
			plottime, 0, .97, 'XCen = ' + string(cen)
			oplot, findgen(800), poly(findgen(800),coeff)
			plottime & plottime,0,0,progver & pause
		end
		;
		ss0=where(abs(xxx-500) lt 25, nn0)
		if (nn0 gt 3) then begin
			coeff=poly_fit(yyy(ss0), ddd(ss0), 2, yfit)
			cen = -coeff(1)/(2*coeff(2))
			plot, yyy(ss0), ddd(ss0), psym=2, xtit='Y position',ytit='Delta change in %', tit=tit
			plottime, 0, .97, 'YCen = ' + string(cen)
			oplot, findgen(800), poly(findgen(800),coeff)
			plottime & plottime,0,0,progver & pause
		end
	end
    end
end
;
end
