PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/other/netcdf_write_matrix/src/nctest/val.c

http://github.com/jbeezley/wrf-fire
C | 245 lines | 219 code | 14 blank | 12 comment | 34 complexity | b68153687976d081dc435fdd2485218c 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/val.c,v 1.12 1996/08/19 22:10:15 davis Exp $
  5. *********************************************************************/
  6. #include <stdio.h>
  7. #include "netcdf.h"
  8. #include "testcdf.h"
  9. #include "val.h"
  10. #include "error.h"
  11. /* fill typed value block with values of specified type */
  12. void
  13. val_fill(type, len, vals)
  14. nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */
  15. long len; /* number of elements to fill with */
  16. void *vals; /* start of first block of values */
  17. {
  18. static char pname[] = "val_fill";
  19. long half = len/2;
  20. int iel;
  21. union {
  22. char *cp;
  23. short *sp;
  24. nclong *lp;
  25. float *fp;
  26. double *dp;
  27. } gp;
  28. switch (type) {
  29. case NC_BYTE:
  30. case NC_CHAR:
  31. gp.cp = (char *) vals;
  32. for (iel = 0; iel < len; iel++)
  33. *gp.cp++ = (char) iel;
  34. break;
  35. case NC_SHORT:
  36. gp.sp = (short *) vals;
  37. for (iel = 0; iel < len; iel++)
  38. *gp.sp++ = (short) (iel - half); /* negative and positive values */
  39. break;
  40. case NC_LONG:
  41. gp.lp = (nclong *) vals;
  42. for (iel = 0; iel < len; iel++)
  43. *gp.lp++ = (nclong) (iel - half);
  44. break;
  45. case NC_FLOAT:
  46. gp.fp = (float *) vals;
  47. for (iel = 0; iel < len; iel++)
  48. *gp.fp++ = (float) (iel+1);
  49. break;
  50. case NC_DOUBLE:
  51. gp.dp = (double *) vals;
  52. for (iel = 0; iel < len; iel++)
  53. *gp.dp++ = (double) (iel - half);
  54. break;
  55. default:
  56. error("%s: bad type, test program error", pname);
  57. }
  58. }
  59. /* fill typed value block with zeros of specified type */
  60. void
  61. val_fill_zero(type, len, vals)
  62. nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */
  63. long len; /* number of elements to fill with */
  64. void *vals; /* start of first block of values */
  65. {
  66. static char pname[] = "val_fill_zero";
  67. int iel;
  68. union {
  69. char *cp;
  70. short *sp;
  71. nclong *lp;
  72. float *fp;
  73. double *dp;
  74. } gp;
  75. switch (type) {
  76. case NC_BYTE:
  77. case NC_CHAR:
  78. gp.cp = (char *) vals;
  79. for (iel = 0; iel < len; iel++)
  80. *gp.cp++ = (char) 0;
  81. break;
  82. case NC_SHORT:
  83. gp.sp = (short *) vals;
  84. for (iel = 0; iel < len; iel++)
  85. *gp.sp++ = (short) 0;
  86. break;
  87. case NC_LONG:
  88. gp.lp = (nclong *) vals;
  89. for (iel = 0; iel < len; iel++)
  90. *gp.lp++ = (nclong) 0;
  91. break;
  92. case NC_FLOAT:
  93. gp.fp = (float *) vals;
  94. for (iel = 0; iel < len; iel++)
  95. *gp.fp++ = (float) 0;
  96. break;
  97. case NC_DOUBLE:
  98. gp.dp = (double *) vals;
  99. for (iel = 0; iel < len; iel++)
  100. *gp.dp++ = (double) 0;
  101. break;
  102. default:
  103. error("%s: bad type, test program error", pname);
  104. }
  105. }
  106. /*
  107. * compare two typed value blocks, return 0 if equal, 1+n otherwise,
  108. * where n is the index of the first differing element.
  109. */
  110. int
  111. val_cmp (type, len, v1, v2)
  112. nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */
  113. long len; /* number of elements of type to compare */
  114. void *v1; /* start of first block of values */
  115. void *v2; /* start of second block of values */
  116. {
  117. static char pname[] = "val_cmp";
  118. int iel;
  119. union {
  120. char *cp;
  121. short *sp;
  122. nclong *lp;
  123. float *fp;
  124. double *dp;
  125. } gp, hp;
  126. switch (type) {
  127. case NC_CHAR:
  128. case NC_BYTE:
  129. gp.cp = (char *) v1;
  130. hp.cp = (char *) v2;
  131. for (iel = 0; iel < len; iel++) {
  132. if (*gp.cp != *hp.cp)
  133. return (iel + 1);
  134. gp.cp++;
  135. hp.cp++;
  136. }
  137. break;
  138. case NC_SHORT:
  139. gp.sp = (short *) v1;
  140. hp.sp = (short *) v2;
  141. for (iel = 0; iel < len; iel++) {
  142. if (*gp.sp != *hp.sp)
  143. return (iel + 1);
  144. gp.sp++;
  145. hp.sp++;
  146. }
  147. break;
  148. case NC_LONG:
  149. gp.lp = (nclong *) v1;
  150. hp.lp = (nclong *) v2;
  151. for (iel = 0; iel < len; iel++) {
  152. if (*gp.lp != *hp.lp)
  153. return (iel + 1);
  154. gp.lp++;
  155. hp.lp++;
  156. }
  157. break;
  158. case NC_FLOAT:
  159. gp.fp = (float *) v1;
  160. hp.fp = (float *) v2;
  161. for (iel = 0; iel < len; iel++) {
  162. if (*gp.fp != *hp.fp)
  163. return (iel + 1);
  164. gp.fp++;
  165. hp.fp++;
  166. }
  167. break;
  168. case NC_DOUBLE:
  169. gp.dp = (double *) v1;
  170. hp.dp = (double *) v2;
  171. for (iel = 0; iel < len; iel++) {
  172. if (*gp.dp != *hp.dp)
  173. return (iel + 1);
  174. gp.dp++;
  175. hp.dp++;
  176. }
  177. break;
  178. default:
  179. error("%s: bad type, test program error", pname);
  180. }
  181. return 0; /* all values agree */
  182. }
  183. /* print typed value block with values of specified type */
  184. void
  185. val_out(type, len, vals)
  186. nc_type type; /* netcdf type, NC_BYTE, ..., NC_DOUBLE */
  187. long len; /* number of elements to fill with */
  188. void *vals; /* start of first block of values */
  189. {
  190. static char pname[] = "val_oout";
  191. int iel;
  192. union {
  193. char *cp;
  194. short *sp;
  195. nclong *lp;
  196. float *fp;
  197. double *dp;
  198. } gp;
  199. (void) fprintf(stderr," ");
  200. switch (type) {
  201. case NC_BYTE:
  202. case NC_CHAR:
  203. gp.cp = (char *) vals;
  204. for (iel = 0; iel < len; iel++)
  205. (void)fprintf(stderr,"%d%s",*gp.cp++,iel<len-1 ? ", " : "");
  206. break;
  207. case NC_SHORT:
  208. gp.sp = (short *) vals;
  209. for (iel = 0; iel < len; iel++)
  210. (void)fprintf(stderr,"%d%s",*gp.sp++,iel<len-1 ? ", " : "");
  211. break;
  212. case NC_LONG:
  213. gp.lp = (nclong *) vals;
  214. for (iel = 0; iel < len; iel++)
  215. (void)fprintf(stderr,"%d%s",*gp.lp++,iel<len-1 ? ", " : "");
  216. break;
  217. case NC_FLOAT:
  218. gp.fp = (float *) vals;
  219. for (iel = 0; iel < len; iel++)
  220. (void)fprintf(stderr,"%g%s",*gp.fp++,iel<len-1 ? ", " : "");
  221. break;
  222. case NC_DOUBLE:
  223. gp.dp = (double *) vals;
  224. for (iel = 0; iel < len; iel++)
  225. (void)fprintf(stderr,"%g%s",*gp.dp++,iel<len-1 ? ", " : "");
  226. break;
  227. default:
  228. error("%s: bad type, test program error", pname);
  229. }
  230. (void) putc('\n',stderr);
  231. }