function [u,x,z] =xzslice(cdf,var,time,yindex)
%  XZSLICE  returns a vertical XZ slice (constant j)
%           from an ECOMSI.CDF file.
%
%     The variable must be a function of x, y, z and time: (e.g. salinity)
%    USAGE [u,x,z] =xzslice(cdf,var,time,yindex)
%
if nargin~=4 ,
  help xzslice; return
end
mexcdf('setopts',0);
ncid=mexcdf('open',cdf,'nowrite');
[name, nx]=mexcdf('diminq',ncid,'xpos');
[name, nz]=mexcdf('diminq',ncid,'zpos');
  u=mexcdf('varget',ncid,var,[(time-1) 0 (yindex-1) 1],[1 nz-1 1 nx-2],1);
if nargout>1,
  d=mexcdf('varget',ncid,'depth',[(yindex-1) 1],[1 nx-2],1);
  ind=find(d==-99999.);
  d(ind)=d(ind)*nan;
  u(ind,:)=u(ind,:)*NaN;
  sigma=mexcdf('varget',ncid,'sigma',0,nz,1);
% assume we are slicing a variable defined at 
% at the center of the sigma level (t,s,u,v,km)
  sigma=0.5*(sigma(1:nz-1)+sigma(2:nz));
  h1=mexcdf('varget',ncid,'h1',[(yindex-1) 1],[1 nx-2],1);
  x=cumsum(h1)/1000.;
  x=x*ones(1,nz-1);
  z=d*(sigma');
end
mexcdf('close',ncid);
