function xnew=boxfiltg(x,n)
%function xnew=boxfiltg(x,n)
%                     skips over NaNs
%                     box car filter of length n
%                     the time base is maintained, and the points
%                     at the beginning and end are returned as their 
%                     original values.

good=find(x(:)~=NaN)';
lg=length(good);
filt=ones(1:n)/n;
xg=conv(x(good),filt);
nbeg=ceil((n+1)/2);
xg=xg(nbeg:nbeg-1+lg);
xg(1:nbeg-1)=ones(1,nbeg-1)*xg(nbeg);
xg(lg-nbeg+2:lg)=ones(1,nbeg-1)*xg(lg-nbeg+1);
xnew=ones(x)*nan;
xnew(good)=xg;

[imax,jmax]=size(x);
if (imax>1)&(jmax>1)
   xnew=remat(xnew,x);
   for i=0:floor(n/2)-1;
	xnew(1+i,:)=x(1+i,:);
	xnew(imax-i,:)=x(imax-i,:);
   end
end


