nc4internal.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /** \file \internal
  2. Internal netcdf-4 functions.
  3. This file contains functions internal to the netcdf4 library. None of
  4. the functions in this file are exposed in the exetnal API. These
  5. functions all relate to the manipulation of netcdf-4's in-memory
  6. buffer of metadata information, i.e. the linked list of NC_FILE_INFO_T
  7. structs.
  8. Copyright 2003-2011, University Corporation for Atmospheric
  9. Research. See the COPYRIGHT file for copying and redistribution
  10. conditions.
  11. */
  12. #include "config.h"
  13. #include "nc4internal.h"
  14. #include "nc.h" /* from libsrc */
  15. #include "ncdispatch.h" /* from libdispatch */
  16. #include <utf8proc.h>
  17. #define MEGABYTE 1048576
  18. /* These are the default chunk cache sizes for HDF5 files created or
  19. * opened with netCDF-4. */
  20. extern size_t nc4_chunk_cache_size;
  21. extern size_t nc4_chunk_cache_nelems;
  22. extern float nc4_chunk_cache_preemption;
  23. /* This is to track opened HDF5 objects to make sure they are
  24. * closed. */
  25. #ifdef EXTRA_TESTS
  26. extern int num_spaces;
  27. #endif /* EXTRA_TESTS */
  28. #ifdef LOGGING
  29. /* This is the severity level of messages which will be logged. Use
  30. severity 0 for errors, 1 for important log messages, 2 for less
  31. important, etc. */
  32. int nc_log_level = -1;
  33. #endif /* LOGGING */
  34. /* Check and normalize and name. */
  35. int
  36. nc4_check_name(const char *name, char *norm_name)
  37. {
  38. char *temp;
  39. int retval;
  40. /* Check the length. */
  41. if (strlen(name) > NC_MAX_NAME)
  42. return NC_EMAXNAME;
  43. /* Make sure this is a valid netcdf name. This should be done
  44. * before the name is normalized, because it gives better error
  45. * codes for bad utf8 strings. */
  46. if ((retval = NC_check_name(name)))
  47. return retval;
  48. /* Normalize the name. */
  49. if (!(temp = (char *)utf8proc_NFC((const unsigned char *)name)))
  50. return NC_EINVAL;
  51. strcpy(norm_name, temp);
  52. free(temp);
  53. return NC_NOERR;
  54. }
  55. /* Given a varid, find its shape. For unlimited dimensions, return
  56. the current number of records. */
  57. static int
  58. find_var_shape_grp(NC_GRP_INFO_T *grp, int varid, int *ndims,
  59. int *dimid, size_t *dimlen)
  60. {
  61. hid_t datasetid = 0, spaceid = 0;
  62. NC_VAR_INFO_T *var;
  63. hsize_t *h5dimlen = NULL, *h5dimlenmax = NULL;
  64. int d, dataset_ndims = 0;
  65. int retval = NC_NOERR;
  66. /* Find this var. */
  67. for (var = grp->var; var; var = var->next)
  68. if (var->varid == varid)
  69. break;
  70. if (!var)
  71. return NC_ENOTVAR;
  72. /* Get the dimids and the ndims for this var. */
  73. if (ndims)
  74. *ndims = var->ndims;
  75. if (dimid)
  76. for (d = 0; d < var->ndims; d++)
  77. dimid[d] = var->dimids[d];
  78. if (dimlen)
  79. {
  80. /* If the var hasn't been created yet, its size is 0. */
  81. if (!var->created)
  82. {
  83. for (d = 0; d < var->ndims; d++)
  84. dimlen[d] = 0;
  85. }
  86. else
  87. {
  88. /* Get the number of records in the dataset. */
  89. if ((retval = nc4_open_var_grp2(grp, var->varid, &datasetid)))
  90. BAIL(retval);
  91. if ((spaceid = H5Dget_space(datasetid)) < 0)
  92. BAIL(NC_EHDFERR);
  93. #ifdef EXTRA_TESTS
  94. num_spaces++;
  95. #endif
  96. /* If it's a scalar dataset, it has length one. */
  97. if (H5Sget_simple_extent_type(spaceid) == H5S_SCALAR)
  98. {
  99. dimlen[0] = 1;
  100. }
  101. else
  102. {
  103. /* Check to make sure ndims is right, then get the len of each
  104. dim in the space. */
  105. if ((dataset_ndims = H5Sget_simple_extent_ndims(spaceid)) < 0)
  106. BAIL(NC_EHDFERR);
  107. if (ndims && dataset_ndims != *ndims)
  108. BAIL(NC_EHDFERR);
  109. if (!(h5dimlen = malloc(dataset_ndims * sizeof(hsize_t))))
  110. BAIL(NC_ENOMEM);
  111. if (!(h5dimlenmax = malloc(dataset_ndims * sizeof(hsize_t))))
  112. BAIL(NC_ENOMEM);
  113. if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid,
  114. h5dimlen, h5dimlenmax)) < 0)
  115. BAIL(NC_EHDFERR);
  116. LOG((5, "find_var_shape_nc: varid %d len %d max: %d",
  117. varid, (int)h5dimlen[0], (int)h5dimlenmax[0]));
  118. for (d=0; d<dataset_ndims; d++)
  119. dimlen[d] = h5dimlen[d];
  120. }
  121. }
  122. }
  123. exit:
  124. if (spaceid > 0 && H5Sclose(spaceid) < 0)
  125. BAIL2(NC_EHDFERR);
  126. #ifdef EXTRA_TESTS
  127. num_spaces--;
  128. #endif
  129. if (h5dimlen) free(h5dimlen);
  130. if (h5dimlenmax) free(h5dimlenmax);
  131. return retval;
  132. }
  133. /* Given an NC_FILE_INFO_T pointer, add the necessary stuff for a
  134. * netcdf-4 file. */
  135. int
  136. nc4_nc4f_list_add(NC_FILE_INFO_T *nc, const char *path, int mode)
  137. {
  138. NC_HDF5_FILE_INFO_T *h5;
  139. NC_GRP_INFO_T *grp;
  140. assert(nc && !nc->nc4_info && path);
  141. /* The NC_FILE_INFO_T was allocated and inited by
  142. ncfunc.c before this function is called. We need to malloc and
  143. initialize the substructure NC_HDF_FILE_INFO_T. */
  144. if (!(nc->nc4_info = calloc(1, sizeof(NC_HDF5_FILE_INFO_T))))
  145. return NC_ENOMEM;
  146. h5 = nc->nc4_info;
  147. /* Hang on to the filename for nc_abort. */
  148. if (!(h5->path = malloc((strlen(path) + 1) * sizeof(char))))
  149. return NC_ENOMEM;
  150. strcpy(h5->path, path);
  151. /* Hang on to cmode, and note that we're in define mode. */
  152. h5->cmode = mode | NC_INDEF;
  153. /* The next_typeid needs to be set beyond the end of our atomic
  154. * types. */
  155. h5->next_typeid = NC_FIRSTUSERTYPEID;
  156. /* There's always at least one open group - the root
  157. * group. Allocate space for one group's worth of information. Set
  158. * its hdf id, name, and a pointer to it's file structure. */
  159. return nc4_grp_list_add(&(h5->root_grp), h5->next_nc_grpid++,
  160. NULL, nc, NC_GROUP_NAME, &grp);
  161. }
  162. /* /\* Given an ncid, find the relevant group and return a pointer to */
  163. /* * it. *\/ */
  164. /* NC_GRP_INFO_T * */
  165. /* find_nc_grp(int ncid) */
  166. /* { */
  167. /* NC_FILE_INFO_T *f; */
  168. /* for (f = nc_file; f; f = f->next) */
  169. /* { */
  170. /* if (f->ext_ncid == (ncid & FILE_ID_MASK)) */
  171. /* { */
  172. /* assert(f->nc4_info && f->nc4_info->root_grp); */
  173. /* return nc4_rec_find_grp(f->nc4_info->root_grp, (ncid & GRP_ID_MASK)); */
  174. /* } */
  175. /* } */
  176. /* return NULL; */
  177. /* } */
  178. /* Given an ncid, find the relevant group and return a pointer to it,
  179. * return an error of this is not a netcdf-4 file (or if strict nc3 is
  180. * turned on for this file.) */
  181. int
  182. nc4_find_nc4_grp(int ncid, NC_GRP_INFO_T **grp)
  183. {
  184. NC_FILE_INFO_T *f = nc4_find_nc_file(ncid);
  185. if(f == NULL) return NC_EBADID;
  186. /* No netcdf-3 files allowed! */
  187. if (!f->nc4_info) return NC_ENOTNC4;
  188. assert(f->nc4_info->root_grp);
  189. /* This function demands netcdf-4 files without strict nc3
  190. * rules.*/
  191. if (f->nc4_info->cmode & NC_CLASSIC_MODEL) return NC_ESTRICTNC3;
  192. /* If we can't find it, the grp id part of ncid is bad. */
  193. if (!(*grp = nc4_rec_find_grp(f->nc4_info->root_grp, (ncid & GRP_ID_MASK))))
  194. return NC_EBADID;
  195. return NC_NOERR;
  196. }
  197. /* Given an ncid, find the relevant group and return a pointer to it,
  198. * also set a pointer to the nc4_info struct of the related file. For
  199. * netcdf-3 files, *h5 will be set to NULL. */
  200. int
  201. nc4_find_grp_h5(int ncid, NC_GRP_INFO_T **grp, NC_HDF5_FILE_INFO_T **h5)
  202. {
  203. NC_FILE_INFO_T *f = nc4_find_nc_file(ncid);
  204. if(f == NULL) return NC_EBADID;
  205. if (f->nc4_info) {
  206. assert(f->nc4_info->root_grp);
  207. /* If we can't find it, the grp id part of ncid is bad. */
  208. if (!(*grp = nc4_rec_find_grp(f->nc4_info->root_grp, (ncid & GRP_ID_MASK))))
  209. return NC_EBADID;
  210. *h5 = (*grp)->file->nc4_info;
  211. assert(*h5);
  212. } else {
  213. *h5 = NULL;
  214. *grp = NULL;
  215. }
  216. return NC_NOERR;
  217. }
  218. int
  219. nc4_find_nc_grp_h5(int ncid, NC_FILE_INFO_T **nc, NC_GRP_INFO_T **grp,
  220. NC_HDF5_FILE_INFO_T **h5)
  221. {
  222. NC_FILE_INFO_T *f = nc4_find_nc_file(ncid);
  223. if(f == NULL) return NC_EBADID;
  224. *nc = f;
  225. if (f->nc4_info) {
  226. assert(f->nc4_info->root_grp);
  227. /* If we can't find it, the grp id part of ncid is bad. */
  228. if (!(*grp = nc4_rec_find_grp(f->nc4_info->root_grp, (ncid & GRP_ID_MASK))))
  229. return NC_EBADID;
  230. *h5 = (*grp)->file->nc4_info;
  231. assert(*h5);
  232. } else {
  233. *h5 = NULL;
  234. *grp = NULL;
  235. }
  236. return NC_NOERR;
  237. }
  238. /* Recursively hunt for a group id. */
  239. NC_GRP_INFO_T *
  240. nc4_rec_find_grp(NC_GRP_INFO_T *start_grp, int target_nc_grpid)
  241. {
  242. NC_GRP_INFO_T *g, *res;
  243. assert(start_grp);
  244. /* Is this the group we are searching for? */
  245. if (start_grp->nc_grpid == target_nc_grpid)
  246. return start_grp;
  247. /* Shake down the kids. */
  248. if (start_grp->children)
  249. for (g = start_grp->children; g; g = g->next)
  250. if ((res = nc4_rec_find_grp(g, target_nc_grpid)))
  251. return res;
  252. /* Can't find if. Fate, why do you mock me? */
  253. return NULL;
  254. }
  255. /* Given an ncid and varid, get pointers to the group and var
  256. * metadata. */
  257. int
  258. nc4_find_g_var_nc(NC_FILE_INFO_T *nc, int ncid, int varid,
  259. NC_GRP_INFO_T **grp, NC_VAR_INFO_T **var)
  260. {
  261. /* Find the group info. */
  262. assert(grp && var && nc && nc->nc4_info && nc->nc4_info->root_grp);
  263. *grp = nc4_rec_find_grp(nc->nc4_info->root_grp, (ncid & GRP_ID_MASK));
  264. /* Find the var info. */
  265. for ((*var) = (*grp)->var; (*var); (*var) = (*var)->next)
  266. if ((*var)->varid == varid)
  267. break;
  268. if (!(*var))
  269. return NC_ENOTVAR;
  270. return NC_NOERR;
  271. }
  272. /* Find a dim in a grp (or parents). */
  273. int
  274. nc4_find_dim(NC_GRP_INFO_T *grp, int dimid, NC_DIM_INFO_T **dim,
  275. NC_GRP_INFO_T **dim_grp)
  276. {
  277. NC_GRP_INFO_T *g, *dg = NULL;
  278. int finished = 0;
  279. assert(grp && dim);
  280. /* Find the dim info. */
  281. for (g = grp; g && !finished; g = g->parent)
  282. for ((*dim) = g->dim; (*dim); (*dim) = (*dim)->next)
  283. if ((*dim)->dimid == dimid)
  284. {
  285. dg = g;
  286. finished++;
  287. break;
  288. }
  289. /* If we didn't find it, return an error. */
  290. if (!(*dim))
  291. return NC_EBADDIM;
  292. /* Give the caller the group the dimension is in. */
  293. if (dim_grp)
  294. *dim_grp = dg;
  295. return NC_NOERR;
  296. }
  297. /* Recursively hunt for a HDF type id. */
  298. NC_TYPE_INFO_T *
  299. nc4_rec_find_hdf_type(NC_GRP_INFO_T *start_grp, hid_t target_hdf_typeid)
  300. {
  301. NC_GRP_INFO_T *g;
  302. NC_TYPE_INFO_T *type, *res;
  303. htri_t equal;
  304. assert(start_grp);
  305. /* Does this group have the type we are searching for? */
  306. for (type = start_grp->type; type; type = type->next)
  307. {
  308. if ((equal = H5Tequal(type->native_typeid ? type->native_typeid : type->hdf_typeid, target_hdf_typeid)) < 0)
  309. return NULL;
  310. if (equal)
  311. return type;
  312. }
  313. /* Shake down the kids. */
  314. if (start_grp->children)
  315. for (g = start_grp->children; g; g = g->next)
  316. if ((res = nc4_rec_find_hdf_type(g, target_hdf_typeid)))
  317. return res;
  318. /* Can't find if. Fate, why do you mock me? */
  319. return NULL;
  320. }
  321. /* Recursively hunt for a netCDF type id. */
  322. NC_TYPE_INFO_T *
  323. nc4_rec_find_nc_type(NC_GRP_INFO_T *start_grp, nc_type target_nc_typeid)
  324. {
  325. NC_GRP_INFO_T *g;
  326. NC_TYPE_INFO_T *type, *res;
  327. assert(start_grp);
  328. /* Does this group have the type we are searching for? */
  329. for (type = start_grp->type; type; type = type->next)
  330. if (type->nc_typeid == target_nc_typeid)
  331. return type;
  332. /* Shake down the kids. */
  333. if (start_grp->children)
  334. for (g = start_grp->children; g; g = g->next)
  335. if ((res = nc4_rec_find_nc_type(g, target_nc_typeid)))
  336. return res;
  337. /* Can't find if. Fate, why do you mock me? */
  338. return NULL;
  339. }
  340. /* Recursively hunt for a netCDF type by name. */
  341. NC_TYPE_INFO_T *
  342. nc4_rec_find_named_type(NC_GRP_INFO_T *start_grp, char *name)
  343. {
  344. NC_GRP_INFO_T *g;
  345. NC_TYPE_INFO_T *type, *res;
  346. assert(start_grp);
  347. /* Does this group have the type we are searching for? */
  348. for (type = start_grp->type; type; type = type->next)
  349. if (!strcmp(type->name, name))
  350. return type;
  351. /* Search subgroups. */
  352. if (start_grp->children)
  353. for (g = start_grp->children; g; g = g->next)
  354. if ((res = nc4_rec_find_named_type(g, name)))
  355. return res;
  356. /* Can't find if. Oh, woe is me! */
  357. return NULL;
  358. }
  359. /* Use a netCDF typeid to find a type in a type_list. */
  360. int
  361. nc4_find_type(NC_HDF5_FILE_INFO_T *h5, nc_type typeid, NC_TYPE_INFO_T **type)
  362. {
  363. if (typeid < 0 || !type)
  364. return NC_EINVAL;
  365. *type = NULL;
  366. /* Atomic types don't have associated NC_TYPE_INFO_T struct, just
  367. * return NOERR. */
  368. if (typeid <= NC_STRING)
  369. return NC_NOERR;
  370. /* Find the type. */
  371. if(!(*type = nc4_rec_find_nc_type(h5->root_grp, typeid)))
  372. return NC_EBADTYPID;
  373. return NC_NOERR;
  374. }
  375. /* Find the actual length of a dim by checking the length of that dim
  376. * in all variables that use it, in grp or children. *len must be
  377. * initialized to zero before this function is called. */
  378. int
  379. nc4_find_dim_len(NC_GRP_INFO_T *grp, int dimid, size_t **len)
  380. {
  381. NC_GRP_INFO_T *g;
  382. NC_VAR_INFO_T *var;
  383. int d, ndims, dimids[NC_MAX_DIMS];
  384. size_t dimlen[NC_MAX_DIMS];
  385. int retval;
  386. assert(grp && len);
  387. LOG((3, "nc4_find_dim_len: grp->name %s dimid %d", grp->name, dimid));
  388. /* If there are any groups, call this function recursively on
  389. * them. */
  390. for (g = grp->children; g; g = g->next)
  391. if ((retval = nc4_find_dim_len(g, dimid, len)))
  392. return retval;
  393. /* For all variables in this group, find the ones that use this
  394. * dimension, and remember the max length. */
  395. for (var = grp->var; var; var = var->next)
  396. {
  397. /* Find dimensions of this var. */
  398. if ((retval = find_var_shape_grp(grp, var->varid, &ndims,
  399. dimids, dimlen)))
  400. return retval;
  401. /* Check for any dimension that matches dimid. If found, check
  402. * if its length is longer than *lenp. */
  403. for (d = 0; d < ndims; d++)
  404. {
  405. if (dimids[d] == dimid)
  406. {
  407. /* Remember the max length in *lenp. */
  408. **len = dimlen[d] > **len ? dimlen[d] : **len;
  409. break;
  410. }
  411. }
  412. }
  413. return NC_NOERR;
  414. }
  415. /* Given a group, find an att. */
  416. int
  417. nc4_find_grp_att(NC_GRP_INFO_T *grp, int varid, const char *name, int attnum,
  418. NC_ATT_INFO_T **att)
  419. {
  420. NC_VAR_INFO_T *var;
  421. NC_ATT_INFO_T *attlist = NULL;
  422. assert(grp && grp->name);
  423. LOG((4, "nc4_find_grp_att: grp->name %s varid %d name %s attnum %d",
  424. grp->name, varid, name, attnum));
  425. /* Get either the global or a variable attribute list. */
  426. if (varid == NC_GLOBAL)
  427. attlist = grp->att;
  428. else
  429. {
  430. for(var = grp->var; var; var = var->next)
  431. {
  432. if (var->varid == varid)
  433. {
  434. attlist = var->att;
  435. break;
  436. }
  437. }
  438. if (!var)
  439. return NC_ENOTVAR;
  440. }
  441. /* Now find the attribute by name or number. If a name is provided,
  442. * ignore the attnum. */
  443. for (*att = attlist; *att; *att = (*att)->next)
  444. if ((name && !strcmp((*att)->name, name)) ||
  445. (!name && (*att)->attnum == attnum))
  446. return NC_NOERR;
  447. /* If we get here, we couldn't find the attribute. */
  448. return NC_ENOTATT;
  449. }
  450. /* Given an ncid, varid, and name or attnum, find and return pointer
  451. to NC_ATT_INFO_T metadata. */
  452. int
  453. nc4_find_nc_att(int ncid, int varid, const char *name, int attnum,
  454. NC_ATT_INFO_T **att)
  455. {
  456. NC_GRP_INFO_T *grp;
  457. NC_HDF5_FILE_INFO_T *h5;
  458. NC_VAR_INFO_T *var;
  459. NC_ATT_INFO_T *attlist = NULL;
  460. int retval;
  461. LOG((4, "nc4_find_nc_att: ncid 0x%x varid %d name %s attnum %d",
  462. ncid, varid, name, attnum));
  463. /* Find info for this file and group, and set pointer to each. */
  464. if ((retval = nc4_find_grp_h5(ncid, &grp, &h5)))
  465. return retval;
  466. assert(grp && h5);
  467. /* Get either the global or a variable attribute list. */
  468. if (varid == NC_GLOBAL)
  469. attlist = grp->att;
  470. else
  471. {
  472. for(var = grp->var; var; var = var->next)
  473. {
  474. if (var->varid == varid)
  475. {
  476. attlist = var->att;
  477. break;
  478. }
  479. }
  480. if (!var)
  481. return NC_ENOTVAR;
  482. }
  483. /* Now find the attribute by name or number. If a name is provided, ignore the attnum. */
  484. for (*att = attlist; *att; *att = (*att)->next)
  485. if ((name && !strcmp((*att)->name, name)) ||
  486. (!name && (*att)->attnum == attnum))
  487. return NC_NOERR;
  488. /* If we get here, we couldn't find the attribute. */
  489. return NC_ENOTATT;
  490. }
  491. void
  492. nc4_file_list_free(void)
  493. {
  494. free_NCList();
  495. }
  496. int
  497. NC4_new_nc(NC** ncpp)
  498. {
  499. NC_FILE_INFO_T** ncp;
  500. /* Allocate memory for this info. */
  501. if (!(ncp = calloc(1, sizeof(NC_FILE_INFO_T))))
  502. return NC_ENOMEM;
  503. if(ncpp) *ncpp = (NC*)ncp;
  504. return NC_NOERR;
  505. }
  506. int
  507. nc4_file_list_add(NC_FILE_INFO_T** ncp, NC_Dispatch* dispatch)
  508. {
  509. NC_FILE_INFO_T *nc;
  510. int status = NC_NOERR;
  511. /* Allocate memory for this info; use the dispatcher to do this */
  512. status = dispatch->new_nc((NC**)&nc);
  513. if(status) return status;
  514. /* Add this file to the list. */
  515. if ((status = add_to_NCList((NC *)nc)))
  516. {
  517. if(nc && nc->ext_ncid > 0)
  518. {
  519. del_from_NCList((NC *)nc);
  520. free(nc);
  521. }
  522. return status;
  523. }
  524. /* Return a pointer to the new struct. */
  525. if(ncp)
  526. *ncp = nc;
  527. return NC_NOERR;
  528. }
  529. /* Remove a NC_FILE_INFO_T from the linked list. This will nc_free the
  530. memory too. */
  531. void
  532. nc4_file_list_del(NC_FILE_INFO_T *nc)
  533. {
  534. /* Remove file from master list. */
  535. del_from_NCList((NC *)nc);
  536. free(nc);
  537. }
  538. /* Given an id, walk the list and find the appropriate
  539. NC_FILE_INFO_T. */
  540. NC_FILE_INFO_T*
  541. nc4_find_nc_file(int ext_ncid)
  542. {
  543. return (NC_FILE_INFO_T*)find_in_NCList(ext_ncid);
  544. }
  545. /* Add to the end of a var list. Return a pointer to the newly
  546. * added var. */
  547. int
  548. nc4_var_list_add(NC_VAR_INFO_T **list, NC_VAR_INFO_T **var)
  549. {
  550. NC_VAR_INFO_T *v;
  551. /* Allocate storage for new variable. */
  552. if (!(*var = calloc(1, sizeof(NC_VAR_INFO_T))))
  553. return NC_ENOMEM;
  554. /* Go to the end of the list and set the last one to point at our
  555. * new var, or, if the list is empty, our new var becomes the
  556. * list. */
  557. if(*list)
  558. {
  559. for (v = *list; v; v = v->next)
  560. if (!v->next)
  561. break;
  562. v->next = *var;
  563. (*var)->prev = v;
  564. }
  565. else
  566. *list = *var;
  567. /* These are the HDF5-1.8.4 defaults. */
  568. (*var)->chunk_cache_size = nc4_chunk_cache_size;
  569. (*var)->chunk_cache_nelems = nc4_chunk_cache_nelems;
  570. (*var)->chunk_cache_preemption = nc4_chunk_cache_preemption;
  571. return NC_NOERR;
  572. }
  573. /* Add to the beginning of a dim list. */
  574. int
  575. nc4_dim_list_add(NC_DIM_INFO_T **list)
  576. {
  577. NC_DIM_INFO_T *dim;
  578. if (!(dim = calloc(1, sizeof(NC_DIM_INFO_T))))
  579. return NC_ENOMEM;
  580. if(*list)
  581. (*list)->prev = dim;
  582. dim->next = *list;
  583. *list = dim;
  584. return NC_NOERR;
  585. }
  586. /* Add to the beginning of a dim list. */
  587. int
  588. nc4_dim_list_add2(NC_DIM_INFO_T **list, NC_DIM_INFO_T **new_dim)
  589. {
  590. NC_DIM_INFO_T *dim;
  591. if (!(dim = calloc(1, sizeof(NC_DIM_INFO_T))))
  592. return NC_ENOMEM;
  593. if(*list)
  594. (*list)->prev = dim;
  595. dim->next = *list;
  596. *list = dim;
  597. /* Return pointer to new dimension. */
  598. if (new_dim)
  599. *new_dim = dim;
  600. return NC_NOERR;
  601. }
  602. /* Add to the end of an att list. */
  603. int
  604. nc4_att_list_add(NC_ATT_INFO_T **list)
  605. {
  606. NC_ATT_INFO_T *att, *a1;
  607. if (!(att = calloc(1, sizeof(NC_ATT_INFO_T))))
  608. return NC_ENOMEM;
  609. if (*list)
  610. {
  611. for (a1 = *list; a1; a1 = a1->next)
  612. if (!a1->next)
  613. break;
  614. a1->next = att;
  615. att->prev = a1;
  616. }
  617. else
  618. {
  619. *list = att;
  620. }
  621. return NC_NOERR;
  622. }
  623. /* Add to the end of a group list. Can't use 0 as a new_nc_grpid -
  624. * it's reserverd for the root group. */
  625. int
  626. nc4_grp_list_add(NC_GRP_INFO_T **list, int new_nc_grpid,
  627. NC_GRP_INFO_T *parent_grp, NC_FILE_INFO_T *nc,
  628. char *name, NC_GRP_INFO_T **grp)
  629. {
  630. NC_GRP_INFO_T *g;
  631. LOG((3, "grp_list_add: new_nc_grpid %d name %s ",
  632. new_nc_grpid, name));
  633. /* Get the memory to store this groups info. */
  634. if (!(*grp = calloc(1, sizeof(NC_GRP_INFO_T))))
  635. return NC_ENOMEM;
  636. /* If the list is not NULL, add this group to it. Otherwise, this
  637. * group structure becomes the list. */
  638. if (*list)
  639. {
  640. /* Move to end of the list. */
  641. for (g = *list; g; g = g->next)
  642. if (!g->next)
  643. break;
  644. g->next = *grp; /* Add grp to end of list. */
  645. (*grp)->prev = g;
  646. }
  647. else
  648. {
  649. *list = *grp;
  650. }
  651. /* Fill in this group's information. */
  652. (*grp)->nc_grpid = new_nc_grpid;
  653. (*grp)->parent = parent_grp;
  654. if (!((*grp)->name = malloc((strlen(name) + 1) * sizeof(char))))
  655. return NC_ENOMEM;
  656. strcpy((*grp)->name, name);
  657. (*grp)->file = nc;
  658. return NC_NOERR;
  659. }
  660. /* Names for groups, variables, and types must not be the same. This
  661. * function checks that a proposed name is not already in
  662. * use. Normalzation of UTF8 strings should happen before this
  663. * function is called. */
  664. int
  665. nc4_check_dup_name(NC_GRP_INFO_T *grp, char *name)
  666. {
  667. NC_TYPE_INFO_T *type;
  668. NC_GRP_INFO_T *g;
  669. NC_VAR_INFO_T *var;
  670. /* Any types of this name? */
  671. for (type = grp->type; type; type = type->next)
  672. if (!strcmp(type->name, name))
  673. return NC_ENAMEINUSE;
  674. /* Any child groups of this name? */
  675. for (g = grp->children; g; g = g->next)
  676. if (!strcmp(g->name, name))
  677. return NC_ENAMEINUSE;
  678. /* Any variables of this name? */
  679. for (var = grp->var; var; var = var->next)
  680. if (!strcmp(var->name, name))
  681. return NC_ENAMEINUSE;
  682. return NC_NOERR;
  683. }
  684. /* Add to the end of a type list. */
  685. int
  686. nc4_type_list_add(NC_TYPE_INFO_T **list, NC_TYPE_INFO_T **new_type)
  687. {
  688. NC_TYPE_INFO_T *type, *t;
  689. if (!(type = calloc(1, sizeof(NC_TYPE_INFO_T))))
  690. return NC_ENOMEM;
  691. if (*list)
  692. {
  693. for (t = *list; t; t = t->next)
  694. if (!t->next)
  695. break;
  696. t->next = type;
  697. type->prev = t;
  698. }
  699. else
  700. {
  701. *list = type;
  702. }
  703. if (new_type)
  704. *new_type = type;
  705. return NC_NOERR;
  706. }
  707. /* Add to the end of a compound field list. */
  708. int
  709. nc4_field_list_add(NC_FIELD_INFO_T **list, int fieldid, const char *name,
  710. size_t offset, hid_t field_hdf_typeid, hid_t native_typeid,
  711. nc_type xtype, int ndims, const int *dim_sizesp)
  712. {
  713. NC_FIELD_INFO_T *field, *f;
  714. int i;
  715. /* Name has already been checked and UTF8 normalized. */
  716. if (!name)
  717. return NC_EINVAL;
  718. /* Allocate storage for this field information. */
  719. if (!(field = calloc(1, sizeof(NC_FIELD_INFO_T))))
  720. return NC_ENOMEM;
  721. /* Add this field to list. */
  722. if (*list)
  723. {
  724. for (f = *list; f; f = f->next)
  725. if (!f->next)
  726. break;
  727. f->next = field;
  728. field->prev = f;
  729. }
  730. else
  731. {
  732. *list = field;
  733. }
  734. /* Store the information about this field. */
  735. field->fieldid = fieldid;
  736. if (!(field->name = malloc((strlen(name) + 1) * sizeof(char))))
  737. return NC_ENOMEM;
  738. strcpy(field->name, name);
  739. field->hdf_typeid = field_hdf_typeid;
  740. field->native_typeid = native_typeid;
  741. field->nctype = xtype;
  742. field->offset = offset;
  743. field->ndims = ndims;
  744. if (ndims)
  745. {
  746. if (!(field->dim_size = malloc(ndims * sizeof(int))))
  747. return NC_ENOMEM;
  748. for (i = 0; i < ndims; i++)
  749. field->dim_size[i] = dim_sizesp[i];
  750. }
  751. return NC_NOERR;
  752. }
  753. /* Add a member to an enum type. */
  754. int
  755. nc4_enum_member_add(NC_ENUM_MEMBER_INFO_T **list, size_t size,
  756. const char *name, const void *value)
  757. {
  758. NC_ENUM_MEMBER_INFO_T *member, *m;
  759. /* Name has already been checked. */
  760. assert(name && size > 0 && value);
  761. LOG((4, "nc4_enum_member_add: size %d name %s", size, name));
  762. /* Allocate storage for this field information. */
  763. if (!(member = calloc(1, sizeof(NC_ENUM_MEMBER_INFO_T))) ||
  764. !(member->value = calloc(1, size)))
  765. return NC_ENOMEM;
  766. /* Add this field to list. */
  767. if (*list)
  768. {
  769. for (m = *list; m; m = m->next)
  770. if (!m->next)
  771. break;
  772. m->next = member;
  773. member->prev = m;
  774. }
  775. else
  776. {
  777. *list = member;
  778. }
  779. /* Store the information about this member. */
  780. if (!(member->name = malloc((strlen(name) + 1) * sizeof(char))))
  781. return NC_ENOMEM;
  782. strcpy(member->name, name);
  783. memcpy(member->value, value, size);
  784. return NC_NOERR;
  785. }
  786. /* Delete a var from a var list, and free the memory. */
  787. static int
  788. var_list_del(NC_VAR_INFO_T **list, NC_VAR_INFO_T *var)
  789. {
  790. NC_ATT_INFO_T *a, *att;
  791. int ret;
  792. /* First delete all the attributes attached to this var. */
  793. att = (*list)->att;
  794. while (att)
  795. {
  796. a = att->next;
  797. if ((ret = nc4_att_list_del(&var->att, att)))
  798. return ret;
  799. att = a;
  800. }
  801. /* Free some things that may be allocated. */
  802. if (var->chunksizes)
  803. free(var->chunksizes);
  804. if (var->hdf5_name)
  805. free(var->hdf5_name);
  806. if (var->name)
  807. free(var->name);
  808. if (var->dimids)
  809. free(var->dimids);
  810. if (var->dim)
  811. free(var->dim);
  812. /* Remove the var from the linked list. */
  813. if(*list == var)
  814. *list = var->next;
  815. else
  816. var->prev->next = var->next;
  817. if(var->next)
  818. var->next->prev = var->prev;
  819. /* Delete any fill value allocation. This must be done before the
  820. * type_info is freed. */
  821. if (var->fill_value)
  822. {
  823. if (var->hdf_datasetid)
  824. {
  825. if (var->type_info->class == NC_VLEN)
  826. nc_free_vlen((nc_vlen_t *)var->fill_value);
  827. else if (var->type_info->nc_typeid == NC_STRING)
  828. free(*(char **)var->fill_value);
  829. }
  830. free(var->fill_value);
  831. }
  832. /* For atomic types we have allocated space for type information. */
  833. /* if (var->hdf_datasetid && var->xtype <= NC_STRING)*/
  834. if (var->xtype <= NC_STRING)
  835. {
  836. if (var->type_info->native_typeid)
  837. if ((H5Tclose(var->type_info->native_typeid)) < 0)
  838. return NC_EHDFERR;
  839. /* Only need to close the hdf_typeid when it was obtained with
  840. * H5Dget_type (which happens when reading a file, but not when
  841. * creating a variable). */
  842. if (var->type_info->close_hdf_typeid || var->xtype == NC_STRING)
  843. if ((H5Tclose(var->type_info->hdf_typeid)) < 0)
  844. return NC_EHDFERR;
  845. /* Free the name. */
  846. if (var->type_info->name)
  847. free(var->type_info->name);
  848. free(var->type_info);
  849. }
  850. /* Delete any HDF5 dimscale objid information. */
  851. if (var->dimscale_hdf5_objids)
  852. free(var->dimscale_hdf5_objids);
  853. /* Delete information about the attachment status of dimscales. */
  854. if (var->dimscale_attached)
  855. free(var->dimscale_attached);
  856. /* Delete the var. */
  857. free(var);
  858. return NC_NOERR;
  859. }
  860. /* Delete a field from a field list, and nc_free the memory. */
  861. static void
  862. field_list_del(NC_FIELD_INFO_T **list, NC_FIELD_INFO_T *field)
  863. {
  864. /* Take this field out of the list. */
  865. if(*list == field)
  866. *list = field->next;
  867. else
  868. field->prev->next = field->next;
  869. if(field->next)
  870. field->next->prev = field->prev;
  871. /* Free some stuff. */
  872. if (field->name)
  873. free(field->name);
  874. if (field->dim_size)
  875. free(field->dim_size);
  876. /* Nc_Free the memory. */
  877. free(field);
  878. }
  879. /* Delete a type from a type list, and nc_free the memory. */
  880. int
  881. type_list_del(NC_TYPE_INFO_T **list, NC_TYPE_INFO_T *type)
  882. {
  883. NC_FIELD_INFO_T *field, *f;
  884. NC_ENUM_MEMBER_INFO_T *enum_member, *em;
  885. /* Close any open user-defined HDF5 typieds. */
  886. if (type->hdf_typeid)
  887. {
  888. if (H5Tclose(type->hdf_typeid) < 0)
  889. return NC_EHDFERR;
  890. }
  891. if (type->native_typeid)
  892. {
  893. if (H5Tclose(type->native_typeid) < 0)
  894. return NC_EHDFERR;
  895. }
  896. /* Free the name. */
  897. if (type->name)
  898. free(type->name);
  899. /* Delete all the fields in this type (there will be some if its a
  900. * compound). */
  901. field = type->field;
  902. while (field)
  903. {
  904. f = field->next;
  905. field_list_del(&type->field, field);
  906. field = f;
  907. }
  908. /* Delete all the enum_members, if any. */
  909. enum_member = type->enum_member;
  910. while (enum_member)
  911. {
  912. em = enum_member->next;
  913. free(enum_member->value);
  914. free(enum_member->name);
  915. free(enum_member);
  916. enum_member = em;
  917. }
  918. /* Take this type out of the list. */
  919. if(*list == type)
  920. *list = type->next;
  921. else
  922. type->prev->next = type->next;
  923. if(type->next)
  924. type->next->prev = type->prev;
  925. /* Nc_Free the memory. */
  926. free(type);
  927. return NC_NOERR;
  928. }
  929. /* Delete a del from a var list, and nc_free the memory. */
  930. int
  931. nc4_dim_list_del(NC_DIM_INFO_T **list, NC_DIM_INFO_T *dim)
  932. {
  933. /* Take this dimension out of the list. */
  934. if(*list == dim)
  935. *list = dim->next;
  936. else
  937. dim->prev->next = dim->next;
  938. if(dim->next)
  939. dim->next->prev = dim->prev;
  940. /* Free memory allocated for names. */
  941. if (dim->name)
  942. free(dim->name);
  943. if (dim->old_name)
  944. free(dim->old_name);
  945. free(dim);
  946. return NC_NOERR;
  947. }
  948. /* Remove a NC_GRP_INFO_T from the linked list. This will nc_free the
  949. memory too. */
  950. static void
  951. grp_list_del(NC_GRP_INFO_T **list, NC_GRP_INFO_T *grp)
  952. {
  953. if(*list == grp)
  954. *list = grp->next;
  955. else
  956. grp->prev->next = grp->next;
  957. if(grp->next)
  958. grp->next->prev = grp->prev;
  959. free(grp);
  960. }
  961. /* Recursively delete the data for a group (and everything it
  962. * contains) in our internal metadata store. */
  963. int
  964. nc4_rec_grp_del(NC_GRP_INFO_T **list, NC_GRP_INFO_T *grp)
  965. {
  966. NC_GRP_INFO_T *g, *c;
  967. NC_VAR_INFO_T *v, *var;
  968. NC_ATT_INFO_T *a, *att;
  969. NC_DIM_INFO_T *d, *dim;
  970. NC_TYPE_INFO_T *type, *t;
  971. int retval;
  972. assert(grp);
  973. LOG((3, "nc4_rec_grp_del: grp->name %s", grp->name));
  974. /* Recursively call this function for each child, if any, stopping
  975. * if there is an error. */
  976. g = grp->children;
  977. while(g)
  978. {
  979. c = g->next;
  980. if ((retval = nc4_rec_grp_del(&(grp->children), g)))
  981. return retval;
  982. g = c;
  983. }
  984. /* Delete all the list contents for vars, dims, and atts, in each
  985. * group. */
  986. att = grp->att;
  987. while (att)
  988. {
  989. LOG((4, "nc4_rec_grp_del: deleting att %s", att->name));
  990. a = att->next;
  991. if ((retval = nc4_att_list_del(&grp->att, att)))
  992. return retval;
  993. att = a;
  994. }
  995. /* Delete all vars. */
  996. var = grp->var;
  997. while (var)
  998. {
  999. LOG((4, "nc4_rec_grp_del: deleting var %s", var->name));
  1000. /* Close HDF5 dataset associated with this var, unless it's a
  1001. * scale. */
  1002. if (var->hdf_datasetid && !var->dimscale &&
  1003. H5Dclose(var->hdf_datasetid) < 0)
  1004. return NC_EHDFERR;
  1005. v = var->next;
  1006. if ((retval = var_list_del(&grp->var, var)))
  1007. return retval;
  1008. var = v;
  1009. }
  1010. /* Delete all dims. */
  1011. dim = grp->dim;
  1012. while (dim)
  1013. {
  1014. LOG((4, "nc4_rec_grp_del: deleting dim %s", dim->name));
  1015. /* Close HDF5 dataset associated with this dim. */
  1016. if (dim->hdf_dimscaleid && H5Dclose(dim->hdf_dimscaleid) < 0)
  1017. return NC_EHDFERR;
  1018. d = dim->next;
  1019. if ((retval = nc4_dim_list_del(&grp->dim, dim)))
  1020. return retval;
  1021. dim = d;
  1022. }
  1023. /* Delete all types. */
  1024. type = grp->type;
  1025. while (type)
  1026. {
  1027. LOG((4, "nc4_rec_grp_del: deleting type %s", type->name));
  1028. t = type->next;
  1029. if ((retval = type_list_del(&grp->type, type)))
  1030. return retval;
  1031. type = t;
  1032. }
  1033. /* Tell HDF5 we're closing this group. */
  1034. LOG((4, "nc4_rec_grp_del: closing group %s", grp->name));
  1035. if (grp->hdf_grpid && H5Gclose(grp->hdf_grpid) < 0)
  1036. return NC_EHDFERR;
  1037. /* Free the name. */
  1038. free(grp->name);
  1039. /* Finally, redirect pointers around this entry in the list, and
  1040. * nc_free its memory. */
  1041. grp_list_del(list, grp);
  1042. return NC_NOERR;
  1043. }
  1044. /* Remove a NC_ATT_INFO_T from the linked list. This will nc_free the
  1045. memory too.
  1046. */
  1047. int
  1048. nc4_att_list_del(NC_ATT_INFO_T **list, NC_ATT_INFO_T *att)
  1049. {
  1050. int i;
  1051. /* Take this att out of the list. */
  1052. if(*list == att)
  1053. *list = att->next;
  1054. else
  1055. att->prev->next = att->next;
  1056. if(att->next)
  1057. att->next->prev = att->prev;
  1058. /* Free memory that was malloced to hold data for this
  1059. * attribute. */
  1060. if (att->data)
  1061. free(att->data);
  1062. /* Free the name. */
  1063. if (att->name)
  1064. free(att->name);
  1065. /* Close the HDF5 typeid. */
  1066. if (att->native_typeid && H5Tclose(att->native_typeid) < 0)
  1067. return NC_EHDFERR;
  1068. /* If this is a string array attribute, delete all members of the
  1069. * string array, then delete the array of pointers to strings. (The
  1070. * array was filled with pointers by HDF5 when the att was read,
  1071. * and memory for each string was allocated by HDF5. That's why I
  1072. * use free and not nc_free, because the netCDF library didn't
  1073. * allocate the memory that is being freed.) */
  1074. if (att->stdata)
  1075. {
  1076. for (i = 0; i < att->len; i++)
  1077. free(att->stdata[i]);
  1078. free(att->stdata);
  1079. }
  1080. /* If this att has vlen data, release it. */
  1081. if (att->vldata)
  1082. {
  1083. for (i = 0; i < att->len; i++)
  1084. nc_free_vlen(&att->vldata[i]);
  1085. free(att->vldata);
  1086. }
  1087. free(att);
  1088. return NC_NOERR;
  1089. }
  1090. /* Normalize a UTF8 name. Put the result in norm_name, which can be
  1091. * NC_MAX_NAME + 1 in size. This function makes sure the free() gets
  1092. * called on the return from utf8proc_NFC, and also ensures that the
  1093. * name is not too long. */
  1094. int
  1095. nc4_normalize_name(const char *name, char *norm_name)
  1096. {
  1097. char *temp_name;
  1098. if (!(temp_name = (char *)utf8proc_NFC((const unsigned char *)name)))
  1099. return NC_EINVAL;
  1100. if (strlen(temp_name) > NC_MAX_NAME)
  1101. {
  1102. free(temp_name);
  1103. return NC_EMAXNAME;
  1104. }
  1105. strcpy(norm_name, temp_name);
  1106. free(temp_name);
  1107. return NC_NOERR;
  1108. }
  1109. /* Print out a bunch of info to stderr about the metadata for
  1110. debugging purposes. */
  1111. #ifdef LOGGING
  1112. /* Use this to set the global log level. Set it to NC_TURN_OFF_LOGGING
  1113. (-1) to turn off all logging. Set it to 0 to show only errors, and
  1114. to higher numbers to show more and more logging details. */
  1115. int
  1116. nc_set_log_level(int new_level)
  1117. {
  1118. /* If the user wants to completely turn off logging, turn off HDF5
  1119. logging too. Now I truely can't think of what to do if this
  1120. fails, so just ignore the return code. */
  1121. if (new_level == NC_TURN_OFF_LOGGING)
  1122. {
  1123. H5Eset_auto(NULL, NULL);
  1124. LOG((1, "HDF5 error messages turned off!"));
  1125. }
  1126. /* Do we need to turn HDF5 logging back on? */
  1127. if (new_level > NC_TURN_OFF_LOGGING &&
  1128. nc_log_level <= NC_TURN_OFF_LOGGING)
  1129. {
  1130. if (H5Eset_auto((H5E_auto_t)&H5Eprint, stderr) < 0)
  1131. LOG((0, "H5Eset_auto failed!"));
  1132. LOG((1, "HDF5 error messages turned on."));
  1133. }
  1134. /* Now remember the new level. */
  1135. nc_log_level = new_level;
  1136. LOG((4, "log_level changed to %d", nc_log_level));
  1137. return 0;
  1138. }
  1139. /* Recursively print the metadata of a group. */
  1140. #define MAX_NESTS 10
  1141. static int
  1142. rec_print_metadata(NC_GRP_INFO_T *grp, int *tab_count)
  1143. {
  1144. NC_GRP_INFO_T *g;
  1145. NC_ATT_INFO_T *att;
  1146. NC_VAR_INFO_T *var;
  1147. NC_DIM_INFO_T *dim;
  1148. NC_TYPE_INFO_T *type;
  1149. NC_FIELD_INFO_T *field;
  1150. char tabs[MAX_NESTS] = "";
  1151. char dims_string[NC_MAX_DIMS*4];
  1152. char temp_string[10];
  1153. int t, retval, d;
  1154. /* Come up with a number of tabs relative to the group. */
  1155. for (t = 0; t < *tab_count && t < MAX_NESTS; t++)
  1156. strcat(tabs, "\t");
  1157. LOG((2, "%s GROUP - %s nc_grpid: %d nvars: %d natts: %d",
  1158. tabs, grp->name, grp->nc_grpid, grp->nvars, grp->natts));
  1159. for(att = grp->att; att; att = att->next)
  1160. LOG((2, "%s GROUP ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
  1161. tabs, att->attnum, att->name, att->xtype, att->len));
  1162. /* To display dims starting with 0 and going up, go through list is
  1163. * reverse order. */
  1164. for(dim = grp->dim; dim && dim->next; dim = dim->next)
  1165. ;
  1166. for( ; dim; dim = dim->prev)
  1167. LOG((2, "%s DIMENSION - dimid: %d name: %s len: %d unlimited: %d",
  1168. tabs, dim->dimid, dim->name, dim->len, dim->unlimited));
  1169. /* To display vars starting with 0 and going up, go through list is
  1170. * reverse order. */
  1171. for(var = grp->var; var && var->next; var = var->next)
  1172. ;
  1173. for( ; var; var = var->prev)
  1174. {
  1175. strcpy(dims_string, "");
  1176. for (d = 0; d < var->ndims; d++)
  1177. {
  1178. sprintf(temp_string, " %d", var->dimids[d]);
  1179. strcat(dims_string, temp_string);
  1180. }
  1181. LOG((2, "%s VARIABLE - varid: %d name: %s type: %d ndims: %d dimscale: %d dimids:%s",
  1182. tabs, var->varid, var->name, var->xtype, var->ndims, var->dimscale,
  1183. dims_string));
  1184. for(att = var->att; att; att = att->next)
  1185. LOG((2, "%s VAR ATTRIBUTE - attnum: %d name: %s type: %d len: %d",
  1186. tabs, att->attnum, att->name, att->xtype, att->len));
  1187. }
  1188. for (type = grp->type; type; type = type->next)
  1189. {
  1190. LOG((2, "%s TYPE - nc_typeid: %d hdf_typeid: 0x%x size: %d committed: %d "
  1191. "name: %s num_fields: %d base_nc_type: %d", tabs, type->nc_typeid,
  1192. type->hdf_typeid, type->size, type->committed, type->name,
  1193. type->num_fields, type->base_nc_type));
  1194. /* Is this a compound type? */
  1195. if (type->class == NC_COMPOUND)
  1196. {
  1197. LOG((3, "compound type"));
  1198. for (field = type->field; field; field = field->next)
  1199. LOG((4, "field %s offset %d nctype %d ndims %d", field->name,
  1200. field->offset, field->nctype, field->ndims));
  1201. }
  1202. else if (type->class == NC_VLEN)
  1203. LOG((3, "VLEN type"));
  1204. else if (type->class == NC_OPAQUE)
  1205. LOG((3, "Opaque type"));
  1206. else if (type->class == NC_ENUM)
  1207. LOG((3, "Enum type"));
  1208. else
  1209. {
  1210. LOG((0, "Unknown class: %d", type->class));
  1211. return NC_EBADTYPE;
  1212. }
  1213. }
  1214. /* Call self for each child of this group. */
  1215. if (grp->children)
  1216. {
  1217. (*tab_count)++;
  1218. for (g = grp->children; g; g = g->next)
  1219. if ((retval = rec_print_metadata(g, tab_count)))
  1220. return retval;
  1221. (*tab_count)--;
  1222. }
  1223. return NC_NOERR;
  1224. }
  1225. /* Print out the internal metadata for a file. This is useful to check
  1226. * that netCDF is working! Nonetheless, this function will print
  1227. * nothing if logging is not set to at least two. */
  1228. int
  1229. log_metadata_nc(NC_FILE_INFO_T *nc)
  1230. {
  1231. NC_HDF5_FILE_INFO_T *h5 = nc->nc4_info;
  1232. int tab_count = 0;
  1233. LOG((2, "*** NetCDF-4 Internal Metadata: int_ncid 0x%x ext_ncid 0x%x",
  1234. nc->int_ncid, nc->ext_ncid));
  1235. if (!h5)
  1236. {
  1237. LOG((2, "This is a netCDF-3 file."));
  1238. return NC_NOERR;
  1239. }
  1240. LOG((2, "FILE - hdfid: 0x%x path: %s cmode: 0x%x parallel: %d redef: %d "
  1241. "fill_mode: %d no_write: %d next_nc_grpid: %d", h5->hdfid, h5->path,
  1242. h5->cmode, h5->parallel, h5->redef, h5->fill_mode, h5->no_write,
  1243. h5->next_nc_grpid));
  1244. return rec_print_metadata(h5->root_grp, &tab_count);
  1245. }
  1246. #endif /*LOGGING */
  1247. /* Show the in-memory metadata for a netcdf file. */
  1248. int
  1249. NC4_show_metadata(int ncid)
  1250. {
  1251. int retval = NC_NOERR;
  1252. #ifdef LOGGING
  1253. NC_FILE_INFO_T *nc;
  1254. int old_log_level = nc_log_level;
  1255. /* Find file metadata. */
  1256. if (!(nc = nc4_find_nc_file(ncid)))
  1257. return NC_EBADID;
  1258. /* Log level must be 2 to see metadata. */
  1259. nc_log_level = 2;
  1260. retval = log_metadata_nc(nc);
  1261. nc_log_level = old_log_level;
  1262. #endif /*LOGGING*/
  1263. return retval;
  1264. }