forked from Unidata/netcdf-c
/
tst_dims2.c
422 lines (359 loc) · 15.1 KB
/
tst_dims2.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* This is part of the netCDF package. Copyright 2008 University
Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
conditions of use. See www.unidata.ucar.edu for more info.
Test netcdf-4 dimensions some more.
$Id: tst_dims2.c,v 1.17 2010/05/25 13:53:04 ed Exp $
*/
#include <config.h>
#include <nc_tests.h>
#include "err_macros.h"
#define FILE_NAME "tst_dims2.nc"
#define NDIMS1 1
#define NDIMS2 2
int
main(int argc, char **argv)
{
printf("\n*** Testing netcdf-4 dimensions some more.\n");
printf("*** Checking non-coordinate variable with same name as dimension...");
{
#define CRAZY "crazy"
#define NUTS "nuts"
#define NUM_CRAZY 3
#define NUM_NUTS 5
int nuts_dimid, crazy_dimid, dimid_in;
int varid, ncid;
nc_type xtype_in, xtype = NC_CHAR;
int ndims_in, nvars_in, natts_in, unlimdimid_in;
char name_in[NC_MAX_NAME + 1];
/* Create a file with 2 dims and one var. The var is named the
* same as one of the dimensions, but uses the other dimension,
* and thus is not a netCDF coordinate variable. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, CRAZY, NUM_CRAZY, &crazy_dimid)) ERR;
if (nc_def_dim(ncid, NUTS, NUM_NUTS, &nuts_dimid)) ERR;
if (nc_def_var(ncid, CRAZY, xtype, NDIMS1, &nuts_dimid, &varid)) ERR;
/* Check out the file. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != -1) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, &dimid_in, &natts_in)) ERR;
if (strcmp(name_in, CRAZY) || xtype_in != xtype || ndims_in != NDIMS1 ||
natts_in != 0 || dimid_in != nuts_dimid) ERR;
if (nc_close(ncid)) ERR;
/* Reopen and check the file. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != -1) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, &dimid_in, &natts_in)) ERR;
if (strcmp(name_in, CRAZY) || xtype_in != xtype || ndims_in != NDIMS1 ||
natts_in != 0 || dimid_in != nuts_dimid) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking 2D coordinate variable with user-provided code...");
{
/* This test is from user Dipl.-Ing. Christian Schlamkow,
* University of Rostock, Coastal Engineering Group. Thanks
* Christian! Next time I'm in Rostock I'll come by and buy you
* a beer. ;-) */
#define TL 15
int ncid;
int time_dim;
int tl_dim;
int time_dimids[NDIMS2];
int time_id;
size_t time_index[NDIMS2];
size_t time_count[NDIMS2];
const char ttext[TL + 1]="20051224.150000";
char ttext_in[TL + 1];
int ndims_in, nvars_in, natts_in, unlimdimid_in, dimids_in[NDIMS2];
nc_type xtype_in;
char name_in[NC_MAX_NAME + 1];
int i;
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/*create dimensions*/
if (nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim)) ERR;
if (nc_def_dim(ncid, "tl", TL, &tl_dim)) ERR;
/*create variables*/
time_dimids[0]=time_dim;
time_dimids[1]=tl_dim;
if (nc_def_var(ncid, "time", NC_CHAR, NDIMS2, time_dimids, &time_id)) ERR;
/*close dataset*/
if (nc_close(ncid)) ERR;
/*reopen dataset*/
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
/*get dimensions and variable ids*/
if (nc_inq_varid(ncid, "time", &time_id)) ERR;
/*fill in data*/
time_index[0] = 0;
time_index[1] = 0;
time_count[0] = 1;
time_count[1] = TL; /* Note we are not writing NULL char. */
if (nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR;
/*close dataset*/
if (nc_close(ncid)) ERR;
/* Reopen and check everything. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != NDIMS2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, "time") || xtype_in != NC_CHAR || ndims_in != NDIMS2 ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS2; i++)
if (time_dimids[i] != dimids_in[i]) ERR;
if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR;
ttext_in[TL] = 0; /* Add a NULL so strcmp will work. */
if (strcmp(ttext, ttext_in)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking 2D coordinate variable with user-provided code some more...");
{
/* This test is from user Dipl.-Ing. Christian Schlamkow,
* University of Rostock, Coastal Engineering Group. Thanks
* Christian! Next time I'm in Rostock I'll come by and buy you
* a beer. ;-) */
#define TL 15
#define NDIMS2 2
#define NUM_TIMES 16
int ncid;
int time_dim;
int tl_dim;
int time_dimids[NDIMS2];
int time_id;
size_t time_index[NDIMS2];
size_t time_count[NDIMS2];
const char ttext[TL + 1]="20051224.150000";
char ttext_in[TL + 1];
int ndims_in, nvars_in, natts_in, unlimdimid_in, dimids_in[NDIMS2];
nc_type xtype_in;
char name_in[NC_MAX_NAME + 1];
int i;
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/*create dimensions*/
if (nc_def_dim(ncid, "time", NC_UNLIMITED, &time_dim)) ERR;
if (nc_def_dim(ncid, "tl", TL, &tl_dim)) ERR;
/*create variables*/
time_dimids[0]=time_dim;
time_dimids[1]=tl_dim;
if (nc_def_var(ncid, "time", NC_CHAR, NDIMS2, time_dimids, &time_id)) ERR;
/*close dataset*/
if (nc_close(ncid)) ERR;
/*reopen dataset*/
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
/*get dimensions and variable ids*/
if (nc_inq_varid(ncid, "time", &time_id)) ERR;
time_index[1] = 0;
time_count[0] = 1;
time_count[1] = TL;
for (time_index[0] = 0; time_index[0] < NUM_TIMES; time_index[0]++)
if(nc_put_vara_text(ncid, time_id, time_index, time_count, ttext)) ERR;
/*close dataset*/
if (nc_close(ncid)) ERR;
/* Reopen and check everything. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != NDIMS2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, "time") || xtype_in != NC_CHAR || ndims_in != NDIMS2 ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS2; i++)
if (time_dimids[i] != dimids_in[i]) ERR;
time_index[0] = 0;
if (nc_get_vara_text(ncid, time_id, time_index, time_count, ttext_in)) ERR;
ttext_in[TL] = 0; /* Add a NULL so strcmp will work. */
if (strcmp(ttext, ttext_in)) ERR;
if (nc_close(ncid)) ERR;
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking 2D coordinate variable...");
{
#define TIME "time"
#define LEN "len"
#define NDIMS 2
int dimids[NDIMS], dimids_in[NDIMS];
int varid, ncid;
nc_type xtype_in, xtype = NC_CHAR;
int ndims_in, nvars_in, natts_in, unlimdimid_in;
char name_in[NC_MAX_NAME + 1];
int i;
/* Create a file with a 2D time coordinate var, with one of the
* dimensions being the length of the time string. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, TIME, NC_UNLIMITED, &dimids[0])) ERR;
if (nc_def_dim(ncid, LEN, 20, &dimids[1])) ERR;
if (nc_def_var(ncid, TIME, xtype, 2, dimids, &varid)) ERR;
/* Check out the file. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, TIME) || xtype_in != xtype || ndims_in != NDIMS ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS; i++)
if (dimids[i] != dimids_in[i]) ERR;
if (nc_close(ncid)) ERR;
/* Recheck the file. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, TIME) || xtype_in != xtype || ndims_in != NDIMS ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS; i++)
if (dimids[i] != dimids_in[i]) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking multiple unlimited dimensions...");
{
#define NDIMS 2
#define MAX_VALUES 3
int dimids[NDIMS], varid, ncid;
double value[MAX_VALUES];
size_t time_len, beam_len, start[NDIMS] = {0, 0}, count[NDIMS] = {1, MAX_VALUES};
int i;
/* Initialize some phony data. */
for (i = 0; i < MAX_VALUES; i++)
value[i] = MAX_VALUES - i;
/* Create a file with 2 unlimited dims, and one var that uses
* both of them. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, "time", NC_UNLIMITED, &dimids[0])) ERR;
if (nc_def_dim(ncid, "beam", NC_UNLIMITED, &dimids[1])) ERR;
if (nc_def_var(ncid, "depth", NC_DOUBLE, NDIMS, dimids, &varid)) ERR;
/* Check the lengths of these dimensions. */
if (nc_inq_dimlen(ncid, dimids[0], &time_len)) ERR;
if (time_len) ERR;
if (nc_inq_dimlen(ncid, dimids[1], &beam_len)) ERR;
if (beam_len) ERR;
/* Write some data, check dim lengths. */
if (nc_put_vara_double(ncid, varid, start, count, value)) ERR;
if (nc_inq_dimlen(ncid, dimids[0], &time_len)) ERR;
if (time_len != 1) ERR;
if (nc_inq_dimlen(ncid, dimids[1], &beam_len)) ERR;
if (beam_len != MAX_VALUES) ERR;
/* Add some more data, check dim lengths again. */
start[0] = 1;
if (nc_put_vara_double(ncid, varid, start, count, value)) ERR;
if (nc_inq_dimlen(ncid, dimids[0], &time_len)) ERR;
if (time_len != 2) ERR;
if (nc_inq_dimlen(ncid, dimids[1], &beam_len)) ERR;
if (beam_len != MAX_VALUES) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking multiple unlimited dimensions with more complex 2D test...");
{
#define MAX(x,y) ((x)>(y)?(x):(y))
int dimids[2];
int varid, ncid, timeDimID, beamDimID;
int i, j;
int value[2000];
size_t time_recs, beam_recs; /* count of records in each dimension */
size_t time_len, beam_len; /* actual dimension lengths in each dimension */
size_t start[] = {0, 0};
size_t count[] = {1, 1};
for (i = 0; i < 2000; i++)
value[i] = 2000 - i;
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
/* Define 2 unlimited dimensions */
if (nc_def_dim(ncid, "time", NC_UNLIMITED, &timeDimID)) ERR;
if (nc_def_dim(ncid, "beam", NC_UNLIMITED, &beamDimID)) ERR;
dimids[0] = timeDimID;
dimids[1] = beamDimID;
if (nc_def_var(ncid, "depth", NC_DOUBLE, 2, dimids, &varid)) ERR;
if (nc_enddef(ncid)) ERR;
time_recs = 0;
beam_recs = 0;
for (j = 0; j < 100; j++)
{
if (j > 500)
count[1] = j;
else
count[1] = 1000-j;
if (nc_put_vara_int(ncid, varid, start, count, value)) ERR;
time_recs = MAX(time_recs, start[0] + count[0]);
beam_recs = MAX(beam_recs, start[1] + count[1]);
if (nc_inq_dimlen(ncid, timeDimID, &time_len)) ERR;
if (time_len != time_recs) ERR;
if (nc_inq_dimlen(ncid, beamDimID, &beam_len)) ERR;
if (beam_len != beam_recs) ERR;
start[0]++;
}
if (nc_close(ncid)) ERR;
/* Check the file. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("*** Checking 2D coordinate variable some more...");
{
#define TIME "time"
#define LEN "len"
#define NDIMS 2
#define NUM_RECS 15
#define TL 15
int dimids[NDIMS], dimids_in[NDIMS];
int varid, ncid;
nc_type xtype_in, xtype = NC_CHAR;
int ndims_in, nvars_in, natts_in, unlimdimid_in;
char name_in[NC_MAX_NAME + 1];
size_t index[NDIMS], count[NDIMS], len_in;
const char ttext[TL + 1]="20051224.150000";
int i;
/* Create a file with a 2D time coordinate var, with one of the
* dimensions being the length of the time string. */
if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, TIME, NC_UNLIMITED, &dimids[0])) ERR;
if (nc_def_dim(ncid, LEN, TL, &dimids[1])) ERR;
if (nc_def_var(ncid, TIME, xtype, 2, dimids, &varid)) ERR;
/* Check out the file. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, TIME) || xtype_in != xtype || ndims_in != NDIMS ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS; i++)
if (dimids[i] != dimids_in[i]) ERR;
/* Write some time values. */
index[0] = 0;
index[1] = 0;
count[0] = 1;
count[1] = TL; /* Note we are not writing NULL char. */
if (nc_put_vara_text(ncid, varid, index, count, ttext)) ERR;
if (nc_close(ncid)) ERR;
/* Reopen and check the file. */
if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, TIME) || xtype_in != xtype || ndims_in != NDIMS ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS; i++)
if (dimids[i] != dimids_in[i]) ERR;
if (nc_inq_dim(ncid, dimids[0], name_in, &len_in)) ERR;
if (strcmp(name_in, TIME) || len_in != 1) ERR;
if (nc_close(ncid)) ERR;
/* Reopen and write some more time values. */
if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
/* Write some more time values. */
index[1] = 0;
count[0] = 1;
count[1] = TL; /* Note we are not writing NULL char. */
for (index[0] = 1; index[0] < NUM_RECS; index[0]++)
if (nc_put_vara_text(ncid, varid, index, count, ttext)) ERR;
/* Check things again. */
if (nc_inq(ncid, &ndims_in, &nvars_in, &natts_in, &unlimdimid_in)) ERR;
if (ndims_in != 2 || nvars_in != 1 || natts_in != 0 || unlimdimid_in != 0) ERR;
if (nc_inq_var(ncid, 0, name_in, &xtype_in, &ndims_in, dimids_in, &natts_in)) ERR;
if (strcmp(name_in, TIME) || xtype_in != xtype || ndims_in != NDIMS ||
natts_in != 0) ERR;
for (i = 0; i < NDIMS; i++)
if (dimids[i] != dimids_in[i]) ERR;
if (nc_inq_dim(ncid, dimids[0], name_in, &len_in)) ERR;
if (strcmp(name_in, TIME) || len_in != NUM_RECS) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}