dattget.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /** \name Getting Attributes
  8. Functions to get the values of attributes.
  9. */
  10. /*! \{ */
  11. /*!
  12. \ingroup attributes
  13. Get an attribute of any type.
  14. The nc_get_att() functions works for any type of attribute, and must
  15. be used to get attributes of user-defined type. We recommend that they
  16. type safe versions of this function be used where possible.
  17. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  18. nc_create(), nc_def_grp(), or associated inquiry functions such as
  19. nc_inq_ncid().
  20. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  21. for a global attribute.
  22. \param name Attribute \ref object_name.
  23. \param value Pointer to location for returned attribute value(s). All
  24. elements of the vector of attribute values are returned, so you must
  25. allocate enough space to hold them. Before using the value as a C
  26. string, make sure it is null-terminated. Call nc_inq_attlen() first to
  27. find out the length of the attribute.
  28. */
  29. int
  30. nc_get_att(int ncid, int varid, const char *name, void *value)
  31. {
  32. NC* ncp;
  33. int stat = NC_NOERR;
  34. nc_type xtype;
  35. if ((stat = NC_check_id(ncid, &ncp)))
  36. return stat;
  37. /* Need to get the type */
  38. if ((stat = nc_inq_atttype(ncid, varid, name, &xtype)))
  39. return stat;
  40. return ncp->dispatch->get_att(ncid, varid, name, value, xtype);
  41. }
  42. /*! \} */
  43. /*!
  44. \ingroup attributes
  45. Get an attribute.
  46. This function gets an attribute from the netCDF file. The nc_get_att()
  47. function works with any type of data, including user defined types.
  48. \note The netCDF library reads all attributes into memory when the
  49. file is opened with nc_open(). Getting an attribute copies the value
  50. from the in-memory store, and does not incure any file I/O penalties.
  51. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  52. nc_create(), nc_def_grp(), or associated inquiry functions such as
  53. nc_inq_ncid().
  54. \param varid Variable ID of the attribute's variable, or ::NC_GLOBAL
  55. for a global attribute.
  56. \param name Attribute \ref object_name.
  57. \param value Pointer to location for returned attribute value(s). All
  58. elements of the vector of attribute values are returned, so you must
  59. allocate enough space to hold them. If you don't know how much
  60. space to reserve, call nc_inq_attlen() first to find out the length of
  61. the attribute.
  62. <h1>Example</h1>
  63. Here is an example using nc_get_att_double() to determine the values
  64. of a variable attribute named valid_range for a netCDF variable named
  65. rh and using nc_get_att_text() to read a global attribute named title
  66. in an existing netCDF dataset named foo.nc.
  67. In this example, it is assumed that we don't know how many values will
  68. be returned, but that we do know the types of the attributes. Hence,
  69. to allocate enough space to store them, we must first inquire about
  70. the length of the attributes.
  71. \code
  72. #include <netcdf.h>
  73. ...
  74. int status;
  75. int ncid;
  76. int rh_id;
  77. int vr_len, t_len;
  78. double *vr_val;
  79. char *title;
  80. extern char *malloc()
  81. ...
  82. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  83. if (status != NC_NOERR) handle_error(status);
  84. ...
  85. status = nc_inq_varid (ncid, "rh", &rh_id);
  86. if (status != NC_NOERR) handle_error(status);
  87. ...
  88. status = nc_inq_attlen (ncid, rh_id, "valid_range", &vr_len);
  89. if (status != NC_NOERR) handle_error(status);
  90. status = nc_inq_attlen (ncid, NC_GLOBAL, "title", &t_len);
  91. if (status != NC_NOERR) handle_error(status);
  92. vr_val = (double *) malloc(vr_len * sizeof(double));
  93. title = (char *) malloc(t_len + 1);
  94. status = nc_get_att_double(ncid, rh_id, "valid_range", vr_val);
  95. if (status != NC_NOERR) handle_error(status);
  96. status = nc_get_att_text(ncid, NC_GLOBAL, "title", title);
  97. if (status != NC_NOERR) handle_error(status);
  98. title[t_len] = '\0';
  99. ...
  100. \endcode
  101. */
  102. /*! \{ */
  103. int
  104. nc_get_att_text(int ncid, int varid, const char *name, char *value)
  105. {
  106. NC* ncp;
  107. int stat = NC_check_id(ncid, &ncp);
  108. if(stat != NC_NOERR) return stat;
  109. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_CHAR);
  110. }
  111. int
  112. nc_get_att_schar(int ncid, int varid, const char *name, signed char *value)
  113. {
  114. NC* ncp;
  115. int stat = NC_check_id(ncid, &ncp);
  116. if(stat != NC_NOERR) return stat;
  117. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_BYTE);
  118. }
  119. int
  120. nc_get_att_uchar(int ncid, int varid, const char *name, unsigned char *value)
  121. {
  122. NC* ncp;
  123. int stat = NC_check_id(ncid, &ncp);
  124. if(stat != NC_NOERR) return stat;
  125. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
  126. }
  127. int
  128. nc_get_att_short(int ncid, int varid, const char *name, short *value)
  129. {
  130. NC* ncp;
  131. int stat = NC_check_id(ncid, &ncp);
  132. if(stat != NC_NOERR) return stat;
  133. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_SHORT);
  134. }
  135. int
  136. nc_get_att_int(int ncid, int varid, const char *name, int *value)
  137. {
  138. NC* ncp;
  139. int stat = NC_check_id(ncid, &ncp);
  140. if(stat != NC_NOERR) return stat;
  141. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT);
  142. }
  143. int
  144. nc_get_att_long(int ncid, int varid, const char *name, long *value)
  145. {
  146. NC* ncp;
  147. int stat = NC_check_id(ncid, &ncp);
  148. if(stat != NC_NOERR) return stat;
  149. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, longtype);
  150. }
  151. int
  152. nc_get_att_float(int ncid, int varid, const char *name, float *value)
  153. {
  154. NC* ncp;
  155. int stat = NC_check_id(ncid, &ncp);
  156. if(stat != NC_NOERR) return stat;
  157. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_FLOAT);
  158. }
  159. int
  160. nc_get_att_double(int ncid, int varid, const char *name, double *value)
  161. {
  162. NC* ncp;
  163. int stat = NC_check_id(ncid, &ncp);
  164. if(stat != NC_NOERR) return stat;
  165. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_DOUBLE);
  166. }
  167. int
  168. nc_get_att_ubyte(int ncid, int varid, const char *name, unsigned char *value)
  169. {
  170. NC* ncp;
  171. int stat = NC_check_id(ncid, &ncp);
  172. if(stat != NC_NOERR) return stat;
  173. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UBYTE);
  174. }
  175. int
  176. nc_get_att_ushort(int ncid, int varid, const char *name, unsigned short *value)
  177. {
  178. NC* ncp;
  179. int stat = NC_check_id(ncid, &ncp);
  180. if(stat != NC_NOERR) return stat;
  181. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_USHORT);
  182. }
  183. int
  184. nc_get_att_uint(int ncid, int varid, const char *name, unsigned int *value)
  185. {
  186. NC* ncp;
  187. int stat = NC_check_id(ncid, &ncp);
  188. if(stat != NC_NOERR) return stat;
  189. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT);
  190. }
  191. int
  192. nc_get_att_longlong(int ncid, int varid, const char *name, long long *value)
  193. {
  194. NC* ncp;
  195. int stat = NC_check_id(ncid, &ncp);
  196. if(stat != NC_NOERR) return stat;
  197. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_INT64);
  198. }
  199. int
  200. nc_get_att_ulonglong(int ncid, int varid, const char *name, unsigned long long *value)
  201. {
  202. NC *ncp;
  203. int stat = NC_check_id(ncid, &ncp);
  204. if(stat != NC_NOERR) return stat;
  205. return ncp->dispatch->get_att(ncid, varid, name, (void *)value, NC_UINT64);
  206. }
  207. int
  208. nc_get_att_string(int ncid, int varid, const char *name, char **value)
  209. {
  210. NC *ncp;
  211. int stat = NC_check_id(ncid, &ncp);
  212. if(stat != NC_NOERR) return stat;
  213. return ncp->dispatch->get_att(ncid,varid,name,(void*)value, NC_STRING);
  214. }
  215. /*! \} */