function ynew=sortsamp(x,y,xnew);
%function ynew=sortsamp(x,y,xnew);
%                xnew is evenly spaced array of n elements, and ynew is 
%                   function of same length
g=find( (x(:)~=nan)&(y(:)~=nan) );
x=x(g);
y=y(g);
n=length(xnew);
xmin=xnew(1);
xmax=xnew(n);

[x,ind]=sort(x);
y=y(ind);
nx=length(x);
nsub=floor(nx/n);
if rem(nsub,2)==0
          nsub=nsub+1;   %   has to be odd number
end
nsub
x=boxfilt(x,nsub);
y=boxfilt(y,nsub);

dx=(xmax-xmin)/(n-1);
xnew=xmin:dx:xmax;

ind=ceil( 1+ (x-xmin)/(xmax-xmin)*(n-1));
g=find( (ind>0)&(ind<=n));
ynew=ones(xnew)*nan;
ynew(ind(g))=y(g);
