Home > custom > netcdf > nc_printinfo.m

nc_printinfo

PURPOSE ^

PURPOSE: Print information on netcdf file dimensions, variables

SYNOPSIS ^

function nc_printinfo(ncfilepath)

DESCRIPTION ^

 PURPOSE: Print information on netcdf file dimensions, variables 
          and attributes for a quick overview. 
 -------------------------------------------------------------------
 USAGE: nc_printinfo(ncfilepath)
 where: ncfilepath is the path to the netcdf file

 Note: Written to get a quick overview on a netcdf file structure.
 Uses: MEXCDF:  http://mexcdf.sourceforge.net
       and obviously netcdf: http://www.unidata.ucar.edu/software/netcdf/

 TODO: Atm, the Dimension attributes are output with the variables they
 are associated with, using Datatype 4. This is depending on the interface
 and the netcdf file, so in order to retrieve the info via mexcdf, this is
 useful. However, just for information sakes, its irritating because the
 attributes show up as variables where they are none. So in a file of 4
 variables, printncinfo() might show 7 or 8, however the IDs for accessing
 the variables are correct this way.

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function nc_printinfo(ncfilepath)
0002 % PURPOSE: Print information on netcdf file dimensions, variables
0003 %          and attributes for a quick overview.
0004 % -------------------------------------------------------------------
0005 % USAGE: nc_printinfo(ncfilepath)
0006 % where: ncfilepath is the path to the netcdf file
0007 %
0008 % Note: Written to get a quick overview on a netcdf file structure.
0009 % Uses: MEXCDF:  http://mexcdf.sourceforge.net
0010 %       and obviously netcdf: http://www.unidata.ucar.edu/software/netcdf/
0011 %
0012 % TODO: Atm, the Dimension attributes are output with the variables they
0013 % are associated with, using Datatype 4. This is depending on the interface
0014 % and the netcdf file, so in order to retrieve the info via mexcdf, this is
0015 % useful. However, just for information sakes, its irritating because the
0016 % attributes show up as variables where they are none. So in a file of 4
0017 % variables, printncinfo() might show 7 or 8, however the IDs for accessing
0018 % the variables are correct this way.
0019 %
0020 % Felix Hebeler, Geography Dept., University Zurich, December 2006.
0021 
0022 if ~exist(ncfilepath, 'file')
0023     error(['Netcdf file not found: ',ncfilepath])
0024 end
0025 
0026 ncid=mexcdf('OPEN',ncfilepath); % open existing file
0027 [ndims, nvars, gnatts, recdim, status] = mexcdf('INQUIRE',ncid);  % retrieve info
0028 disp(['Number of dimensions: ',num2str(ndims)])
0029 for i=0:ndims-1
0030     [dname, dlength, status] = mexcdf('DIMINQ', ncid, i);
0031     if recdim==i;
0032         disp(['Dimension ',num2str(i),' - Name: ',dname,' - Length: ',num2str(dlength),' - recording dimension.'])    
0033     else
0034         disp(['Dimension ',num2str(i),' - Name: ',dname,' - Length: ',num2str(dlength)])
0035     end
0036 end
0037 disp('----')
0038 disp(['Number of variables: ',num2str(nvars)])
0039 
0040 for i=0:nvars-1
0041     % get dimension info for vars
0042     n='Dimensions: ';
0043     [vname, vdatatype, ndims, dim, natts, status] = mexcdf('VARINQ', ncid, i);
0044     if numel(dim)~=0  % print dimension names for each variable  provided they have a dimension
0045        n=[n,mexcdf('DIMINQ', ncid, dim(1))];
0046        for j=2:ndims
0047           n=[n,', ',mexcdf('DIMINQ', ncid, dim(j))];
0048        end
0049        n=[n,' '];
0050     end
0051     n=[n,'. Attributes: '];
0052     if natts>0
0053         for a=0:natts-1
0054             attname=mexcdf('ATTNAME', ncid, i, a);
0055             %[datatype, len, status] = mexcdf('ATTINQ', ncid, a, attname);
0056             avalue = mexcdf('ATTGET', ncid, i, attname);
0057             n=[n,' ',attname,' = ',num2str(avalue),', '];
0058         end
0059     else
0060         n=[n,'- '];
0061     end
0062     disp(['VarID:',num2str(i),' - Name: ',vname,'. ',n,' Datatype: ',num2str(vdatatype)])
0063     n='';
0064 end
0065 disp('----')
0066 disp(['Number of global attributes: ',num2str(gnatts)])
0067 if gnatts>0
0068     for a=0:gnatts-1
0069         attname = mexcdf('ATTNAME', ncid, -1, a);  % -1 is variable id to access global attributes
0070         avalue = mexcdf('ATTGET', ncid, -1, attname);
0071         disp([attname,' = ',num2str(avalue),', ']);
0072     end    
0073 end
0074 
0075 mexcdf('CLOSE',ncid);

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