NCGEN(1)
NCGEN(1) β UNIDATA UTILITIES π
β¨ NAME β¨
ncgen β From a CDL file generate a netCDF-3 file, a netCDF-4 file or a C program.
β¨ SYNOPSIS β¨
ncgen [-format_code] [-1|3|4|5|6|7] [-b] [-c] [-d] [-D debuglevel] [-f]
[-h] [-H] [-k format_name] [-l b|c|f77|java] [-L loglevel] [-M
name] [-n] [-N datasetname] [-o netcdf_filename] [-P] [-x]
β¨ DESCRIPTION β¨
Okay bestie, so 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 language 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. Input is read from standard input if no input_file is specified. 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 example, 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 β¨
-1|3|4|5|6|7β Alternate method to specify the format.3=> netcdf classic format4=> netCDF-4 format (enhanced data model)5=> netcdf 5 format6=> netCDF 64-bit format7=> netCDF-4 classic model format (3+4 == 7) See the-kflag.
-bβ Create a (binary) netCDF file. If the-ooption is absent, a default file name will be constructed from the basename of the CDL file, with any suffix replaced by the.ncextension. 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.-dβ Same as-D1.-D debuglevelβ Set the level of debug output.-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.-hβ Output help information.-Hβ Output the header only; ignore the data section.-k format_name/-format_codeβ The-kflag specifies the format of the file to be created and, by inference, the data model accepted byncgen(i.e. netcdf-3 (classic) versus netcdf-4 vs netcdf-5). As a shortcut, a numericformat_codemay be specified instead. The possibleformat_namevalues for the-koption are:'classic'or'nc3'=> netCDF classic format'64-bit offset'or'nc6'=> netCDF 64-bit format'64-bit data or 'nc5'=> netCDF-5 (64-bit data) format'netCDF-4' 0r 'nc4'=> netCDF-4 format (enhanced data model)'netCDF-4 classic model'or'nc7'=> netCDF-4 classic model format
Accepted format_code numeric arguments, just shortcuts for format_names, are:
3=> netcdf classic format5=> netcdf 5 format6=> netCDF 64-bit format4=> netCDF-4 format (enhanced data model)7=> netCDF-4 classic model format
The numeric code "7" is used because "7=3+4", a mnemonic for the format that uses the netCDF-3 data model for compatibility with the netCDF-4 storage format for performance. Credit is due to NCO for use of these numeric codes instead of the old and confusing format numbers.
Note: The old version format numbers '1', '2', '3', '4', equivalent to the format names 'nc3', 'nc6', 'nc4', or 'nc7' respectively, are also still accepted but deprecated, due to easy confusion between format numbers and format names. Various old format name aliases are also accepted but deprecated, e.g. 'hdf5', 'enhanced-nc3', etc. Also, note that -v is accepted to mean the same thing as -k for backward compatibility.
-l b|c|f77|javaβ The-lflag 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 are:corCβ C language output using the netCDF C API. Supports the full netCDF-4 enhanced data model, including groups, variable-length types, compound types, enum types, opaque types, and strings.f77orfortran77β FORTRAN 77 language output using the netCDF Fortran interface. Only the classic data model is supported. The output format is forced to classic (equivalent to-k nc3) regardless of any-koption. CDL that uses enhanced model constructs will cause an error. The supported classic primitive types and their Fortran equivalents are:byte=>BYTEshort=>INTEGER*2int/long=>INTEGERfloat/real=>REALdouble=>DOUBLE PRECISIONchar=>CHARACTER
The following netCDF-4 types and constructs are not supported in Fortran output: groups, variable-length (vlen) types, compound types, enum types, opaque types, the string type (NC_STRING), and unsigned integer types (ubyte, ushort, uint, uint64). Note also that the programs generated by ncgen using -lf77 use initialization statements to store data in variables, and may fail to compile for large datasets because the generated statements can exceed the line-length or continuation-line limits of some Fortran compilers.
jorjavaβ (Experimental) Java language output targeting the Unidata Java netCDF interface. Only the classic data model is supported and the output format is forced to classic (equivalent to-k nc3). CDL that uses enhanced model constructs will cause an error. Because this output mode is experimental, the generated code may be incomplete or incorrect in some cases, and the API targeted may differ from current Unidata Java library versions.
π Choosing the output format π
The choice of output format is determined by three flags.
-kflag._Formatattribute (see below).- Occurrence of CDF-5 (64-bit data) or netcdf-4 constructs in the input CDL. The term "netCDF-4 constructs" means constructs from the enhanced data model, not just special performance-related attributes such as
_ChunkSizes,_DeflateLevel,_Endianness, etc. The term "CDF-5 constructs" means extended unsigned integer types allowed in the 64-bit data model.
Note that there is an ambiguity between the netCDF-4 case and the CDF-5 case is only an unsigned type is seen in the input.
The rules are as follows, in order of application.
- If either Fortran or Java output is specified, then
-kflag value of1(classic model) will be used. Conflicts with the use of enhanced constructs in the CDL will report an error. - If both the
-kflag and_Formatattribute are specified, the_Formatflag will be ignored. If no-kflag is specified, and a_Formatattribute value is specified, then the-kflag value will be set to that of the_Formatattribute. Otherwise the-kflag is undefined. - If the
-koption is defined and is consistent with the CDL,ncgenwill output a file in the requested form, else an error will be reported. - If the
-kflag is undefined, and if there are CDF-5 constructs, only, in the CDL, a-kflag value of5(64-bit data model) will be used. If there are true netCDF-4 constructs in the CDL, a-kflag value of3(enhanced model) will be used. - If special performance-related attributes are specified in the CDL, a
-kflag value of4(netCDF-4 classic model) will be used. - Otherwise
ncgenwill set the-kflag to1(classic model).
-L loglevel-M nameβ Specify the name for the main function for C, F77, or Java.-n-N datasetname-o netcdf_fileβ Name of the file to pass to calls to"nc_create()". If this option is specified it implies (in the absence of any explicit-lflag) the"-b"option. This option is necessary because netCDF files cannot be written directly to standard output, since standard output is not seekable.-Pβ UseNC_DISKLESSmode to create the file totally in memory before persisting it to disk.-W maxwholevarsizeβ Setwholevarsizemwhere if total number of elements is less thanmaxwholevarsizethen update a variable using a singlenc_put_var. Requires that the variable has no unlimited dimensions.-xβ Don't initialize data with fill values. This can speed up creation of large netCDF files greatly, but later attempts to read unwritten data from the generated file will not be easily detectable.
β¨ 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 -lc foo.cdl >x.c
π¦ 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 example 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}, enum_t.Stratus } ;
} // group h
}
so that's the rest of the CDL file, bestie, screenshot it, love it, memorize it π
Okay so all CDL statements are terminated by a semicolon. Non-negotiable. Spaces, tabs, and newlines can be used freely for readability β go off, format it pretty, no one's stopping you π. Comments may follow the characters // on any line.
A CDL description is made of five optional parts: types, dimensions, variables, data β and they kick off with the keywords types:, dimensions:, and variables:, and data: respectively. Two things to clock: (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. Lowercase or it's over, periodt. π
The variables: section may contain variable declarations and attribute assignments. All sections may contain global attribute assignments. Every single one, yes really β¨
Also, after the data: section, the user may define a series of groups (peep the example above, it's giving organized π«Ά). Groups themselves can contain types, dimensions, variables, data, and other (nested) groups.
πΈ CDL Types, Dimensions, Variables & Attributes πΈ
The netCDF types: section declares the user defined types. These may be constructed using any of the following types: enum, vlen, opaque, or compound. Build-a-bear but for data types π§
A netCDF dimension is used to define the shape of one or more of the multidimensional variables contained in the netCDF file. A netCDF dimension 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. Unlimited. Like my patience for bad CDL. π
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). It's data about data, it's very meta, it's very cunt π
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 type, a variable, a ':', and then an attribute name. The type is optional, and if it's 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.
π Watch out for a potential ambiguity in a specification such asx : a = ...β in this situation,xcould be either a type for a global attribute, or the variable name for an attribute. Since there could be both a type namedxand a variable namedx, we have a whole situation going on. The rule is: in this situation,xwill 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 assigned to non-character attributes by separating the values with commas. All values assigned to an attribute must be of the same type. No mixing, we don't do that here π
π Names, bestie π
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 be escaped if they are used in a name, where the escape character is the backward slash \.
In particular:
- If the leading character of the name is a digit (
0-9), then it must be preceded by the escape character. - In addition, the following characters must be escaped if they occur anywhere in a name:
` !"#$%&()*,:;<=>?[]^`Β΄{}|~\
π Also: attribute names that begin with an underscore (`_') are reserved for the use of Unidata and should not be used in user defined attributes. Stay in your lane, girl.
Also, heads up: the words variables, dimensions, data, group, and types are legal CDL names, but be careful that there is a space between them and any following colon character when used as a variable name. 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.
...
}
One of those will cause an error and it is the first one, I need you to see that π
π The data: Section (where the values live) π
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. So it's row-order, not column order, for matrices. Row-order supremacy, sorry to the column girlies π
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. Sloppy? A little. Convenient? Iconic. β¨
The constant `_' can be used to designate the fill value for a variable.
If the type of the variable is explicitly string, then the special constant NIL can be used to represent a nil string, which is not the same as a zero length string. Nil string β empty string, say it with me πΈ
β¨ Primitive Data Types β¨
charβ charactersbyteβ 8-bit datashortβ 16-bit signed integersintβ 32-bit signed integerslongβ (synonymous withint)int64β 64-bit signed integersfloatβ IEEE single precision floating point (32 bits)realβ (synonymous withfloat)doubleβ IEEE double precision floating point (64 bits)ubyteβ unsigned 8-bit dataushortβ 16-bit unsigned integersuintβ 32-bit unsigned integersuint64β 64-bit unsigned integersstringβ 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. Do not name your variable int, I'm begging π In declarations, type names may be specified in either upper or lower case. π¦
β The tea on each type β
Bytes are intended to hold a full eight bits of data, and the zero byte has no special significance, the way it may for character data. ncgen converts byte declarations to char declarations in the output C code and to the nonstandard BYTE declaration 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. Deprecated, bestie. Do not start something with it. π
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. Zero-based, as the universe intended π
okay so real talk about the char type bestie: technically it's just an unsigned 8-bit value. What those 256 possible values even mean? Unspecified by default, no cap. But you can tag a char variable with an _Encoding attribute to say which character set you're repping: US-ASCII, ISO-8859-1, etc. π
Heads up tho: saying the encoding is UTF-8 is literally the same as saying US-ASCII, because multi-byte UTF-8 characters cannot be squeezed into an 8-bit character. The only legal single-byte UTF-8 values are by definition the 7-bit US-ASCII encoding with the top bit set to zero. That's it, that's the whole list. π
Strings though? Default assumption is they're UTF-8 encoded. Which means multi-byte UTF-8 encodings can totally be chillin' in there, so the number of distinct UTF-8 characters in a string might be smaller than the number of 8-bit bytes used to store that string. Sneaky little bytes. π¦
β¨ CDL Constants β¨
Constants you assign to attributes or variables can be any of the basic netCDF types. The syntax is vibing like C syntax, with one twist: shorts and floats need type suffixes so we can tell them apart from longs and doubles. Otherwise they'd be twinning and we can't have that. π―
A byte constant is an integer constant with a b (or B) tacked on. In the old netCDF-2 API, byte constants could also be written using single characters or standard C character escape sequences like 'a' or '\0'. Still supported for backward compatibility, but deprecated β it's there to make the distinction clear between the numeric byte type and the textual char type. Some byte constants for you:
0b // a zero byte
-1b // -1 as an 8-bit byte
255b // also -1 as a signed 8-bit byte
short integer constants are for representing 16-bit signed quantities. Form: an integer constant with an s or S appended. If it starts with a 0, it's read as octal β unless it starts with 0x, in which case it's hexadecimal. Obsessed honestly:
-2s // a short -2
0123s // octal
0x7ffs //hexadecimal
int integer constants are for 32-bit signed quantities. The form is just an ordinary integer constant, but you may optionally append a single l or L (again, deprecated). β οΈ Careful tho bestie: the L suffix is interpreted as a 32 bit integer, and never as a 64 bit integer. That's confusing because the C long type can ambiguously be either 32 bit or 64 bit. It's giving chaos, fr fr.
If an int constant starts with 0, it's read as octal β unless it starts with 0x, then it's hexadecimal (but see opaque constants below π). Valid int constants, for your viewing pleasure:
-2
1234567890L
0123 // octal
0x7ff // hexadecimal
int64 integer constants are for 64-bit signed quantities. Form: an integer constant with an ll or LL appended. Starts with 0 β octal, starts with 0x β hexadecimal. Slay:
-2ll // an unsigned -2
0123LL // octal
0x7ffLL //hexadecimal
Floating point constants of type float are for floating point data with about seven significant digits of precision. Form: same as a C floating point constant, but with an f or F appended. All of these are acceptable float constants:
-2.0f
3.14159265358979f // will be truncated to less precision
1.f
Floating point constants of type double are for floating point data with about sixteen significant digits of precision. Form: same as a C floating point constant, and an optional d or D may be appended. All acceptable doubles:
-2.0
3.141592653589793
1.0e-20
1.d
Unsigned integer constants can be created by appending 'U' or 'u' between the constant and any trailing size specifier, or immediately at the end of the size specifier. So you could say 10U, 100su, 100000ul, or 1000000llu, for example. Mix and match, it's your world. π
Single character constants can be wrapped in single quotes. But if you enclose a sequence of one or more characters in double quotes, its interpretation must be inferred from context. If the dataset is created using the netCDF classic model, then all such constants are interpreted as a character array, so each character in the constant is interpreted as if it were a single character. If the dataset is netCDF extended, then the constant may be interpreted as for the classic model or as a true string (see below), depending on the type of the attribute or variable the string is going into. Context is the main character here. π
The deal with char constants: values in the printable ASCII range (' '..'~') are assumed to be encoded as the 1-byte subset of UTF-8, which equals US-ASCII. In all cases, the usual C string escape conventions are honored for values from 0 thru 127. Values greater than 127 are allowed, but their encoding is undefined. And for netCDF extended, using the char type is deprecated in favor of the string type. (Growth. We love to see it. π)
Some character constant examples:
'a' // ASCII `a'
"a" // equivalent to 'a'
"Two\nlines\n" // a 10-character string with two embedded newlines
"a bell:\007" // a string containing an ASCII bell
Note that the netCDF character array "a" would fit in a one-element variable, since no terminating NULL character is assumed. However, a zero byte in a character array is interpreted as the end of the significant characters by the ncdump program, following the C convention. So a NULL byte should not be embedded in a character string unless it's at the end β use the byte data type instead for byte arrays that contain the zero byte. π«
String constants are, like character constants, represented using double quotes. This creates a potential ambiguity since a multi-character string may also indicate a dimensioned character value. Disambiguation usually happens by context, but care should be taken to specify the string type to ensure the proper choice. String constants are assumed to always be UTF-8 encoded, which specifically means the string constant may actually contain multi-byte UTF-8 characters. And the special constant NIL can be used to represent a nil string, which is not the same as a zero length string. They're different girlies, do not confuse them. π
Opaque constants are represented as sequences of hexadecimal digits preceded by 0X or 0x: 0xaa34ffff, for example. These constants can still be used as integer constants and will be either truncated or extended as necessary. Flexible icon. β¨
π§ Compound Constant Expressions π§
So you wanna assign values to variables (or attributes) whose type is user-defined? Say less. The constant notation got extended to include sequences of constants enclosed in curly brackets (e.g. "{"..."}"). A constant like that is called a compound constant, and compound constants can be nested. Inception vibes. π
Given a type T(*) vlen_t, where T is some other arbitrary base type, constants for this should be specified as follows.
vlen_t var[2] = {t11,t12,...t1N}, {t21,t22,...t2m};
The values tij are assumed to be constants of type T.
Given a type compound cmpd_t {T1 f1; T2 f2...Tn fn}, where the Ti are other arbitrary base types, constants for this should be specified as follows.
cmpd_t var[2] = {t11,t12,...t1N}, {t21,t22,...t2n};
The values tij are assumed to be constants of type Ti. If the fields are missing, then they'll be set using any specified or default fill value for the field's base type. No field left behind. π«Ά
The general set of rules for using braces is defined in the Specifying Datalists section below.
π Scoping Rules π
With the addition of groups, the name space for defined objects is no longer flat. References (names) of any type, dimension, or variable may be prefixed with the absolute path specifying a specific declaration. So you might say:
variables:
/g1/g2/t1 v1;
The type being referenced (t1) is the one within group g2, which in turn is nested in group g1. The similarity of this notation to Unix file paths is deliberate, and you can think of groups as a form of directory structure. It's giving filesystem but make it netCDF. π
When a name is not prefixed, then scope rules get applied to locate the specified declaration. Currently there are three rules: one for dimensions, one for types and enumeration constants, and one for all others.
- When an unprefixed name of a dimension is used (as in a variable declaration),
ncgenfirst looks in the immediately enclosing group for the dimension. If it's not there, it looks in the group enclosing this group. And it keeps going up the group hierarchy until the dimension is found, or there are no more groups to search. Climbing the ladder fr. πͺ - When an unprefixed name of a type or an enumeration constant is used,
ncgensearches the group tree using a pre-order depth-first search. Essentially this means it'll find the matching declaration that precedes the reference textually in the cdl file and that is "highest" in the group hierarchy. Efficiency queen. π - For all other names, only the immediately enclosing group is searched. Nowhere else. Stay in your lane bestie.
One final note: forward references are not allowed. π«β¨ This means that specifying, for example, /g1/g2/t1 will fail if this reference occurs before g1 and/or g2 are defined. Define first, reference second, that's the order. Periodt.
πΈ Specifying Enumeration Constants πΈ
References to enumeration constants (in data lists) can be ambiguous, since the same enumeration constant name can be defined in more than one enumeration. If a cdl file specifies an ambiguous constant, then ncgen will signal an error. Rude but fair. π Such constants can be disambiguated in two ways:
- Prefix the enumeration constant with the name of the enumeration separated by a dot:
enum.econst, for example. - If case one isn't sufficient to disambiguate the enumeration constant, then you must specify the precise enumeration type using a group path:
/g1/g2/enum.econst, for example.
π Special Attributes π
Special, virtual, attributes can be specified to provide performance-related information about the file format and about variable properties. The file must be a netCDF-4 file for these to take effect, iykyk.
These special virtual attributes are not actually part of the file β they're merely a convenient way to set miscellaneous properties of the data in CDL. Ghost attributes. They're not even really there. πβ¨
The special attributes we're stanning right now are: _Format, _Fletcher32, _ChunkSizes, _Endianness, _DeflateLevel, _Shuffle, and _Storage. That's the whole roster, bestie π
_Format is a global attribute and it's the one that picks which netCDF format variant you're getting. Its value has to be a single string matching one of classic, 64-bit offset, 64-bit data, netCDF-4, or netCDF-4 classic model. No freestyling, she's picky π
The rest of them are all variable attributes. Basically every single one of them maps to some matching nc_def_var_XXX function, as defined in the netCDF-4 API. And for the ones that are essentially boolean boys (_Fletcher32, _Shuffle, and _NOFILL), you can say "true" by using the strings true or 1, or by using the integer 1. For false you gotta hit them with either false, 0, or the integer 0. π«Ά
So here's what each of these actually does:
_Fletcher32sets thefletcher32property for a variable. Checksum queen β¨_Endiannessis eitherlittleorbig, depending on how the variable is stored when it's first written._DeflateLevelis an integer between 0 and 9 inclusive, if compression has been specified for the variable._Shufflespecifies if the the shuffle filter should be used._Storageiscontiguousorcompactorchunked._ChunkSizesis a list of chunk sizes for each dimension of the variable.
Heads up bestie π¨ attributes like "add_offset" or "scale_factor" have no special meaning to ncgen. Those are currently just conventions, handled above the library layer by other utility packages, NCO for example. So don't come for ncgen about them, she literally does not care π
π Specifying Datalists π
Okay so setting up datalists for variables in the data: section? Lowkey complicated. There are rules, and you must follow them if you want ncgen to parse your datalists correctly. No skipping π
First thing: the top level is automatically assumed to be a list of items, so it should not be inside {...}. Which means if your variable is a scalar, there'll be a single top-level element, and if your variable is an array, there'll be N top-level elements. Cute and simple, right?
For each element of that top-level list, here's what applies:
- Instances of UNLIMITED dimensions (other than the first dimension) must be surrounded by
{...}in order to specify the size. - Compound instances must be embedded in
{...} - Non-scalar fields of compound instances must be embedded in
{...}. - Instances of vlens must be surrounded by
{...}in order to specify the size.
Datalists associated with attributes are implicitly a vector (i.e., a list) of values of the type of the attribute, and the above rules must apply with that in mind.
- No other use of braces is allowed. Periodt.
Note that one consequence of these rules is that arrays of values cannot have subarrays within braces. Consider, for example,int var(d1)(d2)...(dn), where none ofd2...dnare unlimited. A datalist for this variable must be a single list of integers, where the number of integers is no more thanD=d1*d2*...dnvalues. And note that the list can be less than D, in which case fill values will be used to pad the list. π¦
Rule 6 about attribute datalist has the following consequence: if the type of the attribute is a compound (or vlen) type, and if the number of entries in the list is one, then the compound instances must be enclosed in braces.
πΈ Specifying Character Datalists πΈ
Datalists for variables of type char? Also a whole thing, bestie. Let's consider, for example:
dimensions: u=UNLIMITED; d1=1; d2=2; d3=3;
d4=4; d5=5; u2=UNLIMITED;
variables: char var(d4,d5);
datalist: var="1", "two", "three";
So we have twenty elements of var to fill (that's d5 X d4) and we have three strings of length 1, 3, and 5. So... how do the characters in those strings get assigned to the twenty elements? Iconic question, no answer yet π€
This one's challenging because it's desirable to mimic the original ncgen (ncgen3). The core algorithm is notionally as follows.
- Assume we have a set of dimensions
D1..Dn, whereD1may optionally be an Unlimited dimension. It is assumed that the sizes of theDiare all known (including unlimited dimensions). - Given a sequence of string or character constants
C1..Cm, our goal is to construct a single string whose length is the cross product ofD1thruDn. Note that for purposes of this algorithm, character constants are treated as strings of size 1. - Construct
Dx = cross product of D1 thru D(n-1). - For each constant
Ci, add fill characters as needed so that its length is a multiple ofDn. - Concatenate the modified
C1..Cmto produce stringS. - Add fill characters to
Sto make its length be a multiple ofDn. - If
Sis longer than theDx * Dn, then truncate and generate a warning.
There are three other cases of note, and they're giving "special episode" energy β¨
- If there is only a single, unlimited dimension, then all of the constants are concatenated and fill characters are added to the end of the resulting string to make its length be that of the unlimited dimension. If the length is larger than the unlimited dimension, then it is truncated with a warning.
- For the case of character typed vlen, "char(*) vlen_t" for example, we simply concatenate all the constants with no filling at all.
- For the case of a character typed attribute, we simply concatenate all the constants.
In netcdf-4, dimensions other than the first can be unlimited. And of course, by the rules above, the interior unlimited instances must be delimited by {...}. For example:
variables: char var(u,u2);
datalist: var={"1", "two"}, {"three"};
In this case u will have the effective length of two. Within each instance of u2, the rules above will apply, leading to this:
datalist: var={"1","t","w","o"}, {"t","h","r","e","e"};
The effective size of u2 will be the max of the two instance lengths (five in this case) and the shorter will be padded to produce this:
datalist: var={"1","t","w","o","\0"}, {"t","h","r","e","e"};
Now consider an even more complicated case, because of course we have to π
variables: char var(u,u2,u3);
datalist: var={{"1", "two"}}, {{"three"},{"four","xy"}};
In this case u again will have the effective length of two. The u2 dimensions will have a size = max(1,2) = 2. Within each instance of u2, the rules above will apply, leading to this:
datalist: var={{"1","t","w","o"}}, {{"t","h","r","e","e"},{"f","o","u","r","x","y"}};
The effective size of u3 will be the max of the two instance lengths (six in this case) and the shorter ones will be padded to produce this:
datalist: var={{"1","t","w","o"," "," "}}, {{"t","h","r","e","e"," "},{"f","o","u","r","x","y"}};
Note however that the first instance of u2 is less than the max length of u2, so we need to add a filler for another instance of u2, producing this:
datalist: var={{"1","t","w","o"," "," "},{" "," "," "," "," "," "}}, {{"t","h","r","e","e"," "},{"f","o","u","r","x","y"}};
Look at her go, padding and filling like a legend π
π BUGS π
Okay so the programs generated by ncgen when you use the -c flag use initialization statements to store data in variables, and they will fail to produce compilable programs if you try to use them for large datasets. Why? Because the resulting statements may exceed the line length or the number of continuation statements permitted by the compiler. So like... for big data, be aware, it's not her fault, it's the compiler's π
Also, the CDL syntax makes it easy to assign what looks like an array of variable-length strings to a netCDF variable, but the strings may simply be concatenated into a single array of characters. Specific use of the string type specifier may solve the problem. iykyk π
π Identifiers and Keywords π
Under certain conditions, some keywords can be used as identifiers. Yes really, she's flexible like that π
- If a type keyword is not a type supported by the format of the
.cdlfile, then it can be used as an identifier. So, for example, when translating a.cdlfile as a netCDF-3 file, then "string" or "uint64" can be used as identifiers. - The keyword "data" can be used as an identifier because it can be tested in a context sensitive fashion to see if "data" is a keyword versus an identifier.
π CDL Grammar π
okay bestie, so if you really wanna know the tea about what counts as valid CDL, the file ncgen.y is the definitive grammar β like, it's the source of truth, the final boss, the one who ate π
. But because not everyone wants to go read a whole yacc file (valid, honestly), a stripped down version is included right here for completeness so you've got the receipts. π§Ύβ¨
ncdesc: NETCDF
datasetid
rootgroup
;
datasetid: DATASETID
rootgroup: '{'
groupbody
subgrouplist
'}';
groupbody:
attrdecllist
typesection
dimsection
vasection
datasection
;
subgrouplist:
/*empty*/
| subgrouplist namedgroup
;
namedgroup: GROUP ident '{'
groupbody
subgrouplist
'}'
attrdecllist
;
typesection: /* empty */
| TYPES
| TYPES typedecls
;
typedecls:
type_or_attr_decl
| typedecls type_or_attr_decl
;
typename: ident ;
type_or_attr_decl:
typedecl
| attrdecl ';'
;
typedecl:
enumdecl optsemicolon
| compounddecl optsemicolon
| vlendecl optsemicolon
| opaquedecl optsemicolon
;
optsemicolon:
/*empty*/
| ';'
;
enumdecl: primtype ENUM typename ;
enumidlist: enumid
| enumidlist ',' enumid
;
enumid: ident '=' constint ;
opaquedecl: OPAQUE '(' INT_CONST ')' typename ;
vlendecl: typeref '(' '*' ')' typename ;
compounddecl: COMPOUND typename '{' fields '}' ;
fields: field ';'
| fields field ';'
;
field: typeref fieldlist ;
primtype: CHAR_K
| BYTE_K
| SHORT_K
| INT_K
| FLOAT_K
| DOUBLE_K
| UBYTE_K
| USHORT_K
| UINT_K
| INT64_K
| UINT64_K
;
dimsection: /* empty */
| DIMENSIONS
| DIMENSIONS dimdecls
;
dimdecls: dim_or_attr_decl ';'
| dimdecls dim_or_attr_decl ';'
;
dim_or_attr_decl: dimdeclist | attrdecl ;
dimdeclist: dimdecl
| dimdeclist ',' dimdecl
;
dimdecl:
dimd '=' UINT_CONST
| dimd '=' INT_CONST
| dimd '=' DOUBLE_CONST
| dimd '=' NC_UNLIMITED_K
;
dimd: ident ;
vasection: /* empty */
| VARIABLES
| VARIABLES vadecls
;
vadecls: vadecl_or_attr ';'
| vadecls vadecl_or_attr ';'
;
vadecl_or_attr: vardecl | attrdecl ;
vardecl: typeref varlist ;
varlist: varspec
| varlist ',' varspec
;
varspec: ident dimspec ;
dimspec: /* empty */
| '(' dimlist ')'
;
dimlist: dimref
| dimlist ',' dimref
;
dimref: path ;
fieldlist:
fieldspec
| fieldlist ',' fieldspec
;
fieldspec: ident fielddimspec ;
fielddimspec: /* empty */
| '(' fielddimlist ')'
;
fielddimlist:
fielddim
| fielddimlist ',' fielddim
;
fielddim:
UINT_CONST
| INT_CONST
;
/* Use this when referencing defined objects */
varref: type_var_ref ;
typeref: type_var_ref ;
type_var_ref:
path
| primtype
;
/* Use this for all attribute decls */
/* Watch out; this is left recursive */
attrdecllist: /*empty*/ | attrdecl ';' attrdecllist ;
attrdecl:
':' ident '=' datalist
| typeref type_var_ref ':' ident '=' datalist
| type_var_ref ':' ident '=' datalist
| type_var_ref ':' _FILLVALUE '=' datalist
| typeref type_var_ref ':' _FILLVALUE '=' datalist
| type_var_ref ':' _STORAGE '=' conststring
| type_var_ref ':' _CHUNKSIZES '=' intlist
| type_var_ref ':' _FLETCHER32 '=' constbool
| type_var_ref ':' _DEFLATELEVEL '=' constint
| type_var_ref ':' _SHUFFLE '=' constbool
| type_var_ref ':' _ENDIANNESS '=' conststring
| type_var_ref ':' _NOFILL '=' constbool
| ':' _FORMAT '=' conststring
;
path:
ident
| PATH
;
datasection: /* empty */
| DATA
| DATA datadecls
;
datadecls:
datadecl ';'
| datadecls datadecl ';'
;
datadecl: varref '=' datalist ;
datalist:
datalist0
| datalist1
;
datalist0:
/*empty*/
;
/* Must have at least 1 element */
datalist1:
dataitem
| datalist ',' dataitem
;
dataitem:
constdata
| '{' datalist '}'
;
constdata:
simpleconstant
| OPAQUESTRING
| FILLMARKER
| NIL
| econstref
| function
;
econstref: path ;
function: ident '(' arglist ')' ;
arglist:
simpleconstant
| arglist ',' simpleconstant
;
simpleconstant:
CHAR_CONST /* never used apparently*/
| BYTE_CONST
| SHORT_CONST
| INT_CONST
| INT64_CONST
| UBYTE_CONST
| USHORT_CONST
| UINT_CONST
| UINT64_CONST
| FLOAT_CONST
| DOUBLE_CONST
| TERMSTRING
;
intlist:
constint
| intlist ',' constint
;
constint:
INT_CONST
| UINT_CONST
| INT64_CONST
| UINT64_CONST
;
conststring: TERMSTRING ;
constbool:
conststring
| constint
;
/* Push all idents thru here for tracking */
ident: IDENT ;
ngl bestie,datalist1literally says "Must have at least 1 element" in the comments and you will respect that, andCHAR_CONSTis flagged as never used apparently β so like, iconic but unemployed π. Also caught thatattrdecllisthas a "Watch out; this is left recursive" warning, so don't come for me if your parser has opinions. π
and that's a wrap on the whole page, my love! π This was Release 4.10.1, dated 2026-03-11, and yeah β that's the whole dang NCGEN(1) manual, start to finish, no crumbs left. go forth and generate those netCDF files, you absolute legend ππ bye bestieee π¦β¨
π¦ where this page comes from
- arch rolling: netcdf 4.10.1-1 Β· custom
- arch rolling: netcdf-openmpi 4.10.1-1 Β· custom
- nixpkgs unstable: netcdf 4.10.1
- nixpkgs unstable: netcdf-mpi 4.10.1
- ubuntu stonking: netcdf-bin 1:4.10.1-1build1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms