forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slabs.c
342 lines (311 loc) · 9.48 KB
/
slabs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctest/slabs.c,v 1.13 2009/02/11 16:17:39 ed Exp $
*********************************************************************/
#include <config.h>
#include <stdlib.h> /* for free() */
#include "netcdf.h"
#include "testcdf.h" /* defines in-memory test cdf structure */
#include "emalloc.h"
#include "add.h" /* functions to update in-memory netcdf */
#include "error.h"
#include "tests.h"
#define LEN_OF(array) ((sizeof array) / (sizeof array[0]))
/* dimension sizes */
#define NDIMS 4 /* number of dimensions */
#define WSIZE 7 /* sizes of dimensions */
#define XSIZE 5
#define YSIZE 6
#define ZSIZE 4
/* Any function that maps dimension values 1-1 to values is OK here */
#define VF(w) 1000*w[0]+100*w[1]+10*w[2]+w[3]
#define NVARS 6 /* number of variables */
/*
* Fill typed array element with specified value, that is
*
* v[ii] = val;
*/
static void
val_stuff(type, v, ii, val) /* v[ii] = val */
nc_type type; /* netcdf type of v, NC_BYTE, ..., NC_DOUBLE */
void *v; /* array of specified type */
int ii; /* it's v[ii] we want to store into */
long val; /* value to store */
{
static char pname[] = "val_stuff";
switch (type) {
case NC_BYTE:
case NC_CHAR:
((char *)v)[ii] = (char) val;
break;
case NC_SHORT:
((short *)v)[ii] = (short) val;
break;
case NC_LONG:
((nclong *)v)[ii] = (nclong) val;
break;
case NC_FLOAT:
((float *)v)[ii] = (float) val;
break;
case NC_DOUBLE:
((double *)v)[ii] = (double) val;
break;
default:
error("%s: bad type, test program error", pname);
}
}
/*
* Compare typed array element with specified value, that is return
*
* (v[ii] != val)
*
* returns 0 if equal, 1 if not equal
*/
static int
val_diff(type, v, ii, val) /* v[ii] != val */
nc_type type; /* netcdf type of v, NC_BYTE, ..., NC_DOUBLE */
void *v; /* array of specified type */
int ii; /* it's v[ii] we want to compare */
long val; /* value to compare with */
{
static char pname[] = "val_diff";
switch (type) {
case NC_BYTE:
case NC_CHAR:
return (((char *)v)[ii] != (char) val);
case NC_SHORT:
return (((short *)v)[ii] != (short) val);
case NC_LONG:
return (((nclong *)v)[ii] != (nclong) val);
case NC_FLOAT:
return (((float *)v)[ii] != (float) val);
case NC_DOUBLE:
return (((double *)v)[ii] != (double) val);
default:
error("%s: bad type, test program error", pname);
return (-1);
}
}
/*
* For each type of variable, put a four-dimensional hypercube of values
* with a single call to ncvarput. Then use ncvarget to retrieve a single
* interior value, an interior vector of values along each of the four
* dimensions, an interior plane of values along each of the six pairs of
* dimensions, and an interior cube of values along each of the four
* triples of dimensions. In each case, compare the retrieved values with
* the written values.
*/
int
test_slabs(cdfid)
int cdfid; /* handle of netcdf open and in data mode */
{
int nerrs = 0;
static char pname[] = "test_slabs";
static struct cdfdim dims[NDIMS] = {
{"w", WSIZE},
{"x", XSIZE},
{"y", YSIZE},
{"z", ZSIZE}
};
int dimids[NDIMS]; /* dimension ids */
long corner[NDIMS], edge[NDIMS], point[NDIMS];
static struct cdfvar va[NVARS] = { /* variables of all types */
{"bytevar", NC_BYTE, NDIMS, ___, 0},
{"charvar", NC_CHAR, NDIMS, ___, 0},
{"shortvar", NC_SHORT, NDIMS, ___, 0},
{"longvar", NC_LONG, NDIMS, ___, 0},
{"floatvar", NC_FLOAT, NDIMS, ___, 0},
{"doublevar", NC_DOUBLE, NDIMS, ___, 0},
};
void *v;
int varid[NVARS], iv; /* variable id */
int idim, jdim, kdim, ldim;
int iw, ix, iy, iz, ii, jj, kk;
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return 1;
}
/* back in define mode OK, now add dimensions */
for (idim = 0; idim < NDIMS; idim++) {
dimids[idim] = ncdimdef(cdfid, dims[idim].name, dims[idim].size);
if (dimids[idim] == -1) {
error("%s: ncdimdef failed", pname);
ncclose(cdfid);
return 1;
}
add_dim(&test, &dims[idim]);
}
/* define a multi-dimensional variable of each type */
for (iv = 0; iv < NVARS; iv++) {
va[iv].dims = (int *) emalloc(sizeof(int) * va[iv].ndims);
for (idim = 0; idim < va[iv].ndims; idim++)
va[iv].dims[idim] = dimids[idim];
varid[iv] = ncvardef(cdfid, va[iv].name, va[iv].type, va[iv].ndims,
va[iv].dims);
if (varid[iv] == -1) {
error("%s: ncvardef failed", pname);
ncclose(cdfid); return 1;
}
add_var(&test, &va[iv]); /* keep in-memory netcdf in sync */
free(va[iv].dims);
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return 1;
}
for (iv = 0; iv < NVARS; iv++) { /* test each type of variable */
v = emalloc(WSIZE*XSIZE*YSIZE*ZSIZE * nctypelen(va[iv].type));
/* fill it with values using a function of dimension indices */
ii = 0;
for (iw=0; iw < WSIZE; iw++) {
corner[0] = iw;
for (ix=0; ix < XSIZE; ix++) {
corner[1] = ix;
for (iy=0; iy < YSIZE; iy++) {
corner[2] = iy;
for (iz=0; iz < ZSIZE; iz++) {
corner[3] = iz;
/* v[ii++] = VF(corner); */
val_stuff(va[iv].type, v, ii, VF(corner));
ii++;
}
}
}
}
for (idim = 0; idim < NDIMS; idim++) {
corner[idim] = 0;
edge[idim] = dims[idim].size;
}
/* ncvarput the whole variable */
if (ncvarput(cdfid, varid[iv], corner, edge, (void *) v) == -1) {
error("%s: ncvarput failed", pname);
nerrs++;
}
add_data(&test, varid[iv], corner, edge); /* keep test in sync */
/*
* For several combinations of fixed dimensions, get a slab and compare
* values to function values.
*/
/* get an interior point */
for (idim=0; idim < NDIMS; idim++) {
corner[idim] = dims[idim].size/2;
edge[idim] = 1;
point[idim] = corner[idim];
}
if (ncvarget(cdfid, varid[iv], corner, edge, (void *) v) == -1) {
error("%s: ncvarget of one point failed", pname);
nerrs++;
}
/* if (v[0] != VF(point)) */
if (val_diff(va[iv].type, v, 0, VF(point))) {
error("%s: ncvarget got wrong value for point", pname);
nerrs++;
}
/* get an interior vector in each direction */
for (idim=0; idim < NDIMS; idim++) {
for (jdim=0; jdim < NDIMS; jdim++) {
corner[jdim] = dims[jdim].size/2;
edge[jdim] = 1;
point[jdim] = corner[jdim];
}
corner[idim] = 1; /* get vector along dimension idim */
edge[idim] = dims[idim].size - 2;
if (ncvarget(cdfid, varid[iv], corner, edge, (void *) v) == -1) {
error("%s: ncvarget of vector failed", pname);
nerrs++;
}
for (ii=(int)corner[idim]; ii <= edge[idim]; ii++) {
point[idim] = ii;
/* if (v[ii-1] != VF(point)) */
if (val_diff(va[iv].type, v, ii-1, VF(point))) {
error("%s: ncvarget got wrong value for vector", pname);
nerrs++;
}
}
}
/* get an interior plane in each direction */
for (idim=0; idim < NDIMS; idim++) {
for (jdim=idim+1; jdim < NDIMS; jdim++) {
for (kdim=0; kdim < NDIMS; kdim++) { /* reset corners and edges */
corner[kdim] = dims[kdim].size/2;
edge[kdim] = 1;
point[kdim] = corner[kdim];
}
corner[idim] = 1; /* interior plane along dimensions idim jdim */
corner[jdim] = 1;
edge[idim] = dims[idim].size - 2;
edge[jdim] = dims[jdim].size - 2;
if (ncvarget(cdfid, varid[iv], corner, edge, (void *) v) == -1) {
error("%s: ncvarget of plane failed", pname);
nerrs++;
}
for (ii=(int)corner[idim]; ii <= edge[idim]; ii++) {
for (jj=(int)corner[jdim]; jj <= edge[jdim]; jj++) {
point[idim] = ii;
point[jdim] = jj;
/* if (v[(ii-1)*edge[jdim]+jj-1] != VF(point)) { */
if (val_diff(va[iv].type, v,
(ii-1)*(int)edge[jdim]+jj-1, VF(point))) {
error("%s: ncvarget got wrong value in plane", pname);
error("idim=%d,jdim=%d,ii=%d,jj=%d",
idim,
jdim,
ii,
jj);
nerrs++;
}
}
}
}
}
/* get an interior cube in each direction */
for (idim=0; idim < NDIMS; idim++) {
for (jdim=idim+1; jdim < NDIMS; jdim++) {
for (kdim=jdim+1; kdim < NDIMS; kdim++) {
for (ldim=0; ldim < NDIMS; ldim++) { /* reset corners, edges */
corner[ldim] = dims[ldim].size/2;
edge[ldim] = 1;
point[ldim] = corner[ldim];
}
corner[idim] = 1; /* intr. cube along idim jdim kdim */
corner[jdim] = 1;
corner[kdim] = 1;
edge[idim] = dims[idim].size - 2;
edge[jdim] = dims[jdim].size - 2;
edge[kdim] = dims[kdim].size - 2;
if (ncvarget(cdfid, varid[iv], corner, edge, (void *) v) == -1) {
error("%s: ncvarget of cube failed", pname);
nerrs++;
}
for (ii=(int)corner[idim]; ii <= edge[idim]; ii++) {
for (jj=(int)corner[jdim]; jj <= edge[jdim]; jj++) {
for (kk=(int)corner[kdim]; kk <= edge[kdim]; kk++) {
point[idim] = ii;
point[jdim] = jj;
point[kdim] = kk;
/* if (v[((ii-1)*edge[jdim]+jj-1)*
edge[kdim]+kk-1] != VF(point)) { */
if (val_diff(va[iv].type,v,
((ii-1)*(int)edge[jdim]+jj-1)*
(int)edge[kdim]+kk-1,VF(point))) {
error("%s: ncvarget got wrong value in cube", pname);
error("idim=%d,jdim=%d,kdim=%d,ii=%d,jj=%d,kk=%d",
idim,
jdim,
kdim,
ii,
jj,
kk);
nerrs++;
}
}
}
}
}
}
}
free(v);
}
return nerrs;
}