function [u,x,z,jd] =jscrum(cdf,var,time,jindex)
%  JSCRUM Extracts a vertical slice of a scalar variable 
%          along row J from a 4D SCRUM NetCDF file. 
%
%  USAGE: [u,x,z,jd]=jscrum(cdf,var,time,j_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)
%    j_index = J index along which to take vertical slice
%
%    u = 2D array of the variable "var"
%    x = distance along slice in km (assuming units in netCDF file are m)
%    z = depth in m
%   jd = Julian time of slice (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,x,z]=jscrum('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 (jindex-1) 0],[1 -1 1 -1],1);
if nargout>1,
  d=mexcdf('varget',ncid,'h',[(jindex-1) 0],[1 -1],1);  
  sigma=mexcdf('varget',ncid,'Cs_r',0,-1,1);            
  pm=mexcdf('varget',ncid,'pm',[(jindex-1) 0],[1 -1],1); 
  h1=ones(size(pm))./pm;                               
  x=cumsum(h1)/1000.;
  x=x*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('varget',ncid,'dstart',0,1);
t=mexcdf('varget',ncid,'scrum_time',time-1,1);
jd=to+t;
end
mexcdf('close',ncid);
