ddim.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /** \file
  2. Dimension functions
  3. These functions define and inquire about dimensions.
  4. Copyright 2010 University Corporation for Atmospheric
  5. Research/Unidata. See COPYRIGHT file for more info.
  6. */
  7. #include "ncdispatch.h"
  8. /*! \defgroup dimensions Dimensions
  9. Dimensions are used to define the shape of data in netCDF.
  10. Dimensions for a netCDF dataset are defined when it is created, while
  11. the netCDF dataset is in define mode. Additional dimensions may be
  12. added later by reentering define mode. A netCDF dimension has a name
  13. and a length. In a netCDF classic or 64-bit offset file, at most one
  14. dimension can have the unlimited length, which means variables using
  15. this dimension can grow along this dimension. In a netCDF-4 file
  16. multiple unlimited dimensions are supported.
  17. There is a suggested limit (100) to the number of dimensions that can
  18. be defined in a single netCDF dataset. The limit is the value of the
  19. predefined macro NC_MAX_DIMS. The purpose of the limit is to make
  20. writing generic applications simpler. They need only provide an array
  21. of NC_MAX_DIMS dimensions to handle any netCDF dataset. The
  22. implementation of the netCDF library does not enforce this advisory
  23. maximum, so it is possible to use more dimensions, if necessary, but
  24. netCDF utilities that assume the advisory maximums may not be able to
  25. handle the resulting netCDF datasets.
  26. Ordinarily, the name and length of a dimension are fixed when the
  27. dimension is first defined. The name may be changed later, but the
  28. length of a dimension (other than the unlimited dimension) cannot be
  29. changed without copying all the data to a new netCDF dataset with a
  30. redefined dimension length.
  31. Dimension lengths in the C interface are type size_t rather than type
  32. int to make it possible to access all the data in a netCDF dataset on
  33. a platform that only supports a 16-bit int data type, for example
  34. MSDOS. If dimension lengths were type int instead, it would not be
  35. possible to access data from variables with a dimension length greater
  36. than a 16-bit int can accommodate.
  37. A netCDF dimension in an open netCDF dataset is referred to by a small
  38. integer called a dimension ID. In the C interface, dimension IDs are
  39. 0, 1, 2, ..., in the order in which the dimensions were defined.
  40. Operations supported on dimensions are:
  41. - Create a dimension, given its name and length.
  42. - Get a dimension ID from its name.
  43. - Get a dimension's name and length from its ID.
  44. - Rename a dimension.
  45. */
  46. /**@{*/
  47. /*! Define a new dimension. The function nc_def_dim adds a new
  48. dimension to an open netCDF dataset in define mode. It returns (as an
  49. argument) a dimension ID, given the netCDF ID, the dimension name, and
  50. the dimension length. At most one unlimited length dimension, called
  51. the record dimension, may be defined for each classic or 64-bit offset
  52. netCDF dataset. NetCDF-4 datasets may have multiple unlimited
  53. dimensions.
  54. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  55. nc_create(), nc_def_grp(), or associated inquiry functions such as
  56. nc_inq_ncid().
  57. \param name Name of the dimension to be created.
  58. \param len Length of the dimension to be created. Use NC_UNLIMITED for
  59. unlimited dimensions.
  60. \param idp Pointer where dimension ID will be stored.
  61. \retval ::NC_NOERR No error.
  62. \returns ::NC_EBADID Not a valid ID.
  63. \returns ::NC_ENOTINDEFINE Not in define mode.
  64. \returns ::NC_EDIMSIZE Invalid dimension size.
  65. \returns ::NC_EUNLIMIT NC_UNLIMITED size already in use
  66. \returns ::NC_EMAXDIMS NC_MAX_DIMS exceeded
  67. \returns ::NC_ENAMEINUSE String match to name in use
  68. \returns ::NC_ENOMEM Memory allocation (malloc) failure
  69. \returns ::NC_EPERM Write to read only
  70. \section Example
  71. Here is an example using nc_def_dim() to create a dimension named lat of
  72. length 18 and a unlimited dimension named rec in a new netCDF dataset
  73. named foo.nc:
  74. \code
  75. #include <netcdf.h>
  76. ...
  77. int status, ncid, latid, recid;
  78. ...
  79. status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
  80. if (status != NC_NOERR) handle_error(status);
  81. ...
  82. status = nc_def_dim(ncid, "lat", 18L, &latid);
  83. if (status != NC_NOERR) handle_error(status);
  84. status = nc_def_dim(ncid, "rec", NC_UNLIMITED, &recid);
  85. if (status != NC_NOERR) handle_error(status);
  86. \endcode
  87. */
  88. int
  89. nc_def_dim(int ncid, const char *name, size_t len, int *idp)
  90. {
  91. NC* ncp;
  92. int stat = NC_check_id(ncid, &ncp);
  93. if(stat != NC_NOERR) return stat;
  94. return ncp->dispatch->def_dim(ncid, name, len, idp);
  95. }
  96. /*!
  97. Find the ID of a dimension from the name.
  98. The function nc_inq_dimid returns (as an argument) the ID of a netCDF
  99. dimension, given the name of the dimension. If ndims is the number of
  100. dimensions defined for a netCDF dataset, each dimension has an ID
  101. between 0 and ndims-1.
  102. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  103. nc_create(), nc_def_grp(), or associated inquiry functions such as
  104. nc_inq_ncid().
  105. \param name Name of the dimension.
  106. \param idp Pointer where dimension ID will be stored.
  107. \returns ::NC_NOERR No error.
  108. \returns ::NC_EBADID Not a valid ID.
  109. \returns ::NC_EBADDIM Invalid dimension ID or name.
  110. */
  111. int
  112. nc_inq_dimid(int ncid, const char *name, int *idp)
  113. {
  114. NC* ncp;
  115. int stat = NC_check_id(ncid, &ncp);
  116. if(stat != NC_NOERR) return stat;
  117. return ncp->dispatch->inq_dimid(ncid,name,idp);
  118. }
  119. /*!
  120. Find the name and length of a dimension.
  121. The length for the unlimited dimension, if any, is the number of
  122. records written so far.
  123. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  124. nc_create(), nc_def_grp(), or associated inquiry functions such as
  125. nc_inq_ncid().
  126. \param dimid Dimension ID, from a previous call to nc_inq_dimid() or
  127. nc_def_dim().
  128. \param name Returned dimension name. The caller must allocate space
  129. for the returned name. The maximum possible length, in characters, of
  130. a dimension name is given by the predefined constant
  131. NC_MAX_NAME. (This doesn't include the null terminator, so declare
  132. your array to be size NC_MAX_NAME+1). The returned character array
  133. will be null-terminated.
  134. \param lenp Pointer to location for returned length of dimension. For
  135. the unlimited dimension, this is the number of records written so far.
  136. \returns ::NC_NOERR No error.
  137. \returns ::NC_EBADID Not a valid ID.
  138. \returns ::NC_EBADDIM Invalid dimension ID or name.
  139. \section Example
  140. Here is an example using nc_inq_dim() to determine the length of a
  141. dimension named lat, and the name and current maximum length of the
  142. unlimited dimension for an existing netCDF dataset named foo.nc:
  143. \code
  144. #include <netcdf.h>
  145. ...
  146. int status, ncid, latid, recid;
  147. size_t latlength, recs;
  148. char recname[NC_MAX_NAME+1];
  149. ...
  150. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  151. if (status != NC_NOERR) handle_error(status);
  152. status = nc_inq_unlimdim(ncid, &recid);
  153. if (status != NC_NOERR) handle_error(status);
  154. ...
  155. status = nc_inq_dimid(ncid, "lat", &latid);
  156. if (status != NC_NOERR) handle_error(status);
  157. status = nc_inq_dimlen(ncid, latid, &latlength);
  158. if (status != NC_NOERR) handle_error(status);
  159. status = nc_inq_dim(ncid, recid, recname, &recs);
  160. if (status != NC_NOERR) handle_error(status);
  161. \endcode
  162. */
  163. int
  164. nc_inq_dim(int ncid, int dimid, char *name, size_t *lenp)
  165. {
  166. NC* ncp;
  167. int stat = NC_check_id(ncid, &ncp);
  168. if(stat != NC_NOERR) return stat;
  169. return ncp->dispatch->inq_dim(ncid,dimid,name,lenp);
  170. }
  171. /*!
  172. Rename a dimension.
  173. This function renames an existing dimension in a netCDF dataset open
  174. for writing. You cannot rename a dimension to have the same name as
  175. another dimension.
  176. For netCDF classic and 64-bit offset files, if the new name is longer
  177. than the old name, the netCDF dataset must be in define mode.
  178. For netCDF-4 files the dataset is switched to define more for the
  179. rename, regardless of the name length.
  180. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  181. nc_create(), nc_def_grp(), or associated inquiry functions such as
  182. nc_inq_ncid().
  183. \param dimid Dimension ID, from a previous call to nc_inq_dimid() or
  184. nc_def_dim().
  185. \param name New name for dimension. Must be a null-terminated string
  186. with length less than NC_MAX_NAME.
  187. \returns ::NC_NOERR No error.
  188. \returns ::NC_EBADID Not a valid ID.
  189. \returns ::NC_EBADDIM Invalid dimension ID or name.
  190. \returns ::NC_ENAMEINUSE String match to name in use
  191. \returns ::NC_ENOMEM Memory allocation (malloc) failure
  192. \returns ::NC_EPERM Write to read only
  193. \section Example
  194. Here is an example using nc_rename_dim to rename the dimension lat to
  195. latitude in an existing netCDF dataset named foo.nc:
  196. \code
  197. #include <netcdf.h>
  198. ...
  199. int status, ncid, latid;
  200. ...
  201. status = nc_open("foo.nc", NC_WRITE, &ncid);
  202. if (status != NC_NOERR) handle_error(status);
  203. ...
  204. status = nc_redef(ncid);
  205. if (status != NC_NOERR) handle_error(status);
  206. status = nc_inq_dimid(ncid, "lat", &latid);
  207. if (status != NC_NOERR) handle_error(status);
  208. status = nc_rename_dim(ncid, latid, "latitude");
  209. if (status != NC_NOERR) handle_error(status);
  210. status = nc_enddef(ncid);
  211. if (status != NC_NOERR) handle_error(status);
  212. \endcode
  213. */
  214. int
  215. nc_rename_dim(int ncid, int dimid, const char *name)
  216. {
  217. NC* ncp;
  218. int stat = NC_check_id(ncid, &ncp);
  219. if(stat != NC_NOERR) return stat;
  220. return ncp->dispatch->rename_dim(ncid,dimid,name);
  221. }
  222. /*!
  223. Find the number of dimensions.
  224. In a classic model netCDF file, this funtion returns the number of
  225. defined dimensions. In a netCDF-4/HDF5 file, this function returns the
  226. number of dimensions available in the group specified by ncid, which
  227. may be less than the total number of dimensions in a file. In a
  228. netCDF-4/HDF5 file, dimensions are in all sub-groups, sub-sub-groups,
  229. etc.
  230. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  231. nc_create(), nc_def_grp(), or associated inquiry functions such as
  232. nc_inq_ncid().
  233. \param ndimsp Pointer where number of dimensions will be
  234. written. Ignored if NULL.
  235. \returns ::NC_NOERR No error.
  236. \returns ::NC_EBADID Not a valid ID.
  237. */
  238. int
  239. nc_inq_ndims(int ncid, int *ndimsp)
  240. {
  241. NC* ncp;
  242. int stat = NC_check_id(ncid, &ncp);
  243. if(stat != NC_NOERR) return stat;
  244. if(ndimsp == NULL) return NC_NOERR;
  245. return ncp->dispatch->inq(ncid,ndimsp,NULL,NULL,NULL);
  246. }
  247. /*!
  248. Find the ID of the unlimited dimension.
  249. This function finds the ID of the unlimited dimension. For
  250. netCDF-4/HDF5 files (which may have more than one unlimited
  251. dimension), the ID of the first unlimited dimesnion is returned. For
  252. these files, nc_inq_unlimdims() will return all the unlimited dimension IDs.
  253. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  254. nc_create(), nc_def_grp(), or associated inquiry functions such as
  255. nc_inq_ncid().
  256. \param unlimdimidp Pointer where unlimited dimension ID will be
  257. stored. If there is no unlimited dimension, -1 will be stored
  258. here. Ignored if NULL.
  259. \returns ::NC_NOERR No error.
  260. \returns ::NC_EBADID Not a valid ID.
  261. */
  262. int
  263. nc_inq_unlimdim(int ncid, int *unlimdimidp)
  264. {
  265. NC* ncp;
  266. int stat = NC_check_id(ncid, &ncp);
  267. if(stat != NC_NOERR) return stat;
  268. return ncp->dispatch->inq_unlimdim(ncid,unlimdimidp);
  269. }
  270. /*!
  271. Find out the name of a dimension.
  272. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  273. nc_create(), nc_def_grp(), or associated inquiry functions such as
  274. nc_inq_ncid().
  275. \param dimid Dimension ID, from a previous call to nc_inq_dimid() or
  276. nc_def_dim().
  277. \param name Returned dimension name. The caller must allocate space
  278. for the returned name. The maximum possible length, in characters, of
  279. a dimension name is given by the predefined constant
  280. NC_MAX_NAME. (This doesn't include the null terminator, so declare
  281. your array to be size NC_MAX_NAME+1). The returned character array
  282. will be null-terminated. Ignored if NULL.
  283. \returns ::NC_NOERR No error.
  284. \returns ::NC_EBADID Not a valid ID.
  285. \returns ::NC_EBADDIM Invalid dimension ID or name.
  286. \section Example
  287. Here is an example using nc_inq_dim() to determine the length of a
  288. dimension named lat, and the name and current maximum length of the
  289. unlimited dimension for an existing netCDF dataset named foo.nc:
  290. \code
  291. #include <netcdf.h>
  292. ...
  293. int status, ncid, latid, recid;
  294. size_t latlength, recs;
  295. char recname[NC_MAX_NAME+1];
  296. ...
  297. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  298. if (status != NC_NOERR) handle_error(status);
  299. status = nc_inq_unlimdim(ncid, &recid);
  300. if (status != NC_NOERR) handle_error(status);
  301. ...
  302. status = nc_inq_dimid(ncid, "lat", &latid);
  303. if (status != NC_NOERR) handle_error(status);
  304. status = nc_inq_dimlen(ncid, latid, &latlength);
  305. if (status != NC_NOERR) handle_error(status);
  306. status = nc_inq_dim(ncid, recid, recname, &recs);
  307. if (status != NC_NOERR) handle_error(status);
  308. \endcode
  309. */
  310. int
  311. nc_inq_dimname(int ncid, int dimid, char *name)
  312. {
  313. NC* ncp;
  314. int stat = NC_check_id(ncid, &ncp);
  315. if(stat != NC_NOERR) return stat;
  316. if(name == NULL) return NC_NOERR;
  317. return ncp->dispatch->inq_dim(ncid,dimid,name,NULL);
  318. }
  319. /*!
  320. Find the length of a dimension.
  321. The length for the unlimited dimension, if any, is the number of
  322. records written so far.
  323. \param ncid NetCDF or group ID, from a previous call to nc_open(),
  324. nc_create(), nc_def_grp(), or associated inquiry functions such as
  325. nc_inq_ncid().
  326. \param dimid Dimension ID, from a previous call to nc_inq_dimid() or
  327. nc_def_dim().
  328. \param lenp Pointer where the length will be stored.
  329. \returns ::NC_NOERR No error.
  330. \returns ::NC_EBADID Not a valid ID.
  331. \returns ::NC_EBADDIM Invalid dimension ID or name.
  332. \section Example
  333. Here is an example using nc_inq_dim() to determine the length of a
  334. dimension named lat, and the name and current maximum length of the
  335. unlimited dimension for an existing netCDF dataset named foo.nc:
  336. \code
  337. #include <netcdf.h>
  338. ...
  339. int status, ncid, latid, recid;
  340. size_t latlength, recs;
  341. char recname[NC_MAX_NAME+1];
  342. ...
  343. status = nc_open("foo.nc", NC_NOWRITE, &ncid);
  344. if (status != NC_NOERR) handle_error(status);
  345. status = nc_inq_unlimdim(ncid, &recid);
  346. if (status != NC_NOERR) handle_error(status);
  347. ...
  348. status = nc_inq_dimid(ncid, "lat", &latid);
  349. if (status != NC_NOERR) handle_error(status);
  350. status = nc_inq_dimlen(ncid, latid, &latlength);
  351. if (status != NC_NOERR) handle_error(status);
  352. status = nc_inq_dim(ncid, recid, recname, &recs);
  353. if (status != NC_NOERR) handle_error(status);
  354. \endcode
  355. */
  356. int
  357. nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
  358. {
  359. NC* ncp;
  360. int stat = NC_check_id(ncid, &ncp);
  361. if(stat != NC_NOERR) return stat;
  362. if(lenp == NULL) return NC_NOERR;
  363. return ncp->dispatch->inq_dim(ncid,dimid,NULL,lenp);
  364. }
  365. /**@}*/