Home > custom > netcdf > nc_writevar.m

nc_writevar

PURPOSE ^

PURPOSE: write timeslice(s) from matlab matrix to nc variable

SYNOPSIS ^

function status = nc_writevar(ncout,varname,data,timeslice,numt)

DESCRIPTION ^

 PURPOSE: write timeslice(s) from matlab matrix to nc variable
 -------------------------------------------------------------------
 USAGE: data = nc_writevar(ncout,varname,data,timeslice,numt)
 where: ncout is the path to the netcdf file [string] or the ncid (int) of an
        already opened file, varname is  the variable name [string], data 
        is the 2D datamatrix that will be written to the netcdf file and 
        timeslice is the first timeslice [int] to export. If timeslice is
        larger than the existing timeslice of the netcdf file, the slice
        is appended, else replaced (!)
        numt (optional, int) is the number of slices to export for 3D 
        matrices, default is 1.

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

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