Home > atmlab > files > loadncvar.m

loadncvar

PURPOSE ^

LOADNCVAR Loads a specific variable(s) from a NetCDF file

SYNOPSIS ^

function [ret, glattr, attr] = loadncvar( filename, varname )

DESCRIPTION ^

 LOADNCVAR   Loads a specific variable(s) from a NetCDF file

    The function enables direct loading of a named variable from a NetCDF
    file. varname can be either a string to load one variable or a cell
    array of several variable names.

    When multiple varnames are given (or if a single varname is given in a cell)
    the return value is a structure containing the loaded variables.

    lat = loadncvar( 'mydata.nc', 'lat');
    grid = loadncvar( 'mydata.nc', { 'lat', 'lon' } );

    Note: If you wish to load the whole netcdf file into a structure use
          struct = loadncfile (filename), instead.

 FORMAT   [ret, attr] = loadncvar( filename, varname )

 OUT   ret        Loaded variable.
 OUT   glattr     structure containting attributes of the read data.
 OUT   attr       structure containting attributes of the read data.
 IN    filename   Name of NetCDF file.
       varname    Name of variable.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

loadncvar.m

SOURCE CODE ^

0001 % LOADNCVAR   Loads a specific variable(s) from a NetCDF file
0002 %
0003 %    The function enables direct loading of a named variable from a NetCDF
0004 %    file. varname can be either a string to load one variable or a cell
0005 %    array of several variable names.
0006 %
0007 %    When multiple varnames are given (or if a single varname is given in a cell)
0008 %    the return value is a structure containing the loaded variables.
0009 %
0010 %    lat = loadncvar( 'mydata.nc', 'lat');
0011 %    grid = loadncvar( 'mydata.nc', { 'lat', 'lon' } );
0012 %
0013 %    Note: If you wish to load the whole netcdf file into a structure use
0014 %          struct = loadncfile (filename), instead.
0015 %
0016 % FORMAT   [ret, attr] = loadncvar( filename, varname )
0017 %
0018 % OUT   ret        Loaded variable.
0019 % OUT   glattr     structure containting attributes of the read data.
0020 % OUT   attr       structure containting attributes of the read data.
0021 % IN    filename   Name of NetCDF file.
0022 %       varname    Name of variable.
0023 
0024 % 2010-02-02   Created by Oliver Lemke.
0025 % 2010-10-11   Modified by Gerrit Holl.
0026 % 2012-01-30   Modified by Gerrit Holl.
0027 % 2012-02-23   Modified by Gerrit Holl.
0028 % 2013-02-13   Modified by Salomon Eliasson.
0029 
0030 function [ret, glattr, attr] = loadncvar( filename, varname )
0031 
0032 errId = ['atmlab:' mfilename];
0033 % UNCOMPRESS if needed
0034 if strcmp(filename(end-2:end),'.gz')
0035     tmpdir = create_tmpfolder;
0036     c= onCleanup(@() rmdir(tmpdir,'s'));
0037     filename = uncompress(filename,tmpdir);
0038     if isempty(filename), error(errId,'Uncompressing failed'); end
0039 end
0040 
0041 ncid = netcdf.open (filename, 'NOWRITE');
0042 
0043 cleanupObject = onCleanup( @()netcdf.close(ncid) );
0044 
0045 if (nargout>1)
0046     glattr = loadncglobalattr(ncid);
0047 else
0048     glattr = [];
0049 end
0050 
0051 if (nargout>2)
0052     attr = loadncattr(ncid,varname);
0053 else
0054     attr = [];
0055 end
0056 
0057 
0058 if ischar(varname)
0059     varid = netcdf.inqVarID (ncid, varname);
0060     ret = nc_read_varid(ncid, varid);
0061 elseif iscell(varname)
0062     if isempty(varname)
0063         ret = struct();
0064     end
0065     for v = varname(:).'
0066         try
0067             varid = netcdf.inqVarID (ncid, v{1});
0068         catch ME
0069             if ~isempty(regexp(ME.message,'Variable not found', 'once'))
0070                 error(errId,'Variable ''%s'' not found in file',v{1})
0071             else
0072                 ME.rethrow();
0073             end
0074         end
0075         ret.(genvarname(v{1})) = nc_read_varid(ncid, varid);
0076     end
0077 else
0078     error(errId,'varname must be either of type char or cell');
0079 end
0080 
0081 end

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