dcompound.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*! \file
  2. Functions for Compound Types
  3. Copyright 2011 University Corporation for Atmospheric
  4. Research/Unidata. See \ref copyright file for more info. */
  5. #include "ncdispatch.h"
  6. /** \name Compound Types
  7. Functions to create and learn about compound types. */
  8. /*! \{ */ /* All these functions are part of this named group... */
  9. /** \ingroup user_types
  10. Create a compound type. Provide an ncid, a name, and a total size (in
  11. bytes) of one element of the completed compound type.
  12. After calling this function, fill out the type with repeated calls to
  13. nc_insert_compound(). Call nc_insert_compound() once for each field
  14. you wish to insert into the compound type.
  15. \param ncid \ref ncid
  16. \param size The size, in bytes, of the compound type.
  17. \param name \ref object_name of the created type.
  18. \param typeidp The type ID of the new type is copied here.
  19. \returns ::NC_NOERR No error.
  20. \returns ::NC_EBADID Bad \ref ncid.
  21. \returns ::NC_EBADTYPE Bad type id.
  22. \returns ::NC_ENAMEINUSE That name is in use.
  23. \returns ::NC_EMAXNAME Name exceeds max length NC_MAX_NAME.
  24. \returns ::NC_EBADNAME Name contains illegal characters.
  25. \returns ::NC_ESTRICTNC3 Attempting a netCDF-4 operation on a netCDF-3 file.
  26. \returns ::NC_ENOTNC4 This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
  27. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  28. \returns ::NC_EPERM Attempt to write to a read-only file.
  29. \returns ::NC_ENOTINDEFINE Not in define mode.
  30. \section Example
  31. \code
  32. struct s1
  33. {
  34. int i1;
  35. int i2;
  36. };
  37. struct s1 data[DIM_LEN], data_in[DIM_LEN];
  38. if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
  39. if (nc_def_compound(ncid, sizeof(struct s1), SVC_REC, &typeid)) ERR;
  40. if (nc_insert_compound(ncid, typeid, BATTLES_WITH_KLINGONS,
  41. HOFFSET(struct s1, i1), NC_INT)) ERR;
  42. if (nc_insert_compound(ncid, typeid, DATES_WITH_ALIENS,
  43. HOFFSET(struct s1, i2), NC_INT)) ERR;
  44. if (nc_def_dim(ncid, STARDATE, DIM_LEN, &dimid)) ERR;
  45. if (nc_def_var(ncid, SERVICE_RECORD, typeid, 1, dimids, &varid)) ERR;
  46. if (nc_put_var(ncid, varid, data)) ERR;
  47. if (nc_close(ncid)) ERR;
  48. \endcode
  49. */
  50. int
  51. nc_def_compound(int ncid, size_t size, const char *name,
  52. nc_type *typeidp)
  53. {
  54. NC* ncp;
  55. int stat = NC_check_id(ncid,&ncp);
  56. if(stat != NC_NOERR) return stat;
  57. return ncp->dispatch->def_compound(ncid,size,name,typeidp);
  58. }
  59. /** \ingroup user_types
  60. Insert a named field into a compound type.
  61. \param ncid \ref ncid
  62. \param xtype The typeid for this compound type, as returned by
  63. nc_def_compound(), or nc_inq_var().
  64. \param name The \ref object_name of the new field.
  65. \param offset Offset in byte from the beginning of the compound type
  66. for this field.
  67. \param field_typeid The type of the field to be inserted.
  68. \returns ::NC_NOERR No error.
  69. \returns ::NC_EBADID Bad \ref ncid.
  70. \returns ::NC_EBADTYPE Bad type id.
  71. \returns ::NC_ENAMEINUSE That name is in use.
  72. \returns ::NC_EMAXNAME Name exceeds max length NC_MAX_NAME.
  73. \returns ::NC_EBADNAME Name contains illegal characters.
  74. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  75. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  76. \returns ::NC_EPERM Attempt to write to a read-only file.
  77. \returns ::NC_ENOTINDEFINE Not in define mode.
  78. */
  79. int
  80. nc_insert_compound(int ncid, nc_type xtype, const char *name,
  81. size_t offset, nc_type field_typeid)
  82. {
  83. NC *ncp;
  84. int stat = NC_check_id(ncid, &ncp);
  85. if(stat != NC_NOERR) return stat;
  86. return ncp->dispatch->insert_compound(ncid, xtype, name,
  87. offset, field_typeid);
  88. }
  89. /** \ingroup user_types
  90. Insert a named array field into a compound type.
  91. \param ncid \ref ncid
  92. \param xtype The typeid for this compound type, as returned by
  93. nc_def_compound(), or nc_inq_var().
  94. \param name The \ref object_name of the new field.
  95. \param offset Offset in byte from the beginning of the compound type
  96. for this field.
  97. \param field_typeid The type of the field to be inserted.
  98. \param ndims Number of dimensions in array.
  99. \param dim_sizes Array of dimension sizes.
  100. \returns ::NC_NOERR No error.
  101. \returns ::NC_EBADID Bad \ref ncid.
  102. \returns ::NC_EBADTYPE Bad type id.
  103. \returns ::NC_ENAMEINUSE That name is in use.
  104. \returns ::NC_EMAXNAME Name exceeds max length NC_MAX_NAME.
  105. \returns ::NC_EBADNAME Name contains illegal characters.
  106. \returns ::NC_ESTRICTNC3 Attempting a netCDF-4 operation on a netCDF-3 file.
  107. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  108. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  109. \returns ::NC_EPERM Attempt to write to a read-only file.
  110. \returns ::NC_ENOTINDEFINE Not in define mode.
  111. */
  112. int
  113. nc_insert_array_compound(int ncid, nc_type xtype, const char *name,
  114. size_t offset, nc_type field_typeid,
  115. int ndims, const int *dim_sizes)
  116. {
  117. NC* ncp;
  118. int stat = NC_check_id(ncid,&ncp);
  119. if(stat != NC_NOERR) return stat;
  120. return ncp->dispatch->insert_array_compound(ncid,xtype,name,offset,field_typeid,ndims,dim_sizes);
  121. }
  122. /** \ingroup user_types
  123. Learn about a compound type. Get the number of fields, len, and
  124. name of a compound type.
  125. \param ncid \ref ncid
  126. \param xtype The typeid for this compound type, as returned by
  127. nc_def_compound(), or nc_inq_var().
  128. \param name Returned \ref object_name of compound type. \ref
  129. ignored_if_null.
  130. \param sizep Returned size of compound type in bytes. \ref ignored_if_null.
  131. \param nfieldsp The number of fields in the compound type will be
  132. placed here. \ref ignored_if_null.
  133. \returns ::NC_NOERR No error.
  134. \returns ::NC_EBADID Bad \ref ncid.
  135. \returns ::NC_EBADTYPE Bad type id.
  136. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  137. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  138. */
  139. int
  140. nc_inq_compound(int ncid, nc_type xtype, char *name,
  141. size_t *sizep, size_t *nfieldsp)
  142. {
  143. int class = 0;
  144. int stat = nc_inq_user_type(ncid,xtype,name,sizep,NULL,nfieldsp,&class);
  145. if(stat != NC_NOERR) return stat;
  146. if(class != NC_COMPOUND) stat = NC_EBADTYPE;
  147. return stat;
  148. }
  149. /** \ingroup user_types
  150. Learn the name of a compound type.
  151. \param ncid \ref ncid
  152. \param xtype The typeid for this compound type, as returned by
  153. nc_def_compound(), or nc_inq_var().
  154. \param name Returned \ref object_name of compound type. \ref
  155. ignored_if_null.
  156. \returns ::NC_NOERR No error.
  157. \returns ::NC_EBADID Bad \ref ncid.
  158. \returns ::NC_EBADTYPE Bad type id.
  159. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  160. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  161. */
  162. int
  163. nc_inq_compound_name(int ncid, nc_type xtype, char *name)
  164. {
  165. return nc_inq_compound(ncid,xtype,name,NULL,NULL);
  166. }
  167. /** \ingroup user_types
  168. Learn the size of a compound type.
  169. \param ncid \ref ncid
  170. \param xtype The typeid for this compound type, as returned by
  171. nc_def_compound(), or nc_inq_var().
  172. \param sizep Returned size of compound type in bytes. \ref
  173. ignored_if_null.
  174. \returns ::NC_NOERR No error.
  175. \returns ::NC_EBADID Bad \ref ncid.
  176. \returns ::NC_EBADTYPE Bad type id.
  177. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  178. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  179. */
  180. int
  181. nc_inq_compound_size(int ncid, nc_type xtype, size_t *sizep)
  182. {
  183. return nc_inq_compound(ncid,xtype,NULL,sizep,NULL);
  184. }
  185. /** \ingroup user_types
  186. Learn the number of fields in a compound type.
  187. \param ncid \ref ncid
  188. \param xtype The typeid for this compound type, as returned by
  189. nc_def_compound(), or nc_inq_var().
  190. \param nfieldsp The number of fields in the compound type will be
  191. placed here. \ref ignored_if_null.
  192. \returns ::NC_NOERR No error.
  193. \returns ::NC_EBADID Bad \ref ncid.
  194. \returns ::NC_EBADTYPE Bad type id.
  195. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  196. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  197. */
  198. int
  199. nc_inq_compound_nfields(int ncid, nc_type xtype, size_t *nfieldsp)
  200. {
  201. return nc_inq_compound(ncid,xtype,NULL,NULL,nfieldsp);
  202. }
  203. /** \ingroup user_types
  204. Get information about one of the fields of a compound type.
  205. \param ncid \ref ncid
  206. \param xtype The typeid for this compound type, as returned by
  207. nc_def_compound(), or nc_inq_var().
  208. \param fieldid A zero-based index number specifying a field in the
  209. compound type.
  210. \param name Returned \ref object_name of the field. \ref
  211. ignored_if_null.
  212. \param offsetp A pointer which will get the offset of the field. \ref
  213. ignored_if_null.
  214. \param field_typeidp A pointer which will get the typeid of the
  215. field. \ref ignored_if_null.
  216. \param ndimsp A pointer which will get the number of dimensions of the
  217. field. \ref ignored_if_null.
  218. \param dim_sizesp A pointer which will get the dimension sizes of the
  219. field. \ref ignored_if_null.
  220. \returns ::NC_NOERR No error.
  221. \returns ::NC_EBADID Bad \ref ncid.
  222. \returns ::NC_EBADTYPE Bad type id.
  223. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  224. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  225. */
  226. int
  227. nc_inq_compound_field(int ncid, nc_type xtype, int fieldid,
  228. char *name, size_t *offsetp,
  229. nc_type *field_typeidp, int *ndimsp,
  230. int *dim_sizesp)
  231. {
  232. NC* ncp;
  233. int stat = NC_check_id(ncid,&ncp);
  234. if(stat != NC_NOERR) return stat;
  235. return ncp->dispatch->inq_compound_field(ncid, xtype, fieldid,
  236. name, offsetp, field_typeidp,
  237. ndimsp, dim_sizesp);
  238. }
  239. /** \ingroup user_types
  240. Get information about one of the fields of a compound type.
  241. \param ncid \ref ncid
  242. \param xtype The typeid for this compound type, as returned by
  243. nc_def_compound(), or nc_inq_var().
  244. \param fieldid A zero-based index number specifying a field in the
  245. compound type.
  246. \param name Returned \ref object_name of the field. \ref
  247. ignored_if_null.
  248. \returns ::NC_NOERR No error.
  249. \returns ::NC_EBADID Bad \ref ncid.
  250. \returns ::NC_EBADTYPE Bad type id.
  251. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  252. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  253. */
  254. int
  255. nc_inq_compound_fieldname(int ncid, nc_type xtype, int fieldid,
  256. char *name)
  257. {
  258. NC* ncp;
  259. int stat = NC_check_id(ncid,&ncp);
  260. if(stat != NC_NOERR) return stat;
  261. return ncp->dispatch->inq_compound_field(ncid, xtype, fieldid,
  262. name, NULL, NULL, NULL,
  263. NULL);
  264. }
  265. /** \ingroup user_types
  266. Get information about one of the fields of a compound type.
  267. \param ncid \ref ncid
  268. \param xtype The typeid for this compound type, as returned by
  269. nc_def_compound(), or nc_inq_var().
  270. \param fieldid A zero-based index number specifying a field in the
  271. compound type.
  272. \param offsetp A pointer which will get the offset of the field. \ref
  273. ignored_if_null.
  274. \returns ::NC_NOERR No error.
  275. \returns ::NC_EBADID Bad \ref ncid.
  276. \returns ::NC_EBADTYPE Bad type id.
  277. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  278. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  279. */
  280. int
  281. nc_inq_compound_fieldoffset(int ncid, nc_type xtype, int fieldid,
  282. size_t *offsetp)
  283. {
  284. NC* ncp;
  285. int stat = NC_check_id(ncid,&ncp);
  286. if(stat != NC_NOERR) return stat;
  287. return ncp->dispatch->inq_compound_field(ncid,xtype,fieldid,NULL,offsetp,NULL,NULL,NULL);
  288. }
  289. /** \ingroup user_types
  290. Get information about one of the fields of a compound type.
  291. \param ncid \ref ncid
  292. \param xtype The typeid for this compound type, as returned by
  293. nc_def_compound(), or nc_inq_var().
  294. \param fieldid A zero-based index number specifying a field in the
  295. compound type.
  296. \param field_typeidp A pointer which will get the typeid of the
  297. field. \ref ignored_if_null.
  298. \returns ::NC_NOERR No error.
  299. \returns ::NC_EBADID Bad \ref ncid.
  300. \returns ::NC_EBADTYPE Bad type id.
  301. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  302. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  303. */
  304. int
  305. nc_inq_compound_fieldtype(int ncid, nc_type xtype, int fieldid,
  306. nc_type *field_typeidp)
  307. {
  308. NC* ncp;
  309. int stat = NC_check_id(ncid,&ncp);
  310. if(stat != NC_NOERR) return stat;
  311. return ncp->dispatch->inq_compound_field(ncid,xtype,fieldid,NULL,NULL,field_typeidp,NULL,NULL);
  312. }
  313. /** \ingroup user_types
  314. Get information about one of the fields of a compound type.
  315. \param ncid \ref ncid
  316. \param xtype The typeid for this compound type, as returned by
  317. nc_def_compound(), or nc_inq_var().
  318. \param fieldid A zero-based index number specifying a field in the
  319. compound type.
  320. \param ndimsp A pointer which will get the number of dimensions of the
  321. field. \ref ignored_if_null.
  322. \returns ::NC_NOERR No error.
  323. \returns ::NC_EBADID Bad \ref ncid.
  324. \returns ::NC_EBADTYPE Bad type id.
  325. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  326. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  327. */
  328. int
  329. nc_inq_compound_fieldndims(int ncid, nc_type xtype, int fieldid,
  330. int *ndimsp)
  331. {
  332. NC* ncp;
  333. int stat = NC_check_id(ncid,&ncp);
  334. if(stat != NC_NOERR) return stat;
  335. return ncp->dispatch->inq_compound_field(ncid,xtype,fieldid,NULL,NULL,NULL,ndimsp,NULL);
  336. }
  337. /** \ingroup user_types
  338. Get information about one of the fields of a compound type.
  339. \param ncid \ref ncid
  340. \param xtype The typeid for this compound type, as returned by
  341. nc_def_compound(), or nc_inq_var().
  342. \param fieldid A zero-based index number specifying a field in the
  343. compound type.
  344. \param dim_sizesp A pointer which will get the dimension sizes of the
  345. field. \ref ignored_if_null.
  346. \returns ::NC_NOERR No error.
  347. \returns ::NC_EBADID Bad \ref ncid.
  348. \returns ::NC_EBADTYPE Bad type id.
  349. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  350. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  351. */
  352. int
  353. nc_inq_compound_fielddim_sizes(int ncid, nc_type xtype, int fieldid,
  354. int *dim_sizesp)
  355. {
  356. NC *ncp;
  357. int stat = NC_check_id(ncid, &ncp);
  358. if(stat != NC_NOERR) return stat;
  359. return ncp->dispatch->inq_compound_field(ncid, xtype, fieldid,
  360. NULL, NULL, NULL, NULL,
  361. dim_sizesp);
  362. }
  363. /** \ingroup user_types
  364. Learn the Index of a Named Field in a Compound Type. Get the index
  365. * of a field in a compound type from the name.
  366. \param ncid \ref ncid
  367. \param xtype The typeid for this compound type, as returned by
  368. nc_def_compound(), or nc_inq_var().
  369. \param name \ref object_name of the field. \ref ignored_if_null.
  370. \param fieldidp A pointer which will get the index of the named
  371. field. \ref ignored_if_null.
  372. \returns ::NC_NOERR No error.
  373. \returns ::NC_EBADID Bad \ref ncid.
  374. \returns ::NC_EBADTYPE Bad type id.
  375. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  376. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  377. */
  378. int
  379. nc_inq_compound_fieldindex(int ncid, nc_type xtype, const char *name,
  380. int *fieldidp)
  381. {
  382. NC* ncp;
  383. int stat = NC_check_id(ncid,&ncp);
  384. if(stat != NC_NOERR) return stat;
  385. return ncp->dispatch->inq_compound_fieldindex(ncid,xtype,name,fieldidp);
  386. }
  387. /*! \} */ /* End of named group ...*/