function [xt,yt,zt,x_span,y_span,z_span]=alterr(xp,yp,zp,nmesh,hx,hy,hz)
r_moon=1737.4e3;
[xc,yc,zc]=sphere(nmesh);
x_span=xc*r_moon;
y_span=yc*r_moon;
z_span=zc*r_moon;
x_span(z_span<(zp-hz))=nan; y_span(z_span<(zp-hz))=nan; z_span(z_span<(zp-hz))=nan;
x_span(z_span>(zp+hz))=nan; y_span(z_span>(zp+hz))=nan; z_span(z_span>(zp+hz))=nan;
x_span(x_span<(xp-hx))=nan; y_span(x_span<(xp-hx))=nan; z_span(x_span<(xp-hx))=nan;
x_span(x_span>(xp+hx))=nan; y_span(x_span>(xp+hx))=nan; z_span(x_span>(xp+hx))=nan;
x_span(y_span<(yp-hy))=nan; y_span(y_span<(yp-hy))=nan; z_span(y_span<(yp-hy))=nan;
x_span(y_span>(yp+hy))=nan; y_span(y_span>(yp+hy))=nan; z_span(y_span>(yp+hy))=nan;

subz=z_span(~isnan(z_span));
subz=unique(subz);  subz=subz([1 end]);
if abs(subz(1))==r_moon
    subz=subz(2);
elseif abs(subz(2))==r_moon
    subz=subz(1);
end
row_try=floor(length(subz)*rand);
if row_try==0
    row_try=length(subz);
end
row_z=find(z_span(:,1)==subz(row_try));
zt=subz(row_try);
col_x=~isnan(x_span(row_z,:));
subx=x_span(row_z,col_x);
suby=y_span(row_z,col_x);
if isnan(z_span(row_z+1,1))
    flag=-1;
elseif isnan(z_span(row_z-1,1))
    flag=1;
end
while isempty(subx)
    row_z=row_z+flag;
    zt=z_span(row_z,1);
    col_x=~isnan(x_span(row_z,:));
    subx=x_span(row_z,col_x);
    suby=y_span(row_z,col_x);
end
yt=nan;
while isnan(yt)
    col_try=floor(length(subx)*rand);
    if col_try==0
        col_try=length(subx);
    end
    xt=subx(col_try);
    yt=suby(col_try);
end
end
