Home > custom > netcdf > nc_readvar.m

nc_readvar

PURPOSE ^

PURPOSE: read timeslice(s) of nc variable into matlab array

SYNOPSIS ^

function data = nc_readvar(ncin,varname,timeslice,numt,rot)

DESCRIPTION ^

 PURPOSE: read timeslice(s) of nc variable into matlab array
 -------------------------------------------------------------------
 USAGE: data = nc_readvar(ncin,varname,timeslice,numt,rot)
 where: ncin is the path to the netcdf file [string] or the ncid (int) of an
 already opened file, varname is  the variable name [string] and timeslice 
 is the first timeslice [int] to import.
 numt (optional, int) is the number of slices to import, default is 1.
 rot is a flag to rotate input grid (only works for 2D matrices!): 
 0: none, -1: 90deg clockwise, 1: 90deg ccw. 

 Note: Time indexing starts with 0
       Time is assumed to be the first dimension of a variable. If this
       doesn't work for you, you have to change the code...

 Uses: MEXCDF:  http://mexcdf.sourceforge.net
       and obviously netcdf: http://www.unidata.ucar.edu/software/netcdf/

  Felix Hebeler, Geography Dept., University Zurich, December 2006.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function data = nc_readvar(ncin,varname,timeslice,numt,rot)
0002 % PURPOSE: read timeslice(s) of nc variable into matlab array
0003 % -------------------------------------------------------------------
0004 % USAGE: data = nc_readvar(ncin,varname,timeslice,numt,rot)
0005 % where: ncin is the path to the netcdf file [string] or the ncid (int) of an
0006 % already opened file, varname is  the variable name [string] and timeslice
0007 % is the first timeslice [int] to import.
0008 % numt (optional, int) is the number of slices to import, default is 1.
0009 % rot is a flag to rotate input grid (only works for 2D matrices!):
0010 % 0: none, -1: 90deg clockwise, 1: 90deg ccw.
0011 %
0012 % Note: Time indexing starts with 0
0013 %       Time is assumed to be the first dimension of a variable. If this
0014 %       doesn't work for you, you have to change the code...
0015 %
0016 % Uses: MEXCDF:  http://mexcdf.sourceforge.net
0017 %       and obviously netcdf: http://www.unidata.ucar.edu/software/netcdf/
0018 %
0019 %  Felix Hebeler, Geography Dept., University Zurich, December 2006.
0020 
0021 %some checking first
0022 if exist('ncin')    % check if ncin exists as path or id
0023     if isstr(ncin)
0024         if ~exist(ncin, 'file')
0025             error(['Netcdf file not found: ',ncin])
0026         else
0027             ncid=mexcdf('OPEN',ncin); % open existing file
0028         end
0029     elseif isinteger(ncin)
0030         [a,a,a,a,a] = mexnc('inq', ncin );
0031         if a~=0  % if status a is anything but 0, there's an error:
0032             error(mexnc('strerror',a)) 
0033         else
0034             ncid=ncin;
0035         end
0036     end
0037 else 
0038    error('No valid Netcdf input file referrer specified')    
0039 end
0040 if ~exist('numt','var')
0041     numt=1;
0042 end
0043 if ~exist('rot','var')
0044     rot=0;
0045 end
0046 
0047 % finally get to operate on the file
0048 [varid, status] = mexnc('inq_varid', ncid, varname );  % get the varid by name
0049 if status~=0
0050     error(['Variable not found or locked: ',varname])
0051 end
0052 [varname, datatype, ndims, dimids, natts, status] = mexnc('inq_var', ncid, varid ); % get all the var info
0053 
0054 % step through dims (assume time is first dim) and get max length for the
0055 % get_vara_XXX command
0056 a=('timeslice');
0057 b=(num2str(numt));
0058 if ndims>=0
0059     for i=2:ndims  % spare dim1 as it is assumed to be time
0060         [name, length, status] = mexnc('inq_dim', ncid, dimids(i));
0061         a=[a,', ','0'];
0062         b=[b,', ',num2str(length)];
0063     end
0064 end
0065 eval(['a=[',a,'];'])
0066 eval(['b=[',b,'];']) 
0067 
0068 % the get_vara_XXX method takes a vector a with the start position along
0069 % each of the variable dimensions and a vector b that gives the length to count
0070 % along the corresponding dimension
0071 data = mexnc(['get_vara_',nc_datatype(datatype)], ncid, varid,a,b); % t
0072 if rot~=0 
0073     if ndims(data)==2
0074         data=rot90(data,rot);
0075     else
0076         display('Rotation only works for 2D matrices due to limitation of Matlab rot90 function!')
0077     end
0078 end
0079 mexcdf('CLOSE',ncid);

Generated on Tue 24-Feb-2009 19:14:50 by m2html © 2003