1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
<!-- Generated by Harlequin WebMaker 2.2.3 (24-Apr-1996)
LispWorks 3.2.2 -->
<HTML> <HEAD>
<TITLE>8 Attributes</TITLE>
</HEAD>
<BODY bgcolor="#ffffff">
<A NAME=HEADING13></A>
<A HREF="guidec-14.html">[Next] </A><A HREF="guidec-12.html">[Previous] </A><A HREF="guidec-1.html">[Top] </A><A HREF="guidec-3.html">[Contents] </A><A HREF="guidec-21.html">[Index] </A><A HREF="http://www.unidata.ucar.edu/packages/netcdf/">[netCDF Home Page]</A><A HREF="http://www.unidata.ucar.edu/">[Unidata Home Page]</A><P>
NetCDF User's Guide for C<P>
<A NAME=HEADING13-0></A>
<H1>8 <A NAME=MARKER-10-2825></A><A NAME=MARKER-9-2826></A>Attributes</H1>
<HR>
<A NAME=MARKER-2-2833></A><A NAME=MARKER-2-2834></A>Attributes may be associated with each netCDF variable to specify such properties as units, special values, maximum and minimum valid values, scaling factors, and offsets. Attributes for a netCDF dataset are defined when the dataset is first created, while the netCDF dataset is in define mode. Additional attributes may be added later by reentering define mode. A netCDF attribute has a netCDF variable to which it is assigned, a name, a type, a length, and a sequence of one or more values. An attribute is designated by its variable ID and name. When an attribute name is not known, it may be designated by its variable ID and number in order to determine its name, using the function <CODE><A NAME=MARKER-2-2835></A>nc_inq_attname</CODE>.<A NAME=MARKER-10-2836></A><P>
<A NAME=MARKER-2-2837></A><A NAME=MARKER-2-2838></A><A NAME=MARKER-2-2839></A><A NAME=MARKER-2-2840></A><A NAME=MARKER-2-2841></A><A NAME=MARKER-2-2842></A><A NAME=MARKER-2-2843></A><A NAME=MARKER-2-2844></A>The attributes associated with a variable are typically defined immediately after the variable is created, while still in <A NAME=MARKER-2-2847></A><A NAME=MARKER-2-2848></A><A NAME=MARKER-2-2851></A><A NAME=MARKER-2-2852></A>define mode. The data type, length, and value of an attribute may be changed even when in data mode, as long as the changed attribute requires no more space than the attribute as originally defined. <P>
It is also possible to have attributes that are not associated with any variable. These are called <A NAME=MARKER-2-2853></A><A NAME=MARKER-2-2854></A><I>global attributes</I> and are identified by using <CODE>NC_GLOBAL</CODE> <CODE><A NAME=MARKER-2-2856></A><A NAME=MARKER-10-2857></A></CODE>as a variable pseudo-ID. Global attributes are usually related to the netCDF dataset as a whole and may be used for purposes such as providing a title or processing history for a netCDF dataset. <P>
<A NAME=MARKER-2-2858></A><A NAME=MARKER-2-2859></A>Operations supported on attributes are:<P>
<UL>
<LI>Create an attribute, given its variable ID, name, data type, length, and value.<P>
<LI>Get attribute's data type and length from its variable ID and name.<P>
<LI>Get attribute's value from its variable ID and name.<P>
<LI>Copy attribute from one netCDF variable to another.<P>
<LI>Get name of attribute from its number.<P>
<LI>Rename an attribute.<P>
<LI>Delete an attribute.<P>
</UL>
<A NAME=HEADING13-12></A>
<H2>8.1 <A NAME=MARKER-9-2860></A>Attribute Conventions</H2>
<HR>
Names commencing with underscore ('<CODE>_</CODE>') are reserved for use by the netCDF library. Most generic applications that process netCDF datasets assume standard <A NAME=MARKER-2-2863></A><A NAME=MARKER-2-2864></A>attribute conventions and it is strongly recommended that these be followed unless there are good reasons for not doing so. Below we list the names and meanings of recommended standard attributes that have proven useful. Note that some of these (e.g. <CODE>units</CODE>, <CODE>valid_range</CODE>, <CODE>scale_factor</CODE>) assume numeric data and should not be used with character data.
<TABLE BORDER="1"><TD><CODE><A NAME=MARKER-2-5></A>units</CODE><TD>A character string that specifies the units used for the variable's data. Unidata has developed a freely-available library of routines to convert between character string and binary forms of unit specifications and to perform various useful operations on the binary forms. This library is used in some netCDF applications. Using the recommended units syntax permits data represented in conformable units to be automatically converted to common units for arithmetic operations. <A HREF=guidec-17.html#MARKER-9-3221>See Appendix A "Units," page 113</A>, for more information. <TR>
<TD><CODE><A NAME=MARKER-2-7></A><A NAME=MARKER-2-8></A>long_name</CODE><TD>A long descriptive name. This could be used for labeling plots, for example. If a variable has no <CODE>long_name</CODE> attribute assigned, the variable name should be used as a default. <TR>
<TD><CODE><A NAME=MARKER-2-9></A><A NAME=MARKER-2-10></A>valid_min</CODE><TD>A scalar specifying the minimum valid value for this variable.<TR>
<TD><CODE><A NAME=MARKER-2-11></A><A NAME=MARKER-2-12></A>valid_max</CODE><TD>A scalar specifying the maximum valid value for this variable.<TR>
<TD><CODE><A NAME=MARKER-2-13></A><A NAME=MARKER-2-14></A>valid_range</CODE><TD>A vector of two numbers specifying the minimum and maximum valid values for this variable, equivalent to specifying values for both <CODE>valid_min</CODE> and <CODE>valid_max</CODE> attributes. Any of these attributes define the <A NAME=MARKER-2-15></A><A NAME=MARKER-2-16></A><A NAME=MARKER-2-17></A><I>valid range</I>. The attribute <CODE>valid_range</CODE> must not be defined if either <CODE>valid_min</CODE> or <CODE>valid_max</CODE> is defined. <P>Generic applications should treat values outside the <I>valid range</I> as missing. The type of each <CODE>valid_range</CODE>, <CODE>valid_min</CODE> and <CODE>valid_max</CODE> attribute should match the type of its variable (except that for <CODE>byte</CODE> data, these can be of a signed integral type to specify the intended range). <P>If neither <CODE>valid_min</CODE>, <CODE>valid_max</CODE> nor <CODE>valid_range</CODE> is defined then generic applications should define a valid range as follows. If the data type is byte and <CODE>_FillValue</CODE> is not explicitly defined, then the valid range should include all possible values. Otherwise, the valid range should exclude the <CODE>_FillValue</CODE> (whether defined explicitly or by default) as follows. If the <CODE>_FillValue</CODE> is positive then it defines a valid maximum, otherwise it defines a valid minimum. For integer types, there should be a difference of 1 between the <CODE>_FillValue</CODE> and this valid minimum or maximum. For floating point types, the difference should be twice the minimum possible (1 in the least significant bit) to allow for rounding error.<TR>
<TD><CODE><A NAME=MARKER-2-20></A><A NAME=MARKER-2-21></A>scale_factor</CODE><TD>If present for a variable, the data are to be <A NAME=MARKER-2-22></A><A NAME=MARKER-2-23></A>multiplied by this factor after the data are read by the application that accesses the data.<TR>
<TD><CODE><A NAME=MARKER-2-26></A><A NAME=MARKER-2-27></A>add_offset</CODE><TD>If present for a variable, this number is to be added to the data after it is read by the application that accesses the data. If both <CODE>scale_factor</CODE> and <CODE>add_offset</CODE> attributes are present, the data are first scaled before the offset is added. The attributes <CODE>scale_factor</CODE> and <CODE>add_offset</CODE> can be used together to provide simple data compression to store low-<A NAME=MARKER-2-30></A><A NAME=MARKER-2-31></A>resolution floating-point data as small integers in a netCDF dataset. When scaled data are written, the application should first subtract the offset and then divide by the scale factor. <P>When <CODE>scale_factor</CODE> and <CODE>add_offset</CODE> are used for packing, the associated variable (containing the packed data) is typically of type byte or short, whereas the unpacked values are intended to be of type float or double. The attributes <CODE>scale_factor</CODE> and <CODE>add_offset</CODE> should both be of the type intended for the unpacked data, e.g. float or double.<TR>
<TD><CODE><A NAME=MARKER-2-34></A><A NAME=MARKER-2-36></A><A NAME=MARKER-2-37></A>_FillValue</CODE><TD>The <CODE>_FillValue</CODE> attribute specifies the <I>fill value</I> used to pre-fill disk space allocated to the variable. Such pre-fill occurs unless <I>nofill mode</I> is set using <CODE>nc_set_fill</CODE>.<A NAME=MARKER-10-40></A> <A HREF=guidec-10.html#MARKER-9-2300>See Section 5.12 "Set Fill Mode for Writes: nc_set_fill," page 39</A>, for details. The <I>fill value</I> is returned when reading values that were never written. If <CODE>_FillValue</CODE> is defined then it should be scalar and of the same type as the variable. It is not necessary to define your own <CODE>_FillValue </CODE>attribute for a variable if the default <I>fill value</I> for the type of the variable is adequate. However, use of the default fill value for data type byte is not recommended. Note that if you change the value of this attribute, the changed value applies only to subsequent writes; previously written data are not changed. <P>Generic applications often need to write a value to represent undefined or missing values. The <I>fill value</I> provides an appropriate value for this purpose because it is normally outside the <I>valid range</I> and therefore treated as missing when read by generic applications. It is legal (but not recommended) for the <I>fill value</I> to be within the <I>valid range</I>. <P><A HREF=guidec-12.html#MARKER-9-2794>See Section 7.16 "Fill Values," page 78</A>, for more information.<TR>
<TD><CODE><A NAME=MARKER-2-43></A><A NAME=MARKER-2-44></A>missing_value</CODE><TD>This attribute is not treated in any special way by the library or conforming generic applications, but is often useful documentation and may be used by specific applications. The <CODE>missing_value</CODE> attribute can be a scalar or vector containing values indicating missing data. These values should all be outside the <I>valid range</I> so that generic applications will treat them as missing.<TR>
<TD><CODE><A NAME=MARKER-2-45></A><A NAME=MARKER-2-46></A>signedness</CODE><TD>Deprecated attribute, originally designed to indicate whether byte values should be treated as <A NAME=MARKER-2-47></A><A NAME=MARKER-2-48></A>signed or unsigned. The attributes <CODE>valid_min</CODE> and <CODE>valid_max</CODE> may be used for this purpose. For example, if you intend that a byte variable store only nonnegative values, you can use <CODE>valid_min = 0</CODE> and <CODE>valid_max = 255</CODE>. This attribute is ignored by the netCDF library.<TR>
<TD><CODE><A NAME=MARKER-2-51></A><A NAME=MARKER-2-52></A>C_format</CODE><TD>A character array providing the format that should be used by C applications to <A NAME=MARKER-2-53></A><A NAME=MARKER-2-54></A>print values for this variable. For example, if you know a variable is only accurate to three significant digits, it would be appropriate to define the <CODE>C_format</CODE> attribute as <CODE>"%.3g"</CODE>. The <CODE>ncdump</CODE> utility program uses this attribute for variables for which it is defined. The format applies to the scaled (internal) type and value, regardless of the presence of the scaling attributes <CODE>scale_factor</CODE> and <CODE>add_offset</CODE>.<TR>
<TD><CODE><A NAME=MARKER-2-55></A><A NAME=MARKER-2-56></A>FORTRAN_format</CODE><TD>A character array providing the format that should be used by FORTRAN applications to print values for this variable. For example, if you know a variable is only accurate to three significant digits, it would be appropriate to define the <CODE>FORTRAN_format</CODE> attribute as <CODE>"(G10.3)"</CODE>.<TR>
<TD><CODE><A NAME=MARKER-2-57></A><A NAME=MARKER-2-58></A>title</CODE><TD>A global attribute that is a character array providing a succinct description of what is in the dataset.<TR>
<TD><CODE><A NAME=MARKER-2-59></A><A NAME=MARKER-2-60></A>history</CODE><TD>A global attribute for an <A NAME=MARKER-2-62></A><A NAME=MARKER-2-63></A><A NAME=MARKER-2-64></A>audit trail. This is a character array with a line for each invocation of a program that has modified the dataset. Well-behaved generic netCDF applications should append a line containing: date, time of day, user name, program name and command arguments.<TR>
<TD><CODE><A NAME=MARKER-2-67></A><A NAME=MARKER-2-68></A>Conventions</CODE><TD>If present, '<CODE>Conventions</CODE>' is a global attribute that is a character array for the name of the conventions followed by the dataset, in the form of a string that is interpreted as a directory name relative to a directory that is a repository of documents describing sets of discipline-specific conventions. This permits a hierarchical structure for conventions and provides a place where descriptions and examples of the conventions may be maintained by the defining institutions and groups. The conventions directory name is currently interpreted relative to the directory <CODE>pub/netcdf/Conventions/</CODE> on the host machine <CODE>ftp.unidata.ucar.edu</CODE>. Alternatively, a full URL specification may be used to name a WWW site where documents that describe the conventions are maintained. <TR>
<TD> <TD>For example, if a group named NUWG agrees upon a set of conventions for dimension names, variable names, required attributes, and netCDF representations for certain <A NAME=MARKER-2-69></A><A NAME=MARKER-2-70></A>discipline-specific data structures, they may store a document describing the agreed-upon conventions in a dataset in the <CODE>NUWG/</CODE> subdirectory of the Conventions directory. Datasets that followed these conventions would contain a global <CODE>Conventions</CODE> attribute with value <CODE>"NUWG"</CODE>. <TR>
<TD> <TD>Later, if the group agrees upon some additional conventions for a specific subset of NUWG data, for example time series data, the description of the additional conventions might be stored in the <CODE>NUWG/Time_series/</CODE> subdirectory, and datasets that adhered to these additional conventions would use the global <CODE>Conventions</CODE> attribute with value <CODE>"NUWG/Time_series"</CODE>, implying that this dataset adheres to the NUWG conventions and also to the additional NUWG time-series conventions.</TABLE>
<P>
<A NAME=HEADING13-14></A>
<H2>8.2 Create an Attribute: <CODE><A NAME=MARKER-2-2865></A>nc_put_att<I>_type</I></CODE> <A NAME=MARKER-10-2866></A><B></B></H2>
<HR>
The function <CODE>nc_put_att_<B>type</B></CODE> <A NAME=MARKER-10-2867></A>adds or changes a variable attribute or global attribute of an open netCDF dataset. If this attribute is new, or if the space required to store the attribute is greater than before, the netCDF dataset must be in <A NAME=MARKER-2-2868></A><A NAME=MARKER-2-2869></A><A NAME=MARKER-2-2870></A>define mode.<P>
<A NAME=HEADING13-16></A>
<H4>Usage </H4>
Although it's possible to create attributes of all types, text and double attributes are adequate for most purposes.<P>
<PRE>
int nc_put_att_text (int ncid, int varid, const char *name,
size_t len, const char *tp);
int nc_put_att_uchar (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const unsigned char *up);
int nc_put_att_schar (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const signed char *cp);
int nc_put_att_short (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const short *sp);
int nc_put_att_int (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const int *ip);
int nc_put_att_long (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const long *lp);
int nc_put_att_float (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const float *fp);
int nc_put_att_double (int ncid, int varid, const char *name,
nc_type xtype, size_t len, const double *dp);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE>varid</CODE><TD>Variable ID of the variable to which the attribute will be assigned or <CODE>NC_GLOBAL</CODE> for a global attribute.<TR>
<TD><CODE>name</CODE><TD><A NAME=MARKER-2-184></A><A NAME=MARKER-2-185></A><A NAME=MARKER-2-186></A>Attribute name. Must begin with an alphabetic character, followed by zero or more alphanumeric characters including the underscore ('<CODE>_</CODE>'). Case is significant. Attribute name conventions are assumed by some netCDF generic applications, e.g., <CODE>units</CODE> as the name for a string attribute that gives the units for a netCDF variable. <A HREF=#MARKER-9-2860>See Section 8.1 "Attribute Conventions," page 81</A>, for examples of attribute conventions. <TR>
<TD><CODE>xtype</CODE><TD>One of the set of predefined netCDF external data types. The type of this parameter, <CODE>nc_type</CODE>, is defined in the netCDF header file. The valid netCDF external data types are <CODE>NC_BYTE</CODE>, <CODE>NC_CHAR</CODE>, <CODE>NC_SHORT</CODE>, <CODE>NC_INT</CODE>, <CODE>NC_FLOAT</CODE>, and <CODE>NC_DOUBLE</CODE>. Although it's possible to create attributes of all types, <CODE>NC_CHAR</CODE> and <CODE>NC_DOUBLE</CODE> attributes are adequate for most purposes. <TR>
<TD><CODE>len</CODE><TD>Number of <A NAME=MARKER-2-189></A><A NAME=MARKER-2-190></A><A NAME=MARKER-2-191></A><A NAME=MARKER-2-192></A>values provided for the attribute.<TR>
<TD><CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE> dp</CODE><TD>Pointer to one or more values. If the type of values differs from the netCDF attribute type specified as <CODE>xtype</CODE>, type conversion will occur. <A HREF=guidec-8.html#MARKER-9-1913>See Section 3.3 "Type Conversion," page 20</A>, for details.</TABLE>
</PRE>
Errors <A NAME=MARKER-10-2871></A><P>
<CODE>nc_put_att_<B>type</B></CODE> <A NAME=MARKER-10-2872></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2873></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.<P>
<LI>The specified netCDF type is invalid.<P>
<LI>The specified length is negative.<P>
<LI>The specified open netCDF dataset is in data mode and the specified attribute would expand.<P>
<LI>The specified open netCDF dataset is in data mode and the specified attribute does not already exist.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
<LI>The <A NAME=MARKER-2-2874></A>number of attributes for this variable exceeds <CODE><A NAME=MARKER-2-2875></A>NC_MAX_ATTRS</CODE>.<A NAME=MARKER-10-2876></A><P>
</UL>
<A NAME=HEADING13-43></A>
<H4>Example </H4>
Here is an example using <CODE><A NAME=MARKER-2-2877></A>nc_put_att_double</CODE> <A NAME=MARKER-10-2878></A>to add a variable attribute named <CODE>valid_range</CODE> for a netCDF variable named <CODE>rh</CODE> and a global attribute named <CODE>title</CODE> to an existing netCDF dataset named <CODE>foo.nc</CODE>: <P>
<PRE>
#include <netcdf.h>
...
int status; /* error status */
int ncid; /* netCDF ID */
int rh_id; /* variable ID */
static double rh_range[] = {0.0, 100.0};/* attribute vals */
static char title[] = "example netCDF dataset";
...
status = nc_open("foo.nc", NC_WRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
...
status = nc_redef(ncid); /* enter define mode */
if (status != NC_NOERR) handle_error(status);
status = nc_inq_varid (ncid, "rh", &rh_id);
if (status != NC_NOERR) handle_error(status);
...
status = nc_put_att_double (ncid, rh_id, "valid_range",
NC_DOUBLE, 2, rh_range);
if (status != NC_NOERR) handle_error(status);
status = nc_put_att_text (ncid, NC_GLOBAL, "title",
NC_CHAR, strlen(title), title)
if (status != NC_NOERR) handle_error(status);
...
status = nc_enddef(ncid); /* leave define mode */
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING13-70></A>
<H2>8.3 <A NAME=MARKER-10-2879></A>Get Information about an Attribute: <CODE><A NAME=MARKER-2-2880></A>nc_inq_att</CODE> <A NAME=MARKER-10-2881></A>Family</H2>
<HR>
This family of functions returns <A NAME=MARKER-2-2882></A><A NAME=MARKER-2-2883></A><A NAME=MARKER-2-2884></A><A NAME=MARKER-2-2885></A><A NAME=MARKER-2-2886></A>information about a netCDF attribute. All but one of these functions require the variable ID and attribute name; the exception is<CODE>nc_inq_attname</CODE>. <CODE><A NAME=MARKER-10-2887></A></CODE> Information about an attribute includes its type, length, name, and number. See the <CODE>nc_get_att</CODE> <A NAME=MARKER-10-2888></A>family for getting attribute values.<P>
The function <CODE>nc_inq_attname</CODE> <A NAME=MARKER-10-2889></A>gets the name of an attribute, given its variable ID and number. This function is useful in <A NAME=MARKER-2-2890></A><A NAME=MARKER-2-2891></A>generic applications that need to get the names of all the attributes associated with a variable, since attributes are accessed by name rather than number in all other attribute functions. The number of an attribute is more volatile than the name, since it can change when other attributes of the same variable are deleted. This is why an attribute number is not called an <A NAME=MARKER-2-2894></A><A NAME=MARKER-2-2895></A>attribute ID. <P>
The function <CODE>nc_inq_att</CODE> <A NAME=MARKER-10-2896></A>returns the attribute's type and length. The other functions each return just one item of information about an attribute.<P>
<A NAME=HEADING13-74></A>
<H4>Usage </H4>
<PRE>
int nc_inq_att (int ncid, int varid, const char *name,
nc_type *xtypep, size_t *lenp);
int <A NAME=MARKER-2-2897></A>nc_inq_atttype(int ncid, int varid, const char *name,
nc_type *xtypep);
int <A NAME=MARKER-2-2898></A>nc_inq_attlen (int ncid, int varid, const char *name, size_t *lenp);
int nc_inq_attname(int ncid, int varid, int attnum, char *name);
int <A NAME=MARKER-2-2899></A>nc_inq_attid (int ncid, int varid, const char *name, int *attnump);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE>varid</CODE><TD>Variable ID of the attribute's variable, or <CODE>NC_GLOBAL</CODE> for a global attribute.<TR>
<TD><CODE>name</CODE><TD>Attribute name. For <CODE>nc_inq_attname</CODE>, this is a pointer to the location for the returned attribute name.<TR>
<TD><CODE>xtypep</CODE><TD>Pointer to location for returned attribute type, one of the set of predefined netCDF external data types. The type of this parameter, <CODE>nc_type</CODE>, is defined in the netCDF header file. The valid netCDF external data types are <CODE>NC_BYTE</CODE>, <CODE>NC_CHAR</CODE>, <CODE>NC_SHORT</CODE>, <CODE>NC_INT</CODE>, <CODE>NC_FLOAT</CODE>, and <CODE>NC_DOUBLE</CODE>. If this parameter is given as '<CODE>0</CODE>' (a null pointer), no type will be returned so no variable to hold the type needs to be declared. <TR>
<TD><CODE>lenp</CODE><TD>Pointer to location for returned number of values currently stored in the attribute. For attributes of type <CODE>NC_CHAR</CODE>, you should not assume that this includes a trailing zero byte; it doesn't if the attribute was stored without a trailing zero byte, for example from a FORTRAN program. Before using the value as a C string, make sure it is null-terminated. If this parameter is given as '<CODE>0</CODE>' (a null pointer), no length will be returned so no variable to hold this information needs to be declared.<TR>
<TD>attnum<TD>For <CODE>nc_inq_attname</CODE>, attribute number. The <A NAME=MARKER-2-193></A><A NAME=MARKER-2-194></A>attributes for each variable are numbered from 0 (the first attribute) to <CODE>natts-1</CODE>, where <CODE>natts</CODE> is the number of attributes for the variable, as returned from a call to <CODE>nc_inq_varnatts</CODE>. <TR>
<TD><CODE>attnump</CODE><TD>For nc_inq_attid, pointer to location for returned attribute number that specifies which attribute this is for this variable (or which global attribute). If you already know the attribute name, knowing its number is not very useful, because accessing information about an attribute requires its name.</TABLE>
</PRE>
Errors <A NAME=MARKER-10-2900></A><P>
Each function returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2901></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.<P>
<LI>The specified attribute does not exist.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
<LI>For <CODE>nc_inq_attname</CODE>,<A NAME=MARKER-10-2902></A> the specified attribute number is negative or more than the number of attributes defined for the specified variable.<P>
</UL>
<A NAME=HEADING13-89></A>
<H4>Example </H4>
Here is an example using <CODE><A NAME=MARKER-2-2903></A>nc_inq_att</CODE> to find out the type and length of a variable attribute named <CODE>valid_range</CODE> for a netCDF variable named <CODE>rh</CODE> and a global attribute named <CODE>title</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>: <P>
<PRE>
#include <netcdf.h>
...
int status; /* error status */
int ncid; /* netCDF ID */
int rh_id; /* variable ID */
nc_type vr_type, t_type; /* attribute types */
int vr_len, t_len; /* attribute lengths */
...
status = nc_open("foo.nc", NC_NOWRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
...
status = nc_inq_varid (ncid, "rh", &rh_id);
if (status != NC_NOERR) handle_error(status);
...
status = nc_inq_att (ncid, rh_id, "valid_range", &vr_type, &vr_len);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_att (ncid, NC_GLOBAL, "title", &t_type, &t_len);
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING13-110></A>
<H2>8.4 <A NAME=MARKER-10-2904></A><A NAME=MARKER-2-2905></A><A NAME=MARKER-2-2906></A><A NAME=MARKER-2-2907></A>Get Attribute's Values:<CODE><A NAME=MARKER-2-2908></A>nc_get_att_<I>type</I></CODE> <A NAME=MARKER-10-2909></A><I></I></H2>
<HR>
Members of the <CODE>nc_get_att_<B>type</B></CODE> <A NAME=MARKER-10-2910></A>family of functions get the value(s) of a netCDF attribute, given its variable ID and name.<P>
<A NAME=HEADING13-112></A>
<H4>Usage </H4>
<PRE>
int nc_get_att_text (int ncid, int varid, const char *name,
char *tp);
int nc_get_att_uchar (int ncid, int varid, const char *name,
unsigned char *up);
int nc_get_att_schar (int ncid, int varid, const char *name,
signed char *cp);
int nc_get_att_short (int ncid, int varid, const char *name,
short *sp);
int nc_get_att_int (int ncid, int varid, const char *name,
int *ip);
int nc_get_att_long (int ncid, int varid, const char *name,
long *lp);
int nc_get_att_float (int ncid, int varid, const char *name,
float *fp);
int nc_get_att_double (int ncid, int varid, const char *name,
double *dp);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE>varid</CODE><TD>Variable ID of the <A NAME=MARKER-2-196></A>attribute's variable, or <CODE>NC_GLOBAL</CODE> for a global attribute.<TR>
<TD><CODE>name</CODE><TD>Attribute name.<TR>
<TD><CODE>tp, up, cp, sp, ip, lp, fp, </CODE>or<CODE> dp</CODE><TD>Pointer to location for returned attribute value(s). All elements of the vector of attribute values are returned, so you must allocate enough space to hold them. For attributes of type NC_CHAR, you should not assume that the returned values include a trailing zero byte; they won't if the attribute was stored without a trailing zero byte, for example from a FORTRAN program. Before using the value as a C string, make sure it is null-terminated. If you don't know how much space to reserve, call <CODE>nc_inq_attlen </CODE>first to find out the length of the attribute.</TABLE>
</PRE>
Errors <A NAME=MARKER-10-2911></A><P>
<CODE>nc_get_att_<B>type</B></CODE> <A NAME=MARKER-10-2912></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2913></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The variable ID is invalid for the specified netCDF dataset.<P>
<LI>The specified attribute does not exist.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
<LI>One or more of the attribute values are out of the range of values representable by the desired type.<P>
</UL>
<A NAME=HEADING13-135></A>
<H4>Example </H4>
Here is an example using <CODE><A NAME=MARKER-2-2914></A>nc_get_att_double</CODE> to determine the values of a variable attribute named <CODE>valid_range</CODE> for a netCDF variable named <CODE>rh</CODE> and a global attribute named <CODE>title</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>. In this example, it is assumed that we don't know how many values will be returned, but that we do know the types of the attributes. Hence, to allocate enough space to store them, we must first inquire about the length of the attributes.<P>
<PRE>
#include <netcdf.h>
...
int status; /* error status */
int ncid; /* netCDF ID */
int rh_id; /* variable ID */
int vr_len, t_len; /* attribute lengths */
double *vr_val; /* ptr to attribute values */
char *title; /* ptr to attribute values */
extern char *malloc(); /* memory allocator */
...
status = nc_open("foo.nc", NC_NOWRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
...
status = nc_inq_varid (ncid, "rh", &rh_id);
if (status != NC_NOERR) handle_error(status);
...
/* find out how much space is needed for attribute values */
status = nc_inq_attlen (ncid, rh_id, "valid_range", &vr_len);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_attlen (ncid, NC_GLOBAL, "title", &t_len);
if (status != NC_NOERR) handle_error(status);
/* allocate required space before retrieving values */
vr_val = (double *) malloc(vr_len * sizeof(double));
title = (char *) malloc(t_len + 1); /* + 1 for trailing null */
/* get attribute values */
status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val);
if (status != NC_NOERR) handle_error(status);
status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
if (status != NC_NOERR) handle_error(status);
title[t_len] = '\0'; /* null terminate */
...
</PRE>
<A NAME=HEADING13-171></A>
<H2>8.5 <A NAME=MARKER-10-2915></A><A NAME=MARKER-2-2916></A><A NAME=MARKER-2-2917></A>Copy Attribute from One NetCDF to Another: <CODE><A NAME=MARKER-2-2918></A>nc_copy_att</CODE> <CODE><A NAME=MARKER-10-2919></A></CODE></H2>
<HR>
The function <CODE>nc_copy_att</CODE> <A NAME=MARKER-10-2920></A>copies an attribute from one open netCDF dataset to another. It can also be used to copy an attribute from one variable to another within the same netCDF. <P>
<A NAME=HEADING13-173></A>
<H4>Usage </H4>
<PRE>
int nc_copy_att (int ncid_in, int varid_in, const char *name,
int ncid_out, int varid_out);
<TABLE BORDER="1"><TD>ncid_in<TD>The netCDF ID of an input netCDF dataset from which the attribute will be copied, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD>varid_in<TD>ID of the variable in the input netCDF dataset from which the attribute will be copied, or <CODE>NC_GLOBAL</CODE> for a global attribute.<TR>
<TD>name<TD>Name of the attribute in the input netCDF dataset to be copied.<TR>
<TD>ncid_out<TD>The netCDF ID of the output netCDF dataset to which the attribute will be copied, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>. It is permissible for the input and output netCDF IDs to be the same. The output netCDF dataset should be in define mode if the attribute to be copied does not already exist for the target variable, or if it would cause an existing target attribute to grow. <TR>
<TD>varid_out<TD>ID of the variable in the output netCDF dataset to which the attribute will be copied, or <CODE>NC_GLOBAL</CODE> to copy to a global attribute.</TABLE>
</PRE>
Errors <A NAME=MARKER-10-2921></A><P>
<CODE>nc_copy_att</CODE> <A NAME=MARKER-10-2922></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2923></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The input or output variable ID is invalid for the specified netCDF dataset.<P>
<LI>The specified attribute does not exist.<P>
<LI>The output netCDF is not in define mode and the attribute is new for the output dataset is larger than the existing attribute.<P>
<LI>The input or output netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING13-184></A>
<H4>Example </H4>
Here is an example using <CODE><A NAME=MARKER-2-2924></A>nc_copy_att</CODE> <A NAME=MARKER-10-2925></A>to copy the variable attribute <CODE>units</CODE> from the variable <CODE>rh</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE> to the variable <CODE>avgrh</CODE> in another existing netCDF dataset named <CODE>bar.nc</CODE>, assuming that the variable <CODE>avgrh</CODE> already exists, but does not yet have a <CODE>units </CODE>attribute: <P>
<PRE>
#include <netcdf.h>
...
int status; /* error status */
int ncid1, ncid2; /* netCDF IDs */
int rh_id, avgrh_id; /* variable IDs */
...
status = nc_open("foo.nc", NC_NOWRITE, ncid1);
if (status != NC_NOERR) handle_error(status);
status = nc_open("bar.nc", NC_WRITE, ncid2);
if (status != NC_NOERR) handle_error(status);
...
status = nc_inq_varid (ncid1, "rh", &rh_id);
if (status != NC_NOERR) handle_error(status);
status = nc_inq_varid (ncid2, "avgrh", &avgrh_id);
if (status != NC_NOERR) handle_error(status);
...
status = nc_redef(ncid2); /* enter define mode */
if (status != NC_NOERR) handle_error(status);
/* copy variable attribute from "rh" to "avgrh" */
status = nc_copy_att(ncid1, rh_id, "units", ncid2, avgrh_id);
if (status != NC_NOERR) handle_error(status);
...
status = nc_enddef(ncid2); /* leave define mode */
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING13-210></A>
<H2>8.6 <A NAME=MARKER-10-2926></A><A NAME=MARKER-2-2927></A><A NAME=MARKER-2-2928></A>Rename an Attribute: <CODE><A NAME=MARKER-2-2929></A>nc_rename_att</CODE> <A NAME=MARKER-10-2930></A><CODE></CODE></H2>
<HR>
The function <CODE>nc_rename_att</CODE> <A NAME=MARKER-10-2931></A>changes the name of an attribute. If the new name is longer than the original name, the netCDF dataset must be in define mode. You cannot rename an attribute to have the same name as another attribute of the same variable. <P>
<A NAME=HEADING13-212></A>
<H4>Usage </H4>
<PRE>
int nc_rename_att (int ncid, int varid, const char* name,
const char* newname);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE><TR>
<TD><CODE>varid</CODE><TD>ID of the attribute's variable, or <CODE>NC_GLOBAL</CODE> for a global attribute<TR>
<TD><CODE>name</CODE><TD>The current attribute name.<TR>
<TD><CODE>newname</CODE><TD>The new name to be assigned to the specified attribute. If the new name is longer than the current name, the netCDF dataset must be in define mode.</TABLE>
</PRE>
Errors <A NAME=MARKER-10-2932></A><P>
<CODE>nc_rename_att</CODE> <A NAME=MARKER-10-2933></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2934></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The specified variable ID is not valid.<P>
<LI>The new attribute name is already in use for another attribute of the specified variable.<P>
<LI>The specified netCDF dataset is in data mode and the new name is longer than the old name.<P>
<LI>The specified attribute does not exist.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING13-222></A>
<H4>Example </H4>
Here is an example using <CODE><A NAME=MARKER-2-2935></A>nc_rename_att</CODE> <A NAME=MARKER-10-2936></A>to rename the variable attribute <CODE>units</CODE> to <CODE>Units</CODE> for a variable <CODE>rh </CODE>in an existing netCDF dataset named <CODE>foo.nc</CODE>: <P>
<PRE>
#include <netcdf.h>
...
int status; /* error status */
int ncid; /* netCDF ID */
int rh_id; /* variable id */
...
status = nc_open("foo.nc", NC_NOWRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
...
status = nc_inq_varid (ncid, "rh", &rh_id);
if (status != NC_NOERR) handle_error(status);
...
/* rename attribute */
status = nc_rename_att(ncid, rh_id, "units", "Units");
if (status != NC_NOERR) handle_error(status);
</PRE>
<A NAME=HEADING13-239></A>
<H2>8.7 <A NAME=MARKER-10-2937></A><A NAME=MARKER-2-2938></A><A NAME=MARKER-2-2939></A><A NAME=MARKER-2-2940></A>Delete an Attribute: <CODE><A NAME=MARKER-2-2941></A>nc_del_att</CODE> <A NAME=MARKER-10-2942></A><CODE></CODE></H2>
<HR>
The function <CODE>nc_del_att</CODE> <A NAME=MARKER-10-2943></A>deletes a netCDF attribute from an open netCDF dataset. The netCDF dataset must be in define mode. <P>
<A NAME=HEADING13-241></A>
<H4>Usage </H4>
<PRE>
int nc_del_att (int ncid, int varid, const char* name);
<TABLE BORDER="1"><TD><CODE>ncid</CODE><TD>NetCDF ID, from a previous call to <CODE>nc_open</CODE> or <CODE>nc_create</CODE>.<TR>
<TD><CODE><A NAME=MARKER-2-199></A><A NAME=MARKER-2-200></A><A NAME=MARKER-2-201></A>varid</CODE><TD>ID of the attribute's variable, or <CODE>NC_GLOBAL</CODE> for a global attribute.<TR>
<TD><CODE>name</CODE><TD>The name of the attribute to be deleted.</TABLE>
</PRE>
Errors <A NAME=MARKER-10-2944></A><P>
<CODE>nc_del_att</CODE> <A NAME=MARKER-10-2945></A>returns the value <CODE>NC_NOERR <A NAME=MARKER-10-2946></A></CODE>if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:<P>
<UL>
<LI>The specified variable ID is not valid.<P>
<LI>The specified netCDF dataset is in data mode.<P>
<LI>The specified attribute does not exist.<P>
<LI>The specified netCDF ID does not refer to an open netCDF dataset.<P>
</UL>
<A NAME=HEADING13-249></A>
<H4>Example </H4>
Here is an example using <CODE><A NAME=MARKER-2-2947></A>nc_del_att</CODE> <A NAME=MARKER-10-2948></A>to delete the variable attribute <CODE>Units</CODE> for a variable <CODE>rh</CODE> in an existing netCDF dataset named <CODE>foo.nc</CODE>: <P>
<PRE>
#include <netcdf.h>
...
int status; /* error status */
int ncid; /* netCDF ID */
int rh_id; /* variable ID */
...
status = nc_open("foo.nc", NC_WRITE, &ncid);
if (status != NC_NOERR) handle_error(status);
...
status = nc_inq_varid (ncid, "rh", &rh_id);
if (status != NC_NOERR) handle_error(status);
...
/* delete attribute */
status = nc_redef(ncid); /* enter define mode */
if (status != NC_NOERR) handle_error(status);
status = nc_del_att(ncid, rh_id, "Units");
if (status != NC_NOERR) handle_error(status);
status = nc_enddef(ncid); /* leave define mode */
if (status != NC_NOERR) handle_error(status);
</PRE>
<!-- TOC --><DL>
<DT><A HREF="guidec-13.html#HEADING13-12"><B>8.1 </B> - Attribute Conventions</A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-14"><B>8.2 </B> - Create an Attribute: nc_put_att_type </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-16"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-43"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-70"><B>8.3 </B> - Get Information about an Attribute: nc_inq_att Family</A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-74"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-89"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-110"><B>8.4 </B> - Get Attribute's Values:nc_get_att_type </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-112"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-135"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-171"><B>8.5 </B> - Copy Attribute from One NetCDF to Another: nc_copy_att </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-173"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-184"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-210"><B>8.6 </B> - Rename an Attribute: nc_rename_att </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-212"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-222"><B>Example</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-239"><B>8.7 </B> - Delete an Attribute: nc_del_att </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-241"><B>Usage</B> - </A>
<DD>
<DT><A HREF="guidec-13.html#HEADING13-249"><B>Example</B> - </A>
<DD>
</DL>
<HR>
<ADDRESS>NetCDF User's Guide for C - 5 JUN 1997</ADDRESS>
<A HREF="guidec-14.html">[Next] </A><A HREF="guidec-12.html">[Previous] </A><A HREF="guidec-1.html">[Top] </A><A HREF="guidec-3.html">[Contents] </A><A HREF="guidec-21.html">[Index] </A><A HREF="http://www.unidata.ucar.edu/packages/netcdf/">[netCDF Home Page]</A><A HREF="http://www.unidata.ucar.edu/">[Unidata Home Page]</A><P>
</BODY>
|