datt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /** \file
  2. Attribute functions
  3. These functions read and write attributes.
  4. Copyright 2010 University Corporation for Atmospheric
  5. Research/Unidata. See \ref copyright file for more info. */
  6. #include "ncdispatch.h"
  7. /** \defgroup attributes Attributes
  8. Attributes hold metadata about data and files.
  9. \image html ncatts.png "Attributes store metadata."
  10. Attributes may be associated with a netCDF variable to specify such
  11. properties as units, special values, maximum and minimum valid values,
  12. scaling factors, and offsets.
  13. Attributes for a netCDF dataset are defined when the dataset is first
  14. created, while the netCDF dataset is in define mode. Additional
  15. attributes may be added later by reentering define mode.
  16. A netCDF attribute has a netCDF variable to which it is assigned, a
  17. name, a type, a length, and a sequence of one or more values.
  18. An attribute is designated by its variable ID and name. When an
  19. attribute name is not known, it may be designated by its variable ID
  20. and number in order to determine its name, using the function
  21. nc_inq_attname().
  22. The attributes associated with a variable are typically defined
  23. immediately after the variable is created, while still in define
  24. mode. The data type, length, and value of an attribute may be changed
  25. even when in data mode, as long as the changed attribute requires no
  26. more space than the attribute as originally defined.
  27. It is also possible to have attributes that are not associated with
  28. any variable. These are called global attributes and are identified by
  29. using ::NC_GLOBAL as a variable pseudo-ID. Global attributes are usually
  30. related to the netCDF dataset as a whole and may be used for purposes
  31. such as providing a title or processing history for a netCDF dataset.
  32. Operations supported on attributes are:
  33. - Create an attribute, given its variable ID, name, data type, length,
  34. and value.
  35. - Get attribute's data type and length from its variable ID and name.
  36. - Get attribute's value from its variable ID and name.
  37. - Copy attribute from one netCDF variable to another.
  38. - Get name of attribute from its number.
  39. - Rename an attribute.
  40. - Delete an attribute.
  41. */
  42. /*! \{*/ /* All these functions are part of the above defgroup... */
  43. /** \name Deleting and Renaming Attributes
  44. Functions to delete or rename an attribute. */
  45. /*! \{ */ /* All these functions are part of this named group... */
  46. /*!
  47. Rename an attribute.
  48. The function nc_rename_att() changes the name of an attribute. If the
  49. new name is longer than the original name, the netCDF dataset must be
  50. in define mode. You cannot rename an attribute to have the same name
  51. as another attribute of the same variable.
  52. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  53. nc_create(), nc_def_grp(), or associated inquiry functions such as
  54. nc_inq_ncid().
  55. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL for
  56. a global attribute.
  57. \param name Attribute \ref object_name.
  58. \param newname The new attribute \ref object_name.
  59. <h1>Example</h1>
  60. Here is an example using nc_rename_att() to rename the variable
  61. attribute units to Units for a variable rh in an existing netCDF
  62. dataset named foo.nc:
  63. \code
  64. #include <netcdf.h>
  65. ...
  66. int status;
  67. int ncid;
  68. int rh_id;
  69. ...
  70. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  71. if (status != NC_NOERR) handle_error(status);
  72. ...
  73. status = nc_inq_varid (ncid, "rh", &rh_id);
  74. if (status != NC_NOERR) handle_error(status);
  75. ...
  76. status = nc_rename_att(ncid, rh_id, "units", "Units");
  77. if (status != NC_NOERR) handle_error(status);
  78. \endcode
  79. */
  80. int
  81. nc_rename_att(int ncid, int varid, const char *name, const char *newname)
  82. {
  83. NC* ncp;
  84. int stat = NC_check_id(ncid, &ncp);
  85. if(stat != NC_NOERR) return stat;
  86. return ncp->dispatch->rename_att(ncid, varid, name, newname);
  87. }
  88. /*!
  89. Delete an attribute.
  90. The function nc_del_att() deletes a netCDF attribute from an open
  91. netCDF dataset. The netCDF dataset must be in define mode.
  92. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  93. nc_create(), nc_def_grp(), or associated inquiry functions such as
  94. nc_inq_ncid().
  95. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  96. for a global attribute.
  97. \param name Attribute name.
  98. <h1>Example</h1>
  99. Here is an example using nc_del_att() to delete the variable attribute
  100. Units for a variable rh in an existing netCDF dataset named foo.nc:
  101. \code
  102. #include <netcdf.h>
  103. ...
  104. int status;
  105. int ncid;
  106. int rh_id;
  107. ...
  108. status = nc_open("foo.nc", NC_WRITE, &ncid);
  109. if (status != NC_NOERR) handle_error(status);
  110. ...
  111. status = nc_inq_varid (ncid, "rh", &rh_id);
  112. if (status != NC_NOERR) handle_error(status);
  113. ...
  114. status = nc_redef(ncid);
  115. if (status != NC_NOERR) handle_error(status);
  116. status = nc_del_att(ncid, rh_id, "Units");
  117. if (status != NC_NOERR) handle_error(status);
  118. status = nc_enddef(ncid);
  119. if (status != NC_NOERR) handle_error(status);
  120. \endcode
  121. */
  122. int
  123. nc_del_att(int ncid, int varid, const char *name)
  124. {
  125. NC* ncp;
  126. int stat = NC_check_id(ncid, &ncp);
  127. if(stat != NC_NOERR) return stat;
  128. return ncp->dispatch->del_att(ncid, varid, name);
  129. }
  130. /*! \} */ /* End of named group ...*/
  131. /*! \} */ /* End of defgroup. */