[Dart-dev] [7595] DART/trunk/matlab/nc_read_att.m: Thsi function uses the native netCDF support in Matlab to query and return

nancy at ucar.edu nancy at ucar.edu
Thu Feb 19 13:39:03 MST 2015


Revision: 7595
Author:   thoar
Date:     2015-02-19 13:39:03 -0700 (Thu, 19 Feb 2015)
Log Message:
-----------
Thsi function uses the native netCDF support in Matlab to query and return
an attribute from a variable or a global attribute. It returns an empty
variable if the attribute does not exist. This is in contrast to the intrinsic
ncreadatt() function that will error out if the attribute does not exist.

Added Paths:
-----------
    DART/trunk/matlab/nc_read_att.m

-------------- next part --------------
Added: DART/trunk/matlab/nc_read_att.m
===================================================================
--- DART/trunk/matlab/nc_read_att.m	                        (rev 0)
+++ DART/trunk/matlab/nc_read_att.m	2015-02-19 20:39:03 UTC (rev 7595)
@@ -0,0 +1,44 @@
+function value = nc_read_att(fname,varid,attname)
+%% If the attribute exists, return the value; if not return an empty object.
+%
+% This differs from Matlab's intrinsic ncreadatt() in that it does not error
+% out if the attribute does not exist.
+%
+% Some examples:
+%
+% cdate = nc_read_att('example.nc',nc_global,'creation_date');
+% units = nc_read_att('example.nc','temperature','units');
+% uhoh  = nc_read_att('example.nc','temperature','something_that_does_not_exit');
+% if isempty(uhoh), fprintf('no such attribute but life goes on.\n'); end
+
+% DART software - Copyright 2004 - 2013 UCAR. This open source software is
+% provided by UCAR, "as is", without charge, subject to all terms of use at
+% http://www.image.ucar.edu/DAReS/DART/DART_download
+%
+% DART $Id: %
+
+value = [];
+
+if (varid == nc_global)
+    finfo = ncinfo(fname);
+    for iatt = 1:length(finfo.Attributes)
+        if (strcmp(finfo.Attributes(iatt).Name, deblank(attname)))
+            value = finfo.Attributes(iatt).Value;
+            return
+        end
+    end
+else
+    vinfo = ncinfo(fname,varid);
+    for iatt = 1:length(vinfo.Attributes)
+        if (strcmp(vinfo.Attributes(iatt).Name, deblank(attname)))
+            value = vinfo.Attributes(iatt).Value;
+            return
+        end
+    end
+end
+
+% <next few lines under version control, do not edit>
+% $URL$
+% $Revision$
+% $Date$
+


Property changes on: DART/trunk/matlab/nc_read_att.m
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author HeadURL Id
Added: svn:eol-style
   + native


More information about the Dart-dev mailing list