


KSLICE: returns horizontal slice at particular layer.
Works on either ECOM or SCRUM files. For ECOM, the layer corresponds
to sigma. For SCRUM it corresponds to s_rho. The function can also be
used to read in 2D and 3D fields such as depth (h), or whatever.
USAGE: [u,x,y]=kslice(cdf,var,[time],[layer])
where
cdf: file name for netCDf file (e.g. 'pom.cdf')
var: the variable to select (eg. 'salt' for salinity)
time: time step
layer: sigma or s_rho layer (e.g 1 for top layer)
Examples:
[s,x,y]=kslice('ecomsi.cdf','salt',2,3);
returns the salinity field from the 3rd sigma level
at the 2nd time step.
[elev,x,y]=kslice('ecomsi.cdf','elev',4);
returns the elevation field from the 4th time step
[depth,x,y]=kslice('ecomsi.cdf','depth');
returns the depth field

0001 function [u,x,y]=kslice(cdf,var,time,layer) 0002 %KSLICE: returns horizontal slice at particular layer. 0003 % 0004 % Works on either ECOM or SCRUM files. For ECOM, the layer corresponds 0005 % to sigma. For SCRUM it corresponds to s_rho. The function can also be 0006 % used to read in 2D and 3D fields such as depth (h), or whatever. 0007 % 0008 % USAGE: [u,x,y]=kslice(cdf,var,[time],[layer]) 0009 % 0010 % where 0011 % cdf: file name for netCDf file (e.g. 'pom.cdf') 0012 % var: the variable to select (eg. 'salt' for salinity) 0013 % time: time step 0014 % layer: sigma or s_rho layer (e.g 1 for top layer) 0015 % 0016 % 0017 % Examples: 0018 % 0019 % [s,x,y]=kslice('ecomsi.cdf','salt',2,3); 0020 % returns the salinity field from the 3rd sigma level 0021 % at the 2nd time step. 0022 % 0023 % [elev,x,y]=kslice('ecomsi.cdf','elev',4); 0024 % returns the elevation field from the 4th time step 0025 % 0026 % [depth,x,y]=kslice('ecomsi.cdf','depth'); 0027 % returns the depth field 0028 % 0029 0030 0031 if (nargin<2 | nargin>4), 0032 help kslice; return 0033 end 0034 0035 % turn off warnings from NetCDf 0036 ncmex('setopts',0); 0037 0038 ncid = ncmex('open', cdf, 'nowrite'); 0039 if ( ncid == -1 ) 0040 fprintf ( 'Could not open %s.\n', cdf ); 0041 return; 0042 end 0043 0044 % 0045 % Assume that a SCRUM file will always contain the 'xi_rho' dimension. 0046 % If we find it, assume that we've got a SCRUM file. 0047 [dimid, rcode] = ncmex('dimid', ncid, 'xi_rho'); 0048 if ( dimid ~= -1 ) 0049 ncmex ( 'close', ncid ); 0050 switch ( nargin ) 0051 case 2 0052 [u,x,y] = scrum_kslice ( cdf, var ); 0053 case 3 0054 [u,x,y] = scrum_kslice ( cdf, var, time ); 0055 case 4 0056 [u,x,y] = scrum_kslice ( cdf, var, time, layer ); 0057 end 0058 return; 0059 end 0060 0061 % 0062 % Assume that an ECOM file will always contain the 'xpos' dimensions. 0063 % If we find it, assume it is an ECOM file. 0064 [dimid, rcode] = ncmex('dimid', ncid, 'xpos'); 0065 if ( dimid ~= -1 ) 0066 ncmex ( 'close', ncid ); 0067 switch ( nargin ) 0068 case 2 0069 [u,x,y] = ecom_kslice ( cdf, var ); 0070 case 3 0071 [u,x,y] = ecom_kslice ( cdf, var, time ); 0072 case 4 0073 [u,x,y] = ecom_kslice ( cdf, var, time, layer ); 0074 end 0075 return; 0076 end 0077 0078 0079 % 0080 % If we get this far, then neither file was recognizable. 0081 fprintf ( 'I can''t make sense out of %s???\n', cdf ); 0082 help kslice; 0083 return;