Home > atmlab > files > loadncglobalattr.m

loadncglobalattr

PURPOSE ^

% loadncglobalattr

SYNOPSIS ^

function global_attributes = loadncglobalattr( ncfile )

DESCRIPTION ^

% loadncglobalattr

 PURPOSE
         Load the global attributes of a netcdf file

 IN
       ncfile          is either a string filename 
                       OR
                       an id of an already open netcdf file
 OUT
       global_attributes       struct

 Salomon Eliasson
 $Id: loadncglobalattr.m 8189 2013-02-11 14:06:53Z olemke $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

loadncglobalattr.m

SOURCE CODE ^

0001 function global_attributes = loadncglobalattr( ncfile )
0002 %% loadncglobalattr
0003 %
0004 % PURPOSE
0005 %         Load the global attributes of a netcdf file
0006 %
0007 % IN
0008 %       ncfile          is either a string filename
0009 %                       OR
0010 %                       an id of an already open netcdf file
0011 % OUT
0012 %       global_attributes       struct
0013 %
0014 % Salomon Eliasson
0015 % $Id: loadncglobalattr.m 8189 2013-02-11 14:06:53Z olemke $
0016 
0017 errId = ['atmlab:' mfilename];
0018 
0019 if ischar(ncfile)
0020     % UNCOMPRESS if needed
0021     if strcmp(ncfile(end-2:end),'.gz')
0022         tmpdir = create_tmpfolder;
0023         c= onCleanup(@() rmdir(tmpdir,'s'));
0024         ncfile = uncompress(ncfile,tmpdir);
0025         if isempty(ncfile), error(errId,'Uncompressing failed'); end
0026     end
0027     
0028     ncid = netcdf.open (ncfile, 'NOWRITE');
0029     cleanupObject = onCleanup(@() netcdf.close (ncid));
0030     
0031 else
0032     ncid = ncfile;
0033 end
0034 
0035 [~,nvars,ngatts] = netcdf.inq(ncid);
0036 
0037 global_attributes = struct();
0038 for i = 0:ngatts-1
0039    attname = netcdf.inqAttName (ncid, netcdf.getConstant('GLOBAL'), i);
0040    global_attributes.(genvarname(attname)) = ...
0041        netcdf.getAtt (ncid, netcdf.getConstant('GLOBAL'), attname);
0042 end
0043

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