forked from Unidata/netcdf-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vardef.c
352 lines (331 loc) · 9.8 KB
/
vardef.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
342
343
344
345
346
347
348
349
350
351
352
/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
* $Header: /upc/share/CVS/netcdf-3/nctest/vardef.c,v 1.18 2009/10/28 18:30:50 dmh Exp $
*********************************************************************/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h> /* for free() */
#ifndef NO_FLOAT_H
#include <float.h> /* for FLT_EPSILON, DBL_EPSILON */
#endif /* NO_FLOAT_H */
#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]))
static float float_eps;
static double double_eps;
static float
float_epsilon()
{
float float_eps;
#ifndef NO_FLOAT_H
float_eps = FLT_EPSILON;
#else /* NO_FLOAT_H */
{
float etop, ebot, eps;
float one = 1.0;
float two = 2.0;
etop = 1.0;
ebot = 0.0;
eps = ebot + (etop - ebot)/two;
while (eps != ebot && eps != etop) {
float epsp1;
epsp1 = one + eps;
if (epsp1 > one)
etop = eps;
else
ebot = eps;
eps = ebot + (etop - ebot)/two;
}
float_eps = two * etop;
}
#endif /* NO_FLOAT_H */
return float_eps;
}
static double
double_epsilon()
{
double double_eps;
#ifndef NO_FLOAT_H
double_eps = DBL_EPSILON;
#else /* NO_FLOAT_H */
{
double etop, ebot, eps;
double one = 1.0;
double two = 2.0;
etop = 1.0;
ebot = 0.0;
eps = ebot + (etop - ebot)/two;
while (eps != ebot && eps != etop) {
double epsp1;
epsp1 = one + eps;
if (epsp1 > one)
etop = eps;
else
ebot = eps;
eps = ebot + (etop - ebot)/two;
}
double_eps = two * etop;
}
#endif /* NO_FLOAT_H */
return double_eps;
}
static void
init_epsilons()
{
float_eps = float_epsilon();
double_eps = double_epsilon();
}
/*
* Test ncvardef
* check that proper define worked with ncvarinq
* check that returned id is one more than previous id
* try redefining an existing variable, check error
* try adding scalar variable (no dimensions)
* try with bad datatype, check error
* try with bad number of dimensions, check error
* try with bad dimension ids, check error
* try in data mode, check error
*/
int
test_ncvardef(path)
const char *path; /* name of writable netcdf file to open */
{
int nerrs = 0;
int cdfid; /* netcdf id */
static char pname[] = "test_ncvardef";
int id, iv;
static struct cdfvar va[] = { /* variables of all shapes and sizes */
{"bytev", NC_BYTE, 6, ___, 0},
{"charv", NC_CHAR, 5, ___, 0},
{"shortv", NC_SHORT, 4, ___, 0},
{"longv", NC_LONG, 3, ___, 0},
{"floatv", NC_FLOAT, 2, ___, 0},
{"doublev", NC_DOUBLE, 1, ___, 0},
{"scalarv", NC_DOUBLE, 0, ___, 0}
};
int nv = LEN_OF(va); /* number of variables to define */
int va_id[LEN_OF(va)]; /* variable ids */
static struct cdfvar tmp = /* variable for testing bad types, etc. */
{"tmpv", NC_DOUBLE, 1, ___, 0};
/* if d5 >= 91 in following, problem on machines with 16-bit ints ??? */
static struct cdfdim di[] = { /* a bunch of dimensions */
{"d0", 2}, {"d1",3}, {"d2",5}, {"d3", 6}, {"d4", 4}, {"d5", 31}};
int nd = LEN_OF(di); /* number of dimensions */
int di_id[LEN_OF(di)]; /* dimension ids */
(void) fprintf(stderr, "*** Testing %s ...\t", &pname[5]);
init_epsilons();
if ((cdfid = ncopen(path, NC_WRITE)) == -1) {
error("%s: ncopen failed", pname);
return ++nerrs;
}
/* opened, defining a variable should fail in data mode */
if (ncvardef(cdfid, va[0].name, va[0].type, va[0].ndims, va[0].dims)
!= -1) {
error("%s: ncvardef should have failed in data mode", pname);
ncclose(cdfid); return ++nerrs;
}
/* enter define mode */
if (ncredef(cdfid) == -1) {
error("%s: cdredef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* Add nd more dimensions */
for (id = 0; id < nd; id++) {
if ((di_id[id] = ncdimdef(cdfid, di[id].name, di[id].size)) == -1) {
error("%s: ncdimdef failed for %s, size %d",
pname, di[id].name, di[id].size);
ncclose(cdfid); return ++nerrs;
}
add_dim(&test, &di[id]); /* keep in-memory netcdf in sync */
}
tmp.dims = (int *) emalloc(sizeof(int) * MAX_VAR_DIMS);
tmp.name = (char *) emalloc(MAX_NC_NAME);
/* in define mode, add variables of each type with various shapes */
for (iv = 0; iv < nv; iv++) {
/* set shape to use subset of dimensions previously defined */
va[iv].dims = (int *) emalloc(sizeof(int) * va[iv].ndims);
for (id = 0; id < va[iv].ndims; id++)
va[iv].dims[id] = di_id[id];
if ((va_id[iv] = ncvardef(cdfid, va[iv].name, va[iv].type,
va[iv].ndims, va[iv].dims)) == -1) {
error("%s: ncvardef failed", pname);
errvar(&test,&va[iv]); /* prints details about variable */
ncclose(cdfid); return ++nerrs;
}
add_var(&test, &va[iv]); /* keep in-memory netcdf in sync */
/* check that var id returned is one more than previous var id */
if (va_id[iv] != test.nvars - 1) {
error("%s: ncvardef returned %d for var id, expected %d",
pname, va_id[iv], test.nvars-1);
ncclose(cdfid); return ++nerrs;
}
/* use ncvarinq to get values just set and compare values */
if (ncvarinq(cdfid, va_id[iv], tmp.name, &tmp.type,
&tmp.ndims, tmp.dims, &tmp.natts) == -1) {
error("%s: ncvarinq failed", pname);
errvar(&test,&va[iv]); /* prints details about variable */
ncclose(cdfid); return ++nerrs;
}
if (strcmp(tmp.name, va[iv].name) != 0 ||
tmp.type != va[iv].type ||
tmp.ndims != va[iv].ndims ||
tmp.natts != va[iv].natts) {
error("%s: ncvardef and ncvarinq don't agree for %s",
pname, va[iv].name);
nerrs++;
errvar(&test,&va[iv]);
errvar(&test,&tmp);
}
for (id = 0; id < va[iv].ndims; id++) {
if (tmp.dims[id] != va[iv].dims[id]) {
error("%s: ncvardef and ncvarinq don't agree on shape of %s",
pname, va[iv].name);
nerrs++;
errvar(&test,&va[iv]);
errvar(&test,&tmp);
}
}
}
/* try adding same variable again, this should fail */
if (ncvardef(cdfid, va[0].name, va[0].type,
va[0].ndims, va[0].dims) != -1) {
error("%s: ncvardef should not allow redefinition", pname);
ncclose(cdfid); return ++nerrs;
}
/* try bad type, should fail */
if (ncvardef(cdfid, "badtype", BAD_TYPE, va[0].ndims, va[0].dims) != -1) {
error("%s: ncvardef should have failed on bad type", pname);
ncclose(cdfid); return ++nerrs;
}
/* try bad ndims, should fail */
if (ncvardef(cdfid, "badndims", va[0].type, -1, va[0].dims) != -1) {
error("%s: ncvardef should have failed on bad ndims", pname);
ncclose(cdfid); return ++nerrs;
}
/* try bad ids in dims vector, should fail */
va[0].dims[va[0].ndims-1] = -1;
if (ncvardef(cdfid, "baddims", va[0].type, va[0].ndims, va[0].dims)
!= -1) {
error("%s: ncvardef should have failed on negative dim id in dims",
pname);
ncclose(cdfid); return ++nerrs;
}
if (ncendef (cdfid) == -1) {
error("%s: ncendef failed", pname);
ncclose(cdfid); return ++nerrs;
}
/* try reading a value of each type, should get appropriate fill value */
for (iv = 0; iv < nv; iv++) {
static long where[] = {0,0,0,0,0,0};
switch(va[iv].type) {
case NC_BYTE:
{
char val, fillval = FILL_BYTE;
if (ncvarget1(cdfid, va_id[iv], where, (void *) &val) != -1) {
if (val != fillval) {
error("%s: unwritten byte not FILL_BYTE", pname);
nerrs++;
}
} else {
error("%s: ncvarget1 failure for byte", pname);
nerrs++;
}
}
break;
case NC_CHAR:
{
char val, fillval = FILL_CHAR;
if (ncvarget1(cdfid, va_id[iv], where, (void *) &val) != -1) {
if (val != fillval) {
error("%s: unwritten char not FILL_CHAR", pname);
nerrs++;
}
} else {
error("%s: ncvarget1 failure for char", pname);
nerrs++;
}
}
break;
case NC_SHORT:
{
short val, fillval = FILL_SHORT;
if (ncvarget1(cdfid, va_id[iv], where, (void *) &val) != -1) {
if (val != fillval) {
error("%s: unwritten short not FILL_SHORT", pname);
nerrs++;
}
} else {
error("%s: ncvarget1 failure for short", pname);
nerrs++;
}
}
break;
case NC_LONG:
{
nclong val, fillval = FILL_LONG;
if (ncvarget1(cdfid, va_id[iv], where, (void *) &val) != -1) {
if (val != fillval) {
error("%s: unwritten long not FILL_LONG", pname);
nerrs++;
}
} else {
error("%s: ncvarget1 failure for long", pname);
nerrs++;
}
}
break;
#define absval(x) ( (x) < 0 ? -(x) : (x) )
case NC_FLOAT:
{
float val, fillval = FILL_FLOAT;
if (ncvarget1(cdfid, va_id[iv], where, (void *) &val) != -1) {
if (absval(val-fillval) > absval(float_eps * fillval)) {
error("%s: unwritten float not FILL_FLOAT", pname);
nerrs++;
}
} else {
error("%s: ncvarget1 failure for float", pname);
nerrs++;
}
}
break;
case NC_DOUBLE:
{
double val, fillval = FILL_DOUBLE;
if (ncvarget1(cdfid, va_id[iv], where, (void *) &val) != -1) {
#define DBL_FUDGE 8.0 /* can be 1.0 on every platform we've seen
except for VAX/Ultrix 4.3 with cc */
if (absval(val-fillval) > DBL_FUDGE * absval(double_eps * fillval)) {
error("%s: unwritten double not FILL_DOUBLE", pname);
nerrs++;
}
} else {
error("%s: ncvarget1 failure for double", pname);
nerrs++;
}
}
break;
default: break;
}
}
if (ncclose (cdfid) == -1) {
error("%s: ncclose failed", pname);
return ++nerrs;
}
free (tmp.dims);
free (tmp.name);
for (iv = 0; iv < nv; iv++)
if (va[iv].dims)
free(va[iv].dims);
if (nerrs > 0)
(void) fprintf(stderr,"FAILED! ***\n");
else
(void) fprintf(stderr,"ok ***\n");
return nerrs;
}