PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/other/netcdf_write_matrix/src/nctest/add.c

http://github.com/jbeezley/wrf-fire
C | 207 lines | 172 code | 20 blank | 15 comment | 24 complexity | 4d7c7525a5c95d514710984efd74501c MD5 | raw file
Possible License(s): AGPL-1.0
  1. /*********************************************************************
  2. * Copyright 1993, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header: /upc/share/CVS/netcdf-3/nctest/add.c,v 1.17 2004/11/16 21:33:08 russ Exp $
  5. *********************************************************************/
  6. /*
  7. * utility functions to update in-memory netcdf by adding new
  8. * dimensions, variables, and attributes.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h> /* for free() */
  13. #include "netcdf.h"
  14. #include "testcdf.h"
  15. #include "add.h"
  16. #include "emalloc.h"
  17. struct netcdf test; /*
  18. * in-memory netcdf structure, kept in sync
  19. * with disk netcdf
  20. */
  21. void
  22. add_dim (test, idim) /* add the dimension idim to the netcdf test */
  23. struct netcdf *test;
  24. struct cdfdim *idim;
  25. {
  26. static char pname[] = "add_dim";
  27. if (test->ndims >= MAX_NC_DIMS) {
  28. (void)fprintf(stderr,
  29. "%s: too many dimensions (%d)", pname, test->ndims);
  30. return;
  31. }
  32. test->dims[test->ndims].size = idim->size;
  33. test->dims[test->ndims].name = (char *) emalloc(strlen(idim->name) + 1);
  34. (void) strcpy(test->dims[test->ndims].name, idim->name);
  35. if (idim->size == NC_UNLIMITED)
  36. test->xdimid = test->ndims;
  37. test->ndims++;
  38. }
  39. void
  40. add_var (test, ivar) /* add the variable ivar to the netcdf test */
  41. struct netcdf *test;
  42. struct cdfvar *ivar;
  43. {
  44. static char pname[] = "add_var";
  45. int i;
  46. if (test->nvars >= MAX_NC_VARS) {
  47. (void)fprintf(stderr,
  48. "%s: too many variables (%d)", pname, test->nvars);
  49. return;
  50. }
  51. test->vars[test->nvars].name = (char *) emalloc(strlen(ivar->name) + 1);
  52. (void) strcpy(test->vars[test->nvars].name, ivar->name);
  53. test->vars[test->nvars].type = ivar->type;
  54. test->vars[test->nvars].ndims = ivar->ndims;
  55. test->vars[test->nvars].dims = (int *) emalloc(sizeof(int)*ivar->ndims);
  56. for (i = 0; i < ivar->ndims; i++)
  57. test->vars[test->nvars].dims[i] = ivar->dims[i];
  58. test->vars[test->nvars].natts = 0;
  59. test->nvars++;
  60. }
  61. void
  62. add_att (test, varid, iatt) /* add attribute iatt to the netcdf test */
  63. struct netcdf *test;
  64. int varid; /* variable id */
  65. struct cdfatt *iatt;
  66. {
  67. static char pname[] = "add_att";
  68. int ia; /* attribute number */
  69. if (test->natts >= MAX_TEST_ATTS) {
  70. (void)fprintf(stderr,
  71. "%s: too many attributes (%d)", pname, test->natts);
  72. return;
  73. }
  74. /* if already defined, change existing attribute and return */
  75. for (ia = 0; ia < test->natts ; ia++) {
  76. if (test->atts[ia].var == varid &&
  77. strcmp(test->atts[ia].name, iatt->name) == 0) {
  78. test->atts[ia].type = iatt->type;
  79. test->atts[ia].len = iatt->len;
  80. test->atts[ia].val = iatt->val;
  81. return;
  82. }
  83. }
  84. /* otherwise, add new attribute to list */
  85. test->atts[test->natts].var = varid;
  86. test->atts[test->natts].name = (char *) emalloc(strlen(iatt->name) + 1);
  87. (void) strcpy(test->atts[test->natts].name, iatt->name);
  88. test->atts[test->natts].type = iatt->type;
  89. test->atts[test->natts].len = iatt->len;
  90. test->atts[test->natts].val = iatt->val;
  91. test->natts++;
  92. if (varid == NC_GLOBAL)
  93. test->ngatts++;
  94. else
  95. test->vars[varid].natts++;
  96. }
  97. void
  98. add_reset(test) /* reset in-memory netcdf test to empty */
  99. struct netcdf *test;
  100. {
  101. test->ndims = 0;
  102. test->nvars = 0;
  103. test->natts = 0;
  104. test->ngatts = 0;
  105. test->xdimid = -1; /* no unlimited dimension */
  106. }
  107. void
  108. del_att (test, varid, iatt) /* delete attribute iatt in the netcdf test */
  109. struct netcdf *test;
  110. int varid; /* variable id */
  111. struct cdfatt *iatt;
  112. {
  113. static char pname[] = "del_att";
  114. int ia, ib; /* attribute number */
  115. for (ia = 0; ia < test->natts ; ia++) { /* find attribute to delete */
  116. if (test->atts[ia].var == varid &&
  117. strcmp(test->atts[ia].name, iatt->name) == 0) {
  118. free(test->atts[ia].name);
  119. for (ib = ia+1; ib < test->natts; ib++) { /* move down */
  120. test->atts[ib-1].var = test->atts[ib].var;
  121. test->atts[ib-1].name = test->atts[ib].name;
  122. test->atts[ib-1].type = test->atts[ib].type;
  123. test->atts[ib-1].len = test->atts[ib].len;
  124. test->atts[ib-1].val = test->atts[ib].val;
  125. }
  126. test->natts--;
  127. if (varid == NC_GLOBAL)
  128. test->ngatts--;
  129. else
  130. test->vars[varid].natts--;
  131. return;
  132. }
  133. }
  134. /* not found */
  135. (void) fprintf(stderr, "%s: no such attribute as (%s, %s)", pname,
  136. test->vars[varid].name, iatt->name);
  137. }
  138. void
  139. add_data(test, varid, start, edges) /* keep max record written updated */
  140. struct netcdf *test;
  141. int varid;
  142. long start[];
  143. long edges[];
  144. {
  145. if (varid != test->xdimid) /* not a record variable */
  146. return;
  147. if (start[0] + edges[0] > test->dims[test->xdimid].size)
  148. test->dims[test->xdimid].size = start[0] + edges[0];
  149. }
  150. void
  151. errvar(cdfp, varp)
  152. struct netcdf *cdfp;
  153. struct cdfvar *varp;
  154. {
  155. const char *types;
  156. int id;
  157. switch (varp->type) {
  158. case NC_BYTE:
  159. types = "NC_BYTE";
  160. break;
  161. case NC_CHAR:
  162. types = "NC_CHAR";
  163. break;
  164. case NC_SHORT:
  165. types = "NC_SHORT";
  166. break;
  167. case NC_LONG:
  168. types = "NC_LONG";
  169. break;
  170. case NC_FLOAT:
  171. types = "NC_FLOAT";
  172. break;
  173. case NC_DOUBLE:
  174. types = "NC_DOUBLE ";
  175. break;
  176. default:
  177. types = "UNKNOWN";
  178. break;
  179. }
  180. (void) fprintf(stderr," name=%s type=%s dims=(",
  181. varp->name, types);
  182. for (id = 0; id < varp->ndims; id++)
  183. (void) fprintf(stderr, "%ld%s",
  184. (long)cdfp->dims[varp->dims[id]].size,
  185. id < varp->ndims - 1 ? ", " : "");
  186. (void) fprintf(stderr, ")\n");
  187. }