nc4dim.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. This file is part of netcdf-4, a netCDF-like interface for HDF5, or a
  3. HDF5 backend for netCDF, depending on your point of view.
  4. This file handles the nc4 dimension functions.
  5. Copyright 2003-5, University Corporation for Atmospheric Research. See
  6. the COPYRIGHT file for copying and redistribution conditions.
  7. $Id: nc4dim.c,v 1.41 2010/05/25 17:54:23 dmh Exp $
  8. */
  9. #include "nc4internal.h"
  10. #ifdef USE_PNETCDF
  11. #include <pnetcdf.h>
  12. #endif
  13. /* Netcdf-4 files might have more than one unlimited dimension, but
  14. return the first one anyway. */
  15. /* Note that this code is inconsistent with nc_inq */
  16. int
  17. NC4_inq_unlimdim(int ncid, int *unlimdimidp)
  18. {
  19. NC_FILE_INFO_T *nc;
  20. NC_GRP_INFO_T *grp, *g;
  21. NC_HDF5_FILE_INFO_T *h5;
  22. NC_DIM_INFO_T *dim;
  23. int found = 0;
  24. int retval;
  25. LOG((2, "called nc_inq_unlimdim"));
  26. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  27. return retval;
  28. #ifdef USE_PNETCDF
  29. /* Take care of files created/opened with parallel-netcdf library. */
  30. if (nc->pnetcdf_file)
  31. return ncmpi_inq_unlimdim(nc->int_ncid, unlimdimidp);
  32. #endif /* USE_PNETCDF */
  33. /* Take care of netcdf-3 files. */
  34. assert(h5);
  35. /* According to netcdf-3 manual, return -1 if there is no unlimited
  36. dimension. */
  37. *unlimdimidp = -1;
  38. for (g = grp; g && !found; g = g->parent)
  39. {
  40. for (dim = g->dim; dim; dim = dim->next)
  41. {
  42. if (dim->unlimited)
  43. {
  44. *unlimdimidp = dim->dimid;
  45. found++;
  46. break;
  47. }
  48. }
  49. }
  50. return NC_NOERR;
  51. }
  52. /* Dimensions are defined in attributes attached to the appropriate
  53. group in the data file. */
  54. int
  55. NC4_def_dim(int ncid, const char *name, size_t len, int *idp)
  56. {
  57. NC_FILE_INFO_T *nc;
  58. NC_GRP_INFO_T *grp;
  59. NC_HDF5_FILE_INFO_T *h5;
  60. NC_DIM_INFO_T *dim;
  61. char norm_name[NC_MAX_NAME + 1];
  62. int retval = NC_NOERR;
  63. LOG((2, "nc_def_dim: ncid 0x%x name %s len %d", ncid, name,
  64. (int)len));
  65. /* Find our global metadata structure. */
  66. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  67. return retval;
  68. #ifdef USE_PNETCDF
  69. /* Take care of files created/opened with parallel-netcdf library. */
  70. if (nc->pnetcdf_file)
  71. return ncmpi_def_dim(nc->int_ncid, name, len, idp);
  72. #endif /* USE_PNETCDF */
  73. /* Take care of netcdf-3 files. */
  74. assert(h5);
  75. assert(h5 && nc && grp);
  76. /* If the file is read-only, return an error. */
  77. if (h5->no_write)
  78. return NC_EPERM;
  79. /* Check some stuff if strict nc3 rules are in effect. */
  80. if (h5->cmode & NC_CLASSIC_MODEL)
  81. {
  82. /* Only one limited dimenson for strict nc3. */
  83. if (len == NC_UNLIMITED)
  84. for (dim = grp->dim; dim; dim = dim->next)
  85. if (dim->unlimited)
  86. return NC_EUNLIMIT;
  87. /* Must be in define mode for stict nc3. */
  88. if (!(h5->flags & NC_INDEF))
  89. return NC_ENOTINDEFINE;
  90. }
  91. /* If it's not in define mode, enter define mode. */
  92. if (!(h5->flags & NC_INDEF))
  93. if ((retval = nc_redef(ncid)))
  94. return retval;
  95. /* Make sure this is a valid netcdf name. */
  96. if ((retval = nc4_check_name(name, norm_name)))
  97. return retval;
  98. /* For classic model: dim length has to fit in a 32-bit unsigned
  99. * int, as permitted for 64-bit offset format. */
  100. if (h5->cmode & NC_CLASSIC_MODEL)
  101. if(len > X_UINT_MAX) /* Backward compat */
  102. return NC_EDIMSIZE;
  103. /* Make sure the name is not already in use. */
  104. for (dim = grp->dim; dim; dim = dim->next)
  105. if (!strncmp(dim->name, norm_name, NC_MAX_NAME))
  106. return NC_ENAMEINUSE;
  107. /* Add a dimension to the list. The ID must come from the file
  108. * information, since dimids are visible in more than one group. */
  109. nc4_dim_list_add(&grp->dim);
  110. grp->dim->dimid = grp->file->nc4_info->next_dimid++;
  111. /* Initialize the metadata for this dimension. */
  112. if (!(grp->dim->name = malloc((strlen(norm_name) + 1) * sizeof(char))))
  113. return NC_ENOMEM;
  114. strcpy(grp->dim->name, norm_name);
  115. grp->dim->len = len;
  116. grp->dim->dirty++;
  117. if (len == NC_UNLIMITED)
  118. grp->dim->unlimited++;
  119. /* Pass back the dimid. */
  120. if (idp)
  121. *idp = grp->dim->dimid;
  122. return retval;
  123. }
  124. /* Given dim name, find its id. */
  125. int
  126. NC4_inq_dimid(int ncid, const char *name, int *idp)
  127. {
  128. NC_FILE_INFO_T *nc;
  129. NC_GRP_INFO_T *grp, *g;
  130. NC_HDF5_FILE_INFO_T *h5;
  131. NC_DIM_INFO_T *dim;
  132. char norm_name[NC_MAX_NAME + 1];
  133. int finished = 0;
  134. int retval;
  135. LOG((2, "nc_inq_dimid: ncid 0x%x name %s", ncid, name));
  136. /* Find metadata for this file. */
  137. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  138. return retval;
  139. #ifdef USE_PNETCDF
  140. /* Take care of files created/opened with parallel-netcdf library. */
  141. if (nc->pnetcdf_file)
  142. return ncmpi_inq_dimid(nc->int_ncid, name, idp);
  143. #endif /* USE_PNETCDF */
  144. /* Handle netcdf-3 files. */
  145. assert(h5);
  146. assert(nc && grp);
  147. /* Normalize name. */
  148. if ((retval = nc4_normalize_name(name, norm_name)))
  149. return retval;
  150. /* Go through each dim and check for a name match. */
  151. for (g = grp; g && !finished; g = g->parent)
  152. for (dim = g->dim; dim; dim = dim->next)
  153. if (!strncmp(dim->name, norm_name, NC_MAX_NAME))
  154. {
  155. if (idp)
  156. *idp = dim->dimid;
  157. return NC_NOERR;
  158. }
  159. return NC_EBADDIM;
  160. }
  161. /* Find out name and len of a dim. For an unlimited dimension, the
  162. length is the largest lenght so far written. If the name of lenp
  163. pointers are NULL, they will be ignored. */
  164. int
  165. NC4_inq_dim(int ncid, int dimid, char *name, size_t *lenp)
  166. {
  167. NC_FILE_INFO_T *nc;
  168. NC_HDF5_FILE_INFO_T *h5;
  169. NC_GRP_INFO_T *grp, *dim_grp;
  170. NC_DIM_INFO_T *dim;
  171. int ret = NC_NOERR;
  172. LOG((2, "nc_inq_dim: ncid 0x%x dimid %d", ncid, dimid));
  173. /* Find our global metadata structure. */
  174. if ((ret = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  175. return ret;
  176. #ifdef USE_PNETCDF
  177. /* Take care of files created/opened with parallel-netcdf library. */
  178. if (nc->pnetcdf_file)
  179. {
  180. MPI_Offset mpi_len;
  181. if ((ret = ncmpi_inq_dim(nc->int_ncid, dimid, name, &mpi_len)))
  182. return ret;
  183. if (lenp)
  184. *lenp = mpi_len;
  185. }
  186. #endif /* USE_PNETCDF */
  187. /* Take care of netcdf-3 files. */
  188. assert(h5);
  189. assert(nc && grp);
  190. /* Find the dimension and its home group. */
  191. if ((ret = nc4_find_dim(grp, dimid, &dim, &dim_grp)))
  192. return ret;
  193. assert(dim);
  194. /* Return the dimension name, if the caller wants it. */
  195. if (name && dim->name)
  196. strcpy(name, dim->name);
  197. /* Return the dimension length, if the caller wants it. */
  198. if (lenp)
  199. {
  200. if (dim->unlimited)
  201. {
  202. /* Since this is an unlimited dimension, go to the file
  203. and see how many records there are. Take the max number
  204. of records from all the vars that share this
  205. dimension. */
  206. *lenp = 0;
  207. if ((ret = nc4_find_dim_len(dim_grp, dimid, &lenp)))
  208. return ret;
  209. }
  210. else
  211. {
  212. if (dim->too_long)
  213. {
  214. ret = NC_EDIMSIZE;
  215. *lenp = NC_MAX_UINT;
  216. }
  217. else
  218. *lenp = dim->len;
  219. }
  220. }
  221. return ret;
  222. }
  223. /* Rename a dimension, for those who like to prevaricate. */
  224. int
  225. NC4_rename_dim(int ncid, int dimid, const char *name)
  226. {
  227. NC_FILE_INFO_T *nc;
  228. NC_GRP_INFO_T *grp;
  229. NC_HDF5_FILE_INFO_T *h5;
  230. NC_DIM_INFO_T *dim;
  231. char norm_name[NC_MAX_NAME + 1];
  232. int retval;
  233. if (!name)
  234. return NC_EINVAL;
  235. LOG((2, "nc_rename_dim: ncid 0x%x dimid %d name %s", ncid,
  236. dimid, name));
  237. /* Find info for this file and group, and set pointer to each. */
  238. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  239. return retval;
  240. assert(nc);
  241. #ifdef USE_PNETCDF
  242. /* Take care of files created/opened with parallel-netcdf library. */
  243. if (nc->pnetcdf_file)
  244. return ncmpi_rename_dim(nc->int_ncid, dimid, name);
  245. #endif /* USE_PNETCDF */
  246. /* Handle netcdf-3 cases. */
  247. assert(h5);
  248. assert(h5 && grp);
  249. /* Trying to write to a read-only file? No way, Jose! */
  250. if (h5->no_write)
  251. return NC_EPERM;
  252. /* Make sure this is a valid netcdf name. */
  253. if ((retval = nc4_check_name(name, norm_name)))
  254. return retval;
  255. /* Make sure the new name is not already in use in this group. */
  256. for (dim = grp->dim; dim; dim = dim->next)
  257. if (!strncmp(dim->name, norm_name, NC_MAX_NAME))
  258. return NC_ENAMEINUSE;
  259. /* Find the dim. */
  260. for (dim = grp->dim; dim; dim = dim->next)
  261. if (dim->dimid == dimid)
  262. break;
  263. if (!dim)
  264. return NC_EBADDIM;
  265. /* If not in define mode, switch to it, unless the new name is
  266. * shorter. (This is in accordance with the v3 interface.) */
  267. /* if (!(h5->flags & NC_INDEF) && strlen(name) > strlen(dim->name)) */
  268. /* { */
  269. /* if (h5->cmode & NC_CLASSIC_MODEL) */
  270. /* return NC_ENOTINDEFINE; */
  271. /* if ((retval = nc_redef(ncid))) */
  272. /* return retval; */
  273. /* } */
  274. /* Save the old name, we'll need it to rename this object when we
  275. * sync to HDF5 file. But if there already is an old_name saved,
  276. * just stick with what we've got, since the user might be renaming
  277. * the crap out of this thing, without ever syncing with the
  278. * file. When the sync does take place, we only need the original
  279. * name of the dim, not any of the intermediate ones. If the user
  280. * could just make up his mind, we could all get on to writing some
  281. * data... */
  282. if (!dim->old_name)
  283. {
  284. if (!(dim->old_name = malloc((strlen(dim->name) + 1) * sizeof(char))))
  285. return NC_ENOMEM;
  286. strcpy(dim->old_name, dim->name);
  287. }
  288. /* Give the dimension its new name in metadata. UTF8 normalization
  289. * has been done. */
  290. free(dim->name);
  291. if (!(dim->name = malloc((strlen(norm_name) + 1) * sizeof(char))))
  292. return NC_ENOMEM;
  293. strcpy(dim->name, norm_name);
  294. return NC_NOERR;
  295. }
  296. /* Returns an array of unlimited dimension ids.The user can get the
  297. number of unlimited dimensions by first calling this with NULL for
  298. the second pointer.
  299. */
  300. int
  301. NC4_inq_unlimdims(int ncid, int *nunlimdimsp, int *unlimdimidsp)
  302. {
  303. NC_DIM_INFO_T *dim;
  304. NC_GRP_INFO_T *grp;
  305. NC_FILE_INFO_T *nc;
  306. NC_HDF5_FILE_INFO_T *h5;
  307. int num_unlim = 0;
  308. int retval;
  309. LOG((2, "nc_inq_unlimdims: ncid 0x%x", ncid));
  310. /* Find info for this file and group, and set pointer to each. */
  311. if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5)))
  312. return retval;
  313. /* Get our dim info. */
  314. assert(h5);
  315. {
  316. for (dim=grp->dim; dim; dim=dim->next)
  317. {
  318. if (dim->unlimited)
  319. {
  320. if (unlimdimidsp)
  321. unlimdimidsp[num_unlim] = dim->dimid;
  322. num_unlim++;
  323. }
  324. }
  325. }
  326. /* Give the number if the user wants it. */
  327. if (nunlimdimsp)
  328. *nunlimdimsp = num_unlim;
  329. return NC_NOERR;
  330. }