NCGEN(1) UNIDATA UTILITIES NCGEN(1)
NAME
ncgen - From a CDL file generate a netCDF-3 file, a netCDF-4 file or a C program
SYNOPSIS
ncgen [-b] [-c] [-f] [-k file format] [-l output language] [-n] [-o netcdf_filename] [-x] input_file
DESCRIPTION
ncgen generates either a netCDF-3 (i.e. classic) binary .nc file, a netCDF-4 (i.e. enhanced) binary .nc file or a file in some source lan-
guage that when executed will construct the corresponding binary .nc file. The input to ncgen is a description of a netCDF file in a small
language known as CDL (network Common Data form Language), described below. If no options are specified in invoking ncgen, it merely
checks the syntax of the input CDL file, producing error messages for any violations of CDL syntax. Other options can be used, for exam-
ple, to create the corresponding netCDF file, or to generate a C program that uses the netCDF C interface to create the netCDF file.
Note that this version of ncgen was originally called ncgen4. The older ncgen program has been renamed to ncgen3.
ncgen may be used with the companion program ncdump to perform some simple operations on netCDF files. For example, to rename a dimension
in a netCDF file, use ncdump to get a CDL version of the netCDF file, edit the CDL file to change the name of the dimensions, and use ncgen
to generate the corresponding netCDF file from the edited CDL file.
OPTIONS
-b Create a (binary) netCDF file. If the -o option is absent, a default file name will be constructed from the netCDF name (specified
after the netcdf keyword in the input) by appending the `.nc' extension. If a file already exists with the specified name, it will
be overwritten.
-c Generate C source code that will create a netCDF file matching the netCDF specification. The C source code is written to standard
output; equivalent to -lc.
-f Generate FORTRAN 77 source code that will create a netCDF file matching the netCDF specification. The source code is written to
standard output; equivalent to -lf77.
-o netcdf_file
Name for the binary netCDF file created. If this option is specified, it implies the "-b" option. (This option is necessary be-
cause netCDF files cannot be written directly to standard output, since standard output is not seekable.)
-k file_format
The -k flag specifies the format of the file to be created and, by inference, the data model accepted by ncgen (i.e. netcdf-3 (clas-
sic) versus netcdf-4). The possible arguments are as follows.
'1', 'classic' => netcdf classic file format, netcdf-3 type model.
'2', '64-bit-offset', '64-bit offset' => netcdf 64 bit classic file format, netcdf-3 type model.
'3', 'hdf5', 'netCDF-4', 'enhanced' => netcdf-4 file format, netcdf-4 type model.
'4', 'hdf5-nc3', 'netCDF-4 classic model', 'enhanced-nc3' => netcdf-4 file format, netcdf-3 type model.
If no -k is specified then it defaults to -k1 (i.e. classic). Note also that -v is accepted to mean the same thing as -k for backward com-
patibility, but -k is preferred, to match the corresponding ncdump option.
-x Don't initialize data with fill values. This can speed up creation of large netCDF files greatly, but later attempts to read un-
written data from the generated file will not be easily detectable.
-l output_language
The -l flag specifies the output language to use when generating source code that will create or define a netCDF file matching the
netCDF specification. The output is written to standard output. The currently supported languages have the following flags.
c|C' => C language output.
f77|fortran77' => FORTRAN 77 language output
; note that currently only the classic model is supported.
j|java' => (experimental) Java language output
; targets the existing Unidata Java interface, which means that only the classic model is supported.
EXAMPLES
Check the syntax of the CDL file `foo.cdl':
ncgen foo.cdl
From the CDL file `foo.cdl', generate an equivalent binary netCDF file named `x.nc':
ncgen -o x.nc foo.cdl
From the CDL file `foo.cdl', generate a C program containing the netCDF function invocations necessary to create an equivalent binary
netCDF file named `x.nc':
ncgen -c -o x.nc foo.cdl
USAGE
CDL Syntax Overview
Below is an example of CDL syntax, describing a netCDF file with several named dimensions (lat, lon, and time), variables (Z, t, p, rh,
lat, lon, time), variable attributes (units, long_name, valid_range, _FillValue), and some data. CDL keywords are in boldface. (This ex-
ample is intended to illustrate the syntax; a real CDL file would have a more complete set of attributes so that the data would be more
completely self-describing.)
netcdf foo { // an example netCDF specification in CDL
types:
ubyte enum enum_t {Clear = 0, Cumulonimbus = 1, Stratus = 2};
opaque(11) opaque_t;
int(*) vlen_t;
dimensions:
lat = 10, lon = 5, time = unlimited ;
variables:
long lat(lat), lon(lon), time(time);
float Z(time,lat,lon), t(time,lat,lon);
double p(time,lat,lon);
long rh(time,lat,lon);
string country(time,lat,lon);
ubyte tag;
// variable attributes
lat:long_name = "latitude";
lat:units = "degrees_north";
lon:long_name = "longitude";
lon:units = "degrees_east";
time:units = "seconds since 1992-1-1 00:00:00";
// typed variable attributes
string Z:units = "geopotential meters";
float Z:valid_range = 0., 5000.;
double p:_FillValue = -9999.;
long rh:_FillValue = -1;
vlen_t :globalatt = {17, 18, 19};
data:
lat = 0, 10, 20, 30, 40, 50, 60, 70, 80, 90;
lon = -140, -118, -96, -84, -52;
group: g {
types:
compound cmpd_t { vlen_t f1; enum_t f2;};
} // group g
group: h {
variables:
/g/cmpd_t compoundvar;
data:
compoundvar = { {3,4,5}, Stratus } ;
} // group h
}
All CDL statements are terminated by a semicolon. Spaces, tabs, and newlines can be used freely for readability. Comments may follow the
characters `//' on any line.
A CDL description consists of five optional parts: types, dimensions, variables, data, beginning with the keyword `types:', `dimensions:',
`variables:', and `data:', respectively. Note several things:(1) the keyword includes the trailing colon, so there must not be any space
before the colon character, and(2) the keywords are required to be lower case.
The variables: section may contain variable declarations and attribute assignments. All sections may contain global attribute assignments.
In addition, after the data: section, the user may define a series of groups (see the example above). Groups themselves can contain types,
dimensions, variables, data, and other (nested) groups.
The netCDF types: section declares the user defined types. These may be constructed using any of the following types: enum, vlen, opaque,
or compound.
A netCDF dimension is used to define the shape of one or more of the multidimensional variables contained in the netCDF file. A netCDF di-
mension has a name and a size. A dimension can have the unlimited size, which means a variable using this dimension can grow to any length
in that dimension.
A variable represents a multidimensional array of values of the same type. A variable has a name, a data type, and a shape described by
its list of dimensions. Each variable may also have associated attributes (see below) as well as data values. The name, data type, and
shape of a variable are specified by its declaration in the variable section of a CDL description. A variable may have the same name as a
dimension; by convention such a variable is one-dimensional and contains coordinates of the dimension it names. Dimensions need not have
corresponding variables.
A netCDF attribute contains information about a netCDF variable or about the whole netCDF dataset. Attributes are used to specify such
properties as units, special values, maximum and minimum valid values, scaling factors, offsets, and parameters. Attribute information is
represented by single values or arrays of values. For example, "units" is an attribute represented by a character array such as "celsius".
An attribute has an associated variable, a name, a data type, a length, and a value. In contrast to variables that are intended for data,
attributes are intended for metadata (data about data). Unlike netCDF-3, attribute types can be any user defined type as well as the usual
built-in types.
In CDL, an attribute is designated by a a type, a variable, a ':', and then an attribute name. The type is optional and if missing, it
will be inferred from the values assigned to the attribute. It is possible to assign global attributes not associated with any variable to
the netCDF as a whole by omitting the variable name in the attribute declaration. Notice that there is a potential ambiguity in a specifi-
cation such as
x : a = ...
In this situation, x could be either a type for a global attribute, or the variable name for an attribute. Since there could both be a type
named x and a variable named x, there is an ambiguity. The rule is that in this situation, x will be interpreted as a type if possible,
and otherwise as a variable.
If not specified, the data type of an attribute in CDL is derived from the type of the value(s) assigned to it. The length of an attribute
is the number of data values assigned to it, or the number of characters in the character string assigned to it. Multiple values are as-
signed to non-character attributes by separating the values with commas. All values assigned to an attribute must be of the same type.
The names for CDL dimensions, variables, attributes, types, and groups may contain any non-control utf-8 character except the forward slash
character (`/'). However, certain characters must escaped if they are used in a name, where the escape character is the backward slash
`'. In particular, if the leading character off the name is a digit (0-9), then it must be preceded by the escape character. In addi-
tion, the characters ` !"#$%&()*,:;<=>?[]^`'{}|~' must be escaped if they occur anywhere in a name.
Note also that the words `variable', `dimension', `data', `group', and `types' are legal CDL names, but be careful that there is a space
between them and any following colon character. This is mostly an issue with attribute declarations. For example, consider this.
netcdf ... {
variables:
int dimensions;
dimensions: attribute=0 ; // this will cause an error
dimensions : attribute=0 ; // this is ok.
}
The optional data: section of a CDL specification is where netCDF variables may be initialized. The syntax of an initialization is simple:
a variable name, an equals sign, and a comma-delimited list of constants (possibly separated by spaces, tabs and newlines) terminated with
a semicolon. For multi-dimensional arrays, the last dimension varies fastest. Thus row-order rather than column order is used for matri-
ces. If fewer values are supplied than are needed to fill a variable, it is extended with a type-dependent `fill value', which can be
overridden by supplying a value for a distinguished variable attribute named `_FillValue'. The types of constants need not match the type
declared for a variable; coercions are done to convert integers to floating point, for example. The constant `_' can be used to designate
the fill value for a variable.
Primitive Data Types
char characters
byte 8-bit data
short 16-bit signed integers
int 32-bit signed integers
long (synonymous with int)
int64 64-bit signed integers
float IEEE single precision floating point (32 bits)
real (synonymous with float)
double IEEE double precision floating point (64 bits)
ubyte unsigned 8-bit data
ushort 16-bit unsigned integers
uint 32-bit unsigned integers
uint64 64-bit unsigned integers
string arbitrary length strings
CDL supports a superset of the primitive data types of C. The names for the primitive data types are reserved words in CDL, so the names
of variables, dimensions, and attributes must not be primitive type names. In declarations, type names may be specified in either upper or
lower case.
Bytes differ from characters in that they are intended to hold a full eight bits of data, and the zero byte has no special significance, as
it does for character data. ncgen converts byte declarations to char declarations in the output C code and to the nonstandard BYTE decla-
ration in output Fortran code.
Shorts can hold values between -32768 and 32767. ncgen converts short declarations to short declarations in the output C code and to the
nonstandard INTEGER*2 declaration in output Fortran code.
Ints can hold values between -2147483648 and 2147483647. ncgen converts int declarations to int declarations in the output C code and to
INTEGER declarations in output Fortran code. long is accepted as a synonym for int in CDL declarations, but is deprecated since there are
now platforms with 64-bit representations for C longs.
Int64 can hold values between -9223372036854775808 and 9223372036854775807. ncgen converts int64 declarations to longlong declarations in
the output C code.
Floats can hold values between about -3.4+38 and 3.4+38. Their external representation is as 32-bit IEEE normalized single-precision
floating point numbers. ncgen converts float declarations to float declarations in the output C code and to REAL declarations in output
Fortran code. real is accepted as a synonym for float in CDL declarations.
Doubles can hold values between about -1.7+308 and 1.7+308. Their external representation is as 64-bit IEEE standard normalized double-
precision floating point numbers. ncgen converts double declarations to double declarations in the output C code and to DOUBLE PRECISION
declarations in output Fortran code.
The unsigned counterparts of the above integer types are mapped to the corresponding unsigned C types. Their ranges are suitably modified
to start at zero.
CDL Constants
Constants assigned to attributes or variables may be of any of the basic netCDF types. The syntax for constants is similar to C syntax,
except that type suffixes must be appended to shorts and floats to distinguish them from longs and doubles.
A byte constant is represented by a single character or multiple character escape sequence enclosed in single quotes. For example,
'a' // ASCII `a'
'