forked from Unidata/netcdf-c
/
tst_fills.c
363 lines (322 loc) · 11.9 KB
/
tst_fills.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
353
354
355
356
357
358
359
360
361
362
363
/* This is part of the netCDF package. Copyright 2018 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use. See www.unidata.ucar.edu for more info.
Create a test file with default fill values for variables of each
type.
Ed Hartnett
*/
#include <nc_tests.h>
#include "err_macros.h"
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>
#define FILE_NAME "tst_fills.nc"
int
main(int argc, char **argv)
{ /* create tst_classic_fills.nc */
printf("\n*** Testing fill values.\n");
/* printf("*** testing very simple scalar string var..."); */
/* { */
/* #define STRING_VAR_NAME "Petition_of_Right" */
/* #define NDIMS_STRING 1 */
/* int ncid, varid; */
/* int varid_in; */
/* const char *data_out[NDIMS_STRING] = { */
/* "Humbly show unto our Sovereign Lord the King, the Lords " */
/* "Spiritual and Temporal, and Commons in Parliament assembles, " */
/* "that whereas it is declared and enacted by a statute made in " */
/* "the time of the reign of King Edward I, commonly called " */
/* "Stratutum de Tellagio non Concedendo, that no tallage or " */
/* "aid shall be laid or levied by the king or his heirs in this " */
/* "realm, without the good will and assent of the archbishops, " */
/* "bishops, earls, barons, knights, burgesses, and other the " */
/* "freemen of the commonalty of this realm; and by authority " */
/* "of parliament holden in the five-and-twentieth year of the " */
/* "reign of King Edward III, it is declared and enacted, that " */
/* "from thenceforth no person should be compelled to make any " */
/* "loans to the king against his will, because such loans were " */
/* "against reason and the franchise of the land; and by other " */
/* "laws of this realm it is provided, that none should be charged " */
/* "by any charge or imposition called a benevolence, nor by such " */
/* "like charge; by which statutes before mentioned, and other the " */
/* "good laws and statutes of this realm, your subjects have inherited " */
/* "this freedom, that they should not be compelled to contribute " */
/* "to any tax, tallage, aid, or other like charge not set by " */
/* "common consent, in parliament." */
/* }; */
/* char *data_in[NDIMS_STRING]; */
/* size_t index1[NDIMS_STRING] = {0}; */
/* /\* Create file with a 1D string var. Set its fill value to the */
/* * empty string. *\/ */
/* if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR; */
/* if (nc_def_var(ncid, STRING_VAR_NAME, NC_STRING, 0, NULL, &varid)) ERR; */
/* /\* Check it out. *\/ */
/* if (nc_inq_varid(ncid, STRING_VAR_NAME, &varid_in)) ERR; */
/* if (varid_in != varid) ERR; */
/* /\* Write string. *\/ */
/* if (nc_put_var1_string(ncid, varid_in, index1, data_out)) ERR; */
/* /\* Get the string, check it, and free it. *\/ */
/* if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */
/* if (strcmp(data_in[0], data_out[0])) ERR; */
/* if (nc_free_string(1, data_in)) ERR; */
/* /\* Close the file. *\/ */
/* if (nc_close(ncid)) ERR; */
/* /\* Now re-open file, read data, and check values again. *\/ */
/* if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; */
/* /\* Get the string, check it, and free it. *\/ */
/* if (nc_get_var_string(ncid, varid_in, data_in)) ERR; */
/* if (strcmp(data_in[0], data_out[0])) ERR; */
/* if (nc_free_string(1, data_in)) ERR; */
/* /\* Close the file. *\/ */
/* if (nc_close(ncid)) ERR; */
/* } */
/* SUMMARIZE_ERR; */
printf("*** testing fill values of one var...");
{
#define V1_NAME "v1"
#define MAX_VALS 10
int ncid, varid, rec_id, dims[2];
static int rec[1] = {1};
size_t start[2] = {0, 0};
size_t count[2] = {1, MAX_VALS};
char vals[MAX_VALS];
int i;
nc_set_log_level(4);
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Define dimensions and two vars, a 1D coordinate var for
* unlimited dimension, and a 2D var which uses the unlimited
* dimension. */
if (nc_def_dim(ncid, "rec", NC_UNLIMITED, &dims[0])) ERR;
if (nc_def_dim(ncid, "len", MAX_VALS, &dims[1])) ERR;
if (nc_def_var(ncid, "rec", NC_INT, 1, dims, &rec_id)) ERR;
if (nc_def_var(ncid, V1_NAME, NC_CHAR, 2, dims, &varid)) ERR;
/* Extend record dimension by 1. */
if (nc_put_vara_int(ncid, rec_id, start, count, rec)) ERR;
/* Read the other variable; it must have only fill values. */
if (nc_get_vara_text(ncid, 1, start, count, vals)) ERR;
for (i = 0; i < MAX_VALS; i++)
if(vals[i] != NC_FILL_CHAR) ERR;
if (nc_close(ncid)) ERR;
/* Now re-open file, read data, and check values again. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
/* Read the other variable; it must have only fill values. */
if (nc_get_vara_text(ncid, 1, start, count, vals)) ERR;
for (i = 0; i < MAX_VALS; i++)
if(vals[i] != NC_FILL_CHAR) ERR;
if(nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing fill values of lots of vars...");
{
int ncid; /* netCDF id */
#define NVALS 10 /* values per fixed-size variable or record */
#define NFIXVARS 6 /* number of fixed-size vars, one of each type */
#define NRECVARS 6 /* number of record vars, one of each type */
#define RANK_REC 1
#define RANK_FIXVARS 1
#define RANK_RECVARS 2
/* dimension ids */
int rec_dim;
int len_dim;
/* dimension lengths */
size_t rec_len = NC_UNLIMITED;
size_t len_len = NVALS;
/* variable ids */
int rec_id;
int fixvar_ids[NFIXVARS];
int recvar_ids[NRECVARS];
int rec_dims[RANK_REC];
int fixvar_dims[RANK_FIXVARS];
int recvar_dims[RANK_RECVARS];
int fixvar, recvar, i;
char *fnames[] = {"c", "b", "s", "i", "f", "d"};
char *rnames[] = {"cr", "br", "sr", "ir", "fr", "dr"};
nc_type types[] = {NC_CHAR, NC_BYTE, NC_SHORT, NC_INT, NC_FLOAT, NC_DOUBLE};
/*if (nc_set_default_format(format + 1, NULL)) ERR;*/
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* define dimensions */
if (nc_def_dim(ncid, "rec", rec_len, &rec_dim)) ERR;
if (nc_def_dim(ncid, "len", len_len, &len_dim)) ERR;
rec_dims[0] = rec_dim;
if (nc_def_var(ncid, "rec", NC_INT, RANK_REC, rec_dims, &rec_id)) ERR;
/* define fixed and record variables of all 6 primitive types */
fixvar_dims[0] = len_dim;
for (fixvar = 0; fixvar < NFIXVARS; fixvar++)
if (nc_def_var(ncid, fnames[fixvar], types[fixvar], RANK_FIXVARS,
fixvar_dims, &fixvar_ids[fixvar])) ERR;
recvar_dims[0] = rec_dim;
recvar_dims[1] = len_dim;
for (recvar = 0; recvar < NRECVARS; recvar++)
if (nc_def_var(ncid, rnames[recvar], types[recvar], RANK_RECVARS,
recvar_dims, &recvar_ids[recvar])) ERR;
/* leave define mode */
if (nc_enddef(ncid)) ERR;
{ /* store rec */
static size_t rec_start[RANK_REC];
static size_t rec_count[RANK_REC];
static int rec[] = {1};
rec_len = 1; /* number of records of rec data */
rec_start[0] = 0;
rec_count[0] = rec_len;
if (nc_put_vara_int(ncid, rec_id, rec_start, rec_count, rec)) ERR;
}
if (nc_close(ncid)) ERR;
/* Now re-open file, read data, and check values */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
/* Check that fixed-size variables are full of fill values */
for (fixvar = 0; fixvar < NFIXVARS; fixvar++) {
int varid;
nc_type type;
if (nc_inq_varid(ncid, fnames[fixvar], &varid)) ERR;
if (nc_inq_vartype(ncid, varid, &type)) ERR;
switch(type) {
case NC_CHAR:
{
char vals[NVALS];
if (nc_get_var_text(ncid, varid, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_CHAR) ERR;
}
break;
case NC_BYTE:
{
signed char vals[NVALS];
if (nc_get_var_schar(ncid, varid, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_BYTE) ERR;
}
break;
case NC_SHORT:
{
short vals[NVALS];
if (nc_get_var_short(ncid, varid, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_SHORT) ERR;
}
break;
case NC_INT:
{
int vals[NVALS];
if (nc_get_var_int(ncid, varid, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_INT) ERR;
}
break;
case NC_FLOAT:
{
float vals[NVALS];
if (nc_get_var_float(ncid, varid, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_FLOAT) ERR;
}
break;
case NC_DOUBLE:
{
double vals[NVALS];
if (nc_get_var_double(ncid, varid, vals)) ERR;
for (i = 0; i < NVALS; i++)
if (vals[i] != NC_FILL_DOUBLE) ERR;
}
break;
default:
ERR;
}
}
/* Read record, check record variables have only fill values */
for (recvar = 0; recvar < NRECVARS; recvar++) {
int varid;
nc_type type;
size_t start[] = {0, 0};
size_t count[] = {1, NVALS};
if (nc_inq_varid(ncid, rnames[recvar], &varid)) ERR;
if (nc_inq_vartype(ncid, varid, &type)) ERR;
switch(type) {
case NC_CHAR:
{
char vals[NVALS];
if (nc_get_vara_text(ncid, varid, start, count, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_CHAR) ERR;
}
break;
case NC_BYTE:
{
signed char vals[NVALS];
if (nc_get_vara_schar(ncid, varid, start, count, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_BYTE) ERR;
}
break;
case NC_SHORT:
{
short vals[NVALS];
if (nc_get_vara_short(ncid, varid, start, count, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_SHORT) ERR;
}
break;
case NC_INT:
{
int vals[NVALS];
if (nc_get_vara_int(ncid, varid, start, count, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_INT) ERR;
}
break;
case NC_FLOAT:
{
float vals[NVALS];
if (nc_get_vara_float(ncid, varid, start, count, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_FLOAT) ERR;
}
break;
case NC_DOUBLE:
{
double vals[NVALS];
if (nc_get_vara_double(ncid, varid, start, count, vals)) ERR;
for (i = 0; i < NVALS; i++)
if(vals[i] != NC_FILL_DOUBLE) ERR;
}
break;
default:
ERR;
}
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** testing fill mode...");
#define NDIM1 1
#define DIM_LEN 4
#define DIM_NAME "my_dim"
#define VAR_NAME "my_var"
{
int ncid;
int dimid;
int varid;
int cmode = 0;
char testfile[] = "test.nc";
size_t index = 2;
int test_val = 42;
int no_fill;
int ret;
if ((ret = nc_create(testfile, cmode, &ncid)))
return ret;
if ((ret = nc_def_dim(ncid, DIM_NAME, DIM_LEN, &dimid)))
return ret;
if ((ret = nc_def_var(ncid, VAR_NAME, NC_INT, NDIM1, &dimid, &varid)))
return ret;
if ((ret = nc_enddef(ncid)))
return ret;
if ((ret = nc_put_var1_int(ncid, varid, &index, &test_val)))
return ret;
if ((ret = nc_inq_var_fill(ncid, varid, &no_fill, NULL)))
return ret;
if (no_fill) ERR;
if ((ret = nc_close(ncid)))
return ret;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}