function varz=zsliceg(data,grid,depth) % ZSLICEG: horizontal slice 3D data at fixed vertical level % using generic input structures (e.g returned by the NJ Toolbox) % Usage: varz=zsliceg(data,grid,depth); % Input: data = 3D array of data % grid = grid structure containing 1D or 3D array of z values % (containing grid.z, which if 1d, will be expanded to 3d) % zlev = desired z level for horizontal slice % Output: varz = 2D array of data % Example: % url='http://geoport.whoi.edu/thredds/dodsC/usgs/vault0/models/examples/bora_feb.nc'; % [t,g]=nj_tslice(url,'temp',1);% grab 3d field of 'temp' at time step 1 % tz = zsliceg(t,g,-5); % return temperature slice at 5 m depth % pcolorjw(g.lon,g.lat,double(tz)); % plot temp at 5 m depth [NZ,NY,NX]=size(data); z=grid.z; [~,ny,nx]=size(z); if(nx==1 & ny==1), % if z is a 1d array z3d(:,1,1)=z; z=repmat(z3d,[1 NY NX]); end if z(1,1,1)depth)~=0); zl_ind = zl_ind + [1:1:length(zg_ind)].'; data_lesser_z = data(zl_ind); depth_lesser_z = z(zl_ind); % Linearly interpolate between the data values. alpha = (depth-depth_greater_z)./(depth_lesser_z-depth_greater_z); data_at_depth = (data_lesser_z.*alpha)+(data_greater_z.*(1-alpha)); varz = reshape(data_at_depth,[NY NX]); varz(iland)=nan; % mask out land