Home > atmlab > files > loadncfile.m

loadncfile

PURPOSE ^

LOADNCFILE Loads a NetCDF file into a structure.

SYNOPSIS ^

function ret = loadncfile( filename )

DESCRIPTION ^

 LOADNCFILE   Loads a NetCDF file into a structure.

    The function enables direct loading of a whole NetCDF file into
    a structure.

    Global attributes are stored in ret.global_attributes.(attname)
    Variable attributes are stored in ret.attributes.(varname).(attname)
    Variables are stored in ret.(varname)

 FORMAT   ret = loadncfile( filename )
        
 OUT   ret        Loaded NetCDF file structure.
 IN    filename   Name of NetCDF file. (can be gzipped)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

DOWNLOAD ^

loadncfile.m

SOURCE CODE ^

0001 % LOADNCFILE   Loads a NetCDF file into a structure.
0002 %
0003 %    The function enables direct loading of a whole NetCDF file into
0004 %    a structure.
0005 %
0006 %    Global attributes are stored in ret.global_attributes.(attname)
0007 %    Variable attributes are stored in ret.attributes.(varname).(attname)
0008 %    Variables are stored in ret.(varname)
0009 %
0010 % FORMAT   ret = loadncfile( filename )
0011 %
0012 % OUT   ret        Loaded NetCDF file structure.
0013 % IN    filename   Name of NetCDF file. (can be gzipped)
0014 
0015 % 2013-02-08   Updated by Salomon
0016 % 2010-02-10   Created by Oliver Lemke.
0017 
0018 function ret = loadncfile( filename )
0019 
0020 errId = ['atmlab:' mfilename];
0021 % UNCOMPRESS if needed
0022 if strcmp(filename(end-2:end),'.gz')
0023     tmpdir = create_tmpfolder;
0024     c= onCleanup(@() rmdir(tmpdir,'s'));
0025     filename = uncompress(filename,tmpdir);
0026     if isempty(filename), error(errId,'Uncompressing failed'); end
0027 end
0028   
0029 ncid = netcdf.open (filename, 'NOWRITE');
0030 
0031 cleanupObject = onCleanup(@() netcdf.close (ncid));
0032 
0033 ret.global_attributes =  loadncglobalattr( ncid );
0034 
0035 [~,nvars] = netcdf.inq(ncid);
0036 
0037 for i = 0:nvars-1
0038    [varname, ~, ~, natts] = netcdf.inqVar (ncid, i);
0039    svarname = genvarname(varname);
0040    ret.(svarname) = nc_read_varid(ncid,i);
0041    for j = 0:natts-1
0042        attname = netcdf.inqAttName (ncid, i, j);
0043        ret.attributes.(svarname).(genvarname(attname)) = ...
0044            netcdf.getAtt (ncid, i, attname);
0045    end 
0046 end
0047 
0048 if (which('netcdf.inqGrps'))
0049     grpids = netcdf.inqGrps (ncid);
0050     for gid = grpids
0051         grp = read_group(ncid, gid);
0052         ret = catstruct(ret, grp);
0053     end
0054 else
0055     warning(errId, ...
0056         'This Matlab version does not support NetCDF groups.');
0057 end
0058 
0059 end
0060 
0061 function grp = read_group(~, gid)
0062 
0063 varids = netcdf.inqVarIDs(gid);
0064 chgrpids = netcdf.inqGrps(gid);
0065 grpname = genvarname(netcdf.inqGrpName(gid));
0066 grp.(grpname)=struct();
0067 for i = varids
0068     [varname, ~, ~, natts] = netcdf.inqVar (gid, i);
0069     svarname = genvarname(varname);
0070     grp.(grpname).(svarname) = netcdf.getVar (gid, i);
0071     for j = 0:natts-1
0072         attname = netcdf.inqAttName (gid, i, j);
0073         grp.(grpname).attributes.(svarname).(genvarname(attname)) = ...
0074             netcdf.getAtt (gid, i, attname);
0075     end
0076 end
0077 for chgid = chgrpids
0078     childgroup = read_group(gid, chgid);
0079     grp.(grpname) = catstruct(grp.(grpname), childgroup);
0080 end
0081 
0082 
0083 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005