File: bigmeta.c

package info (click to toggle)
netcdf 1%3A4.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 104,952 kB
  • sloc: ansic: 228,683; sh: 10,980; yacc: 2,561; makefile: 1,319; lex: 1,173; xml: 173; awk: 2
file content (275 lines) | stat: -rw-r--r-- 6,296 bytes parent folder | download | duplicates (7)
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
/*! \file

Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
2015, 2016, 2017, 2018
University Corporation for Atmospheric Research/Unidata.

See \ref copyright file for more info.

*/

/*
Create a netcdf-4 file with horrendously large metadata.
*/

#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <netcdf.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#undef DEBUG

/*
Total number of groups is NGROUPS^TREEDEPTH
TREEDEPTH =  Depth of the group tree
NGROUPS =  Number subgroups per group
NGROUPATTRS =  Number group attributes per group
NDIMS =  Number dimensions per group
NTYPES =  Number user types per group
NVARS =  Number of variables per group
VARRANK =  Rank of variables: must be <= NDIMS
NVARATTRS =  Number attributes per variable
*/

/* Define the Defaults */
#if 1
#define TREEDEPTH 6
#define NGROUPS 2
#define NGROUPATTRS 100
#define NDIMS 100
#define NTYPES 10
#define NVARS 100
#define VARRANK 2
#define NVARATTRS 500
#endif

/* Alternate Defaults for testing*/
#if 0
#define TREEDEPTH 2
#define NGROUPS 2
#define NGROUPATTRS 2
#define NDIMS 2
#define NTYPES 2
#define NVARS 2
#define VARRANK 2
#define NVARATTRS NGROUPATTRS
#endif

#define FILE "bigmeta.nc"

#define CHECK(expr) assert((expr) == NC_NOERR)

static int treedepth = TREEDEPTH;
static int ngroups = NGROUPS;
static int ngroupattrs = NGROUPATTRS;
static int ndims = NDIMS;
static int ntypes = NTYPES;
static int nvars = NVARS;
static int varrank = VARRANK;
static int nvarattrs = NVARATTRS;

/* Define the getopt tags */
#define OPT_UNKNOWN 0
#define OPT_TREEDEPTH 1
#define OPT_NGROUPS 2
#define OPT_NGROUPATTRS 3
#define OPT_NDIMS 4
#define OPT_NTYPES 5
#define OPT_NVARS 6
#define OPT_VARRANK 7
#define OPT_NVARATTRS 8

static struct option options[] = {
{"treedepth", 1, NULL, OPT_TREEDEPTH},
{"ngroups", 1, NULL, OPT_NGROUPS},
{"ngroupattrs", 1, NULL, OPT_NGROUPATTRS},
{"ndims", 1, NULL, OPT_NDIMS},
{"ntypes", 1, NULL, OPT_NTYPES},
{"nvars", 1, NULL, OPT_NVARS},
{"varrank", 1, NULL, OPT_VARRANK},
{"nvarattrs", 1, NULL, OPT_NVARATTRS},
{NULL, 0, NULL, 0}
};

/**************************************************/

static void
reportparameters(void)
{
    fprintf(stderr,"--treedepth=%d\n",treedepth);
    fprintf(stderr,"--ngroups=%d\n",ngroups);
    fprintf(stderr,"--ngroupattrs=%d\n",ngroupattrs);
    fprintf(stderr,"--ndims=%d\n",ndims);
    fprintf(stderr,"--ntypes=%d\n",ntypes);
    fprintf(stderr,"--nvars=%d\n",nvars);
    fprintf(stderr,"--varrank=%d\n",varrank);
    fprintf(stderr,"--nvarattrs=%d\n",nvarattrs);
    fflush(stderr);
}

/* Build a compound type with two fields */
static void
buildcmpdtype(int grpid, int typindex)
{
    char name[NC_MAX_NAME+1];
    int typid;
    snprintf(name,NC_MAX_NAME,"cmpd%d",typindex);
    CHECK(nc_def_compound(grpid, 2*sizeof(int), name, &typid));
    CHECK(nc_insert_compound(grpid, typid, "f1", 0, NC_INT));
    CHECK(nc_insert_compound(grpid, typid, "f2", sizeof(int), NC_INT));
}

/* Build an enum type with two members */
static void
buildenumtype(int grpid, int typindex)
{
    char name[NC_MAX_NAME+1];
    int typid;
    int val0 = 17;
    int val1 = 37;
    snprintf(name,NC_MAX_NAME,"enum%d",typindex);
    CHECK(nc_def_enum(grpid, NC_INT, name, &typid));
    CHECK(nc_insert_enum(grpid, typid, "m0", &val0));
    CHECK(nc_insert_enum(grpid, typid, "m1", &val1));
}

/* Build attributes for either group or var */
static void
buildatts(int grpid, int varid)
{
    char name[NC_MAX_NAME+1];
    int i, count;

    count = (varid == NC_GLOBAL? ngroupattrs : nvarattrs);

    for(i=0;i<count;i++) {
        snprintf(name,NC_MAX_NAME,"a%d",i);
        CHECK(nc_put_att_int(grpid, varid, name, NC_INT, 1, &i));
    }
}

static void
buildgroup(int parent, int grpindex, int depth)
{
    char name[NC_MAX_NAME+1];
    int i, grpid, varid;
    int dimids[NDIMS];

    if(depth == 0) return;

    snprintf(name,NC_MAX_NAME,"g%d",grpindex);
    CHECK(nc_def_grp(parent, name, &grpid));

    /* Add dimensions and capture ids */
    for(i=0;i<ndims;i++) {
	snprintf(name,NC_MAX_NAME,"d%d",i);
        CHECK(nc_def_dim(grpid, name, i, &dimids[i]));
    }

    /* Add types: even index => compound, odd => enum */
    for(i=0;i<ntypes;i++) {
	if((i % 2) == 0)
	    buildcmpdtype(grpid,i);
	else
	    buildenumtype(grpid,i);
    }

    /* Add variables (plus their attributes */
    for(i=0;i<nvars;i++) {
	snprintf(name,NC_MAX_NAME,"v%d",i);
        CHECK(nc_def_var(grpid, name, NC_INT, VARRANK, dimids, &varid));
	/* Make variable be nofill */
	CHECK(nc_def_var_fill(grpid,varid,1,NULL));
	buildatts(grpid,varid);
    }

    /* Add group attributes */
    buildatts(grpid,NC_GLOBAL);

    /* Build subgroups */
    for(i=0;i<ngroups;i++) {
	buildgroup(grpid,i,depth-1);
    }

}

int
main(int argc, char **argv)
{
    int i, ncid;
    time_t starttime, endtime;
    long long delta;
    int tag;

    if(argc > 1) {
	while ((tag = getopt_long_only(argc, argv, "", options, NULL)) >= 0) {
#ifdef DEBUG
fprintf(stderr,"arg=%s value=%s\n",argv[optind-1],optarg);
#endif
	    switch (tag) {
	    case OPT_TREEDEPTH:
		treedepth = atoi(optarg);
		break;
	    case OPT_NGROUPS:
		ngroups = atoi(optarg);
		break;
	    case OPT_NGROUPATTRS:
		ngroupattrs = atoi(optarg);
		break;
	    case OPT_NDIMS:
		ndims = atoi(optarg);
		break;
	    case OPT_NTYPES:
		ntypes = atoi(optarg);
		break;
	    case OPT_NVARS:
		nvars = atoi(optarg);
		break;
	    case OPT_VARRANK:
		varrank = atoi(optarg);
		break;
	    case OPT_NVARATTRS:
		nvarattrs = atoi(optarg);
		break;
	    case ':':
		fprintf(stderr,"missing argument\n");
		exit(1);
	    case '?':
	    default:
		fprintf(stderr,"unknown option\n");
		exit(1);
	    }
	}
    }

    reportparameters();

    starttime = 0;
    endtime = 0;

    time(&starttime);

    CHECK(nc_create(FILE, NC_NETCDF4, &ncid));
    /* Build subgroups */
    for(i=0;i<NGROUPS;i++) {
	buildgroup(ncid,i,TREEDEPTH - 1);
    }

    CHECK(nc_close(ncid));

    time(&endtime);

    /* Compute the delta 1 second resolution is fine for this */
    delta = (long long)(endtime - starttime);
    printf("create delta=%lld\n",delta);
    return 0;

    return 0;
}