[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
NetCDF User's Guide for FORTRAN
One of the primary reasons for using the netCDF interface for applications that deal with arrays is to take advantage of higher-level netCDF utilities and generic applications for netCDF data. Currently two netCDF utilities are available as part of the netCDF software distribution:
ncdump
reads a netCDF dataset and prints a textual representation of the information in the dataset
ncgen
reads a textual representation of a netCDF dataset and generates the corresponding binary netCDF file or a C or FORTRAN program to create the netCDF dataset
Two more general-purpose netCDF utilities are available as part of the FAN (File Array Notation) package:
ncmeta
prints selected metadata from one or more netCDF datasets
ncrob
performs various operations (copy, sum, mean, max, min, ...) with data read from and printed or written to text files and/or selected parts of netCDF variables or attributes.
For more information on FAN, see http://www.unidata.ucar.edu/packages/netcdf/fan_utils.html
.
Users have contributed other netCDF utilities, and various visualization and analysis packages are available that access netCDF data. For an up-to-date list of freely-available and commercial software that can access or manipulate netCDF data, see the NetCDF Software list, http://www.unidata.ucar.edu/packages/netcdf/software.html
.
This chapter describes the ncgen
and ncdump
utilities. These two tools convert between binary netCDF datasets and a text representation of netCDF datasets. The output of ncdump
and the input to ncgen
is a text description of a netCDF dataset in a tiny language known as CDL (network Common data form Description Language).
Below is an example of CDL, describing a netCDF dataset with several named dimensions (lat
, lon
, time
), variables (z
, t
, p
, rh
, lat
, lon
, time
), variable attributes (units
, _FillValue
, valid_range
), and some data.
netcdf foo { // example netCDF specification in CDL dimensions: lat = 10, lon = 5, time = unlimited; variables: int lat(lat), lon(lon), time(time); float z(time,lat,lon), t(time,lat,lon); double p(time,lat,lon); int rh(time,lat,lon); lat:units = "degrees_north"; lon:units = "degrees_east"; time:units = "seconds"; z:units = "meters"; z:valid_range = 0., 5000.; p:_FillValue = -9999.; rh:_FillValue = -1; data: lat = 0, 10, 20, 30, 40, 50, 60, 70, 80, 90; lon = -140, -118, -96, -84, -52; }
All CDL statements are terminated by a semicolon. Spaces, tabs, and newlines can be used freely for readability. Comments may follow the double slash characters //
on any line.
A CDL description consists of three optional parts: dimensions, variables, and data. The variable part may contain variable declarations and attribute assignments.
A dimension is used to define the shape of one or more of the multidimensional variables described by the CDL description. A dimension has a name and a length. At most one dimension in a CDL description can have the unlimited length, which means a variable using this dimension can grow to any length (like a record number in a file).
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 contains coordinates of the dimension it names.
An attribute contains information about a variable or about the whole netCDF dataset. Attributes may be used to specify such properties as units, special values, maximum and minimum valid values, and packing 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 ancillary data (data about data).
In CDL, an attribute is designated by a variable and attribute name, separated by a colon (':
'). It is possible to assign global attributes to the netCDF dataset as a whole by omitting the variable name and beginning the attribute name with a colon (':
'). The data type of an attribute in CDL is derived from the type of the value assigned to it. The length of an attribute is the number of data values 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.
CDL names for variables, attributes, and dimensions may be any combination of alphabetic or numeric characters as well as '_
' and '-
' characters, but names beginning with '_
' are reserved for use by the library. Case is significant in CDL names. The netCDF library does not enforce any restrictions on netCDF names, so it is possible (though unwise) to define variables with names that are not valid CDL names. The names for the primitive data types are reserved words in CDL, so the names of variables, dimensions, and attributes must not be type names.
The optional data section of a CDL description is where netCDF variables may be initialized. The syntax of an initialization is simple:
variable = value_1, value_2, ...;
The comma-delimited list of constants may be separated by spaces, tabs, and newlines. For multidimensional arrays, the last dimension varies fastest. Thus, row-order rather than column order is used for matrices. If fewer values are supplied than are needed to fill a variable, it is extended with the fill value. The types of constants need not match the type declared for a variable; coercions are done to convert integers to floating point, for example. All meaningful type conversions are supported.
A special notation for fill values is supported: the _
character designates a fill value for variables.
The CDL data types are:
| Characters. |
| Eight-bit integers. |
| 16-bit signed integers. |
| 32-bit signed integers. |
| (Deprecated, currently synonymous with int) |
| IEEE single-precision floating point (32 bits). |
| (Synonymous with float). |
| IEEE double-precision floating point (64 bits). |
Except for the added data-type byte
and the lack of the type qualifier unsigned
, CDL supports the same primitive data types as C. In declarations, type names may be specified in either upper or lower case.
The byte
type differs from the char
type in that it is intended for eight-bit data, and the zero byte has no special significance, as it may for character data. The ncgen
utility converts byte
declarations to char
declarations in the output C code and to BYTE
, INTEGER*1
, or similar platform-specific declaration in output FORTRAN code.
The short
type holds values between -32768 and 32767. The ncgen
utility converts short
declarations to short
declarations in the output C code and to INTEGER*2
declaration in output FORTRAN code.
The int
type can hold values between -2147483648 and 2147483647. The ncgen
utility converts int
declarations to int
declarations in the output C code and to INTEGER
declarations in output FORTRAN code. In CDL declarations integer
and long
are accepted as synonyms for int
.
The float
type can hold values between about -3.4+38 and 3.4+38, with external representation as 32-bit IEEE normalized single-precision floating-point numbers. The ncgen
utility converts float
declarations to float
declarations in the output C code and to REAL
declarations in output FORTRAN code. In CDL declarations real
is accepted as a synonym for float
.
The double
type can hold values between about -1.7+308 and 1.7+308, with external representation as 64-bit IEEE standard normalized double-precision, floating-point numbers. The ncgen
utility converts double
declarations to double
declarations in the output C code and to DOUBLE PRECISION
declarations in output FORTRAN code.
This section describes the CDL notation for constants.
Attributes are initialized in the variables
section of a CDL description by providing a list of constants that determines the attribute's type and length. (In the C and FORTRAN procedural interfaces to the netCDF library, the type and length of an attribute must be explicitly provided when it is defined.) CDL defines a syntax for constant values that permits distinguishing among different netCDF types. The syntax for CDL constants is similar to C syntax, except that type suffixes are appended to short
s and float
s to distinguish them from int
s and double
s.
A byte constant is represented by a single character or multiple character escape sequence enclosed in single quotes. For example:
'a' // ASCII a '\0' // a zero byte '\n' // ASCII newline character '\33' // ASCII escape character (33 octal) '\x2b' // ASCII plus (2b hex) '\376' // 377 octal = -127 (or 254) decimal
Character constants are enclosed in double quotes. A character array may be represented as a string enclosed in double quotes. Multiple strings are concatenated into a single array of characters, permitting long character arrays to appear on multiple lines. To support multiple variable-length string values, a conventional delimiter such as '
,
' may be used, but interpretation of any such convention for a string delimiter must be implemented in software above the netCDF library layer. The usual escape conventions for C strings are honored. For example:
"a" // ASCII 'a' "Two\nlines\n" // a 10-character string with two embedded newlines "a bell:\007" // a string containing an ASCII bell "ab","cde" // the same as "abcde"
The form of a short
constant is an integer constant with an 's
' or 'S
' appended. If a short
constant begins with '0
', it is interpreted as octal. When it begins with '0x
', it is interpreted as a hexadecimal constant. For example:
2s // a short 2 0123s // octal 0x7ffs // hexadecimal
The form of an int
constant is an ordinary integer constant. If an int
constant begins with '0
', it is interpreted as octal. When it begins with '0x', it is interpreted as a hexadecimal constant. Examples of valid int
constants include:
-2 0123 // octal 0x7ff // hexadecimal 1234567890L // deprecated, uses old long suffix
The float
type is appropriate for representing data with about seven significant digits of precision. The form of a float
constant is the same as a C floating-point constant with an 'f
' or 'F
' appended. A decimal point is required in a CDL float
to distinguish it from an integer. For example, the following are all acceptable float
constants:
-2.0f 3.14159265358979f // will be truncated to less precision 1.f .1f
The double
type is appropriate for representing floating-point data with about 16 significant digits of precision. The form of a double
constant is the same as a C floating-point constant. An optional 'd
' or 'D
' may be appended. A decimal point is required in a CDL double
to distinguish it from an integer
. For example, the following are all acceptable double constants:
-2.0 3.141592653589793 1.0e-20 1.d
ncgen
The ncgen
tool generates a netCDF file or a C or FORTRAN program that creates a netCDF dataset. If no options are specified in invoking ncgen
, the program merely checks the syntax of the CDL input, producing error messages for any violations of CDL syntax.
UNIX syntax for invoking ncgen
:
ncgen [-b] [-o netcdf-file] [-c] [-f] [-n] [input-file]
where:
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 bar.nc
:
ncgen -o bar.nc foo.cdl
From the CDL file foo.cdl
, generate a C program containing netCDF function invocations that will create an equivalent binary netCDF dataset:
ncgen -c foo.cdl > foo.c
ncdump
The ncdump
tool generates the CDL text representation of a netCDF dataset on standard output, optionally excluding some or all of the variable data in the output. The output from ncdump
is intended to be acceptable as input to ncgen
. Thus ncdump
and ncgen
can be used as inverses to transform data representation between binary and text representations.
ncdump
may also be used as a simple browser for netCDF datasets, to display the dimension names and lengths; variable names, types, and shapes; attribute names and values; and optionally, the values of data for all variables or selected variables in a netCDF dataset.
ncdump
defines a default format used for each type of netCDF variable data, but this can be overridden if a C_format
attribute is defined for a netCDF variable. In this case, ncdump
will use the C_format
attribute to format values for that variable. For example, if floating-point data for the netCDF variable Z
is known to be accurate to only three significant digits, it might be appropriate to use this variable attribute:
Z:C_format = "%.3g"
ncdump
uses '_
' to represent data values that are equal to the _FillValue
attribute for a variable, intended to represent data that has not yet been written. If a variable has no _FillValue
attribute, the default fill value for the variable type is used unless the variable is of byte type.
UNIX syntax for invoking ncdump
:
ncdump [ -c | -h] [-v var1,...] [-b lang] [-f lang] [-l len] [ -p fdig[,ddig]] [ -n name] [input-file]
where:
-c | Show the values of coordinate variables (variables that are also dimensions) as well as the declarations of all dimensions, variables, and attribute values. Data values of non-coordinate variables are not included in the output. This is often the most suitable option to use for a brief look at the structure and contents of a netCDF dataset. |
| Show only the header information in the output, that is, output only the declarations for the netCDF dimensions, variables, and attributes of the input file, but no data values for any variables. The output is identical to using the ' |
| The output will include data values for the specified variables, in addition to the declarations of all dimensions, variables, and attributes. One or more variables must be specified by name in the comma-delimited list following this option. The list must be a single argument to the command, hence cannot contain blanks or other white space characters. The named variables must be valid netCDF variables in the input-file. The default, without this option and in the absence of the ' |
| A brief annotation in the form of a CDL comment (text beginning with the characters ' |
| Full annotations in the form of trailing CDL comments (text beginning with the characters ' |
| Changes the default maximum line length (80) used in formatting lists of non-character data values. |
| |
| Specifies default precision (number of significant digits) to use in displaying floating-point or double precision data values for attributes and variables. If specified, this value overrides the value of the |
| CDL requires a name for a netCDF dataset, for use by ' |
Look at the structure of the data in the netCDF dataset foo.nc
:
ncdump -c foo.nc
Produce an annotated CDL version of the structure and data in the netCDF dataset foo.nc
, using C-style indexing for the annotations:
ncdump -b c foo.nc > foo.cdl
Output data for only the variables uwind
and vwind
from the netCDF dataset foo.nc
, and show the floating-point data with only three significant digits of precision:
ncdump -v uwind,vwind -p 3 foo.nc
Produce a fully-annotated (one data value per line) listing of the data for the variable omega
, using FORTRAN conventions for indices, and changing the netCDF dataset name in the resulting CDL file to omega
:
ncdump -v omega -f fortran -n omega foo.nc > Z.cdl
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]