dattinq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /** \file
  2. Attribute inquiry functions
  3. These functions find out about attributes.
  4. Copyright 2011 University Corporation for Atmospheric
  5. Research/Unidata. See \ref copyright file for more info. */
  6. #include "ncdispatch.h"
  7. /** \name Learning about Attributes
  8. Functions to learn about the attributes in a file. */
  9. /*! \{ */ /* All these functions are part of this named group... */
  10. /**
  11. \ingroup attributes
  12. Return information about a netCDF attribute.
  13. The function nc_inq_att returns the attribute's type and length.
  14. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  15. nc_create(), nc_def_grp(), or associated inquiry functions such as
  16. nc_inq_ncid().
  17. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  18. for a global attribute.
  19. \param name Pointer to the location for the returned attribute \ref
  20. object_name. \ref ignored_if_null.
  21. \param xtypep Pointer to location for returned attribute \ref
  22. data_type. \ref ignored_if_null.
  23. \param lenp Pointer to location for returned number of values
  24. currently stored in the attribute. For attributes of type ::NC_CHAR,
  25. you should not assume that this includes a trailing zero byte; it
  26. doesn't if the attribute was stored without a trailing zero byte, for
  27. example from a FORTRAN program. Before using the value as a C string,
  28. make sure it is null-terminated. \ref ignored_if_null.
  29. \section Example
  30. Here is an example using nc_inq_att() to find out the type and length of
  31. a variable attribute named valid_range for a netCDF variable named rh
  32. and a global attribute named title in an existing netCDF dataset named
  33. foo.nc:
  34. \code
  35. #include <netcdf.h>
  36. ...
  37. int status;
  38. int ncid;
  39. int rh_id;
  40. nc_type vr_type, t_type;
  41. size_t vr_len, t_len;
  42. ...
  43. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  44. if (status != NC_NOERR) handle_error(status);
  45. ...
  46. status = nc_inq_varid (ncid, "rh", &rh_id);
  47. if (status != NC_NOERR) handle_error(status);
  48. ...
  49. status = nc_inq_att (ncid, rh_id, "valid_range", &vr_type, &vr_len);
  50. if (status != NC_NOERR) handle_error(status);
  51. status = nc_inq_att (ncid, NC_GLOBAL, "title", &t_type, &t_len);
  52. if (status != NC_NOERR) handle_error(status);
  53. \endcode
  54. */
  55. int
  56. nc_inq_att(int ncid, int varid, const char *name, nc_type *xtypep,
  57. size_t *lenp)
  58. {
  59. NC* ncp;
  60. int stat = NC_check_id(ncid, &ncp);
  61. if(stat != NC_NOERR) return stat;
  62. return ncp->dispatch->inq_att(ncid, varid, name, xtypep, lenp);
  63. }
  64. /**
  65. \ingroup attributes
  66. Find an attribute ID.
  67. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  68. nc_create(), nc_def_grp(), or associated inquiry functions such as
  69. nc_inq_ncid().
  70. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL for
  71. a global attribute.
  72. \param name Attribute \ref object_name.
  73. \param idp Pointer to location for returned attribute number that
  74. specifies which attribute this is for this variable (or which global
  75. attribute). If you already know the attribute name, knowing its number
  76. is not very useful, because accessing information about an attribute
  77. requires its name.
  78. */
  79. int
  80. nc_inq_attid(int ncid, int varid, const char *name, int *idp)
  81. {
  82. NC* ncp;
  83. int stat = NC_check_id(ncid, &ncp);
  84. if(stat != NC_NOERR) return stat;
  85. return ncp->dispatch->inq_attid(ncid, varid, name, idp);
  86. }
  87. /**
  88. \ingroup attributes
  89. Find the name of an attribute.
  90. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  91. nc_create(), nc_def_grp(), or associated inquiry functions such as
  92. nc_inq_ncid().
  93. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  94. for a global attribute.
  95. \param attnum Attribute number. The attributes for each variable are
  96. numbered from 0 (the first attribute) to natts-1, where natts is the
  97. number of attributes for the variable, as returned from a call to
  98. nc_inq_varnatts().
  99. \param name Pointer to the location for the returned attribute \ref
  100. object_name.
  101. */
  102. int
  103. nc_inq_attname(int ncid, int varid, int attnum, char *name)
  104. {
  105. NC* ncp;
  106. int stat = NC_check_id(ncid, &ncp);
  107. if(stat != NC_NOERR) return stat;
  108. return ncp->dispatch->inq_attname(ncid, varid, attnum, name);
  109. }
  110. /**
  111. \ingroup attributes
  112. Find number of global or group attributes.
  113. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  114. nc_create(), nc_def_grp(), or associated inquiry functions such as
  115. nc_inq_ncid().
  116. \param nattsp Pointer where number of global or group attributes will be
  117. written. \ref ignored_if_null.
  118. */
  119. int
  120. nc_inq_natts(int ncid, int *nattsp)
  121. {
  122. NC* ncp;
  123. int stat = NC_check_id(ncid, &ncp);
  124. if(stat != NC_NOERR) return stat;
  125. if(nattsp == NULL) return NC_NOERR;
  126. return ncp->dispatch->inq(ncid, NULL, NULL, nattsp, NULL);
  127. }
  128. /**
  129. \ingroup attributes
  130. Find the type of an attribute.
  131. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  132. nc_create(), nc_def_grp(), or associated inquiry functions such as
  133. nc_inq_ncid().
  134. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  135. for a global or group attribute.
  136. \param name Attribute \ref object_name.
  137. \param xtypep Pointer to location for returned attribute \ref data_type.
  138. */
  139. int
  140. nc_inq_atttype(int ncid, int varid, const char *name, nc_type *xtypep)
  141. {
  142. NC* ncp;
  143. int stat = NC_check_id(ncid, &ncp);
  144. if(stat != NC_NOERR) return stat;
  145. return ncp->dispatch->inq_att(ncid, varid, name, xtypep, NULL);
  146. }
  147. /**
  148. \ingroup attributes
  149. Find the length of an attribute.
  150. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  151. nc_create(), nc_def_grp(), or associated inquiry functions such as
  152. nc_inq_ncid().
  153. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  154. for a global or group attribute.
  155. \param name Attribute \ref object_name.
  156. \param lenp Pointer to location for returned number of values
  157. currently stored in the attribute. Before using the value as a C
  158. string, make sure it is null-terminated. \ref ignored_if_null.
  159. */
  160. int
  161. nc_inq_attlen(int ncid, int varid, const char *name, size_t *lenp)
  162. {
  163. NC* ncp;
  164. int stat = NC_check_id(ncid, &ncp);
  165. if(stat != NC_NOERR) return stat;
  166. return ncp->dispatch->inq_att(ncid, varid, name, NULL, lenp);
  167. }
  168. /*! \} */ /* End of named group ...*/