1NCGEN(1) UNIDATA UTILITIES NCGEN(1)
2
3
4
6 ncgen - From a CDL file generate a netCDF file, a C program, or a For‐
7 tran program
8
10 ncgen [-b] [-c] [-f] [-k kind_of_file] [-x] [-n] [-o netcdf_filename]
11 input_file
12
14 ncgen generates either a netCDF file, or C or Fortran source code to
15 create a netCDF file. The input to ncgen is a description of a netCDF
16 file in a small language known as CDL (network Common Data form Lan‐
17 guage), described below. If no options are specified in invoking nc‐
18 gen, it merely checks the syntax of the input CDL file, producing error
19 messages for any violations of CDL syntax. Other options can be used
20 to create the corresponding netCDF file, to generate a C program that
21 uses the netCDF C interface to create the netCDF file, or to generate a
22 Fortran program that uses the netCDF Fortran interface to create the
23 same netCDF file.
24
25 ncgen may be used with the companion program ncdump to perform some
26 simple operations on netCDF files. For example, to rename a dimension
27 in a netCDF file, use ncdump to get a CDL version of the netCDF file,
28 edit the CDL file to change the name of the dimensions, and use ncgen
29 to generate the corresponding netCDF file from the edited CDL file.
30
32 -b Create a (binary) netCDF file. If the -o option is absent, a
33 default file name will be constructed from the netCDF name
34 (specified after the netcdf keyword in the input) by appending
35 the `.nc' extension. If a file already exists with the speci‐
36 fied name, it will be overwritten.
37
38 -c Generate C source code that will create a netCDF file matching
39 the netCDF specification. The C source code is written to stan‐
40 dard output.
41
42 -f Generate Fortran source code that will create a netCDF file
43 matching the netCDF specification. The Fortran source code is
44 written to standard output.
45
46 -o netcdf_file
47 Name for the binary netCDF file created. If this option is
48 specified, it implies the "-b" option. (This option is neces‐
49 sary because netCDF files cannot be written directly to standard
50 output, since standard output is not seekable.)
51
52 -k kind_of_file
53 Using -k2 or -k "64-bit-offset" specifies that generated file
54 (or program) should use version 2 of format that employs 64-bit
55 file offsets. The default is to use version 1 ("classic") for‐
56 mat with 32-bit file offsets, although this limits the size of
57 the netCDF file, variables, and records to the sizes supported
58 by the classic format. (NetCDF-4 will support additional kinds
59 of netCDF files, "hdf5" and "hdf5-nc3".) Note: -v is also ac‐
60 cepted to mean the same thing as -k for backward compatibility,
61 but -k is preferred, to match the corresponding ncdump option.
62
63 -x Don't initialize data with fill values. This can speed up cre‐
64 ation of large netCDF files greatly, but later attempts to read
65 unwritten data from the generated file will not be easily de‐
66 tectable.
67
69 Check the syntax of the CDL file `foo.cdl':
70
71 ncgen foo.cdl
72
73 From the CDL file `foo.cdl', generate an equivalent binary netCDF file
74 named `x.nc':
75
76 ncgen -o x.nc foo.cdl
77
78 From the CDL file `foo.cdl', generate a C program containing the netCDF
79 function invocations necessary to create an equivalent binary netCDF
80 file named `x.nc':
81
82 ncgen -c -o x.nc foo.cdl
83
85 CDL Syntax Summary
86 Below is an example of CDL syntax, describing a netCDF file with sever‐
87 al named dimensions (lat, lon, and time), variables (Z, t, p, rh, lat,
88 lon, time), variable attributes (units, long_name, valid_range, _Fill‐
89 Value), and some data. CDL keywords are in boldface. (This example is
90 intended to illustrate the syntax; a real CDL file would have a more
91 complete set of attributes so that the data would be more completely
92 self-describing.)
93
94 netcdf foo { // an example netCDF specification in CDL
95
96 dimensions:
97 lat = 10, lon = 5, time = unlimited ;
98
99 variables:
100 long lat(lat), lon(lon), time(time);
101 float Z(time,lat,lon), t(time,lat,lon);
102 double p(time,lat,lon);
103 long rh(time,lat,lon);
104
105 // variable attributes
106 lat:long_name = "latitude";
107 lat:units = "degrees_north";
108 lon:long_name = "longitude";
109 lon:units = "degrees_east";
110 time:units = "seconds since 1992-1-1 00:00:00";
111 Z:units = "geopotential meters";
112 Z:valid_range = 0., 5000.;
113 p:_FillValue = -9999.;
114 rh:_FillValue = -1;
115
116 data:
117 lat = 0, 10, 20, 30, 40, 50, 60, 70, 80, 90;
118 lon = -140, -118, -96, -84, -52;
119 }
120
121 All CDL statements are terminated by a semicolon. Spaces, tabs, and
122 newlines can be used freely for readability. Comments may follow the
123 characters `//' on any line.
124
125 A CDL description consists of three optional parts: dimensions, vari‐
126 ables, and data, beginning with the keyword dimensions:, variables:,
127 and data, respectively. The variable part may contain variable decla‐
128 rations and attribute assignments.
129
130 A netCDF dimension is used to define the shape of one or more of the
131 multidimensional variables contained in the netCDF file. A netCDF di‐
132 mension has a name and a size. At most one dimension in a netCDF file
133 can have the unlimited size, which means a variable using this dimen‐
134 sion can grow to any length (like a record number in a file).
135
136 A variable represents a multidimensional array of values of the same
137 type. A variable has a name, a data type, and a shape described by its
138 list of dimensions. Each variable may also have associated attributes
139 (see below) as well as data values. The name, data type, and shape of
140 a variable are specified by its declaration in the variable section of
141 a CDL description. A variable may have the same name as a dimension;
142 by convention such a variable is one-dimensional and contains coordi‐
143 nates of the dimension it names. Dimensions need not have correspond‐
144 ing variables.
145
146 A netCDF attribute contains information about a netCDF variable or
147 about the whole netCDF dataset. Attributes are used to specify such
148 properties as units, special values, maximum and minimum valid values,
149 scaling factors, offsets, and parameters. Attribute information is
150 represented by single values or arrays of values. For example, "units"
151 is an attribute represented by a character array such as "celsius". An
152 attribute has an associated variable, a name, a data type, a length,
153 and a value. In contrast to variables that are intended for data, at‐
154 tributes are intended for metadata (data about data).
155
156 In CDL, an attribute is designated by a variable and attribute name,
157 separated by `:'. It is possible to assign global attributes not asso‐
158 ciated with any variable to the netCDF as a whole by using `:' before
159 the attribute name. The data type of an attribute in CDL is derived
160 from the type of the value assigned to it. The length of an attribute
161 is the number of data values assigned to it, or the number of charac‐
162 ters in the character string assigned to it. Multiple values are as‐
163 signed to non-character attributes by separating the values with com‐
164 mas. All values assigned to an attribute must be of the same type.
165
166 The names for CDL dimensions, variables, and attributes must begin with
167 an alphabetic character or `_', and subsequent characters may be al‐
168 phanumeric or `_' or `-'.
169
170 The optional data section of a CDL specification is where netCDF vari‐
171 ables may be initialized. The syntax of an initialization is simple: a
172 variable name, an equals sign, and a comma-delimited list of constants
173 (possibly separated by spaces, tabs and newlines) terminated with a
174 semicolon. For multi-dimensional arrays, the last dimension varies
175 fastest. Thus row-order rather than column order is used for matrices.
176 If fewer values are supplied than are needed to fill a variable, it is
177 extended with a type-dependent `fill value', which can be overridden by
178 supplying a value for a distinguished variable attribute named `_Fill‐
179 Value'. The types of constants need not match the type declared for a
180 variable; coercions are done to convert integers to floating point, for
181 example. The constant `_' can be used to designate the fill value for
182 a variable.
183
184 Primitive Data Types
185 char characters
186 byte 8-bit data
187 short 16-bit signed integers
188 long 32-bit signed integers
189 int (synonymous with long)
190 float IEEE single precision floating point (32 bits)
191 real (synonymous with float)
192 double IEEE double precision floating point (64 bits)
193
194 Except for the added data-type byte and the lack of unsigned, CDL sup‐
195 ports the same primitive data types as C. The names for the primitive
196 data types are reserved words in CDL, so the names of variables, dimen‐
197 sions, and attributes must not be type names. In declarations, type
198 names may be specified in either upper or lower case.
199
200 Bytes differ from characters in that they are intended to hold a full
201 eight bits of data, and the zero byte has no special significance, as
202 it does for character data. ncgen converts byte declarations to char
203 declarations in the output C code and to the nonstandard BYTE declara‐
204 tion in output Fortran code.
205
206 Shorts can hold values between -32768 and 32767. ncgen converts short
207 declarations to short declarations in the output C code and to the non‐
208 standard INTEGER*2 declaration in output Fortran code.
209
210 Longs can hold values between -2147483648 and 2147483647. ncgen con‐
211 verts long declarations to long declarations in the output C code and
212 to INTEGER declarations in output Fortran code. int and integer are
213 accepted as synonyms for long in CDL declarations. Now that there are
214 platforms with 64-bit representations for C longs, it may be better to
215 use the int synonym to avoid confusion.
216
217 Floats can hold values between about -3.4+38 and 3.4+38. Their exter‐
218 nal representation is as 32-bit IEEE normalized single-precision float‐
219 ing point numbers. ncgen converts float declarations to float declara‐
220 tions in the output C code and to REAL declarations in output Fortran
221 code. real is accepted as a synonym for float in CDL declarations.
222
223 Doubles can hold values between about -1.7+308 and 1.7+308. Their ex‐
224 ternal representation is as 64-bit IEEE standard normalized double-pre‐
225 cision floating point numbers. ncgen converts double declarations to
226 double declarations in the output C code and to DOUBLE PRECISION decla‐
227 rations in output Fortran code.
228
229 CDL Constants
230 Constants assigned to attributes or variables may be of any of the ba‐
231 sic netCDF types. The syntax for constants is similar to C syntax, ex‐
232 cept that type suffixes must be appended to shorts and floats to dis‐
233 tinguish them from longs and doubles.
234
235 A byte constant is represented by a single character or multiple char‐
236 acter escape sequence enclosed in single quotes. For example,
237 'a' // ASCII `a'
238 '\0' // a zero byte
239 '\n' // ASCII newline character
240 '\33' // ASCII escape character (33 octal)
241 '\x2b' // ASCII plus (2b hex)
242 '\377' // 377 octal = 255 decimal, non-ASCII
243
244 Character constants are enclosed in double quotes. A character array
245 may be represented as a string enclosed in double quotes. The usual C
246 string escape conventions are honored. For example
247 "a" // ASCII `a'
248 "Two\nlines\n" // a 10-character string with two embedded newlines
249 "a bell:\007" // a string containing an ASCII bell
250 Note that the netCDF character array "a" would fit in a one-element
251 variable, since no terminating NULL character is assumed. However, a
252 zero byte in a character array is interpreted as the end of the signif‐
253 icant characters by the ncdump program, following the C convention.
254 Therefore, a NULL byte should not be embedded in a character string un‐
255 less at the end: use the byte data type instead for byte arrays that
256 contain the zero byte. NetCDF and CDL have no string type, but only
257 fixed-length character arrays, which may be multi-dimensional.
258
259 short integer constants are intended for representing 16-bit signed
260 quantities. The form of a short constant is an integer constant with
261 an `s' or `S' appended. If a short constant begins with `0', it is in‐
262 terpreted as octal, except that if it begins with `0x', it is inter‐
263 preted as a hexadecimal constant. For example:
264 -2s // a short -2
265 0123s // octal
266 0x7ffs //hexadecimal
267
268 Long integer constants are intended for representing 32-bit signed
269 quantities. The form of a long constant is an ordinary integer con‐
270 stant, although it is acceptable to append an optional `l' or `L'. If
271 a long constant begins with `0', it is interpreted as octal, except
272 that if it begins with `0x', it is interpreted as a hexadecimal con‐
273 stant. Examples of valid long constants include:
274 -2
275 1234567890L
276 0123 // octal
277 0x7ff // hexadecimal
278
279 Floating point constants of type float are appropriate for representing
280 floating point data with about seven significant digits of precision.
281 The form of a float constant is the same as a C floating point constant
282 with an `f' or `F' appended. For example the following are all accept‐
283 able float constants:
284 -2.0f
285 3.14159265358979f // will be truncated to less precision
286 1.f
287
288 Floating point constants of type double are appropriate for represent‐
289 ing floating point data with about sixteen significant digits of preci‐
290 sion. The form of a double constant is the same as a C floating point
291 constant. An optional `d' or `D' may be appended. For example the
292 following are all acceptable double constants:
293 -2.0
294 3.141592653589793
295 1.0e-20
296 1.d
297
298
300 The programs generated by ncgen when using the -c or -f use initializa‐
301 tion statements to store data in variables, and will fail to produce
302 compilable programs if you try to use them for large datasets, since
303 the resulting statements may exceed the line length or number of con‐
304 tinuation statements permitted by the compiler.
305
306 The CDL syntax makes it easy to assign what looks like an array of
307 variable-length strings to a netCDF variable, but the strings will sim‐
308 ply be concatenated into a single array of characters, since netCDF
309 cannot represent an array of variable-length strings in one netCDF
310 variable.
311
312 NetCDF and CDL do not yet support a type corresponding to a 64-bit in‐
313 teger.
314
315
316
317Printed: 119-6-22 $Date: 2006/08/29 15:41:59 $ NCGEN(1)