dvarinq.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*! \file
  2. Functions for inquiring about variables.
  3. Copyright 2010 University Corporation for Atmospheric
  4. Research/Unidata. See COPYRIGHT file for more info.
  5. */
  6. #include "ncdispatch.h"
  7. /** \name Learning about Variables
  8. Functions to learn about the variables in a file. */
  9. /*! \{ */ /* All these functions are part of this named group... */
  10. /**
  11. \ingroup variables
  12. Find the ID of a variable, from the name.
  13. The function nc_inq_varid returns the ID of a netCDF variable, given
  14. its name.
  15. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  16. nc_create(), nc_def_grp(), or associated inquiry functions such as
  17. nc_inq_ncid().
  18. \param name Name of the variable.
  19. \param varidp Pointer to location for returned variable ID. \ref
  20. ignored_if_null.
  21. \returns ::NC_NOERR No error.
  22. \returns ::NC_EBADID Bad ncid.
  23. \section Example
  24. Here is an example using nc_inq_varid to find out the ID of a variable
  25. named rh in an existing netCDF dataset named foo.nc:
  26. \code
  27. #include <netcdf.h>
  28. ...
  29. int status, ncid, rh_id;
  30. ...
  31. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  32. if (status != NC_NOERR) handle_error(status);
  33. ...
  34. status = nc_inq_varid (ncid, "rh", &rh_id);
  35. if (status != NC_NOERR) handle_error(status);
  36. \endcode
  37. */
  38. int
  39. nc_inq_varid(int ncid, const char *name, int *varidp)
  40. {
  41. NC* ncp;
  42. int stat = NC_check_id(ncid, &ncp);
  43. if(stat != NC_NOERR) return stat;
  44. return ncp->dispatch->inq_varid(ncid, name, varidp);
  45. }
  46. /**
  47. \ingroup variables
  48. Learn about a variable.
  49. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  50. nc_create(), nc_def_grp(), or associated inquiry functions such as
  51. nc_inq_ncid().
  52. \param varid Variable ID
  53. \param name Returned \ref object_name of variable. \ref
  54. ignored_if_null.
  55. \param xtypep Pointer where typeid will be stored. \ref ignored_if_null.
  56. \param ndimsp Pointer where number of dimensions will be
  57. stored. \ref ignored_if_null.
  58. \param dimidsp Pointer where array of dimension IDs will be
  59. stored. \ref ignored_if_null.
  60. \param nattsp Pointer where number of attributes will be
  61. stored. \ref ignored_if_null.
  62. \returns ::NC_NOERR No error.
  63. \returns ::NC_EBADID Bad ncid.
  64. \returns ::NC_ENOTVAR Invalid variable ID.
  65. \section Example
  66. Here is an example using nc_inq_var() to find out about a variable named
  67. rh in an existing netCDF dataset named foo.nc:
  68. \code
  69. #include <netcdf.h>
  70. ...
  71. int status
  72. int ncid;
  73. int rh_id;
  74. nc_type rh_type;
  75. int rh_ndims;
  76. int rh_dimids[NC_MAX_VAR_DIMS];
  77. int rh_natts
  78. ...
  79. status = nc_open ("foo.nc", NC_NOWRITE, &ncid);
  80. if (status != NC_NOERR) handle_error(status);
  81. ...
  82. status = nc_inq_varid (ncid, "rh", &rh_id);
  83. if (status != NC_NOERR) handle_error(status);
  84. status = nc_inq_var (ncid, rh_id, 0, &rh_type, &rh_ndims, rh_dimids,
  85. &rh_natts);
  86. if (status != NC_NOERR) handle_error(status);
  87. \endcode
  88. */
  89. int
  90. nc_inq_var(int ncid, int varid, char *name, nc_type *xtypep,
  91. int *ndimsp, int *dimidsp, int *nattsp)
  92. {
  93. NC* ncp;
  94. int stat = NC_check_id(ncid, &ncp);
  95. if(stat != NC_NOERR) return stat;
  96. return ncp->dispatch->inq_var_all(ncid, varid, name, xtypep, ndimsp,
  97. dimidsp, nattsp, NULL, NULL, NULL,
  98. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
  99. }
  100. /**
  101. \ingroup variables
  102. Learn the name of a variable.
  103. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  104. nc_create(), nc_def_grp(), or associated inquiry functions such as
  105. nc_inq_ncid().
  106. \param varid Variable ID
  107. \param name Returned variable name. The caller must allocate space for
  108. the returned name. The maximum length is ::NC_MAX_NAME. Ignored if
  109. NULL.
  110. \returns ::NC_NOERR No error.
  111. \returns ::NC_EBADID Bad ncid.
  112. \returns ::NC_ENOTVAR Invalid variable ID.
  113. */
  114. int
  115. nc_inq_varname(int ncid, int varid, char *name)
  116. {
  117. return nc_inq_var(ncid, varid, name, NULL, NULL,
  118. NULL, NULL);
  119. }
  120. /** Learn the type of a variable.
  121. \ingroup variables
  122. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  123. nc_create(), nc_def_grp(), or associated inquiry functions such as
  124. nc_inq_ncid().
  125. \param varid Variable ID
  126. \param typep Pointer where typeid will be stored. \ref ignored_if_null.
  127. \returns ::NC_NOERR No error.
  128. \returns ::NC_EBADID Bad ncid.
  129. \returns ::NC_ENOTVAR Invalid variable ID.
  130. */
  131. int
  132. nc_inq_vartype(int ncid, int varid, nc_type *typep)
  133. {
  134. return nc_inq_var(ncid, varid, NULL, typep, NULL,
  135. NULL, NULL);
  136. }
  137. /** Learn how many dimensions are associated with a variable.
  138. \ingroup variables
  139. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  140. nc_create(), nc_def_grp(), or associated inquiry functions such as
  141. nc_inq_ncid().
  142. \param varid Variable ID
  143. \param ndimsp Pointer where number of dimensions will be
  144. stored. \ref ignored_if_null.
  145. \returns ::NC_NOERR No error.
  146. \returns ::NC_EBADID Bad ncid.
  147. \returns ::NC_ENOTVAR Invalid variable ID.
  148. */
  149. int
  150. nc_inq_varndims(int ncid, int varid, int *ndimsp)
  151. {
  152. return nc_inq_var(ncid, varid, NULL, NULL, ndimsp, NULL, NULL);
  153. }
  154. /** Learn the dimension IDs associated with a variable.
  155. \ingroup variables
  156. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  157. nc_create(), nc_def_grp(), or associated inquiry functions such as
  158. nc_inq_ncid().
  159. \param varid Variable ID
  160. \param dimidsp Pointer where array of dimension IDs will be
  161. stored. \ref ignored_if_null.
  162. \returns ::NC_NOERR No error.
  163. \returns ::NC_EBADID Bad ncid.
  164. \returns ::NC_ENOTVAR Invalid variable ID.
  165. */
  166. int
  167. nc_inq_vardimid(int ncid, int varid, int *dimidsp)
  168. {
  169. return nc_inq_var(ncid, varid, NULL, NULL, NULL,
  170. dimidsp, NULL);
  171. }
  172. /** Learn how many attributes are associated with a variable.
  173. \ingroup variables
  174. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  175. nc_create(), nc_def_grp(), or associated inquiry functions such as
  176. nc_inq_ncid().
  177. \param varid Variable ID
  178. \param nattsp Pointer where number of attributes will be
  179. stored. \ref ignored_if_null.
  180. \returns ::NC_NOERR No error.
  181. \returns ::NC_EBADID Bad ncid.
  182. \returns ::NC_ENOTVAR Invalid variable ID.
  183. */
  184. int
  185. nc_inq_varnatts(int ncid, int varid, int *nattsp)
  186. {
  187. if (varid == NC_GLOBAL)
  188. return nc_inq_natts(ncid,nattsp);
  189. /*else*/
  190. return nc_inq_var(ncid, varid, NULL, NULL, NULL, NULL,
  191. nattsp);
  192. }
  193. #ifdef USE_NETCDF4
  194. /** \ingroup variables
  195. Learn the storage and deflate settings for a variable.
  196. This is a wrapper for nc_inq_var_all().
  197. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  198. nc_create(), nc_def_grp(), or associated inquiry functions such as
  199. nc_inq_ncid().
  200. \param varid Variable ID
  201. \param shufflep A 1 will be written here if the shuffle filter is
  202. turned on for this variable, and a 0 otherwise. \ref ignored_if_null.
  203. \param deflatep If this pointer is non-NULL, the nc_inq_var_deflate
  204. function will write a 1 if the deflate filter is turned on for this
  205. variable, and a 0 otherwise. \ref ignored_if_null.
  206. \param deflate_levelp If the deflate filter is in use for this
  207. variable, the deflate_level will be writen here. \ref ignored_if_null.
  208. \returns ::NC_NOERR No error.
  209. \returns ::NC_ENOTNC4 Not a netCDF-4 file.
  210. \returns ::NC_EBADID Bad ncid.
  211. \returns ::NC_ENOTVAR Invalid variable ID.
  212. */
  213. int
  214. nc_inq_var_deflate(int ncid, int varid, int *shufflep, int *deflatep,
  215. int *deflate_levelp)
  216. {
  217. NC* ncp;
  218. int stat = NC_check_id(ncid,&ncp);
  219. if(stat != NC_NOERR) return stat;
  220. return ncp->dispatch->inq_var_all(
  221. ncid, varid,
  222. NULL, /*name*/
  223. NULL, /*xtypep*/
  224. NULL, /*ndimsp*/
  225. NULL, /*dimidsp*/
  226. NULL, /*nattsp*/
  227. shufflep, /*shufflep*/
  228. deflatep, /*deflatep*/
  229. deflate_levelp, /*deflatelevelp*/
  230. NULL, /*fletcher32p*/
  231. NULL, /*contiguousp*/
  232. NULL, /*chunksizep*/
  233. NULL, /*nofillp*/
  234. NULL, /*fillvaluep*/
  235. NULL, /*endianp*/
  236. NULL, /*optionsmaskp*/
  237. NULL /*pixelsp*/
  238. );
  239. }
  240. /** \ingroup variables
  241. Learn the szip settings of a variable.
  242. This function returns the szip settings for a variable. NetCDF does
  243. not allow variables to be created with szip (due to license problems
  244. with the szip library), but we do enable read-only access of HDF5
  245. files with szip compression.
  246. This is a wrapper for nc_inq_var_all().
  247. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  248. nc_create(), nc_def_grp(), or associated inquiry functions such as
  249. nc_inq_ncid().
  250. \param varid Variable ID
  251. \param options_maskp The szip options mask will be copied to this
  252. pointer. \ref ignored_if_null.
  253. \param pixels_per_blockp The szip pixels per block will be copied
  254. here. \ref ignored_if_null.
  255. \returns ::NC_NOERR No error.
  256. \returns ::NC_EBADID Bad ncid.
  257. \returns ::NC_ENOTNC4 Not a netCDF-4 file.
  258. \returns ::NC_ENOTVAR Invalid variable ID.
  259. */
  260. int
  261. nc_inq_var_szip(int ncid, int varid, int *options_maskp, int *pixels_per_blockp)
  262. {
  263. NC* ncp;
  264. int stat = NC_check_id(ncid,&ncp);
  265. if(stat != NC_NOERR) return stat;
  266. return ncp->dispatch->inq_var_all(
  267. ncid, varid,
  268. NULL, /*name*/
  269. NULL, /*xtypep*/
  270. NULL, /*ndimsp*/
  271. NULL, /*dimidsp*/
  272. NULL, /*nattsp*/
  273. NULL, /*shufflep*/
  274. NULL, /*deflatep*/
  275. NULL, /*deflatelevelp*/
  276. NULL, /*fletcher32p*/
  277. NULL, /*contiguousp*/
  278. NULL, /*chunksizep*/
  279. NULL, /*nofillp*/
  280. NULL, /*fillvaluep*/
  281. NULL, /*endianp*/
  282. options_maskp, /*optionsmaskp*/
  283. pixels_per_blockp /*pixelsp*/
  284. );
  285. }
  286. /** \ingroup variables
  287. Learn the checksum settings for a variable.
  288. This is a wrapper for nc_inq_var_all().
  289. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  290. nc_create(), nc_def_grp(), or associated inquiry functions such as
  291. nc_inq_ncid().
  292. \param varid Variable ID
  293. \param fletcher32p Will be set to ::NC_FLETCHER32 if the fletcher32
  294. checksum filter is turned on for this variable, and ::NC_NOCHECKSUM if
  295. it is not. \ref ignored_if_null.
  296. \returns ::NC_NOERR No error.
  297. \returns ::NC_EBADID Bad ncid.
  298. \returns ::NC_ENOTNC4 Not a netCDF-4 file.
  299. \returns ::NC_ENOTVAR Invalid variable ID.
  300. */
  301. int
  302. nc_inq_var_fletcher32(int ncid, int varid, int *fletcher32p)
  303. {
  304. NC* ncp;
  305. int stat = NC_check_id(ncid,&ncp);
  306. if(stat != NC_NOERR) return stat;
  307. return ncp->dispatch->inq_var_all(
  308. ncid, varid,
  309. NULL, /*name*/
  310. NULL, /*xtypep*/
  311. NULL, /*ndimsp*/
  312. NULL, /*dimidsp*/
  313. NULL, /*nattsp*/
  314. NULL, /*shufflep*/
  315. NULL, /*deflatep*/
  316. NULL, /*deflatelevelp*/
  317. fletcher32p, /*fletcher32p*/
  318. NULL, /*contiguousp*/
  319. NULL, /*chunksizep*/
  320. NULL, /*nofillp*/
  321. NULL, /*fillvaluep*/
  322. NULL, /*endianp*/
  323. NULL, /*optionsmaskp*/
  324. NULL /*pixelsp*/
  325. );
  326. }
  327. /** \ingroup variables
  328. This is a wrapper for nc_inq_var_all().
  329. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  330. nc_create(), nc_def_grp(), or associated inquiry functions such as
  331. nc_inq_ncid().
  332. \param varid Variable ID
  333. \param storagep Address of returned storage property, returned as
  334. ::NC_CONTIGUOUS if this variable uses contiguous storage, or
  335. ::NC_CHUNKED if it uses chunked storage. \ref ignored_if_null.
  336. \param chunksizesp The chunksizes will be copied here. \ref
  337. ignored_if_null.
  338. \returns ::NC_NOERR No error.
  339. \returns ::NC_EBADID Bad ncid.
  340. \returns ::NC_ENOTNC4 Not a netCDF-4 file.
  341. \returns ::NC_ENOTVAR Invalid variable ID.
  342. */
  343. int
  344. nc_inq_var_chunking(int ncid, int varid, int *storagep, size_t *chunksizesp)
  345. {
  346. NC *ncp;
  347. int stat = NC_check_id(ncid, &ncp);
  348. if(stat != NC_NOERR) return stat;
  349. return ncp->dispatch->inq_var_all(ncid, varid, NULL, NULL, NULL, NULL,
  350. NULL, NULL, NULL, NULL, NULL, storagep,
  351. chunksizesp, NULL, NULL, NULL, NULL, NULL);
  352. }
  353. /** \ingroup variables
  354. Learn the fill mode of a variable.
  355. The fill mode of a variable is set by nc_def_var_fill().
  356. This is a wrapper for nc_inq_var_all().
  357. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  358. nc_create(), nc_def_grp(), or associated inquiry functions such as
  359. nc_inq_ncid().
  360. \param varid Variable ID
  361. \param no_fill Pointer to an integer which will get a 1 if no_fill
  362. mode is set for this variable. \ref ignored_if_null.
  363. \param fill_valuep A pointer which will get the fill value for this
  364. variable. \ref ignored_if_null.
  365. \returns ::NC_NOERR No error.
  366. \returns ::NC_EBADID Bad ncid.
  367. \returns ::NC_ENOTVAR Invalid variable ID.
  368. */
  369. int
  370. nc_inq_var_fill(int ncid, int varid, int *no_fill, void *fill_valuep)
  371. {
  372. NC* ncp;
  373. int stat = NC_check_id(ncid,&ncp);
  374. if(stat != NC_NOERR) return stat;
  375. return ncp->dispatch->inq_var_all(
  376. ncid, varid,
  377. NULL, /*name*/
  378. NULL, /*xtypep*/
  379. NULL, /*ndimsp*/
  380. NULL, /*dimidsp*/
  381. NULL, /*nattsp*/
  382. NULL, /*shufflep*/
  383. NULL, /*deflatep*/
  384. NULL, /*deflatelevelp*/
  385. NULL, /*fletcher32p*/
  386. NULL, /*contiguousp*/
  387. NULL, /*chunksizep*/
  388. no_fill, /*nofillp*/
  389. fill_valuep, /*fillvaluep*/
  390. NULL, /*endianp*/
  391. NULL, /*optionsmaskp*/
  392. NULL /*pixelsp*/
  393. );
  394. }
  395. /** \ingroup variables
  396. Find the endianness of a variable.
  397. This is a wrapper for nc_inq_var_all().
  398. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  399. nc_create(), nc_def_grp(), or associated inquiry functions such as
  400. nc_inq_ncid().
  401. \param varid Variable ID
  402. \param endianp Storage which will get ::NC_ENDIAN_LITTLE if this
  403. variable is stored in little-endian format, ::NC_ENDIAN_BIG if it is
  404. stored in big-endian format, and ::NC_ENDIAN_NATIVE if the endianness
  405. is not set, and the variable is not created yet.
  406. \returns ::NC_NOERR No error.
  407. \returns ::NC_ENOTNC4 Not a netCDF-4 file.
  408. \returns ::NC_EBADID Bad ncid.
  409. \returns ::NC_ENOTVAR Invalid variable ID.
  410. */
  411. int
  412. nc_inq_var_endian(int ncid, int varid, int *endianp)
  413. {
  414. NC* ncp;
  415. int stat = NC_check_id(ncid,&ncp);
  416. if(stat != NC_NOERR) return stat;
  417. return ncp->dispatch->inq_var_all(
  418. ncid, varid,
  419. NULL, /*name*/
  420. NULL, /*xtypep*/
  421. NULL, /*ndimsp*/
  422. NULL, /*dimidsp*/
  423. NULL, /*nattsp*/
  424. NULL, /*shufflep*/
  425. NULL, /*deflatep*/
  426. NULL, /*deflatelevelp*/
  427. NULL, /*fletcher32p*/
  428. NULL, /*contiguousp*/
  429. NULL, /*chunksizep*/
  430. NULL, /*nofillp*/
  431. NULL, /*fillvaluep*/
  432. endianp, /*endianp*/
  433. NULL, /*optionsmaskp*/
  434. NULL /*pixelsp*/
  435. );
  436. }
  437. /** Return number and list of unlimited dimensions.
  438. In netCDF-4 files, it's possible to have multiple unlimited
  439. dimensions. This function returns a list of the unlimited dimension
  440. ids visible in a group.
  441. Dimensions are visible in a group if they have been defined in that
  442. group, or any ancestor group.
  443. ncid
  444. NetCDF group ID, from a previous call to nc_open, nc_create, nc_def_grp, etc.
  445. nunlimdimsp
  446. A pointer to an int which will get the number of visible unlimited dimensions. Ignored if NULL.
  447. unlimdimidsp
  448. A pointer to an already allocated array of int which will get the ids of all visible unlimited dimensions. Ignored if NULL. To allocate the correct length for this array, call nc_inq_unlimdims with a NULL for this parameter and use the nunlimdimsp parameter to get the number of visible unlimited dimensions.
  449. Errors
  450. NC_NOERR
  451. No error.
  452. NC_EBADID
  453. Bad group id.
  454. NC_ENOTNC4
  455. Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
  456. NC_ESTRICTNC3
  457. This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
  458. NC_EHDFERR
  459. An error was reported by the HDF5 layer.
  460. */
  461. int
  462. nc_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
  463. {
  464. NC* ncp;
  465. int stat = NC_check_id(ncid,&ncp);
  466. if(stat != NC_NOERR) return stat;
  467. return ncp->dispatch->inq_unlimdims(ncid, nunlimdimsp,
  468. unlimdimidsp);
  469. }
  470. #endif /* USE_NETCDF4 */
  471. /*! \} */ /* End of named group ...*/