Code covered by the BSD License  

Highlights from
ncdfread.m

ncdfread.m

by

 

09 Nov 2010 (Updated )

Reads netcdf files in MATLAB 7.11, returns info in three structs: variables, attributes, dimensions

ncdfread(fname)
function[vars varatts dims globeatts] = ncdfread(fname)

% read a netcdf file in the new MATLAB 7.11 PITA way

% output:

% vars: the variables and their values
% varatts: the variable attributes and their values
% dims: dimensions of all variables

% currently set up to read, not write a netcdf file

% Ryan Eastman
% November 2010
% report issues to: rmeast@atmos.washington.edu

% tested with NCAR/NCEP reanalysis files only - so far

% open the file
ncid = netcdf.open(fname,'nowrite');

% estract information on:

% numdims - # of dimensions
% numvars - # of variables
% numglobalatts - # number of global attributes
% unlimdimid - ID of dimension defined with unlimited length
[numdims numvars numglobalatts unlimdimid] = netcdf.inq(ncid);

% dimensions - struct form dims.[Dimension Name] = [Dimension Length]

for i = 0:numdims-1,
    
    % return dimension name and length
    [dname dlength] = netcdf.inqDim(ncid,i);
    
    % assign struct: dims.[Dimension Name] = [Dimension length]
    eval(['dims.',dname,' = dlength;']);
    
end;

% attributes - per variable, put in struct form: varatts.[Variable
% Name].[Attribute Name] = [Attribute Value]

for i = 0:numvars-1,
    
    % return variable name, type, ID's of dimensions and # of attributes
    [vname xtype dimids natts] = netcdf.inqVar(ncid,i);
    
    % regurn variable ID
    [varid] = netcdf.inqVarID(ncid,vname);
    
    % cycle through # of attributes per variable
    for j = 0:natts-1,
        
        % attribute name
        [atname] = netcdf.inqAttName(ncid,varid,j);
        atnamestr = atname;
        
        while atnamestr(1) == '_',
            switch atnamestr(1)
                case {'_'},
                    atnamestr = atnamestr(2:end);
            end;
        end;
        
        % attribute value
        at = netcdf.getAtt(ncid,varid,atname);
        
        % assign struct: varatts.[Variable Name].[Attribute Name] =
        % [Attribute Value]
        
        eval(['varatts.',vname,'.',atnamestr,' = at;']);

    end;
    
end;

% some redundancy here for simplicity...

% variable values in struct form: vars.[Variable Name] = [Variable Value]

for i = 0:numvars-1,
    
    % return variable name and length
    [vname vlength] = netcdf.inqVar(ncid,i);
    
    % return variable ID
    vid = netcdf.inqVarID(ncid,vname);
    
    % return variable value
    vval = netcdf.getVar(ncid,vid);
    
    % assign struct: vars.[Variable Name] = [Variable Value]
    
    % reverse the dimension order using permute.m if necessary
    switch length(size(vval))
        case {1,2},
            eval(['vars.',vname,' = vval;']);
            
        otherwise,
            
            eval(['vars.',vname,' = permute(vval,[length(size(vval)):-1:1]);']);
    end;
    
end;

% determine the names and values of the global attributes - new 4/12/12

for i = 0:numglobalatts-1,
    
    ganame = netcdf.inqAttName(ncid,netcdf.getConstant('NC_GLOBAL'),0);
    gaval = netcdf.getAtt(ncid,netcdf.getConstant('NC_GLOBAL'),ganame);
    
    eval(['globeatts.',ganame,' = gaval;']);
end;

% close netcdf file
netcdf.close(ncid);


Contact us