function [tau,x,y]=ksliceuv(cdf,time)
%  KSLICETAU
%     returns a matrix containing a horizontal slice of
%     wind stress
%     at a given time step from an ECOMSI.CDF file
%
%       USAGE: [tau,x,y]=ksliceuv(cdf,time)
%
%      tau will be a complex number (tauu + i tauv).
%
if (nargin<2 | nargin>2),
  help kslicetau; return
end
mexcdf('setopts',0);
ncid=mexcdf('open',cdf,'nowrite');
if(ncid==-1),
  disp(['file ' cdf ' not found'])
  return
end
[name, nx]=mexcdf('diminq',ncid,'xpos');
[name, ny]=mexcdf('diminq',ncid,'ypos');
[name, nz]=mexcdf('diminq',ncid,'zpos');
if(nargout==3),
  x=mexcdf('varget',ncid,'x',[0 0],[ny nx]);
  y=mexcdf('varget',ncid,'y',[0 0],[ny nx]);
end
depth=mexcdf('varget',ncid,'depth',[0 0],[ny nx]);
ang=mexcdf('varget',ncid,'ang',[0 0],[ny nx]);
if(isempty(ang)),
  ang=zeros(size(depth)),
end;
taux=mexcdf('varget',ncid,'taux',[(time-1) 0 0],...
    [1 ny nx],1); % twod field
tauy=mexcdf('varget',ncid,'tauy',[(time-1) 0 0],...
    [1 ny nx],1); % twod field
mexcdf('close',ncid);
%
% average taux and tauy to center of grid cells
%
taux=taux(1:nx-1,:)+.5*diff(taux);
taux(nx,:)=taux(nx-1,:);
tauy=tauy(:,1:ny-1)+.5*diff(tauy')';
tauy(:,ny)=tauy(:,ny-1);
tau=taux+sqrt(-1)*tauy;
 
dind=find(depth==-99999);
tau(dind)=tau(dind)*NaN;
%
% rotate into east/north components
tau=tau.*exp(sqrt(-1)*ang);
