function [u,y,z,jd] =iscrum(cdf,var,time,iindex)
%  ISCRUM  Extracts a vertical slice of a scalar variable 
%          along column I from a 4D SCRUM NetCDF file. 
%
%  USAGE: [u,y,z,jd]=iscrum(cdf,var,time,i_index)
%    cdf = NetCDF file name (string) (e.g. 'ecomsi.cdf')
%    var = variable name (string) (e.g. 'salt' for salinity)
%    time = time step (integer) (e.g. 1 for 1st time step)
%    i_index = I index along which to take vertical slice
%
%    u = 2D array of the variable "var"
%    y = distance along scrum in km (assuming units in netCDF file are m)
%    z = depth in m
%   jd = Julian time of scrum (type "help julian" for details)
%
%
%   Example:  to extract salinity along J=10
%             from the 3rd time step in the file 'scrum_c.nc', do:
%
%          [salt,y,z]=iscrum('scrum_c.nc','salt',3,10);
%

% Rich Signell rsignell@usgs.gov

if nargin~=4 ,
  help jscrum; return
end
mexcdf('setopts',0);
ncid=mexcdf('open',cdf,'nowrite');
u=mexcdf('varget',ncid,var,[(time-1) 0 0 (iindex-1)],[1 -1 -1 1],1);
if nargout>1,
  d=mexcdf('varget',ncid,'h',[0 (iindex-1)],[-1 1],1);
  sigma=mexcdf('varget',ncid,'Cs_r',0,-1,1);
  pn=mexcdf('varget',ncid,'pn',[0 (iindex-1)],[-1 1],1);
  h2=ones(size(pn))./pn;
  y=cumsum(h2)/1000.;
  y=y*ones(1,length(sigma));
  z=d*(sigma');
  ind=find(isnan(z));
  z(ind)=zeros(size(ind));
end
% get time stuff
if(nargout>3)
t0=mexcdf('varget1',ncid,'dstart',0);
t=mexcdf('varget',ncid,'scrum_time',time-1,1);
jd=t0+t/3600/24;
end
mexcdf('close',ncid);
