This guide describes the netCDF object model. This document applies to netCDF version 3.6.2, and was last updated on 15 February 2007.
Interface guides are available for C (see The NetCDF C Interface Guide), C++ (see The NetCDF C++ Interface Guide), Fortran 77 (see The NetCDF Fortran 77 Interface Guide), and Fortran 90 (see The NetCDF Fortran 90 Interface Guide).
Separate documentation for the netCDF Java library can be found at the netCDF-Java website, http://www.unidata.ucar.edu/software/netcdf-java.
For installation and porting information, see The NetCDF Installation and Porting Guide.
--- The Detailed Node Listing ---
Introduction
Components of a NetCDF Dataset
Data
Forms of Data Access
File Structure and Performance
NetCDF Utilities
Unidata (http://www.unidata.ucar.edu) is a National Science Foundation-sponsored program empowering U.S. universities, through innovative applications of computers and networks, to make the best use of atmospheric and related data for enhancing education and research. For analyzing and displaying such data, the Unidata Program Center offers universities several supported software packages developed by other organizations. Underlying these is a Unidata-developed system for acquiring and managing data in real time, making practical the Unidata principle that each university should acquire and manage its own data holdings as local requirements dictate. It is significant that the Unidata program has no data center–the management of data is a "distributed" function.
The Network Common Data Form (netCDF) software described in this guide was originally intended to provide a common data access method for the various Unidata applications. These deal with a variety of data types that encompass single-point observations, time series, regularly-spaced grids, and satellite or radar images.
The netCDF software functions as an I/O library, callable from C, FORTRAN, C++, Perl, or other language for which a netCDF library is available. The library stores and retrieves data in self-describing, machine-independent datasets. Each netCDF dataset can contain multidimensional, named variables (with differing types that include integers, reals, characters, bytes, etc.), and each variable may be accompanied by ancillary data, such as units of measure or descriptive text. The interface includes a method for appending data to existing netCDF datasets in prescribed ways, functionality that is not unlike a (fixed length) record structure. However, the netCDF library also allows direct-access storage and retrieval of data by variable name and index and therefore is useful only for disk-resident (or memory-resident) datasets.
NetCDF access has been implemented in about half of Unidata's software, so far, and it is planned that such commonality will extend across all Unidata applications in order to:
A measure of success has been achieved. NetCDF is now in use on computing platforms that range from personal computers to supercomputers and include most UNIX-based workstations. It can be used to create a complex dataset on one computer (say in FORTRAN) and retrieve that same self-describing dataset on another computer (say in C) without intermediate translations–netCDF datasets can be transferred across a network, or they can be accessed remotely using a suitable network file system or remote access protocols.
Because we believe that the use of netCDF access in non-Unidata software will benefit Unidata's primary constituency–such use may result in more options for analyzing and displaying Unidata information–the netCDF library is distributed without licensing or other significant restrictions, and current versions can be obtained via anonymous FTP. Apparently the software has been well received by a wide range of institutions beyond the atmospheric science community, and a substantial number of public domain and commercial data analysis systems can now accept netCDF datasets as input.
Several organizations have adopted netCDF as a data access standard, and there is an effort underway at the National Center for Supercomputer Applications (NCSA, which is associated with the University of Illinois at Urbana-Champaign) to support the netCDF programming interfaces as a means to store and retrieve data in "HDF files," i.e., in the format used by the popular NCSA tools. We have encouraged and cooperated with these efforts.
Questions occasionally arise about the level of support provided for the netCDF software. Unidata's formal position, stated in the copyright notice which accompanies the netCDF library, is that the software is provided "as is". In practice, the software is updated from time to time, and Unidata intends to continue making improvements for the foreseeable future. Because Unidata's mission is to serve geoscientists at U.S. universities, problems reported by that community necessarily receive the greatest attention.
We hope the reader will find the software useful and will give us feedback on its application as well as suggestions for its improvement.
David Fulker, 1996
Unidata Program Center Director, University Corporation for Atmospheric Research
The purpose of the Network Common Data Form (netCDF) interface is to allow you to create, access, and share array-oriented data in a form that is self-describing and portable. "Self-describing" means that a dataset includes information defining the data it contains. "Portable" means that the data in a dataset is represented in a form that can be accessed by computers with different ways of storing integers, characters, and floating-point numbers. Using the netCDF interface for creating new datasets makes the data portable. Using the netCDF interface in software for data access, management, analysis, and display can make the software more generally useful.
The netCDF software includes C, Fortran 77, Fortran 90, and C++ interfaces for accessing netCDF data. These libraries are available for many common computing platforms.
The community of netCDF users has contributed ports of the software to additional platforms and interfaces for other programming languages as well. Source code for netCDF software libraries is freely available to encourage the sharing of both array-oriented data and the software that makes the data useful.
This User's Guide presents the netCDF data model. It explains how the netCDF data model uses dimensions, variables, and attributes to store data. Language specific programming guides are available for C (see The NetCDF C Interface Guide), C++ (see The NetCDF C++ Interface Guide), Fortran 77 (see The NetCDF Fortran 77 Interface Guide), and Fortran 90 (see The NetCDF Fortran 90 Interface Guide).
Reference documentation for UNIX systems, in the form of UNIX 'man' pages for the C and FORTRAN interfaces is also available at the netCDF web site (http://www.unidata.ucar.edu/software/netcdf), and with the netCDF distribution.
The latest version of this document, and the language specific guides, can be found at the netCDF web site, http://www.unidata.ucar.edu/software/netcdf, along with extensive additional information about netCDF, including pointers to other software that works with netCDF data.
Separate documentation of the Java netCDF library can be found at http://www.unidata.ucar.edu/software/netcdf-java.
For installation and porting information See The NetCDF Installation and Porting Guide.
The Network Common Data Form, or netCDF, is an interface to a library of data access functions for storing and retrieving data in the form of arrays. An array is an n-dimensional (where n is 0, 1, 2, ...) rectangular structure containing items which all have the same data type (e.g., 8-bit character, 32-bit integer). A scalar (simple single value) is a 0-dimensional array.
NetCDF is an abstraction that supports a view of data as a collection of self-describing, portable objects that can be accessed through a simple interface. Array values may be accessed directly, without knowing details of how the data are stored. Auxiliary information about the data, such as what units are used, may be stored with the data. Generic utilities and application programs can access netCDF datasets and transform, combine, analyze, or display specified fields of the data. The development of such applications has led to improved accessibility of data and improved re-usability of software for array-oriented data management, analysis, and display.
The netCDF software implements an abstract data type, which means that all operations to access and manipulate data in a netCDF dataset must use only the set of functions provided by the interface. The representation of the data is hidden from applications that use the interface, so that how the data are stored could be changed without affecting existing programs. The physical representation of netCDF data is designed to be independent of the computer on which the data were written.
Unidata supports the netCDF interfaces for C, (see The NetCDF C Interface Guide), FORTRAN 77 (see The NetCDF Fortran 77 Interface Guide), FORTRAN 90 (see The NetCDF Fortran 90 Interface Guide), and C++ (see The NetCDF C++ Interface Guide).
The netCDF library is supported for various UNIX operating systems. A MS Windows port is also available. The software is also ported and tested on a few other operating systems, with assistance from users with access to these systems, before each major release. Unidata's netCDF software is freely available via FTP to encourage its widespread use. (ftp://ftp.unidata.ucar.edu/pub/netcdf).
For detailed installation instructions, see the Porting and Installation Guide. See The NetCDF Installation and Porting Guide.
Why not use an existing database management system for storing array-oriented data? Relational database software is not suitable for the kinds of data access supported by the netCDF interface.
First, existing database systems that support the relational model do not support multidimensional objects (arrays) as a basic unit of data access. Representing arrays as relations makes some useful kinds of data access awkward and provides little support for the abstractions of multidimensional data and coordinate systems. A quite different data model is needed for array-oriented data to facilitate its retrieval, modification, mathematical manipulation and visualization.
Related to this is a second problem with general-purpose database systems: their poor performance on large arrays. Collections of satellite images, scientific model outputs and long-term global weather observations are beyond the capabilities of most database systems to organize and index for efficient retrieval.
Finally, general-purpose database systems provide, at significant cost in terms of both resources and access performance, many facilities that are not needed in the analysis, management, and display of array-oriented data. For example, elaborate update facilities, audit trails, report formatting, and mechanisms designed for transaction-processing are unnecessary for most scientific applications.
Until version 3.6.0, all versions of netCDF employed only one binary data format, now referred to as netCDF classic format. NetCDF classic is the default format for all versions of netCDF.
In version 3.6.0 a new binary format was introduced, 64-bit offset format. Nearly identical to netCDF classic format, it uses 64-bit offsets (hence the name), and allows users to create far larger datasets.
By default, netCDF uses the classic format. To use the 64-bit offset set the appropriate mode flag when creating the file.
To achieve network-transparency (machine-independence), netCDF classic and 64-bit offset formats are implemented in terms of an external representation much like XDR (eXternal Data Representation, see http://www.ietf.org/rfc/rfc1832.txt), a standard for describing and encoding data. This representation provides encoding of data into machine-independent sequences of bits. It has been implemented on a wide variety of computers, by assuming only that eight-bit bytes can be encoded and decoded in a consistent way. The IEEE 754 floating-point standard is used for floating-point data representation.
Descriptions of the overall structure of netCDF classic and 64-bit offset files are provided later in this manual. See File Structure and Performance.
The details of the classic and 64-bit offset formats are described in an appendix. See File Format Specification. However, users are discouraged from using the format specification to develop independent low-level software for reading and writing netCDF files, because this could lead to compatibility problems if the format is ever modified.
The format of a netCDF file is determined at create time. The default is classic format. The 64-bit offset format will only be used if the mode parameter to the create function includes the flag for 64-bit offset format.
When opening an existing netCDF file the netCDF library will transparently detect its format and adjust accordingly. However, netCDF library versions earlier than 3.6.0 cannot read 64-bit offset format files. NetCDF classic format files (even if created by version 3.6.0 or later) remain compatible with older versions of the netCDF library.
Users are encouraged to use netCDF classic format to distribute data, for maximum portability.
To select 64-bit offset format files, C programmers should use flag NC_64BIT_OFFSET in function nc_create. See nc_create.
In Fortran 77, use flag nf_64bit_offset in function NF_CREATE. See NF_CREATE.)
In Fortran 90, use flag NF90_64BIT_OFFSET in function NF90_CREATE. See NF90_CREATE.)
It is also possible to change the default creation format, to convert a large body of code without changing every create call. C programmers see nc_set_default_format. Fortran programs see NF_SET_DEFAULT_FORMAT.
The original netCDF format is identified using four bytes in the file header. All files in this format have “CDF\001” at the beginning of the file. In this documentation this format is referred to as “netCDF classic format.”
NetCDF classic format is identical to the format used by every previous version of netCDF. It has maximum portability, and is still the default netCDF format.
Files with the 64-bit offsets are identified with a “CDF\002” at the beginning of the file. In this documentation this format is called “64-bit offset format.”
For some users, the various 2 GiB format limitations of the classic format become a problem. (see NetCDF Classic Format Limitations). For these users, 64-bit offset format is a natural choice. It greatly eases the size restrictions of netCDF classic files.
Since 64-bit offset format was introduced in version 3.6.0, earlier versions of the netCDF library can't read 64-bit offset files.
Create files in 64-bit offset-4 format by specifying the 64 bit offset flag when creating a file.
One of the goals of netCDF is to support efficient access to small subsets of large datasets. To support this goal, netCDF uses direct access rather than sequential access. This can be much more efficient when the order in which data is read is different from the order in which it was written, or when it must be read in different orders for different applications.
The amount of overhead for a portable external representation depends on many factors, including the data type, the type of computer, the granularity of data access, and how well the implementation has been tuned to the computer on which it is run. This overhead is typically small in comparison to the overall resources used by an application. In any case, the overhead of the external representation layer is usually a reasonable price to pay for portable data access.
Although efficiency of data access has been an important concern in designing and implementing netCDF, it is still possible to use the netCDF interface to access data in inefficient ways: for example, by requesting a slice of data that requires a single value from each record. Advice on how to use the interface efficiently is provided in File Structure and Performance.
NetCDF can be used as a general-purpose archive format for storing arrays. Compression of data is possible with netCDF (e.g., using arrays of eight-bit or 16-bit integers to encode low-resolution floating-point numbers instead of arrays of 32-bit numbers), but the current version of netCDF was not designed to achieve optimal compression of data. Hence, using netCDF may require more space than special-purpose archive formats that exploit knowledge of particular characteristics of specific datasets.
The mere use of netCDF is not sufficient to make data "self-describing" and meaningful to both humans and machines. The names of variables and dimensions should be meaningful and conform to any relevant conventions. Dimensions should have corresponding coordinate variables where sensible.
Attributes play a vital role in providing ancillary information. It is important to use all the relevant standard attributes using the relevant conventions. For a description of reserved attributes (used by the netCDF library) and attribute conventions for generic application software, see Attribute Conventions.
A number of groups have defined their own additional conventions and styles for netCDF data. Descriptions of these conventions, as well as examples incorporating them can be accessed from the netCDF Conventions site, http://www.unidata.ucar.edu/software/netcdfconventions.html.
These conventions should be used where suitable. Additional conventions are often needed for local use. These should be contributed to the above netCDF conventions site if likely to interest other users in similar areas.
The development of the netCDF interface began with a modest goal related to Unidata's needs: to provide a common interface between Unidata applications and real-time meteorological data. Since Unidata software was intended to run on multiple hardware platforms with access from both C and FORTRAN, achieving Unidata's goals had the potential for providing a package that was useful in a broader context. By making the package widely available and collaborating with other organizations with similar needs, we hoped to improve the then current situation in which software for scientific data access was only rarely reused by others in the same discipline and almost never reused between disciplines (Fulker, 1988).
Important concepts employed in the netCDF software originated in a paper (Treinish and Gough, 1987) that described data-access software developed at the NASA Goddard National Space Science Data Center (NSSDC). The interface provided by this software was called the Common Data Format (CDF). The NASA CDF was originally developed as a platform-specific FORTRAN library to support an abstraction for storing arrays.
The NASA CDF package had been used for many different kinds of data in an extensive collection of applications. It had the virtues of simplicity (only 13 subroutines), independence from storage format, generality, ability to support logical user views of data, and support for generic applications.
Unidata held a workshop on CDF in Boulder in August 1987. We proposed exploring the possibility of collaborating with NASA to extend the CDF FORTRAN interface, to define a C interface, and to permit the access of data aggregates with a single call, while maintaining compatibility with the existing NASA interface.
Independently, Dave Raymond at the New Mexico Institute of Mining and Technology had developed a package of C software for UNIX that supported sequential access to self-describing array-oriented data and a "pipes and filters" (or "data flow") approach to processing, analyzing, and displaying the data. This package also used the "Common Data Format" name, later changed to C-Based Analysis and Display System (CANDIS). Unidata learned of Raymond's work (Raymond, 1988), and incorporated some of his ideas, such as the use of named dimensions and variables with differing shapes in a single data object, into the Unidata netCDF interface.
In early 1988, Glenn Davis of Unidata developed a prototype netCDF package in C that was layered on XDR. This prototype proved that a single-file, XDR-based implementation of the CDF interface could be achieved at acceptable cost and that the resulting programs could be implemented on both UNIX and VMS systems. However, it also demonstrated that providing a small, portable, and NASA CDF-compatible FORTRAN interface with the desired generality was not practical. NASA's CDF and Unidata's netCDF have since evolved separately, but recent CDF versions share many characteristics with netCDF.
In early 1988, Joe Fahle of SeaSpace, Inc. (a commercial software development firm in San Diego, California), a participant in the 1987 Unidata CDF workshop, independently developed a CDF package in C that extended the NASA CDF interface in several important ways (Fahle, 1989). Like Raymond's package, the SeaSpace CDF software permitted variables with unrelated shapes to be included in the same data object and permitted a general form of access to multidimensional arrays. Fahle's implementation was used at SeaSpace as the intermediate form of storage for a variety of steps in their image-processing system. This interface and format have subsequently evolved into the Terascan data format.
After studying Fahle's interface, we concluded that it solved many of the problems we had identified in trying to stretch the NASA interface to our purposes. In August 1988, we convened a small workshop to agree on a Unidata netCDF interface, and to resolve remaining open issues. Attending were Joe Fahle of SeaSpace, Michael Gough of Apple (an author of the NASA CDF software), Angel Li of the University of Miami (who had implemented our prototype netCDF software on VMS and was a potential user), and Unidata systems development staff. Consensus was reached at the workshop after some further simplifications were discovered. A document incorporating the results of the workshop into a proposed Unidata netCDF interface specification was distributed widely for comments before Glenn Davis and Russ Rew implemented the first version of the software. Comparison with other data-access interfaces and experience using netCDF are discussed in Rew and Davis (1990a), Rew and Davis (1990b), Jenter and Signell (1992), and Brown, Folk, Goucher, and Rew (1993).
In October 1991, we announced version 2.0 of the netCDF software distribution. Slight modifications to the C interface (declaring dimension lengths to be long rather than int) improved the usability of netCDF on inexpensive platforms such as MS-DOS computers, without requiring recompilation on other platforms. This change to the interface required no changes to the associated file format.
Release of netCDF version 2.3 in June 1993 preserved the same file format but added single call access to records, optimizations for accessing cross-sections involving non-contiguous data, subsampling along specified dimensions (using 'strides'), accessing non-contiguous data (using 'mapped array sections'), improvements to the ncdump and ncgen utilities, and an experimental C++ interface.
In version 2.4, released in February 1996, support was added for new platforms and for the C++ interface, significant optimizations were implemented for supercomputer architectures, and the file format was formally specified in an appendix to the User's Guide.
FAN (File Array Notation), software providing a high-level interface to netCDF data, was made available in May 1996. The capabilities of the FAN utilities include extracting and manipulating array data from netCDF datasets, printing selected data from netCDF arrays, copying ASCII data into netCDF arrays, and performing various operations (sum, mean, max, min, product, and others) on netCDF arrays.
In 1996 and 1997, Joe Sirott implemented and made available the first implementation of a read-only netCDF interface for Java, Bill Noon made a Python module available for netCDF, and Konrad Hinsen contributed another netCDF interface for Python.
In May 1997, Version 3.3 of netCDF was released. This included a new type-safe interface for C and Fortran, as well as many other improvements. A month later, Charlie Zender released version 1.0 of the NCO (netCDF Operators) package, providing command-line utilities for general purpose operations on netCDF data.
Version 3.4 of Unidata's netCDF software, released in March 1998, included initial large file support, performance enhancements, and improved Cray platform support. Later in 1998, Dan Schmitt provided a Tcl/Tk interface, and Glenn Davis provided version 1.0 of netCDF for Java.
In May 1999, Glenn Davis, who was instrumental in creating and developing netCDF, died in a small plane crash during a thunderstorm. The memory of Glenn's passions and intellect continue to inspire those of us who worked with him.
In February 2000, an experimental Fortran 90 interface developed by Robert Pincus was released.
John Caron released netCDF for Java, version 2.0 in February 2001. This version incorporated a new high-performance package for multidimensional arrays, simplified the interface, and included OPeNDAP (known previously as DODS) remote access, as well as remote netCDF access via HTTP contributed by Don Denbo.
In March 2001, NetCDF 3.5.0 was released. This release fully integrated the new Fortran 90 interface, enhanced portability, improved the C++ interface, and added a few new tuning functions.
Also in 2001, Takeshi Horinouchi and colleagues made a netCDF interface for Ruby available, as did David Pierce for the R language for statistical computing and graphics. Charles Denham released WetCDF, an independent implementation of the netCDF interface for MATLAB, as well as updates to the popular netCDF Toolbox for MATLAB.
In 2002, Unidata and collaborators developed NcML, an XML representation for netCDF data useful for cataloging data holdings, aggregation of data from multiple datasets, augmenting metadata in existing datasets, and support for alternative views of data. The Java interface currently provides access to netCDF data through NcML.
Additional developments in 2002 included translation of C and Fortran User Guides into Japanese by Masato Shiotani and colleagues, creation of a “Best Practices” guide for writing netCDF files, and provision of an Ada-95 interface by Alexandru Corlan.
In July 2003 a group of researchers at Northwestern University and Argonne National Laboratory (Jianwei Li, Wei-keng Liao, Alok Choudhary, Robert Ross, Rajeev Thakur, William Gropp, and Rob Latham) contributed a new parallel interface for writing and reading netCDF data, tailored for use on high performance platforms with parallel I/O. The implementation built on the MPI-IO interface, providing portability to many platforms.
In October 2003, Greg Sjaardema contributed support for an alternative format with 64-bit offsets, to provide more complete support for very large files. These changes, with slight modifications at Unidata, were incorporated into version 3.6.0, released in December, 2004.
In 2004, thanks to a NASA grant, Unidata and NCSA began a collaboration to increase the interoperability of netCDF and HDF5, and bring some advanced HDF5 features to netCDF users. This resulted in netCDF-4.0, currently (as of March, 2006) in alpha release.
This Guide documents the 3.6.2 release of netCDF, which fixes some bugs and improves documentation and portability.
The netCDF data model is widely applicable to data that can be organized into a collection of named array variables with named attributes, but there are some important limitations to the model and its implementation in software.
Currently, netCDF offers a limited number of external numeric data types: 8-, 16-, 32-bit integers, or 32- or 64-bit floating-point numbers. This limited set of sizes may use file space inefficiently compared to packing data in bit fields. For example, arrays of 9-bit values must be stored in 16-bit short integers. Storing arrays of 1- or 2-bit values in 8-bit values is even less optimal.
With the classic netCDF file format, there are constraints that limit how a dataset is structured to store more than 2 GiBytes (2^30 or 1,073,741,824 bytes, as compared to a Gbyte, which is 1,000,000,000 bytes) of data in a single netCDF dataset. (see NetCDF Classic Format Limitations). This limitation is a result of 32-bit offsets used for storing relative offsets within a classic netCDF format file. Since one of the goals of netCDF is portable data and some computing platforms still can't deal with files larger than 2 GiB, it is best to keep files that must be portable below this limit. Nevertheless, it is possible to create and access netCDF files larger than 2 GiB on platforms that provide support for such files (see Large File Support).
The new 64-bit offset format (introduced version 3.6.0) allows large files, and makes it easy to create to create fixed variables of about 4 GiB, and record variables of about 4 GiB per record. (see NetCDF 64 bit Offset Format Limitations). However, old netCDF applications will not be able to read the 64-bit offset files until they are upgraded to at least version 3.6.0 of netCDF.
Another limitation of the classic (and 64-bit offset) model is that only one unlimited (changeable) dimension is permitted for each netCDF data set. Multiple variables can share an unlimited dimension, but then they must all grow together. Hence the netCDF model does not permit variables with several unlimited dimensions or the use of multiple unlimited dimensions in different variables within the same dataset. Variables that have non-rectangular shapes (for example, ragged arrays) cannot be represented conveniently.
The extent to which data can be completely self-describing is limited: there is always some assumed context without which sharing and archiving data would be impractical. NetCDF permits storing meaningful names for variables, dimensions, and attributes; units of measure in a form that can be used in computations; text strings for attribute values that apply to an entire data set; and simple kinds of coordinate system information. But for more complex kinds of metadata (for example, the information necessary to provide accurate georeferencing of data on unusual grids or from satellite images), it is often necessary to develop conventions.
Specific additions to the netCDF data model might make some of these conventions unnecessary or allow some forms of metadata to be represented in a uniform and compact way. For example, adding explicit georeferencing to the netCDF data model would simplify elaborate georeferencing conventions at the cost of complicating the model. The problem is finding an appropriate trade-off between the richness of the model and its generality (i.e., its ability to encompass many kinds of data). A data model tailored to capture the shared context among researchers within one discipline may not be appropriate for sharing or combining data from multiple disciplines.
The classic netCDF data model does not support nested data structures such as trees, nested arrays, or other recursive structures. (This limitation also applies to 64-bit offset files.) Through use of indirection and conventions it is possible to represent some kinds of nested structures, but the result may fall short of the netCDF goal of self-describing data.
Finally, concurrent access to a netCDF dataset is limited. One writer and multiple readers may access data in a single dataset simultaneously, but there is no support for multiple concurrent writers.
NetCDF-4.0 is currently available in alpha release. It allows the use of HDF5 files from the netCDF API. New features available with netCDF-4/HDF5 files include:
NetCDF-4 also contains a complete upgrade of the installation and build system, including the use of the libtool package to build shared libraries on systems which support them.
More information about netCDF-4 can be found at the netCDF-4 web page http://www.unidata.ucar.edu/software/netcdf/netcdf-4.
For more information about HDF5, see the HDF5 web site: http://hdf.ncsa.uiuc.edu/HDF5.
A netCDF dataset contains dimensions, variables, and attributes, which all have both a name and an ID number by which they are identified. These components can be used together to capture the meaning of data and relations among data fields in an array-oriented dataset. The netCDF library allows simultaneous access to multiple netCDF datasets which are identified by dataset ID numbers, in addition to ordinary file names.
The names of dimensions, variables and attributes consist of arbitrary sequences of alphanumeric characters (as well as underscore '_', period '.' and hyphen '-'), beginning with a letter or underscore. (However names commencing with underscore are reserved for system use.) Case is significant in netCDF names. A zero-length name is not allowed.
We will use a small netCDF example to illustrate the concepts of the netCDF data model. This includes dimensions, variables, and attributes. The notation used to describe this simple netCDF object is called CDL (network Common Data form Language), which provides a convenient way of describing netCDF datasets. The netCDF system includes utilities for producing human-oriented CDL text files from binary netCDF datasets and vice versa.
netcdf example_1 { // example of CDL notation for a netCDF dataset dimensions: // dimension names and lengths are declared first lat = 5, lon = 10, level = 4, time = unlimited; variables: // variable types, names, shapes, attributes float temp(time,level,lat,lon); temp:long_name = "temperature"; temp:units = "celsius"; float rh(time,lat,lon); rh:long_name = "relative humidity"; rh:valid_range = 0.0, 1.0; // min and max int lat(lat), lon(lon), level(level); lat:units = "degrees_north"; lon:units = "degrees_east"; level:units = "millibars"; short time(time); time:units = "hours since 1996-1-1"; // global attributes :source = "Fictional Model Output"; data: // optional data assignments level = 1000, 850, 700, 500; lat = 20, 30, 40, 50, 60; lon = -160,-140,-118,-96,-84,-52,-45,-35,-25,-15; time = 12; rh =.5,.2,.4,.2,.3,.2,.4,.5,.6,.7, .1,.3,.1,.1,.1,.1,.5,.7,.8,.8, .1,.2,.2,.2,.2,.5,.7,.8,.9,.9, .1,.2,.3,.3,.3,.3,.7,.8,.9,.9, 0,.1,.2,.4,.4,.4,.4,.7,.9,.9; }
The CDL notation for a netCDF dataset can be generated automatically by using ncdump, a utility program described later (see ncdump). Another netCDF utility, ncgen, generates a netCDF dataset (or optionally C or FORTRAN source code containing calls needed to produce a netCDF dataset) from CDL input (see ncgen).
The CDL notation is simple and largely self-explanatory. It will be explained more fully as we describe the components of a netCDF dataset. For now, note that CDL statements are terminated by a semicolon. Spaces, tabs, and newlines can be used freely for readability. Comments in CDL follow the characters '//' on any line. A CDL description of a netCDF dataset takes the form
netCDF name { dimensions: ... variables: ... data: ... }
where the name is used only as a default in constructing file names by the ncgen utility. The CDL description consists of three optional parts, introduced by the keywords dimensions, variables, and data. NetCDF dimension declarations appear after the dimensions keyword, netCDF variables and attributes are defined after the variables keyword, and variable data assignments appear after the data keyword.
The ncgen utility provides a command line option which indicates the desired output format. Limitations are enforced for the selected format - that is, some cdl files may be expressible only in 64-bit offset format.
For example, trying to create a file with very large variables in classic format may result in an error because size limits are violated.
A dimension may be used to represent a real physical dimension, for example, time, latitude, longitude, or height. A dimension might also be used to index other quantities, for example station or model-run-number.
A netCDF dimension has both a name and a length.
A dimension length is an arbitrary positive integer, except that one dimension can have the length UNLIMITED.
Such a dimension is called the unlimited dimension or the record dimension. A variable with an unlimited dimension can grow to any length along that dimension. The unlimited dimension index is like a record number in conventional record-oriented files.
A netCDF classic or 64-bit offset dataset can have at most one unlimited dimension, but need not have any. If a variable has an unlimited dimension, that dimension must be the most significant (slowest changing) one. Thus any unlimited dimension must be the first dimension in a CDL shape and the first dimension in corresponding C array declarations.
To grow variables along an unlimited dimension, write the data using any of the netCDF data writing functions, and specify the index of the unlimited dimension to the desired record number. The netCDF library will write however many records are needed (using the fill value, unless turned off) to fill in any intervening records.
CDL dimension declarations may appear on one or more lines following the CDL keyword dimensions. Multiple dimension declarations on the same line may be separated by commas. Each declaration is of the form name = length.
There are four dimensions in the above example: lat, lon, level, and time (see The NetCDF Data Model). The first three are assigned fixed lengths; time is assigned the length UNLIMITED, which means it is the unlimited dimension.
The basic unit of named data in a netCDF dataset is a variable. When a variable is defined, its shape is specified as a list of dimensions. These dimensions must already exist. The number of dimensions is called the rank (a.k.a. dimensionality). A scalar variable has rank 0, a vector has rank 1 and a matrix has rank 2.
It is possible (since version 3.1 of netCDF) to use the same dimension more than once in specifying a variable shape. For example, correlation(instrument, instrument) could be a matrix giving correlations between measurements using different instruments. But data whose dimensions correspond to those of physical space/time should have a shape comprising different dimensions, even if some of these have the same length.
Variables are used to store the bulk of the data in a netCDF dataset. A variable represents an array of values of the same type. A scalar value is treated as a 0-dimensional array. A variable has a name, a data type, and a shape described by its list of dimensions specified when the variable is created. A variable may also have associated attributes, which may be added, deleted or changed after the variable is created.
A variable external data type is one of a small set of netCDF types. In classic and 64-bit offset files, only the original six types are available (byte, character, short, int, float, and double).
For more information on types for the C interface, See Variable Types.
For more information on types for the Fortran interface, See Variable Types.
In the CDL notation, only classic and 64-bit offset type can be used. They are given the simpler names byte, char, short, int, float, and double. real may be used as a synonym for float in the CDL notation. long is a deprecated synonym for int. For the exact meaning of each of the types see netCDF external data types.
CDL variable declarations appear after the variable keyword in a CDL unit. They have the form
type variable_name ( dim_name_1, dim_name_2, ... );
for variables with dimensions, or
type variable_name;
for scalar variables.
In the above CDL example there are six variables. As discussed below, four of these are coordinate variables. The remaining variables (sometimes called primary variables), temp and rh, contain what is usually thought of as the data. Each of these variables has the unlimited dimension time as its first dimension, so they are called record variables. A variable that is not a record variable has a fixed length (number of data values) given by the product of its dimension lengths. The length of a record variable is also the product of its dimension lengths, but in this case the product is variable because it involves the length of the unlimited dimension, which can vary. The length of the unlimited dimension is the number of records.
It is legal for a variable to have the same name as a dimension. Such variables have no special meaning to the netCDF library. However there is a convention that such variables should be treated in a special way by software using this library.
A variable with the same name as a dimension is called a coordinate variable. It typically defines a physical coordinate corresponding to that dimension. The above CDL example includes the coordinate variables lat, lon, level and time, defined as follows:
int lat(lat), lon(lon), level(level); short time(time); ... data: level = 1000, 850, 700, 500; lat = 20, 30, 40, 50, 60; lon = -160,-140,-118,-96,-84,-52,-45,-35,-25,-15; time = 12;
These define the latitudes, longitudes, barometric pressures and times corresponding to positions along these dimensions. Thus there is data at altitudes corresponding to 1000, 850, 700 and 500 millibars; and at latitudes 20, 30, 40, 50 and 60 degrees north. Note that each coordinate variable is a vector and has a shape consisting of just the dimension with the same name.
A position along a dimension can be specified using an index. This is an integer with a minimum value of 0 for C programs, 1 in Fortran programs. Thus the 700 millibar level would have an index value of 2 in the example above in a C program, and 3 in a Fortran program.
If a dimension has a corresponding coordinate variable, then this provides an alternative, and often more convenient, means of specifying position along it. Current application packages that make use of coordinate variables commonly assume they are numeric vectors and strictly monotonic (all values are different and either increasing or decreasing).
NetCDF attributes are used to store data about the data (ancillary data or metadata), similar in many ways to the information stored in data dictionaries and schema in conventional database systems. Most attributes provide information about a specific variable. These are identified by the name (or ID) of that variable, together with the name of the attribute.
Some attributes provide information about the dataset as a whole and are called global attributes. These are identified by the attribute name together with a blank variable name (in CDL) or a special null "global variable" ID (in C or Fortran).
An attribute has an associated variable (the null "global variable" for a global or group-level attribute), a name, a data type, a length, and a value. The current version treats all attributes as vectors; scalar values are treated as single-element vectors.
Conventional attribute names should be used where applicable. New names should be as meaningful as possible.
The external type of an attribute is specified when it is created. The types permitted for attributes are the same as the netCDF external data types for variables. Attributes with the same name for different variables should sometimes be of different types. For example, the attribute valid_max specifying the maximum valid data value for a variable of type int should be of type int, whereas the attribute valid_max for a variable of type double should instead be of type double.
Attributes are more dynamic than variables or dimensions; they can be deleted and have their type, length, and values changed after they are created, whereas the netCDF interface provides no way to delete a variable or to change its type or shape.
The CDL notation for defining an attribute is
variable_name:attribute_name = list_of_values;
for a variable attribute, or
:attribute_name = list_of_values;
for a global attribute.
The type and length of each attribute are not explicitly declared in CDL; they are derived from the values assigned to the attribute. All values of an attribute must be of the same type. The notation used for constant values of the various netCDF types is discussed later (see CDL Notation for Data Constants).
In the netCDF example (see The NetCDF Data Model), units is an attribute for the variable lat that has a 13-character array value 'degrees_north'. And valid_range is an attribute for the variable rh that has length 2 and values '0.0' and '1.0'.
One global attribute, called “source”, is defined for the example netCDF dataset. This is a character array intended for documenting the data. Actual netCDF datasets might have more global attributes to document the origin, history, conventions, and other characteristics of the dataset as a whole.
Most generic applications that process netCDF datasets assume standard attribute conventions and it is strongly recommended that these be followed unless there are good reasons for not doing so. For information about units, long_name, valid_min, valid_max, valid_range, scale_factor, add_offset, _FillValue, and other conventional attributes, see Attribute Conventions.
Attributes may be added to a netCDF dataset long after it is first defined, so you don't have to anticipate all potentially useful attributes. However adding new attributes to an existing classic or 64-bit offset format dataset can incur the same expense as copying the dataset. For a more extensive discussion see File Structure and Performance.
In contrast to variables, which are intended for bulk data, attributes are intended for ancillary data, or information about the data. The total amount of ancillary data associated with a netCDF object, and stored in its attributes, is typically small enough to be memory-resident. However variables are often too large to entirely fit in memory and must be split into sections for processing.
Another difference between attributes and variables is that variables may be multidimensional. Attributes are all either scalars (single-valued) or vectors (a single, fixed dimension).
Variables are created with a name, type, and shape before they are assigned data values, so a variable may exist with no values. The value of an attribute is specified when it is created, unless it is a zero-length attribute.
A variable may have attributes, but an attribute cannot have attributes. Attributes assigned to variables may have the same units as the variable (for example, valid_range) or have no units (for example, scale_factor). If you want to store data that requires units different from those of the associated variable, it is better to use a variable than an attribute. More generally, if data require ancillary data to describe them, are multidimensional, require any of the defined netCDF dimensions to index their values, or require a significant amount of storage, that data should be represented using variables rather than attributes.
This chapter discusses the primitive netCDF external data types, the kinds of data access supported by the netCDF interface, and how data structures other than arrays may be implemented in a netCDF dataset.
The atomic external types supported by the netCDF interface are:
C name | Fortran name | storage
|
NC_BYTE | nf_byte | 8-bit signed integer
|
NC_CHAR | nf_char | 8-bit unsigned integer
|
NC_SHORT | nf_short | 16-bit signed integer
|
NC_INT (or NC_LONG) | nf_int | 32-bit signed integer
|
NC_FLOAT | nf_float | 32-bit floating point
|
NC_DOUBLE | nf_double | 64-bit floating point
|
These types were chosen to provide a reasonably wide range of trade-offs between data precision and number of bits required for each value. These external data types are independent from whatever internal data types are supported by a particular machine and language combination.
These types are called "external", because they correspond to the portable external representation for netCDF data. When a program reads external netCDF data into an internal variable, the data is converted, if necessary, into the specified internal type. Similarly, if you write internal data into a netCDF variable, this may cause it to be converted to a different external type, if the external type for the netCDF variable differs from the internal type.
The separation of external and internal types and automatic type conversion have several advantages. You need not be aware of the external type of numeric variables, since automatic conversion to or from any desired numeric type is available. You can use this feature to simplify code, by making it independent of external types, using a sufficiently wide internal type, e.g., double precision, for numeric netCDF data of several different external types. Programs need not be changed to accommodate a change to the external type of a variable.
If conversion to or from an external numeric type is necessary, it is handled by the library.
Converting from one numeric type to another may result in an error if the target type is not capable of representing the converted value. For example, an internal short integer type may not be able to hold data stored externally as an integer. When accessing an array of values, a range error is returned if one or more values are out of the range of representable values, but other values are converted properly.
Note that mere loss of precision in type conversion does not return an error. Thus, if you read double precision values into a single-precision floating-point variable, for example, no error results unless the magnitude of the double precision value exceeds the representable range of single-precision floating point numbers on your platform. Similarly, if you read a large integer into a float incapable of representing all the bits of the integer in its mantissa, this loss of precision will not result in an error. If you want to avoid such precision loss, check the external types of the variables you access to make sure you use an internal type that has adequate precision.
The names for the primitive external data types (byte, char, short, ushort, int, uint, int64, uint64, float or real, double, bool, string) are reserved words in CDL, so the names of variables, dimensions, and attributes must not be type names.
It is possible to interpret byte data as either signed (-128 to 127) or unsigned (0 to 255). However, when reading byte data to be converted into other numeric types, it is interpreted as signed.
For the correspondence between netCDF external data types and the data types of a language see Variables.
To access (read or write) netCDF data you specify an open netCDF dataset, a netCDF variable, and information (e.g., indices) identifying elements of the variable. The name of the access function corresponds to the internal type of the data. If the internal type has a different representation from the external type of the variable, a conversion between the internal type and external type will take place when the data is read or written.
Access to data in classic and 64-bit offset format is direct. In either case you can access a small subset of data from a large dataset efficiently, without first accessing all the data that precedes it.
Reading and writing data by specifying a variable, instead of a position in a file, makes data access independent of how many other variables are in the dataset, making programs immune to data format changes that involve adding more variables to the data.
In the C and FORTRAN interfaces, datasets are not specified by name every time you want to access data, but instead by a small integer called a dataset ID, obtained when the dataset is first created or opened.
Similarly, a variable is not specified by name for every data access either, but by a variable ID, a small integer used to identify each variable in a netCDF dataset.
The netCDF interface supports several forms of direct access to data values in an open netCDF dataset. We describe each of these forms of access in order of increasing generality:
The four types of vector (index vector, count vector, stride vector and index mapping vector) each have one element for each dimension of the variable. Thus, for an n-dimensional variable (rank = n), n-element vectors are needed. If the variable is a scalar (no dimensions), these vectors are ignored.
An array section is a "slab" or contiguous rectangular block that is specified by two vectors. The index vector gives the indices of the element in the corner closest to the origin. The count vector gives the lengths of the edges of the slab along each of the variable's dimensions, in order. The number of values accessed is the product of these edge lengths.
A subsampled array section is similar to an array section, except that an additional stride vector is used to specify sampling. This vector has an element for each dimension giving the length of the strides to be taken along that dimension. For example, a stride of 4 means every fourth value along the corresponding dimension. The total number of values accessed is again the product of the elements of the count vector.
A mapped array section is similar to a subsampled array section except that an additional index mapping vector allows one to specify how data values associated with the netCDF variable are arranged in memory. The offset of each value from the reference location, is given by the sum of the products of each index (of the imaginary internal array which would be used if there were no mapping) by the corresponding element of the index mapping vector. The number of values accessed is the same as for a subsampled array section.
The use of mapped array sections is discussed more fully below, but first we present an example of the more commonly used array-section access.
Assume that in our earlier example of a netCDF dataset (see Network Common Data Form Language (CDL)), we wish to read a cross-section of all the data for the temp variable at one level (say, the second), and assume that there are currently three records (time values) in the netCDF dataset. Recall that the dimensions are defined as
lat = 5, lon = 10, level = 4, time = unlimited;
and the variable temp is declared as
float temp(time, level, lat, lon);
in the CDL notation.
A corresponding C variable that holds data for only one level might be declared as
#define LATS 5 #define LONS 10 #define LEVELS 1 #define TIMES 3 /* currently */ ... float temp[TIMES*LEVELS*LATS*LONS]; to keep the data in a one-dimensional array, or ... float temp[TIMES][LEVELS][LATS][LONS];
using a multidimensional array declaration.
To specify the block of data that represents just the second level, all times, all latitudes, and all longitudes, we need to provide a start index and some edge lengths. The start index should be (0, 1, 0, 0) in C, because we want to start at the beginning of each of the time, lon, and lat dimensions, but we want to begin at the second value of the level dimension. The edge lengths should be (3, 1, 5, 10) in C, (since we want to get data for all three time values, only one level value, all five lat values, and all 10 lon values. We should expect to get a total of 150 floating-point values returned (3 * 1 * 5 * 10), and should provide enough space in our array for this many. The order in which the data will be returned is with the last dimension, lon, varying fastest:
temp[0][1][0][0] temp[0][1][0][1] temp[0][1][0][2] temp[0][1][0][3] ... temp[2][1][4][7] temp[2][1][4][8] temp[2][1][4][9]
Different dimension orders for the C, FORTRAN, or other language interfaces do not reflect a different order for values stored on the disk, but merely different orders supported by the procedural interfaces to the languages. In general, it does not matter whether a netCDF dataset is written using the C, FORTRAN, or another language interface; netCDF datasets written from any supported language may be read by programs written in other supported languages.
The use of mapped array sections allows non-trivial relationships between the disk addresses of variable elements and the addresses where they are stored in memory. For example, a matrix in memory could be the transpose of that on disk, giving a quite different order of elements. In a regular array section, the mapping between the disk and memory addresses is trivial: the structure of the in-memory values (i.e., the dimensional lengths and their order) is identical to that of the array section. In a mapped array section, however, an index mapping vector is used to define the mapping between indices of netCDF variable elements and their memory addresses.
With mapped array access, the offset (number of array elements) from the origin of a memory-resident array to a particular point is given by the inner product[1] of the index mapping vector with the point's coordinate offset vector. A point's coordinate offset vector gives, for each dimension, the offset from the origin of the containing array to the point.In C, a point's coordinate offset vector is the same as its coordinate vector.
The index mapping vector for a regular array section would have–in order from most rapidly varying dimension to most slowly–a constant 1, the product of that value with the edge length of the most rapidly varying dimension of the array section, then the product of that value with the edge length of the next most rapidly varying dimension, and so on. In a mapped array, however, the correspondence between netCDF variable disk locations and memory locations can be different.
For example, the following C definitions
struct vel { int flags; float u; float v; } vel[NX][NY]; ptrdiff_t imap[2] = { sizeof(struct vel), sizeof(struct vel)*NY };
where imap is the index mapping vector, can be used to access the memory-resident values of the netCDF variable, vel(NY,NX), even though the dimensions are transposed and the data is contained in a 2-D array of structures rather than a 2-D array of floating-point values.
A detailed example of mapped array access is presented in the description of the interfaces for mapped array access. See Write a Mapped Array of Values - nc_put_varm_ type.
Note that, although the netCDF abstraction allows the use of subsampled or mapped array-section access there use is not required. If you do not need these more general forms of access, you may ignore these capabilities and use single value access or regular array section access instead.
Assume that in our earlier example of a netCDF dataset (see The NetCDF Data Model), we wish to read a cross-section of all the data for the temp variable at one level (say, the second), and assume that there are currently three records (time values) in the netCDF dataset. Recall that the dimensions are defined as
lat = 5, lon = 10, level = 4, time = unlimited;
and the variable temp is declared as
float temp(time, level, lat, lon);
in the CDL notation.
In FORTRAN, the dimensions are reversed from the CDL declaration with the first dimension varying fastest and the record dimension as the last dimension of a record variable. Thus a FORTRAN declarations for a variable that holds data for only one level is
INTEGER LATS, LONS, LEVELS, TIMES PARAMETER (LATS=5, LONS=10, LEVELS=1, TIMES=3) ... REAL TEMP(LONS, LATS, LEVELS, TIMES)
To specify the block of data that represents just the second level, all times, all latitudes, and all longitudes, we need to provide a start index and some edge lengths. The start index should be (1, 1, 2, 1) in FORTRAN, because we want to start at the beginning of each of the time, lon, and lat dimensions, but we want to begin at the second value of the level dimension. The edge lengths should be (10, 5, 1, 3) in FORTRAN, since we want to get data for all three time values, only one level value, all five lat values, and all 10 lon values. We should expect to get a total of 150 floating-point values returned (3 * 1 * 5 * 10), and should provide enough space in our array for this many. The order in which the data will be returned is with the first dimension, LON, varying fastest:
TEMP( 1, 1, 2, 1) TEMP( 2, 1, 2, 1) TEMP( 3, 1, 2, 1) TEMP( 4, 1, 2, 1) ... TEMP( 8, 5, 2, 3) TEMP( 9, 5, 2, 3) TEMP(10, 5, 2, 3)
Different dimension orders for the C, FORTRAN, or other language interfaces do not reflect a different order for values stored on the disk, but merely different orders supported by the procedural interfaces to the languages. In general, it does not matter whether a netCDF dataset is written using the C, FORTRAN, or another language interface; netCDF datasets written from any supported language may be read by programs written in other supported languages.
The use of mapped array sections allows non-trivial relationships between the disk addresses of variable elements and the addresses where they are stored in memory. For example, a matrix in memory could be the transpose of that on disk, giving a quite different order of elements. In a regular array section, the mapping between the disk and memory addresses is trivial: the structure of the in-memory values (i.e., the dimensional lengths and their order) is identical to that of the array section. In a mapped array section, however, an index mapping vector is used to define the mapping between indices of netCDF variable elements and their memory addresses.
With mapped array access, the offset (number of array elements) from the origin of a memory-resident array to a particular point is given by the inner product[1] of the index mapping vector with the point's coordinate offset vector. A point's coordinate offset vector gives, for each dimension, the offset from the origin of the containing array to the point. In FORTRAN, the values of a point's coordinate offset vector are one less than the corresponding values of the point's coordinate vector, e.g., the array element A(3,5) has coordinate offset vector [2, 4].
The index mapping vector for a regular array section would have–in order from most rapidly varying dimension to most slowly–a constant 1, the product of that value with the edge length of the most rapidly varying dimension of the array section, then the product of that value with the edge length of the next most rapidly varying dimension, and so on. In a mapped array, however, the correspondence between netCDF variable disk locations and memory locations can be different.
A detailed example of mapped array access is presented in the description of the interfaces for mapped array access. See nf_put_varm_ type.
Note that, although the netCDF abstraction allows the use of subsampled or mapped array-section access there use is not required. If you do not need these more general forms of access, you may ignore these capabilities and use single value access or regular array section access instead.
Each netCDF variable has an external type, specified when the variable is first defined. This external type determines whether the data is intended for text or numeric values, and if numeric, the range and precision of numeric values.
If the netCDF external type for a variable is char, only character data representing text strings can be written to or read from the variable. No automatic conversion of text data to a different representation is supported.
If the type is numeric, however, the netCDF library allows you to access the variable data as a different type and provides automatic conversion between the numeric data in memory and the data in the netCDF variable. For example, if you write a program that deals with all numeric data as double-precision floating point values, you can read netCDF data into double-precision arrays without knowing or caring what the external type of the netCDF variables are. On reading netCDF data, integers of various sizes and single-precision floating-point values will all be converted to double-precision, if you use the data access interface for double-precision values. Of course, you can avoid automatic numeric conversion by using the netCDF interface for a value type that corresponds to the external data type of each netCDF variable, where such value types exist.
The automatic numeric conversions performed by netCDF are easy to understand, because they behave just like assignment of data of one type to a variable of a different type. For example, if you read floating-point netCDF data as integers, the result is truncated towards zero, just as it would be if you assigned a floating-point value to an integer variable. Such truncation is an example of the loss of precision that can occur in numeric conversions.
Converting from one numeric type to another may result in an error if the target type is not capable of representing the converted value. For example, an integer may not be able to hold data stored externally as an IEEE floating-point number. When accessing an array of values, a range error is returned if one or more values are out of the range of representable values, but other values are converted properly.
Note that mere loss of precision in type conversion does not result in an error. For example, if you read double precision values into an integer, no error results unless the magnitude of the double precision value exceeds the representable range of integers on your platform. Similarly, if you read a large integer into a float incapable of representing all the bits of the integer in its mantissa, this loss of precision will not result in an error. If you want to avoid such precision loss, check the external types of the variables you access to make sure you use an internal type that has a compatible precision.
Whether a range error occurs in writing a large floating-point value near the boundary of representable values may be depend on the platform. The largest floating-point value you can write to a netCDF float variable is the largest floating-point number representable on your system that is less than 2 to the 128th power. The largest double precision value you can write to a double variable is the largest double-precision number representable on your system that is less than 2 to the 1024th power.
The only kind of data structure directly supported by the netCDF classic (and 64-bit offset) abstraction is a collection of named arrays with attached vector attributes. NetCDF is not particularly well-suited for storing linked lists, trees, sparse matrices, ragged arrays or other kinds of data structures requiring pointers.
It is possible to build other kinds of data structures in netCDF, from sets of arrays by adopting various conventions regarding the use of data in one array as pointers into another array. The netCDF library won't provide much help or hindrance with constructing such data structures, but netCDF provides the mechanisms with which such conventions can be designed.
The following netCDF classic example stores a ragged array ragged_mat using an attribute row_index to name an associated index variable giving the index of the start of each row. In this example, the first row contains 12 elements, the second row contains 7 elements (19 - 12), and so on.
float ragged_mat(max_elements); ragged_mat:row_index = "row_start"; int row_start(max_rows); data: row_start = 0, 12, 19, ...
As another example, netCDF variables may be grouped within a netCDF classic or 64-bit offset dataset by defining attributes that list the names of the variables in each group, separated by a conventional delimiter such as a space or comma. Using a naming convention for attribute names for such groupings permits any number of named groups of variables. A particular conventional attribute for each variable might list the names of the groups of which it is a member. Use of attributes, or variables that refer to other attributes or variables, provides a flexible mechanism for representing some kinds of complex structures in netCDF datasets.
This chapter describes the file structure of a netCDF classic or 64-bit offset dataset in enough detail to aid in understanding netCDF performance issues.
NetCDF is a data abstraction for array-oriented data access and a software library that provides a concrete implementation of the interfaces that support that abstraction. The implementation provides a machine-independent format for representing arrays. Although the netCDF file format is hidden below the interfaces, some understanding of the current implementation and associated file structure may help to make clear why some netCDF operations are more expensive than others.
For a detailed description of the netCDF format, see File Structure and Performance. Knowledge of the format is not needed for reading and writing netCDF data or understanding most efficiency issues. Programs that use only the documented interfaces and that make no assumptions about the format will continue to work even if the netCDF format is changed in the future, because any such change will be made below the documented interfaces and will support earlier versions of the netCDF file format.
A netCDF classic or 64-bit offset dataset is stored as a single file comprising two parts:
Both the header and data parts are represented in a machine-independent form. This form is very similar to XDR (eXternal Data Representation), extended to support efficient storage of arrays of non-byte data.
The header at the beginning of the file contains information about the dimensions, variables, and attributes in the file, including their names, types, and other characteristics. The information about each variable includes the offset to the beginning of the variable's data for fixed-size variables or the relative offset of other variables within a record. The header also contains dimension lengths and information needed to map multidimensional indices for each variable to the appropriate offsets.
By default, this header has little usable extra space; it is only as large as it needs to be for the dimensions, variables, and attributes (including all the attribute values) in the netCDF dataset, with a small amount of extra space from rounding up to the nearest disk block size. This has the advantage that netCDF files are compact, requiring very little overhead to store the ancillary data that makes the datasets self-describing. A disadvantage of this organization is that any operation on a netCDF dataset that requires the header to grow (or, less likely, to shrink), for example adding new dimensions or new variables, requires moving the data by copying it. This expense is incurred when the enddef function is called: nc_enddef in C (see nc_enddef), NF_ENDDEF in Fortran (see NF_ENDDEF), after a previous call to the redef function: nc_redef in C (see nc_redef) or NF_REDEF in Fortran (see NF_REDEF). If you create all necessary dimensions, variables, and attributes before writing data, and avoid later additions and renamings of netCDF components that require more space in the header part of the file, you avoid the cost associated with later changing the header.
Alternatively, you can use an alternative version of the enddef function with two underbar characters instead of one to explicitly reserve extra space in the file header when the file is created: in C nc__enddef (see nc__enddef), in Fortran NF__ENDDEF (see NF__ENDDEF), after a previous call to the redef function. This avoids the expense of moving all the data later by reserving enough extra space in the header to accommodate anticipated changes, such as the addition of new attributes or the extension of existing string attributes to hold longer strings.
When the size of the header is changed, data in the file is moved, and the location of data values in the file changes. If another program is reading the netCDF dataset during redefinition, its view of the file will be based on old, probably incorrect indexes. If netCDF datasets are shared across redefinition, some mechanism external to the netCDF library must be provided that prevents access by readers during redefinition, and causes the readers to call nc_sync/NF_SYNC before any subsequent access.
The fixed-size data part that follows the header contains all the variable data for variables that do not employ an unlimited dimension. The data for each variable is stored contiguously in this part of the file. If there is no unlimited dimension, this is the last part of the netCDF file.
The record-data part that follows the fixed-size data consists of a variable number of fixed-size records, each of which contains data for all the record variables. The record data for each variable is stored contiguously in each record.
The order in which the variable data appears in each data section is the same as the order in which the variables were defined, in increasing numerical order by netCDF variable ID. This knowledge can sometimes be used to enhance data access performance, since the best data access is currently achieved by reading or writing the data in sequential order.
For more detail see File Format Specification.
XDR is a standard for describing and encoding data and a library of functions for external data representation, allowing programmers to encode data structures in a machine-independent way. Classic or 64-bit offset NetCDF employs an extended form of XDR for representing information in the header part and the data parts. This extended XDR is used to write portable data that can be read on any other machine for which the library has been implemented.
The cost of using a canonical external representation for data varies according to the type of data and whether the external form is the same as the machine's native form for that type.
For some data types on some machines, the time required to convert data to and from external form can be significant. The worst case is reading or writing large arrays of floating-point data on a machine that does not use IEEE floating-point as its native representation.
It is possible to write netCDF files that exceed 2 GiByte on platforms that have "Large File Support" (LFS). Such files are platform-independent to other LFS platforms, but trying to open them on an older platform without LFS yields a "file too large" error.
Without LFS, no files larger than 2 GiBytes can be used. The rest of this section applies only to systems with LFS.
The original binary format of netCDF (classic format) limits the size of data files by using a signed 32-bit offset within its internal structure. Files larger than 2 GiB can be created, with certain limitations. See NetCDF Classic Format Limitations.
In version 3.6.0, netCDF included its first-ever variant of the underlying data format. The new format introduced in 3.6.0 uses 64-bit file offsets in place of the 32-bit offsets. There are still some limits on the sizes of variables, but the new format can create very large datasets. See NetCDF 64 bit Offset Format Limitations.
The original data format (netCDF classic), is still the default data format for the netCDF library.
The following table summarizes the size limitations of various permutations of LFS support, netCDF version, and data format. Note that 1 GiB = 2^30 bytes or about 1.07e+9 bytes, 1 EiB = 2^60 bytes or about 1.15e+18 bytes. Note also that all sizes are really 4 bytes less than the ones given below. For example the maximum size of a fixed variable in netCDF 3.6 classic format is really 2 GiB - 4 bytes.
Limit | No LFS | v3.5 | v3.6/classic | v3.6/64-bit
offset
|
Max File Size | 2 GiB | 8 EiB | 8 EiB | 8 EiB
|
Max Number of Fixed Vars > 2 GiB | 0 | 1 (last) | 1 (last) | 2^32
|
Max Record Vars w/ Rec Size > 2 GiB | 0 | 1 (last) | 1 (last) | 2^32
|
Max Size of Fixed/Record Size of Record Var | 2 GiB | 2 GiB | 2 GiB | 4 GiB
|
Max Record Size | 2 GiB/nrecs | 4 GiB | 8 EiB/nrecs | 8 EiB/nrecs
|
For more information about the different file formats of netCDF See Which Format.
Although the 64-bit offset format allows the creation of much larger netCDF files than was possible with the classic format, there are still some restrictions on the size of variables.
It's important to note that without Large File Support (LFS) in the operating system, it's impossible to create any file larger than 2 GiBytes. Assuming an operating system with LFS, the following restrictions apply to the netCDF 64-bit offset format.
No fixed-size variable can require more than 2^32 - 4 bytes (i.e. 4GiB - 4 bytes, or 4,294,967,292 bytes) of storage for its data, unless it is the last fixed-size variable and there are no record variables. When there are no record variables, the last fixed-size variable can be any size supported by the file system, e.g. terabytes.
A 64-bit offset format netCDF file can have up to 2^32 - 1 fixed sized variables, each under 4GiB in size. If there are no record variables in the file the last fixed variable can be any size.
No record variable can require more than 2^32 - 4 bytes of storage for each record's worth of data, unless it is the last record variable. A 64-bit offset format netCDF file can have up to 2^32 - 1 records, of up to 2^32 - 1 variables, as long as the size of one record's data for each record variable except the last is less than 4 GiB - 4.
Note also that all netCDF variables and records are padded to 4 byte boundaries.
There are important constraints on the structure of large netCDF classic files that result from the 32-bit relative offsets that are part of the netCDF classic file format:
The maximum size of a record in the classic format in versions 3.5.1 and earlier is 2^32 - 4 bytes, or about 4 GiB. In versions 3.6.0 and later, there is no such restriction on total record size for the classic format or 64-bit offset format.
The maximum size of a record in the classic format in versions 3.5.1 and earlier is 2^32 - 4 bytes, or about 4 GiB. In versions 3.6.0 and later, there is no such restriction on total record size for the classic format or 64-bit offset format.
If you don't use the unlimited dimension, only one variable can exceed 2 GiB in size, but it can be as large as the underlying file system permits. It must be the last variable in the dataset, and the offset to the beginning of this variable must be less than about 2 GiB.
The limit is really 2^31 - 4. If you were to specify a variable size of 2^31 -3, for example, it would be rounded up to the nearest multiple of 4 bytes, which would be 2^31, which is larger than the largest signed integer, 2^31 - 1.
For example, the structure of the data might be something like:
netcdf bigfile1 { dimensions: x=2000; y=5000; z=10000; variables: double x(x); // coordinate variables double y(y); double z(z); double var(x, y, z); // 800 Gbytes }
If you use the unlimited dimension, record variables may exceed 2 GiB in size, as long as the offset of the start of each record variable within a record is less than 2 GiB - 4. For example, the structure of the data in a 2.4 Tbyte file might be something like:
netcdf bigfile2 { dimensions: x=2000; y=5000; z=10; t=UNLIMITED; // 1000 records, for example variables: double x(x); // coordinate variables double y(y); double z(z); double t(t); // 3 record variables, 2400000000 bytes per record double var1(t, x, y, z); double var2(t, x, y, z); double var3(t, x, y, z); }
For netCDF classic and 64-bit offset files, an I/O layer implemented much like the C standard I/O (stdio) library is used by netCDF to read and write portable data to netCDF datasets. Hence an understanding of the standard I/O library provides answers to many questions about multiple processes accessing data concurrently, the use of I/O buffers, and the costs of opening and closing netCDF files. In particular, it is possible to have one process writing a netCDF dataset while other processes read it.
Data reads and writes are no more atomic than calls to stdio fread() and fwrite(). An nc_sync/NF_SYNC call is analogous to the fflush call in the C standard I/O library, writing unwritten buffered data so other processes can read it; The C function nc_sync (see nc_sync), or the Fortran function NF_SYNC (see NF_SYNC), also brings header changes up-to-date (for example, changes to attribute values). Opening the file with the NC_SHARE (in C) or the NF_SHARE (in Fortran) is analogous to setting a stdio stream to be unbuffered with the _IONBF flag to setvbuf.
As in the stdio library, flushes are also performed when "seeks" occur to a different area of the file. Hence the order of read and write operations can influence I/O performance significantly. Reading data in the same order in which it was written within each record will minimize buffer flushes.
You should not expect netCDF classic or 64-bit offset format data access to work with multiple writers having the same file open for writing simultaneously.
It is possible to tune an implementation of netCDF for some platforms by replacing the I/O layer with a different platform-specific I/O layer. This may change the similarities between netCDF and standard I/O, and hence characteristics related to data sharing, buffering, and the cost of I/O operations.
The distributed netCDF implementation is meant to be portable. Platform-specific ports that further optimize the implementation for better I/O performance are practical in some cases.
It should be noted that no UNICOS platform has been available at Unidata for netCDF testing for some years. The following information is left here for historical reasons.
As was mentioned in the previous section, it is possible to replace the I/O layer in order to increase I/O efficiency. This has been done for UNICOS, the operating system of Cray computers similar to the Cray Y-MP.
Additionally, it is possible for the user to obtain even greater I/O efficiency through appropriate setting of the NETCDF_FFIOSPEC environment variable. This variable specifies the Flexible File I/O buffers for netCDF I/O when executing under the UNICOS operating system (the variable is ignored on other operating systems). An appropriate specification can greatly increase the efficiency of netCDF I/O–to the extent that it can surpass default FORTRAN binary I/O. Possible specifications include the following:
bufa:336:2
cache:256:8
cachea:256:8:2
cachea:8:256:0
cache:8:256,cachea.sds:1024:4:1
All of the options/configurations supported in CRI's FFIO library are available through this mechanism. We recommend that you look at CRI's I/O optimization guide for information on using FFIO to its fullest. This mechanism is also compatible with CRI's EIE I/O library.
Tuning the NETCDF_FFIOSPEC variable to a program's I/O pattern can dramatically improve performance. Speedups of two orders of magnitude have been seen.
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
ncgen
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/software/netcdfsoftware.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.
char
byte
short
int
long
float
real
double
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 ushort type is an unsigned short type. It holds values between 0 and 65536.
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 uint type is an unsigned int type. It holds values between 0 and 4294967296.
The int64 is an 8-byte signed integer. It can hold values between -9223372036854775808 and 9223372036854775807.
The uint64 is an unsigned 8-byte integer type. It can hold values between 0 and 18446744073709551616.
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.
The string type holds variable length strings.
The bool type holds boolean values.
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 shorts and floats to distinguish them from ints 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 '\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
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] [-k kind] [-x] [input-file]
where:
-b
-o netcdf-file
-c
-f
-k kind
-x
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
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. Another use for ncdump is to determine what kind of netCDF file is used (which variant of the netCDF file format).
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] [-k] [input-file]
where:
-c
-h
-v var1,...
-b lang
-f lang
-l len
-p float_digits[,double_digits]
-n name
-k
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
The Unidata Program Center has developed a units library to convert between formatted and binary forms of units specifications and perform unit algebra on the binary form. Though the units library is self-contained and there is no dependency between it and the netCDF library, it is nevertheless useful in writing generic netCDF programs and we suggest you obtain it. The library and associated documentation is available from http://www.unidata.ucar.edu/software/udunits/.
The following are examples of units strings that can be interpreted by the utScan() function of the Unidata units library:
10 kilogram.meters/seconds2 10 kg-m/sec2 10 kg m/s^2 10 kilogram meter second-2 (PI radian)2 degF 100rpm geopotential meters 33 feet water milliseconds since 1992-12-31 12:34:0.1 -7:00
A unit is specified as an arbitrary product of constants and unit-names raised to arbitrary integral powers. Division is indicated by a slash '/'. Multiplication is indicated by white space, a period '.', or a hyphen '-'. Exponentiation is indicated by an integer suffix or by the exponentiation operators '^' and '**'. Parentheses may be used for grouping and disambiguation. The time stamp in the last example is handled as a special case.
Arbitrary Galilean transformations (i.e., y = ax + b) are allowed. In particular, temperature conversions are correctly handled. The specification:
degF 32
indicates a Fahrenheit scale with the origin shifted to thirty-two degrees Fahrenheit (i.e., to zero Celsius). Thus, the Celsius scale is equivalent to the following unit:
1.8 degF 32
Note that the origin-shift operation takes precedence over multiplication. In order of increasing precedence, the operations are division, multiplication, origin-shift, and exponentiation.
utScan() understands all the SI prefixes (e.g. "mega" and "milli") plus their abbreviations (e.g. "M" and "m")
The function utPrint() always encodes a unit specification one way. To reduce misunderstandings, it is recommended that this encoding style be used as the default. In general, a unit is encoded in terms of basic units, factors, and exponents. Basic units are separated by spaces, and any exponent directly appends its associated unit. The above examples would be encoded as follows:
10 kilogram meter second-2 9.8696044 radian2 0.555556 kelvin 255.372 10.471976 radian second-1 9.80665 meter2 second-2 98636.5 kilogram meter-1 second-2 0.001 seconds since 1992-12-31 19:34:0.1000 UTC
(Note that the Fahrenheit unit is encoded as a deviation, in fractional kelvins, from an origin at 255.372 kelvin, and that the time in the last example has been referenced to UTC.)
The database for the units library is a formatted file containing unit definitions and is used to initialize this package. It is the first place to look to discover the set of valid names and symbols.
The format for the units-file is documented internally and the file may be modified by the user as necessary. In particular, additional units and constants may be easily added (including variant spellings of existing units or constants).
utScan() is case-sensitive. If this causes difficulties, you might try making appropriate additional entries to the units-file.
Some unit abbreviations in the default units-file might seem counterintuitive. In particular, note the following:
For | Use | Not | Which Instead Means
|
Celsius | Celsius | C | coulomb
|
gram | gram | g | <standard free fall>
|
gallon | gallon | gal | <acceleration>
|
radian | radian | rad | <absorbed dose>
|
Newton | newton or N | nt | nit (unit of photometry)
|
For additional information on this units library, please consult the manual pages that come with its distribution.
Names commencing with underscore ('_') are reserved for use by the netCDF library. Most generic applications that process netCDF datasets assume standard attribute conventions and it is strongly recommended that these be followed unless there are good reasons for not doing so. Below we list the names and meanings of recommended standard attributes that have proven useful. Note that some of these (e.g. units, valid_range, scale_factor) assume numeric data and should not be used with character data.
units
long_name
valid_min
valid_max
valid_range
Generic applications should treat values outside the valid range as missing. The type of each valid_range, valid_min and valid_max attribute should match the type of its variable (except that for byte data, these can be of a signed integral type to specify the intended range).
If neither valid_min, valid_max nor valid_range is defined then
generic applications should define a valid range as follows. If the
data type is byte and _FillValue is not explicitly defined, then the
valid range should include all possible values. Otherwise, the valid
range should exclude the _FillValue (whether defined explicitly or by
default) as follows. If the _FillValue is positive then it defines a
valid maximum, otherwise it defines a valid minimum. For integer
types, there should be a difference of 1 between the _FillValue and
this valid minimum or maximum. For floating point types, the
difference should be twice the minimum possible (1 in the least
significant bit) to allow for rounding error.
scale_factor
add_offset
When scale_factor and add_offset are used for packing, the associated
variable (containing the packed data) is typically of type byte or
short, whereas the unpacked values are intended to be of type float or
double. The attributes scale_factor and add_offset should both be of
the type intended for the unpacked data, e.g. float or double.
_FillValue
Generic applications often need to write a value to represent undefined or missing values. The fill value provides an appropriate value for this purpose because it is normally outside the valid range and therefore treated as missing when read by generic applications. It is legal (but not recommended) for the fill value to be within the valid range.
For more information for C programmers see Fill Values. For more information for Fortran
programmers see Fill Values.
missing_value
signedness
C_format
FORTRAN_format
title
history
Conventions
For example, if a group named NUWG agrees upon a set of conventions for dimension names, variable names, required attributes, and netCDF representations for certain discipline-specific data structures, they may store a document describing the agreed-upon conventions in a dataset in the NUWG/ subdirectory of the Conventions directory. Datasets that followed these conventions would contain a global Conventions attribute with value "NUWG".
Later, if the group agrees upon some additional conventions for a specific subset of NUWG data, for example time series data, the description of the additional conventions might be stored in the NUWG/Time_series/ subdirectory, and datasets that adhered to these additional conventions would use the global Conventions attribute with value "NUWG/Time_series", implying that this dataset adheres to the NUWG conventions and also to the additional NUWG time-series conventions.
This appendix specifies the netCDF file format version 1 (netCDF classic format).
NetCDF 64-bit offset format differs from netCDF classic only in that the VERSION_BYTE = 2, and one of the internal offsets uses a 64-byte, instead of a 31-byte, offset into the file.
The classic format is first presented formally, using a BNF grammar notation. In the grammar, optional components are enclosed between braces ('[' and ']'). Comments follow '//' characters. Nonterminals are in lower case, and terminals are in upper case. A sequence of zero or more occurrences of an entity are denoted by '[entity ...]'.
netcdf_file := header data header := magic numrecs dim_array gatt_array var_array magic := 'C' 'D' 'F' VERSION_BYTE VERSION_BYTE := '\001' | // File format version number for // netCDF classic format. '\002' // File format version number for // 64-bit offset format. numrecs := NON_NEG dim_array := ABSENT | NC_DIMENSION nelems [dim ...] gatt_array := att_array // global attributes att_array := ABSENT | NC_ATTRIBUTE nelems [attr ...] var_array := ABSENT | NC_VARIABLE nelems [var ...] ABSENT := ZERO ZERO // Means array not present (equivalent to // nelems == 0). nelems := NON_NEG // number of elements in following sequence dim := name dim_length name := string dim_length := NON_NEG // If zero, this is the record dimension. // There can be at most one record dimension. attr := name nc_type nelems [values] nc_type := NC_BYTE | NC_CHAR | NC_SHORT | NC_INT | NC_FLOAT | NC_DOUBLE var := name nelems [dimid ...] vatt_array nc_type vsize begin // nelems is the rank (dimensionality) of the // variable; 0 for scalar, 1 for vector, 2 for // matrix, ... vatt_array := att_array // variable-specific attributes dimid := NON_NEG // Dimension ID (index into dim_array) for // variable shape. We say this is a "record // variable" if and only if the first // dimension is the record dimension. vsize := NON_NEG // Variable size. If not a record variable, // the amount of space, in bytes, allocated to // that variable's data. This number is the // product of the dimension lengths times the // size of the type, padded to a four byte // boundary. If a record variable, it is the // amount of space per record. The netCDF // "record size" is calculated as the sum of // the vsize's of the record variables. begin := OFFSET // Variable start location. The offset in // bytes (seek index) in the file of the // beginning of data for this variable. data := non_recs recs non_recs := [values ...] // Data for first non-record var, second // non-record var, ... recs := [rec ...] // First record, second record, ... rec := [values ...] // Data for first record variable for record // n, second record variable for record n, ... // See the note below for a special case. values := [bytes] | [chars] | [shorts] | [ints] | [floats] | [doubles] string := nelems [chars] bytes := [BYTE ...] padding chars := [CHAR ...] padding shorts := [SHORT ...] padding ints := [INT ...] floats := [FLOAT ...] doubles := [DOUBLE ...] padding := <0, 1, 2, or 3 bytes to next 4-byte boundary> // In header, padding is with 0 bytes. In // data, padding is with variable's fill-value. NON_NEG := <INT with non-negative value> OFFSET := <INT with non-negative value> | // for classic format or <INT64 with non-negative value> // for 64-bit offset format ZERO := <INT with zero value> BYTE := <8-bit byte> CHAR := <8-bit ACSII/ISO encoded character> SHORT := <16-bit signed integer, Bigendian, two's complement> INT := <32-bit signed integer, Bigendian, two's complement> INT64 := <64-bit signed integer, Bigendian, two's complement> FLOAT := <32-bit IEEE single-precision float, Bigendian> DOUBLE := <64-bit IEEE double-precision float, Bigendian> // tags are 32-bit INTs NC_BYTE := 1 // data is array of 8 bit signed integer NC_CHAR := 2 // data is array of characters, i.e., text NC_SHORT := 3 // data is array of 16 bit signed integer NC_INT := 4 // data is array of 32 bit signed integer NC_FLOAT := 5 // data is array of IEEE single precision float NC_DOUBLE := 6 // data is array of IEEE double precision float NC_DIMENSION := 10 NC_VARIABLE := 11 NC_ATTRIBUTE := 12
To calculate the offset (position within the file) of a specified data value (in a classic format data file), let external_sizeof be the external size in bytes of one data value of the appropriate type for the specified variable, nc_type:
NC_BYTE 1 NC_CHAR 1 NC_SHORT 2 NC_INT 4 NC_FLOAT 4 NC_DOUBLE 8
On a call to nc_open (or nc_enddef), scan through the array of variables, denoted var_array above, and sum the vsize fields of "record" variables to compute recsize.
Form the products of the dimension lengths for the variable from right to left, skipping the leftmost (record) dimension for record variables, and storing the results in a product array for each variable. For example:
Non-record variable:
dimension lengths: [ 5 3 2 7] product: [210 42 14 7]
Record variable:
dimension lengths: [0 2 9 4] product: [0 72 36 4]
At this point, the leftmost product, when rounded up to the next multiple of 4, is the variable size, vsize, in the grammar above. For example, in the non-record variable above, the value of the vsize field is 212 (210 rounded up to a multiple of 4). For the record variable, the value of vsize is just 72, since this is already a multiple of 4.
Let coord be an array of the coordinates of the desired data value, and offset be the desired result. Then offset is just the file offset of the first data value of the desired variable (its begin field) added to the inner product of the coord and product vectors times the size, in bytes, of each datum for the variable. Finally, if the variable is a record variable, the product of the record number, 'coord[0]', and the record size, recsize is added to yield the final offset value.
In pseudo-C code, here's the calculation of offset:
for (innerProduct = i = 0; i < var.rank; i++) innerProduct += product[i] * coord[i] offset = var.begin; offset += external_sizeof * innerProduct if(IS_RECVAR(var)) offset += coord[0] * recsize;
So, to get the data value (in external representation):
lseek(fd, offset, SEEK_SET); read(fd, buf, external_sizeof);
A special case: Where there is exactly one record variable, we drop the restriction that each record be four-byte aligned, so in this case there is no record padding.
By using the grammar above, we can derive the smallest valid netCDF file, having no dimensions, no variables, no attributes, and hence, no data. A CDL representation of the empty netCDF file is
netcdf empty { }
This empty netCDF file has 32 bytes, as you may verify by using 'ncgen -b empty.cdl' to generate it from the CDL representation. It begins with the four-byte "magic number" that identifies it as a netCDF version 1 file: 'C', 'D', 'F', '\001'. Following are seven 32-bit integer zeros representing the number of records, an empty array of dimensions, an empty array of global attributes, and an empty array of variables.
Below is an (edited) dump of the file produced on a big-endian machine using the Unix command
od -xcs empty.nc
Each 16-byte portion of the file is displayed with 4 lines. The first line displays the bytes in hexadecimal. The second line displays the bytes as characters. The third line displays each group of two bytes interpreted as a signed 16-bit integer. The fourth line (added by human) presents the interpretation of the bytes in terms of netCDF components and values.
4344 4601 0000 0000 0000 0000 0000 0000 C D F 001 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 17220 17921 00000 00000 00000 00000 00000 00000 [magic number ] [ 0 records ] [ 0 dimensions (ABSENT) ] 0000 0000 0000 0000 0000 0000 0000 0000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 00000 00000 00000 00000 00000 00000 00000 00000 [ 0 global atts (ABSENT) ] [ 0 variables (ABSENT) ]
As a slightly less trivial example, consider the CDL
netcdf tiny { dimensions: dim = 5; variables: short vx(dim); data: vx = 3, 1, 4, 1, 5 ; }
which corresponds to a 92-byte netCDF file. The following is an edited dump of this file:
4344 4601 0000 0000 0000 000a 0000 0001 C D F 001 \0 \0 \0 \0 \0 \0 \0 \n \0 \0 \0 001 17220 17921 00000 00000 00000 00010 00000 00001 [magic number ] [ 0 records ] [NC_DIMENSION ] [ 1 dimension ] 0000 0003 6469 6d00 0000 0005 0000 0000 \0 \0 \0 003 d i m \0 \0 \0 \0 005 \0 \0 \0 \0 00000 00003 25705 27904 00000 00005 00000 00000 [ 3 char name = "dim" ] [ size = 5 ] [ 0 global atts 0000 0000 0000 000b 0000 0001 0000 0002 \0 \0 \0 \0 \0 \0 \0 013 \0 \0 \0 001 \0 \0 \0 002 00000 00000 00000 00011 00000 00001 00000 00002 (ABSENT) ] [NC_VARIABLE ] [ 1 variable ] [ 2 char name = 7678 0000 0000 0001 0000 0000 0000 0000 v x \0 \0 \0 \0 \0 001 \0 \0 \0 \0 \0 \0 \0 \0 30328 00000 00000 00001 00000 00000 00000 00000 "vx" ] [1 dimension ] [ with ID 0 ] [ 0 attributes 0000 0000 0000 0003 0000 000c 0000 0050 \0 \0 \0 \0 \0 \0 \0 003 \0 \0 \0 \f \0 \0 \0 P 00000 00000 00000 00003 00000 00012 00000 00080 (ABSENT) ] [type NC_SHORT] [size 12 bytes] [offset: 80] 0003 0001 0004 0001 0005 8001 \0 003 \0 001 \0 004 \0 001 \0 005 200 001 00003 00001 00004 00001 00005 -32767 [ 3] [ 1] [ 4] [ 1] [ 5] [fill ]
_FillValue
: Attribute Conventions_IONBF flag
: The NetCDF-3 IO Layeradd_offset
: Attribute Conventionsbyte
: CDL Data TypesC_format
: Attribute Conventionschar
: CDL Data TypesConventions
: Attribute Conventionsdouble
: CDL Data Typesfloat
: CDL Data TypesFORTRAN_format
: Attribute Conventionshistory
: Attribute Conventionsint
: CDL Data Typeslong
: CDL Data Typeslong_name
: Attribute Conventionsmissing_value
: Attribute ConventionsNC_BYTE
: VariablesNC_CHAR
: VariablesNC_DOUBLE
: VariablesNC_FLOAT
: VariablesNC_INT
: VariablesNC_LONG
: VariablesNC_SHARE
: The NetCDF-3 IO LayerNC_SHORT
: Variablesnc_sync
: The NetCDF-3 IO LayerNETCDF_FFIOSPEC
: UNICOS Optimizationnf_byte
: Variablesnf_char
: Variablesnf_double
: Variablesnf_float
: Variablesnf_int1
: Variablesnf_int2
: Variablesnf_real
: VariablesNF_SHARE
: The NetCDF-3 IO Layernf_short
: VariablesNF_SYNC
: The NetCDF-3 IO Layerreal
: CDL Data Typesscale_factor
: Attribute Conventionsshort
: CDL Data Typessignedness
: Attribute Conventionstitle
: Attribute Conventionsunits
: Attribute Conventionsvalid_max
: Attribute Conventionsvalid_min
: Attribute Conventionsvalid_range
: Attribute Conventions