Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

NetCDF C Interface Guide


Next: , Previous: (dir), Up: (dir)

The NetCDF C Interface Guide

This document describes the C interface to the netCDF library; it applies to netCDF version 4.0-beta2 and was last updated on 5 June 2008.

For a complete description of the netCDF format and utilities see The NetCDF Users Guide (The NetCDF Users Guide).

--- The Detailed Node Listing ---

Use of the NetCDF Library

Datasets

Groups

Dimensions

User Defined Data Types

Compound Types Introduction

Variables

Reading and Writing Character String Values

Attributes


Next: , Previous: Top, Up: Top

1 Use of the NetCDF Library

You can use the netCDF library without knowing about all of the netCDF interface. If you are creating a netCDF dataset, only a handful of routines are required to define the necessary dimensions, variables, and attributes, and to write the data to the netCDF dataset. (Even less are needed if you use the ncgen utility to create the dataset before running a program using netCDF library calls to write data.) Similarly, if you are writing software to access data stored in a particular netCDF object, only a small subset of the netCDF library is required to open the netCDF dataset and access the data. Authors of generic applications that access arbitrary netCDF datasets need to be familiar with more of the netCDF library.

In this chapter we provide templates of common sequences of netCDF calls needed for common uses. For clarity we present only the names of routines; omit declarations and error checking; omit the type-specific suffixes of routine names for variables and attributes; indent statements that are typically invoked multiple times; and use ... to represent arbitrary sequences of other statements. Full parameter lists are described in later chapters.


Next: , Previous: Use of the NetCDF Library, Up: Use of the NetCDF Library

1.1 Creating a NetCDF Dataset

Here is a typical sequence of netCDF calls used to create a new netCDF dataset:

         nc_create           /* create netCDF dataset: enter define mode */
              ...
            nc_def_dim       /* define dimensions: from name and length */
              ...
            nc_def_var       /* define variables: from name, type, ... */
              ...
            nc_put_att       /* put attribute: assign attribute values */
              ...
         nc_enddef           /* end definitions: leave define mode */
              ...
            nc_put_var       /* provide values for variables */
              ...
         nc_close            /* close: save new netCDF dataset */

Only one call is needed to create a netCDF dataset, at which point you will be in the first of two netCDF modes. When accessing an open netCDF dataset, it is either in define mode or data mode. In define mode, you can create dimensions, variables, and new attributes, but you cannot read or write variable data. In data mode, you can access data and change existing attributes, but you are not permitted to create new dimensions, variables, or attributes.

One call to nc_def_dim is needed for each dimension created. Similarly, one call to nc_def_var is needed for each variable creation, and one call to a member of the nc_put_att family is needed for each attribute defined and assigned a value. To leave define mode and enter data mode, call nc_enddef.

Once in data mode, you can add new data to variables, change old values, and change values of existing attributes (so long as the attribute changes do not require more storage space). Single values may be written to a netCDF variable with one of the members of the nc_put_var1 family, depending on what type of data you have to write. All the values of a variable may be written at once with one of the members of the nc_put_var family. Arrays or array cross-sections of a variable may be written using members of the nc_put_vara family. Subsampled array sections may be written using members of the nc_put_vars family. Mapped array sections may be written using members of the nc_put_varm family. (Subsampled and mapped access are general forms of data access that are explained later.)

Finally, you should explicitly close all netCDF datasets that have been opened for writing by calling nc_close. By default, access to the file system is buffered by the netCDF library. If a program terminates abnormally with netCDF datasets open for writing, your most recent modifications may be lost. This default buffering of data is disabled by setting the NC_SHARE flag when opening the dataset. But even if this flag is set, changes to attribute values or changes made in define mode are not written out until nc_sync or nc_close is called.


Next: , Previous: Creating, Up: Use of the NetCDF Library

1.2 Reading a NetCDF Dataset with Known Names

Here we consider the case where you know the names of not only the netCDF datasets, but also the names of their dimensions, variables, and attributes. (Otherwise you would have to do "inquire" calls.) The order of typical C calls to read data from those variables in a netCDF dataset is:

         nc_open                /* open existing netCDF dataset */
              ...
            nc_inq_dimid        /* get dimension IDs */
              ...
            nc_inq_varid        /* get variable IDs */
              ...
            nc_get_att          /* get attribute values */
              ...
            nc_get_var          /* get values of variables */
              ...
         nc_close               /* close netCDF dataset */

First, a single call opens the netCDF dataset, given the dataset name, and returns a netCDF ID that is used to refer to the open netCDF dataset in all subsequent calls.

Next, a call to nc_inq_dimid for each dimension of interest gets the dimension ID from the dimension name. Similarly, each required variable ID is determined from its name by a call to nc_inq_varid Once variable IDs are known, variable attribute values can be retrieved using the netCDF ID, the variable ID, and the desired attribute name as input to a member of the nc_get_att family (typically nc_get_att_text or nc_get_att_double) for each desired attribute. Variable data values can be directly accessed from the netCDF dataset with calls to members of the nc_get_var1 family for single values, the nc_get_var family for entire variables, or various other members of the nc_get_vara, nc_get_vars, or nc_get_varm families for array, subsampled or mapped access.

Finally, the netCDF dataset is closed with nc_close. There is no need to close a dataset open only for reading.


Next: , Previous: Reading Known, Up: Use of the NetCDF Library

1.3 Reading a netCDF Dataset with Unknown Names

It is possible to write programs (e.g., generic software) which do such things as processing every variable, without needing to know in advance the names of these variables. Similarly, the names of dimensions and attributes may be unknown.

Names and other information about netCDF objects may be obtained from netCDF datasets by calling inquire functions. These return information about a whole netCDF dataset, a dimension, a variable, or an attribute. The following template illustrates how they are used:

         nc_open                   /* open existing netCDF dataset */
           ...
         nc_inq                    /* find out what is in it */
              ...
            nc_inq_dim             /* get dimension names, lengths */
              ...
            nc_inq_var             /* get variable names, types, shapes */
                 ...
               nc_inq_attname      /* get attribute names */
                 ...
               nc_inq_att          /* get attribute types and lengths */
                 ...
               nc_get_att          /* get attribute values */
                 ...
            nc_get_var             /* get values of variables */
              ...
         nc_close                  /* close netCDF dataset */

As in the previous example, a single call opens the existing netCDF dataset, returning a netCDF ID. This netCDF ID is given to the nc_inq routine, which returns the number of dimensions, the number of variables, the number of global attributes, and the ID of the unlimited dimension, if there is one.

All the inquire functions are inexpensive to use and require no I/O, since the information they provide is stored in memory when a netCDF dataset is first opened.

Dimension IDs use consecutive integers, beginning at 0. Also dimensions, once created, cannot be deleted. Therefore, knowing the number of dimension IDs in a netCDF dataset means knowing all the dimension IDs: they are the integers 0, 1, 2, ...up to the number of dimensions. For each dimension ID, a call to the inquire function nc_inq_dim returns the dimension name and length.

Variable IDs are also assigned from consecutive integers 0, 1, 2, ... up to the number of variables. These can be used in nc_inq_var calls to find out the names, types, shapes, and the number of attributes assigned to each variable.

Once the number of attributes for a variable is known, successive calls to nc_inq_attname return the name for each attribute given the netCDF ID, variable ID, and attribute number. Armed with the attribute name, a call to nc_inq_att returns its type and length. Given the type and length, you can allocate enough space to hold the attribute values. Then a call to a member of the nc_get_att family returns the attribute values.

Once the IDs and shapes of netCDF variables are known, data values can be accessed by calling a member of the nc_get_var1 family for single values, or members of the nc_get_var, nc_get_vara, nc_get_vars, or nc_get_varm for various kinds of array access.


Next: , Previous: Reading Unknown, Up: Use of the NetCDF Library

1.4 Adding New Dimensions, Variables, Attributes

An existing netCDF dataset can be extensively altered. New dimensions, variables, and attributes can be added or existing ones renamed, and existing attributes can be deleted. Existing dimensions, variables, and attributes can be renamed. The following code template lists a typical sequence of calls to add new netCDF components to an existing dataset:

         nc_open             /* open existing netCDF dataset */
           ...
         nc_redef            /* put it into define mode */
             ...
           nc_def_dim        /* define additional dimensions (if any) */
             ...
           nc_def_var        /* define additional variables (if any) */
             ...
           nc_put_att        /* define additional attributes (if any) */
             ...
         nc_enddef           /* check definitions, leave define mode */
             ...
           nc_put_var        /* provide values for new variables */
             ...
         nc_close            /* close netCDF dataset */

A netCDF dataset is first opened by the nc_open call. This call puts the open dataset in data mode, which means existing data values can be accessed and changed, existing attributes can be changed (so long as they do not grow), but nothing can be added. To add new netCDF dimensions, variables, or attributes you must enter define mode, by calling nc_redef. In define mode, call nc_def_dim to define new dimensions, nc_def_var to define new variables, and a member of the nc_put_att family to assign new attributes to variables or enlarge old attributes.

You can leave define mode and reenter data mode, checking all the new definitions for consistency and committing the changes to disk, by calling nc_enddef. If you do not wish to reenter data mode, just call nc_close, which will have the effect of first calling nc_enddef.

Until the nc_enddef call, you may back out of all the redefinitions made in define mode and restore the previous state of the netCDF dataset by calling nc_abort. You may also use the nc_abort call to restore the netCDF dataset to a consistent state if the call to nc_enddef fails. If you have called nc_close from definition mode and the implied call to nc_enddef fails, nc_abort will automatically be called to close the netCDF dataset and leave it in its previous consistent state (before you entered define mode).

For netCDF-4/HDF5 format files, define mode is still important, but the user does not have to called nc_enddef - it is called automatically when needed. It may also be called by the user.

In netCDF-4/HDF5 files, there are some settings which can only be modified during the very first define mode of the file. For example the compression level of a variable may be set only after the nc_def_var call and before the next nc_enddef call, whether it is called by the user explicitly, or when the user tries to read of write some data.

At most one process should have a netCDF dataset open for writing at one time. The library is designed to provide limited support for multiple concurrent readers with one writer, via disciplined use of the nc_sync function and the NC_SHARE flag. If a writer makes changes in define mode, such as the addition of new variables, dimensions, or attributes, some means external to the library is necessary to prevent readers from making concurrent accesses and to inform readers to call nc_sync before the next access.


Next: , Previous: Adding, Up: Use of the NetCDF Library

1.5 Error Handling

The netCDF library provides the facilities needed to handle errors in a flexible way. Each netCDF function returns an integer status value. If the returned status value indicates an error, you may handle it in any way desired, from printing an associated error message and exiting to ignoring the error indication and proceeding (not recommended!). For simplicity, the examples in this guide check the error status and call a separate function, handle_err(), to handle any errors. One possible definition of handle_err() can be found within the documentation of nc_strerror (see nc_strerror).

The nc_strerror function is available to convert a returned integer error status into an error message string.

Occasionally, low-level I/O errors may occur in a layer below the netCDF library. For example, if a write operation causes you to exceed disk quotas or to attempt to write to a device that is no longer available, you may get an error from a layer below the netCDF library, but the resulting write error will still be reflected in the returned status value.


Previous: Errors, Up: Use of the NetCDF Library

1.6 Compiling and Linking with the NetCDF Library

Details of how to compile and link a program that uses the netCDF C or FORTRAN interfaces differ, depending on the operating system, the available compilers, and where the netCDF library and include files are installed. Nevertheless, we provide here examples of how to compile and link a program that uses the netCDF library on a Unix platform, so that you can adjust these examples to fit your installation.

Every C file that references netCDF functions or constants must contain an appropriate #include statement before the first such reference:

     #include <netcdf.h>

Unless the netcdf.h file is installed in a standard directory where the C compiler always looks, you must use the -I option when invoking the compiler, to specify a directory where netcdf.h is installed, for example:

     cc -c -I/usr/local/netcdf/include myprogram.c

Alternatively, you could specify an absolute path name in the #include statement, but then your program would not compile on another platform where netCDF is installed in a different location.

Unless the netCDF library is installed in a standard directory where the linker always looks, you must use the -L and -l options to link an object file that uses the netCDF library. For example:

     cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf

Alternatively, you could specify an absolute path name for the library:

     cc -o myprogram myprogram.o -l/usr/local/netcdf/lib/libnetcdf.a


Next: , Previous: Use of the NetCDF Library, Up: Top

2 Datasets

This chapter presents the interfaces of the netCDF functions that deal with a netCDF dataset or the whole netCDF library.

A netCDF dataset that has not yet been opened can only be referred to by its dataset name. Once a netCDF dataset is opened, it is referred to by a netCDF ID, which is a small non-negative integer returned when you create or open the dataset. A netCDF ID is much like a file descriptor in C or a logical unit number in FORTRAN. In any single program, the netCDF IDs of distinct open netCDF datasets are distinct. A single netCDF dataset may be opened multiple times and will then have multiple distinct netCDF IDs; however at most one of the open instances of a single netCDF dataset should permit writing. When an open netCDF dataset is closed, the ID is no longer associated with a netCDF dataset.

Functions that deal with the netCDF library include:

The operations supported on a netCDF dataset as a single object are:


Next: , Previous: Datasets, Up: Datasets

2.1 NetCDF Library Interface Descriptions

Each interface description for a particular netCDF function in this and later chapters contains:

The examples follow a simple convention for error handling, always checking the error status returned from each netCDF function call and calling a handle_error function in case an error was detected. For an example of such a function, see nc_strerror.


Next: , Previous: Interface Descriptions, Up: Datasets

2.2 Parallel Access for NetCDF Files

For netCDF-4 files only, parallel read/write access is possible on systems which support it, and only if parallel HDF5 was installed on the system before netCDF, and only if the HDF5 parallel compiler was used during the netCDF configure. (Parallel HDF5 requires the MPI library).

To use parallel access, open or create the file with nc_open_par (see nc_open_par) or nc_create_par (see nc_create_par). Only netCDF-4 files can be opened or created for parallel access.

The following example shows the creation of a file using parallel access, and how a program might write data to such a file.

     #include "netcdf.h"
     #include <mpi.h>
     #include <assert.h>
     #include "hdf5.h"
     #include <string.h>
     #include <stdlib.h>
     
     #define BAIL(e) do { \
     printf("Bailing out in file %s, line %d, error:%s.\n", __FILE__, __LINE__, nc_strerror(e)); \
     return e; \
     } while (0)
     
     #define FILE "test_par.nc"
     #define NDIMS 2
     #define DIMSIZE 24
     #define QTR_DATA (DIMSIZE*DIMSIZE/4)
     #define NUM_PROC 4
     
     int
     main(int argc, char **argv)
     {
         /* MPI stuff. */
         int mpi_namelen;
         char mpi_name[MPI_MAX_PROCESSOR_NAME];
         int mpi_size, mpi_rank;
         MPI_Comm comm = MPI_COMM_WORLD;
         MPI_Info info = MPI_INFO_NULL;
     
         /* Netcdf-4 stuff. */
         int ncid, v1id, dimids[NDIMS];
         size_t start[NDIMS], count[NDIMS];
     
         int data[DIMSIZE*DIMSIZE], j, i, res;
     
         /* Initialize MPI. */
         MPI_Init(&argc,&argv);
         MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
         MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
         MPI_Get_processor_name(mpi_name, &mpi_namelen);
         printf("mpi_name: %s size: %d rank: %d\n", mpi_name,
                mpi_size, mpi_rank);
     
         /* Create a parallel netcdf-4 file. */
         if ((res = nc_create_par(FILE, NC_NETCDF4|NC_MPIIO, comm,
                                  info, &ncid)))
             BAIL(res);
     
         /* Create two dimensions. */
         if ((res = nc_def_dim(ncid, "d1", DIMSIZE, dimids)))
            BAIL(res);
         if ((res = nc_def_dim(ncid, "d2", DIMSIZE, &dimids[1])))
            BAIL(res);
     
         /* Create one var. */
         if ((res = nc_def_var(ncid, "v1", NC_INT, NDIMS, dimids, &v1id)))
            BAIL(res);
     
         if ((res = nc_enddef(ncid)))
            BAIL(res);
     
         /* Set up slab for this process. */
         start[0] = mpi_rank * DIMSIZE/mpi_size;
         start[1] = 0;
         count[0] = DIMSIZE/mpi_size;
         count[1] = DIMSIZE;
         printf("mpi_rank=%d start[0]=%d start[1]=%d count[0]=%d count[1]=%d\n",
                mpi_rank, start[0], start[1], count[0], count[1]);
     
         /* Create phony data. We're going to write a 24x24 array of ints,
            in 4 sets of 144. */
         printf("mpi_rank*QTR_DATA=%d (mpi_rank+1)*QTR_DATA-1=%d\n",
                mpi_rank*QTR_DATA, (mpi_rank+1)*QTR_DATA);
         for (i=mpi_rank*QTR_DATA; i<(mpi_rank+1)*QTR_DATA; i++)
            data[i] = mpi_rank;
     
         /*if ((res = nc_var_par_access(ncid, v1id, NC_COLLECTIVE)))
           BAIL(res);*/
         if ((res = nc_var_par_access(ncid, v1id, NC_INDEPENDENT)))
            BAIL(res);
     
         /* Write slabs of phony data. */
         if ((res = nc_put_vara_int(ncid, v1id, start, count,
                                    &data[mpi_rank*QTR_DATA])))
            BAIL(res);
     
         /* Close the netcdf file. */
         if ((res = nc_close(ncid)))
             BAIL(res);
     
         /* Shut down MPI. */
         MPI_Finalize();
     
         return 0;
     }
     


Next: , Previous: parallel access, Up: Datasets

2.3 Get error message corresponding to error status: nc_strerror

The function nc_strerror returns a static reference to an error message string corresponding to an integer netCDF error status or to a system error number, presumably returned by a previous call to some other netCDF function. The list of netCDF error status codes is available in the appropriate include file for each language binding.

Usage

     const char * nc_strerror(int ncerr);
ncerr
An error status that might have been returned from a previous call to some netCDF function.

Errors

If you provide an invalid integer error status that does not correspond to any netCDF error message or or to any system error message (as understood by the system strerror function), nc_strerror returns a string indicating that there is no such error status.

Example

Here is an example of a simple error handling function that uses nc_strerror to print the error message corresponding to the netCDF error status returned from any netCDF function call and then exit:

     #include <netcdf.h>
        ...
     void handle_error(int status) {
     if (status != NC_NOERR) {
        fprintf(stderr, "%s\n", nc_strerror(status));
        exit(-1);
        }
     }


Next: , Previous: nc_strerror, Up: Datasets

2.4 Get netCDF library version: nc_inq_libvers

The function nc_inq_libvers returns a string identifying the version of the netCDF library, and when it was built.

Usage

     const char * nc_inq_libvers(void);

Errors

This function takes no arguments, and thus no errors are possible in its invocation.

Example

Here is an example using nc_inq_libvers to print the version of the netCDF library with which the program is linked:

     #include <netcdf.h>
        ...
        printf("%s\n", nc_inq_libvers());


Next: , Previous: nc_inq_libvers, Up: Datasets

2.5 Create a NetCDF Dataset: nc_create

This function creates a new netCDF dataset, returning a netCDF ID that can subsequently be used to refer to the netCDF dataset in other netCDF function calls. The new netCDF dataset opened for write access and placed in define mode, ready for you to add dimensions, variables, and attributes.

A creation mode flag specifies:

Usage

NOTE: When creating a netCDF-4 file HDF5 error reporting is turned off, if it is on. This doesn't stop the HDF5 error stack from recording the errors, it simply stops their display to the user through stderr.

     int nc_create (const char* path, int cmode, int *ncidp);
path
The file name of the new netCDF dataset.
cmode
The creation mode flag. The following flags are available: NC_NOCLOBBER, NC_SHARE, NC_64BIT_OFFSET, NC_NETCDF4.

Setting NC_NOCLOBBER means you do not want to clobber (overwrite) an existing dataset; an error (NC_EEXIST) is returned if the specified dataset already exists.

The NC_SHARE flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimized for sequential access, programs that do not access data sequentially may see some performance improvement by setting the NC_SHARE flag.

Setting NC_64BIT_OFFSET causes netCDF to create a 64-bit offset format file, instead of a netCDF classic format file. The 64-bit offset format imposes far fewer restrictions on very large (i.e. over 2 GB) data files. See Large File Support (The NetCDF Users Guide).

A zero value (defined for convenience as NC_CLOBBER) specifies the default behavior: overwrite any existing dataset with the same file name and buffer and cache accesses for efficiency. The dataset will be in netCDF classic format. See NetCDF Classic Format Limitations (The NetCDF Users Guide).

Setting NC_NETCDF4 causes netCDF to create a HDF5/NetCDF-4 file.

ncidp
Pointer to location where returned netCDF ID is to be stored.

Errors

nc_create returns the value NC_NOERR if no errors occurred. Possible causes of errors include:

Return Codes

NC_NOERR
No error.
NC_ENOMEM
System out of memory.
NC_EHDFERR
HDF5 error (netCDF-4 files only).
NC_EFILEMETA
Error writing netCDF-4 file-level metadata in HDF5 file. (netCDF-4 files only).

Examples

In this example we create a netCDF dataset named foo.nc; we want the dataset to be created in the current directory only if a dataset with that name does not already exist:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
     if (status != NC_NOERR) handle_error(status);

In this example we create a netCDF dataset named foo_large.nc. It will be in the 64-bit offset format.

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo_large.nc", NC_NOCLOBBER|NC_64BIT_OFFSET, &ncid);
     if (status != NC_NOERR) handle_error(status);

In this example we create a netCDF dataset named foo_HDF5.nc. It will be in the HDF5 format.

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo_HDF5.nc", NC_NOCLOBBER|NC_NETCDF4, &ncid);
     if (status != NC_NOERR) handle_error(status);

In this example we create a netCDF dataset named foo_HDF5_classic.nc. It will be in the HDF5 format, but will not allow the use of any netCDF-4 advanced features. That is, it will conform to the classic netCDF-3 data model.

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo_HDF5_classic.nc", NC_NOCLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL, &ncid);
     if (status != NC_NOERR) handle_error(status);

A variant of nc_create, nc__create (note the double underscore) allows users to specify two tuning parameters for the file that it is creating. These tuning parameters are not written to the data file, they are only used for so long as the file remains open after an nc__create. See nc__create.


Next: , Previous: nc_create, Up: Datasets

2.6 Create a NetCDF Dataset With Performance Options: nc__create

This function is a variant of nc_create, nc__create (note the double underscore) allows users to specify two tuning parameters for the file that it is creating. These tuning parameters are not written to the data file, they are only used for so long as the file remains open after an nc__create.

This function creates a new netCDF dataset, returning a netCDF ID that can subsequently be used to refer to the netCDF dataset in other netCDF function calls. The new netCDF dataset opened for write access and placed in define mode, ready for you to add dimensions, variables, and attributes.

A creation mode flag specifies whether to overwrite any existing dataset with the same name and whether access to the dataset is shared, and whether this file should be in netCDF classic format (the default), or the new 64-bit offset format.

Usage

     int nc__create(const char *path, int cmode, size_t initialsz,
                    size_t *chunksizehintp, int *ncidp);
path
The file name of the new netCDF dataset.
cmode
The creation mode flag. The following flags are available: NC_NOCLOBBER, NC_SHARE, and NC_64BIT_OFFSET.

Setting NC_NOCLOBBER means you do not want to clobber (overwrite) an existing dataset; an error (NC_EEXIST) is returned if the specified dataset already exists.

The NC_SHARE flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimized for sequential access, programs that do not access data sequentially may see some performance improvement by setting the NC_SHARE flag.

Setting NC_64BIT_OFFSET causes netCDF to create a 64-bit offset format file, instead of a netCDF classic format file. The 64-bit offset format imposes far fewer restrictions on very large (i.e. over 2 GB) data files. See Large File Support (The NetCDF Users Guide).

A zero value (defined for convenience as NC_CLOBBER) specifies the default behavior: overwrite any existing dataset with the same file name and buffer and cache accesses for efficiency. The dataset will be in netCDF classic format. See NetCDF Classic Format Limitations (The NetCDF Users Guide).

initialsz
On some systems, and with custom I/O layers, it may be advantageous to set the size of the output file at creation time. This parameter sets the initial size of the file at creation time.
chunksizehintp
The argument referenced by chunksizehintp controls a space versus time tradeoff, memory allocated in the netcdf library versus number of system calls.

Because of internal requirements, the value may not be set to exactly the value requested. The actual value chosen is returned by reference.

Using the value NC_SIZEHINT_DEFAULT causes the library to choose a default. How the system chooses the default depends on the system. On many systems, the "preferred I/O block size" is available from the stat() system call, struct stat member st_blksize. If this is available it is used. Lacking that, twice the system pagesize is used.

Lacking a call to discover the system pagesize, we just set default chunksize to 8192.

The chunksize is a property of a given open netcdf descriptor ncid, it is not a persistent property of the netcdf dataset.

ncidp
Pointer to location where returned netCDF ID is to be stored.

Errors

nc_create returns the value NC_NOERR if no errors occurred. Possible causes of errors include:

Examples

In this example we create a netCDF dataset named foo.nc; we want the dataset to be created in the current directory only if a dataset with that name does not already exist:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
     if (status != NC_NOERR) handle_error(status);

In this example we create a netCDF dataset named foo_large.nc; we want the dataset to be created in the current directory only if a dataset with that name does not already exist. We also specify that chunksize and initial size for the file.

     #include <netcdf.h>
        ...
     int status;
     int ncid;
     int intialsz = 2048;
     int *chunksize;
        ...
     *chunksize = 1024;
     status = nc__create("foo.nc", NC_NOCLOBBER, initialsz, chunksize, &ncid);
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc__create, Up: Datasets

2.7 Create a NetCDF Dataset With Performance Options: nc_create_par

This function is a variant of nc_create, nc_create_par allows users to open a file on a MPI/IO or MPI/Posix parallel file system.

The parallel parameters are not written to the data file, they are only used for so long as the file remains open after an nc_create_par.

This function creates a new netCDF dataset, returning a netCDF ID that can subsequently be used to refer to the netCDF dataset in other netCDF function calls. The new netCDF dataset opened for write access and placed in define mode, ready for you to add dimensions, variables, and attributes.

This function is only available for netCDF-4 files. The creation mode flag must include NC_NETCDF4.

When a netCDF-4 file is created for parallel access, collective operations are the default. To use independent access on a variable, See nc_var_par_access.

Usage

     int nc_create_par(const char *path, int cmode, MPI_Comm comm,
                       MPI_Info info, int ncidp);
path
The file name of the new netCDF dataset.
cmode
Either the NC_MPIIO or NC_MPIPOSIX flags must be present. The NC_NETCDF4 flag is also required.

Setting NC_NOCLOBBER means you do not want to clobber (overwrite) an existing dataset; an error (NC_EEXIST) is returned if the specified dataset already exists.

The NC_SHARE flag is ignored.

comm
The MPI_Comm object returned by the MPI layer.
info
The MPI_Info object returned by the MPI layer, if MPI/IO is being used, or 0 if MPI/Posix is being used.
ncidp
Pointer to location where returned netCDF ID is to be stored.

Return Codes

NC_NOERR
No error.
Passing a dataset name that includes a directory that does not exist.
Specifying a dataset name of a file that exists and also specifying NC_NOCLOBBER.
Specifying a meaningless value for the creation mode.
Attempting to create a netCDF dataset in a directory where you don't have permission to create files.

Examples

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     *chunksize = 1024;
     status = nc__create("foo.nc", NC_NOCLOBBER, initialsz, chunksize, &ncid);
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_create_par, Up: Datasets

2.8 Open a NetCDF Dataset for Access: nc_open

The function nc_open opens an existing netCDF dataset for access. It determines the underlying file format automatically. Use the same call to open a netCDF classic, 64-bit offset, or netCDF-4 file.

Usage

     int nc_open (const char *path, int omode, int *ncidp);
path
File name for netCDF dataset to be opened.
omode
A zero value (or NC_NOWRITE) specifies the default behavior: open the dataset with read-only access, buffering and caching accesses for efficiency

Otherwise, the open mode is NC_WRITE, NC_SHARE, or NC_WRITE|NC_SHARE. Setting the NC_WRITE flag opens the dataset with read-write access. ("Writing" means any kind of change to the dataset, including appending or changing data, adding or renaming dimensions, variables, and attributes, or deleting attributes.)

The NC_SHARE flag is only used for netCDF classic and 64-bit offset files. It is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimized for sequential access, programs that do not access data sequentially may see some performance improvement by setting the NC_SHARE flag.

It is not necessary to pass any information about the format of the file being opened. The file type will be detected automatically by the netCDF library.

ncidp
Pointer to location where returned netCDF ID is to be stored.

Errors

When opening a netCDF-4 file HDF5 error reporting is turned off, if it is on. This doesn't stop the HDF5 error stack from recording the errors, it simply stops their display to the user through stderr.

nc_open returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Return Codes

NC_NOERR
No error.
NC_NOMEM
Out of memory.
NC_EHDFERR
HDF5 error. (NetCDF-4 files only.)
NC_EDIMMETA
Error in netCDF-4 dimension metadata. (NetCDF-4 files only.)
NC_ENOCOMPOIND
(NetCDF-4 files only.)

Example

Here is an example using nc_open to open an existing netCDF dataset named foo.nc for read-only, non-shared access:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_open("foo.nc", 0, &ncid);
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_open, Up: Datasets

2.9 Open a NetCDF Dataset for Access with Performance Tuning: nc__open

A function opens a netCDF dataset for access with an additional performance tuning parameter.

Usage

     int nc__open(const char *path, int mode, size_t *chunksizehintp, int *ncidp);
path
File name for netCDF dataset to be opened.
omode
A zero value (or NC_NOWRITE) specifies the default behavior: open the dataset with read-only access, buffering and caching accesses for efficiency

Otherwise, the open mode is NC_WRITE, NC_SHARE, or NC_WRITE|NC_SHARE. Setting the NC_WRITE flag opens the dataset with read-write access. ("Writing" means any kind of change to the dataset, including appending or changing data, adding or renaming dimensions, variables, and attributes, or deleting attributes.) The NC_SHARE flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently; it means that dataset accesses are not buffered and caching is limited. Since the buffering scheme is optimized for sequential access, programs that do not access data sequentially may see some performance improvement by setting the NC_SHARE flag.

chunksizehintp
The argument referenced by chunksizehintp controls a space versus time tradeoff, memory allocated in the netcdf library versus number of system calls.

Because of internal requirements, the value may not be set to exactly the value requested. The actual value chosen is returned by reference.

Using the value NC_SIZEHINT_DEFAULT causes the library to choose a default. How the system chooses the default depends on the system. On many systems, the "preferred I/O block size" is available from the stat() system call, struct stat member st_blksize. If this is available it is used. Lacking that, twice the system pagesize is used.

Lacking a call to discover the system pagesize, we just set default chunksize to 8192.

The chunksize is a property of a given open netcdf descriptor ncid, it is not a persistent property of the netcdf dataset.

ncidp
Pointer to location where returned netCDF ID is to be stored.

Errors

nc__open returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc__open to open an existing netCDF dataset named foo.nc for read-only, non-shared access:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
     int *chunksize;
        ...
     *chunksize = 1024;
     status = nc_open("foo.nc", 0, chunksize, &ncid);
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc__open, Up: Datasets

2.10 Open a NetCDF Dataset for Parallel Access

This function opens a netCDF-4 dataset for parallel access.

This opens the file using either MPI-IO or MPI-POSIX. The file must be a netCDF-4 file. (That is, it must have been created using NC_NETCDF4 in the creation mode).

This function is only available if netCDF-4 was configured with the –use-parallel option before being built. Also HDF5 parallel must be installed (before netCDF-4 is installed.)

Before either HDF5 or netCDF-4 can be installed with support for parallel programming, and MPI layer must also be installed on the machine, and usually a parallel file system.

NetCDF-4 exposes the parallel access functionality of HDF5. For more information about what is required to install and use the parallel access functions, see the HDF5 web site.

When a netCDF-4 file is opened for parallel access, collective operations are the default. To use independent access on a variable, See nc_var_par_access.

Usage

     int nc_open_par(const char *path, int mode, MPI_Comm comm,
                     MPI_Info info, int *ncidp);
path
File name for netCDF dataset to be opened.
omode
Either the NC_MPIIO or NC_MPIPOSIX flags must be present.

The flag NC_WRITE opens the dataset with read-write access. ("Writing" means any kind of change to the dataset, including appending or changing data, adding or renaming dimensions, variables, and attributes, or deleting attributes.)

All other flags are ignored or not allowed. The NC_NETCDF4 flag is not required, as the file type is detected when the file is opened.

comm
MPI_Comm object returned by the MPI layer.
info
MPI_Info object returned by the MPI layer, or NULL if MPI-POSIX access is desired.
ncidp
Pointer to location where returned netCDF ID is to be stored.

Return Codes

NC_NOERR
No error.
NC_ENOTNC4
Not a netCDF-4 file.
The specified netCDF dataset does not exist.
A meaningless mode was specified.

Example

Here is an example using nc_open_par to open an existing netCDF dataset named foo.nc for read-only, non-shared, MPI/IO access:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
     int *chunksize;
        ...
     


Next: , Previous: nc_open_par, Up: Datasets

2.11 Put Open NetCDF Dataset into Define Mode: nc_redef

The function nc_redef puts an open netCDF dataset into define mode, so dimensions, variables, and attributes can be added or renamed and attributes can be deleted.

Usage

For netCDF-4 files (i.e. files created with NC_NETCDF4 in the cmode, see nc_create), it is not necessary to call nc_redef unless the file was also created with NC_STRICT_NC3. For straight-up netCDF-4 files, nc_redef is called automatically, as needed.

For all netCDF-4 files, the root ncid must be used. This is the ncid returned by nc_open and nc_create, and points to the root of the hierarchy tree for netCDF-4 files.

     int nc_redef(int ncid);
ncid
netCDF ID, from a previous call to nc_open or nc_create.

Errors

nc_redef returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Errors

NC_NOERR
No error.
NC_EBADID
Bad ncid.
NC_EBADGRPID
The ncid must refer to the root group of the file, that is, the group returned by nc_open or nc_create. (see nc_open see nc_create).
NC_EINDEFINE
Already in define mode.
NC_EPERM
File is read-only.

Example

Here is an example using nc_redef to open an existing netCDF dataset named foo.nc and put it into define mode:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);  /* open dataset */
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_redef(ncid);                      /* put in define mode */
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_redef, Up: Datasets

2.12 Leave Define Mode: nc_enddef

The function nc_enddef takes an open netCDF dataset out of define mode. The changes made to the netCDF dataset while it was in define mode are checked and committed to disk if no problems occurred. Non-record variables may be initialized to a "fill value" as well. See nc_set_fill. The netCDF dataset is then placed in data mode, so variable data can be read or written.

It's not necessary to call nc_enddef for netCDF-4 files. With netCDF-4 files, nc_enddef is called when needed by the netcdf-4 library. User calls to nc_enddef for netCDF-4 files still flush the metadata to disk.

This call may involve copying data under some circumstances. For a more extensive discussion see File Structure and Performance (The NetCDF Users Guide).

For netCDF-4/HDF5 format files there are some variable settings (the compression, endianness, fletcher32 error correction, and fill value) which must be set (if they are going to be set at all) between the nc_def_var and the next nc_enddef. Once the nc_enddef is called, these settings can no longer be changed for a variable.

Usage

     int nc_enddef(int ncid);
ncid
NetCDF ID, from a previous call to nc_open or nc_create. If you use a group id, the enddef will apply to the entire file. That all, the enddef will not just end define mode in one group, but in the entire file.

Errors

nc_enddef returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_enddef to finish the definitions of a new netCDF dataset named foo.nc and put it into data mode:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
     if (status != NC_NOERR) handle_error(status);
     
        ...       /* create dimensions, variables, attributes */
     
     status = nc_enddef(ncid);  /*leave define mode*/
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_enddef, Up: Datasets

2.13 Leave Define Mode with Performance Tuning: nc__enddef

The function nc__enddef takes an open netCDF dataset out of define mode. The changes made to the netCDF dataset while it was in define mode are checked and committed to disk if no problems occurred. Non-record variables may be initialized to a "fill value" as well. See nc_set_fill. The netCDF dataset is then placed in data mode, so variable data can be read or written.

This call may involve copying data under some circumstances. For a more extensive discussion see File Structure and Performance (The NetCDF Users Guide).

Caution: this function exposes internals of the netcdf version 1 file format. Users should use nc_enddef in most circumstances. This function may not be available on future netcdf implementations.

The current netcdf file format has three sections, the "header" section, the data section for fixed size variables, and the data section for variables which have an unlimited dimension (record variables).

The header begins at the beginning of the file. The index (offset) of the beginning of the other two sections is contained in the header. Typically, there is no space between the sections. This causes copying overhead to accrue if one wishes to change the size of the sections, as may happen when changing names of things, text attribute values, adding attributes or adding variables. Also, for buffered i/o, there may be advantages to aligning sections in certain ways.

The minfree parameters allow one to control costs of future calls to nc_redef, nc_enddef by requesting that minfree bytes be available at the end of the section.

The align parameters allow one to set the alignment of the beginning of the corresponding sections. The beginning of the section is rounded up to an index which is a multiple of the align parameter. The flag value ALIGN_CHUNK tells the library to use the chunksize (see above) as the align parameter.

The file format requires mod 4 alignment, so the align parameters are silently rounded up to multiples of 4. The usual call,

     nc_enddef(ncid);

is equivalent to

     nc__enddef(ncid, 0, 4, 0, 4);

The file format does not contain a "record size" value, this is calculated from the sizes of the record variables. This unfortunate fact prevents us from providing minfree and alignment control of the "records" in a netcdf file. If you add a variable which has an unlimited dimension, the third section will always be copied with the new variable added.

Usage

     int nc__enddef(int ncid, size_t h_minfree, size_t v_align,
                    size_t v_minfree, size_t r_align);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.
h_minfree
Sets the pad at the end of the "header" section.
v_align
Controls the alignment of the beginning of the data section for fixed size variables.
v_minfree
Sets the pad at the end of the data section for fixed size variables.
r_align
Controls the alignment of the beginning of the data section for variables which have an unlimited dimension (record variables).

Errors

nc__enddef returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_enddef to finish the definitions of a new netCDF dataset named foo.nc and put it into data mode:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
     if (status != NC_NOERR) handle_error(status);
     
        ...       /* create dimensions, variables, attributes */
     
     status = nc_enddef(ncid);  /*leave define mode*/
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc__enddef, Up: Datasets

2.14 Close an Open NetCDF Dataset: nc_close

The function nc_close closes an open netCDF dataset.

If the dataset in define mode, nc_enddef will be called before closing. (In this case, if nc_enddef returns an error, nc_abort will automatically be called to restore the dataset to the consistent state before define mode was last entered.) After an open netCDF dataset is closed, its netCDF ID may be reassigned to the next netCDF dataset that is opened or created.

Usage

For netCDF-4 files, the ncid of the root group must be passed into nc_close.

     int nc_close(int ncid);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.

Errors

nc_close returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

NC_NOERR
No error.
NC_EBADID
Invalid id passed.
NC_EBADGRPID
ncid did not contain the root group id of this file. (NetCDF-4 only).

Example

Here is an example using nc_close to finish the definitions of a new netCDF dataset named foo.nc and release its netCDF ID:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_create("foo.nc", NC_NOCLOBBER, &ncid);
     if (status != NC_NOERR) handle_error(status);
     
        ...       /* create dimensions, variables, attributes */
     
     status = nc_close(ncid);       /* close netCDF dataset */
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_close, Up: Datasets

2.15 Inquire about an Open NetCDF Dataset: nc_inq Family

Members of the nc_inq family of functions return information about an open netCDF dataset, given its netCDF ID. Dataset inquire functions may be called from either define mode or data mode. The first function, nc_inq, returns values for the number of dimensions, the number of variables, the number of global attributes, and the dimension ID of the dimension defined with unlimited length, if any. The other functions in the family each return just one of these items of information.

For C, these functions include nc_inq, nc_inq_ndims, nc_inq_nvars, nc_inq_natts, and nc_inq_unlimdim. An additional function, nc_inq_format, returns the (rarely needed) format version.

No I/O is performed when these functions are called, since the required information is available in memory for each open netCDF dataset.

Usage

     int nc_inq          (int ncid, int *ndimsp, int *nvarsp, int *ngattsp,
                          int *unlimdimidp);
     int nc_inq_ndims    (int ncid, int *ndimsp);
     int nc_inq_nvars    (int ncid, int *nvarsp);
     int nc_inq_natts    (int ncid, int *ngattsp);
     int nc_inq_unlimdim (int ncid, int *unlimdimidp);
     int nc_inq_format   (int ncid, int *formatp);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.
ndimsp
Pointer to location for returned number of dimensions defined for this netCDF dataset.
nvarsp
Pointer to location for returned number of variables defined for this netCDF dataset.
ngattsp
Pointer to location for returned number of global attributes defined for this netCDF dataset.
unlimdimidp
Pointer to location for returned ID of the unlimited dimension, if there is one for this netCDF dataset. If no unlimited length dimension has been defined, -1 is returned.
formatp
Pointer to location for returned format version, one of NC_FORMAT_CLASSIC, NC_FORMAT_64BIT, NC_FORMAT_NETCDF4, NC_FORMAT_NETCDF4_CLASSIC.

Errors

All members of the nc_inq family return the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_inq to find out about a netCDF dataset named foo.nc:

     #include <netcdf.h>
        ...
     int status, ncid, ndims, nvars, ngatts, unlimdimid;
        ...
     status = nc_open("foo.nc", NC_NOWRITE, &ncid);
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid);
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_inq Family, Up: Datasets

2.16 Synchronize an Open NetCDF Dataset to Disk: nc_sync

The function nc_sync offers a way to synchronize the disk copy of a netCDF dataset with in-memory buffers. There are two reasons you might want to synchronize after writes:

This function is backward-compatible with previous versions of the netCDF library. The intent was to allow sharing of a netCDF dataset among multiple readers and one writer, by having the writer call nc_sync after writing and the readers call nc_sync before each read. For a writer, this flushes buffers to disk. For a reader, it makes sure that the next read will be from disk rather than from previously cached buffers, so that the reader will see changes made by the writing process (e.g., the number of records written) without having to close and reopen the dataset. If you are only accessing a small amount of data, it can be expensive in computer resources to always synchronize to disk after every write, since you are giving up the benefits of buffering.

An easier way to accomplish sharing (and what is now recommended) is to have the writer and readers open the dataset with the NC_SHARE flag, and then it will not be necessary to call nc_sync at all. However, the nc_sync function still provides finer granularity than the NC_SHARE flag, if only a few netCDF accesses need to be synchronized among processes.

It is important to note that changes to the ancillary data, such as attribute values, are not propagated automatically by use of the NC_SHARE flag. Use of the nc_sync function is still required for this purpose.

Sharing datasets when the writer enters define mode to change the data schema requires extra care. In previous releases, after the writer left define mode, the readers were left looking at an old copy of the dataset, since the changes were made to a new copy. The only way readers could see the changes was by closing and reopening the dataset. Now the changes are made in place, but readers have no knowledge that their internal tables are now inconsistent with the new dataset schema. 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 before any subsequent access.

When calling nc_sync, the netCDF dataset must be in data mode. A netCDF dataset in define mode is synchronized to disk only when nc_enddef is called. A process that is reading a netCDF dataset that another process is writing may call nc_sync to get updated with the changes made to the data by the writing process (e.g., the number of records written), without having to close and reopen the dataset.

Data is automatically synchronized to disk when a netCDF dataset is closed, or whenever you leave define mode.

Usage

     int nc_sync(int ncid);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.

Errors

nc_sync returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_sync to synchronize the disk writes of a netCDF dataset named foo.nc:

     #include <netcdf.h>
        ...
     int status;
     int ncid;
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);  /* open for writing */
     if (status != NC_NOERR) handle_error(status);
     
        ...           /* write data or change attributes */
     
     status = nc_sync(ncid);      /* synchronize to disk */
     if (status != NC_NOERR) handle_error(status);


Next: , Previous: nc_sync, Up: Datasets

2.17 Back Out of Recent Definitions: nc_abort

You no longer need to call this function, since it is called automatically by nc_close in case the dataset is in define mode and something goes wrong with committing the changes. The function nc_abort just closes the netCDF dataset, if not in define mode. If the dataset is being created and is still in define mode, the dataset is deleted. If define mode was entered by a call to nc_redef, the netCDF dataset is restored to its state before definition mode was entered and the dataset is closed.

Usage

     int nc_abort(int ncid);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.

Errors

nc_abort returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_abort to back out of redefinitions of a dataset named foo.nc:

     #include <netcdf.h>
        ...
     int ncid, status, latid;
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);/* open for writing */
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_redef(ncid);                    /* enter define mode */
     if (status != NC_NOERR) handle_error(status);
        ...
     status = nc_def_dim(ncid, "lat", 18L, &latid);
     if (status != NC_NOERR) {
        handle_error(status);
        status = nc_abort(ncid);                 /* define failed, abort */
        if (status != NC_NOERR) handle_error(status);
     }


Next: , Previous: nc_abort, Up: Datasets

2.18 Set Fill Mode for Writes: nc_set_fill

This function is intended for advanced usage, to optimize writes under some circumstances described below. The function nc_set_fill sets the fill mode for a netCDF dataset open for writing and returns the current fill mode in a return parameter. The fill mode can be specified as either NC_FILL or NC_NOFILL. The default behavior corresponding to NC_FILL is that data is pre-filled with fill values, that is fill values are written when you create non-record variables or when you write a value beyond data that has not yet been written. This makes it possible to detect attempts to read data before it was written. For more information on the use of fill values see Fill Values. For information about how to define your own fill values see Attribute Conventions (NetCDF Users' Guide).

The behavior corresponding to NC_NOFILL overrides the default behavior of prefilling data with fill values. This can be used to enhance performance, because it avoids the duplicate writes that occur when the netCDF library writes fill values that are later overwritten with data.

A value indicating which mode the netCDF dataset was already in is returned. You can use this value to temporarily change the fill mode of an open netCDF dataset and then restore it to the previous mode.

After you turn on NC_NOFILL mode for an open netCDF dataset, you must be certain to write valid data in all the positions that will later be read. Note that nofill mode is only a transient property of a netCDF dataset open for writing: if you close and reopen the dataset, it will revert to the default behavior. You can also revert to the default behavior by calling nc_set_fill again to explicitly set the fill mode to NC_FILL.

There are three situations where it is advantageous to set nofill mode:

  1. Creating and initializing a netCDF dataset. In this case, you should set nofill mode before calling nc_enddef and then write completely all non-record variables and the initial records of all the record variables you want to initialize.
  2. Extending an existing record-oriented netCDF dataset. Set nofill mode after opening the dataset for writing, then append the additional records to the dataset completely, leaving no intervening unwritten records.
  3. Adding new variables that you are going to initialize to an existing netCDF dataset. Set nofill mode before calling nc_enddef then write all the new variables completely.

If the netCDF dataset has an unlimited dimension and the last record was written while in nofill mode, then the dataset may be shorter than if nofill mode was not set, but this will be completely transparent if you access the data only through the netCDF interfaces.

The use of this feature may not be available (or even needed) in future releases. Programmers are cautioned against heavy reliance upon this feature.

Usage

     int nc_set_fill (int ncid, int fillmode, int *old_modep);
ncid
NetCDF ID, from a previous call to nc_open or nc_create.
fillmode
Desired fill mode for the dataset, either NC_NOFILL or NC_FILL.
old_modep
Pointer to location for returned current fill mode of the dataset before this call, either NC_NOFILL or NC_FILL.

Errors

nc_set_fill returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_set_fill to set nofill mode for subsequent writes of a netCDF dataset named foo.nc:

     #include <netcdf.h>
        ...
     int ncid, status, old_fill_mode;
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);  /* open for writing */
     if (status != NC_NOERR) handle_error(status);
     
        ...           /* write data with default prefilling behavior */
     
     status = nc_set_fill(ncid, NC_NOFILL, &old_fill_mode); /* set nofill */
     if (status != NC_NOERR) handle_error(status);
     
        ...           /* write data with no prefilling */


Previous: nc_set_fill, Up: Datasets

2.19 Set Default Creation Format: nc_set_default_format

This function is intended for advanced users.

Starting in version 3.6, netCDF introduced a new data format, the first change in the underlying binary data format since the netCDF interface was released. The new format, 64-bit offset format, was introduced to greatly relax the limitations on creating very large files.

Users are warned that creating files in the 64-bit offset format makes them unreadable by the netCDF library prior to version 3.6.0. For reasons of compatibility, users should continue to create files in netCDF classic format.

Users who do want to use 64-bit offset format files can create them directory from nc_create, using the proper cmode flag. (see nc_create).

The function nc_set_default_format allows the user to change the format of the netCDF file to be created by future calls to nc_create (or nc__create) without changing the cmode flag.

This allows the user to convert a program to use 64-bit offset formation without changing all calls the nc_create. See Large File Support (The NetCDF Users Guide).

Once the default format is set, all future created files will be in the desired format.

Two constants are provided in the netcdf.h file to be used with this function, NC_FORMAT_64BIT and NC_FORMAT_CLASSIC.

If a non-NULL pointer is provided, it is assumed to point to an int, where the existing default format will be written.

Using nc_create with a cmode including NC_64BIT_OFFSET overrides the default format, and creates a 64-bit offset file.

Usage

     int nc_set_default_format(int format, int *old_formatp);
format
Either NC_FORMAT_CLASSIC (the default setting) or NC_FORMAT_64BIT.
old_formatp
Either NULL (in which case it will be ignored), or a pointer to an int where the existing default format (i.e. before being changed to the new format) will be written. This allows you to get the existing default format while setting a new default format.

Errors

nc_set_default_format returns the value NC_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using nc_set_default_format to create the same file in both formats with the same nc_create call:

     #include <netcdf.h>
        ...
     int ncid, status, old_fill_mode;
        ...
     status = nc_open("foo.nc", NC_WRITE, &ncid);  /* open for writing */
     if (status != NC_NOERR) handle_error(status);
     
        ...           /* write data with default prefilling behavior */
     
     status = nc_set_fill(ncid, NC_NOFILL, &old_fill_mode); /* set nofill */
     if (status != NC_NOERR) handle_error(status);
     
        ...           /* write data with no prefilling */


Next: , Previous: Datasets, Up: Top

3 Groups

NetCDF-4 added support for hierarchical groups within netCDF datasets.

Groups are identified with a ncid, which identifies both the open file, and the group within that file. When a file is opened with nc_open or nc_create, the ncid for the root group of that file is provided. Using that as a starting point, users can add new groups, or list and navigate existing groups.

All netCDF calls take a ncid which determines where the call will take its action. For example, the nc_def_var function takes a ncid as its first parameter. It will create a variable in whichever group its ncid refers to. Use the root ncid provided by nc_create or nc_open to create a variable in the root group. Or use nc_def_grp to create a group and use its ncid to define a variable in the new group.

Variable are only visible in the group in which they are defined. The same applies to attributes. “Global” attributes are associated with the group whose ncid is used.

Dimensions are visible in their groups, and all child groups.

Group operations are only permitted on netCDF-4 files - that is, files created with the HDF5 flag in nc_create. (see nc_create). Groups are not compatible with the netCDF classic data model, so files created with the NC_CLASSIC_MODEL file cannot contain groups (except the root group).


Next: , Previous: Groups, Up: Groups

3.1 Find a Group ID: nc_inq_ncid

Given an ncid and group name (NULL or "" gets root group), return ncid of the named group.

Usage

     int nc_inq_ncid(int ncid, char *name, int *grp_ncid);
ncid
The group id for this operation.
name
A char array that holds the name of the desired group.
grp_ncid
An int pointer that will receive the group id, if the group is found.

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example

        int root_ncid, child_ncid;
        char file[] = "nc4_test.nc";
     
        /* Open the file. */
        if ((res = nc_open(file, NC_NOWRITE, &root_ncid)))
           return res;
     
        /* Get the ncid of an existing group. */
        if ((res = nc_inq_ncid(root_ncid, "group1", &child_ncid)))
           return res;


Next: , Previous: nc_inq_ncid, Up: Groups

3.2 Get a List of Groups in a Group: nc_inq_grps

Given a location id, return the number of groups it contains, and an array of their ncids.

Usage

     int nc_inq_grps(int ncid, int *numgrps, int *ncids);
ncid
The group id for this operation.
numgrps
Pointer to an int which will get number of groups in this group. If NULL, it's ignored.
ncids
Pointer to a already allocated array of ints which will receive the ids of all the groups in this group. If NULL, it's ignored. Call this function with NULL for ncids parameter to find out how many groups there are.

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example

        int root_ncid, numgrps;
        int *ncids;
        char file[] = "nc4_test.nc";
     
        /* Open the file. */
        if ((res = nc_open(file, NC_NOWRITE, &root_ncid)))
           return res;
     
        /* Get a list of ncids for the root group. (That is, find out of
           there are any groups already defined. */
        if ((res = nc_inq_grps(root_ncid, &numgrps, NULL)))
           return res;
        ncids = malloc(sizeof(int) * numgrps);
        if ((res = nc_inq_grps(root_ncid, NULL, ncids)))
           return res;


Next: , Previous: nc_inq_grps, Up: Groups

3.3 Find all the Variables in a Group: nc_inq_varids

Find all varids for a location.

Usage

     int nc_inq_varids(int ncid, int *varids);
ncid
The group id for this operation.
varids
An already allocated array to store the list of varids. Ignored if NULL. Use nc_inq_nvars to find out how many variables there are. (see nc_inq Family).

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example

        int root_ncid, numvars;
        int *varids;
        char file[] = "nc4_test.nc";
     
        /* Open the file. */
        if ((res = nc_open(file, NC_NOWRITE, &root_ncid)))
           return res;
     
        /* Get a list of varids for the root group. (That is, find out of
           there are any groups already defined. */
        if ((res = nc_inq_nvars(root_ncid, &numvars)))
           return res;
        varids = malloc(sizeof(int) * numvars);
        if ((res = nc_inq_grps(root_ncid, NULL, varids)))
           return res;


Next: , Previous: nc_inq_varids, Up: Groups

3.4 Find all Dimensions Visible in a Group: nc_inq_dimids

Find all dimids for a location. This finds all dimensions in a group, or any of its parents.

Usage

     int nc_inq_dimids(int ncid, int *dimids);
ncid
The group id for this operation.
dimids
An already allocated array of ints when the dimids of the visible dimensions will be stashed. Use nc_inq_ndims to find out how many dims are visible from this group. (see nc_inq Family).

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example

        int root_ncid, numdims;
        int *dimids;
        char file[] = "nc4_test.nc";
     
        /* Open the file. */
        if ((res = nc_open(file, NC_NOWRITE, &root_ncid)))
           return res;
     
        /* Get a list of dimids for the root group. (That is, find out of
           there are any groups already defined. */
        if ((res = nc_inq_ndims(root_ncid, &numdims)))
           return res;
        dimids = malloc(sizeof(int) * numdims);
        if ((res = nc_inq_grps(root_ncid, NULL, dimids)))
           return res;


Next: , Previous: nc_inq_dimids, Up: Groups

3.5 Find the Length of a Group's Full Name: nc_inq_grpname_len

Given ncid, find len of name. (Root group is named "/", with length 1.)

Usage

     int nc_inq_grpname_len(int ncid, size_t *lenp);
ncid
The group id for this operation.
lenp
Pointer to an int where the length will be placed.

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example



Next: , Previous: nc_inq_grpname_len, Up: Groups

3.6 Find a Group's Name: nc_inq_grpname

Given ncid, find complete name of group. (Root group is named "", a full "path" for each group is provided in the name, with groups separated with a forward slash / as in Unix directory names. For example "group1/subgrp1/subsubgrp1")

Usage

     int nc_inq_grpname(int ncid, char *name);
ncid
The group id for this operation.
name
Pointer to allocated space of correct length. The name of the group will be copied there. To find the required length, call nc_inq_grpname_len (see nc_inq_grpname_len)..

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example



Next: , Previous: nc_inq_grpname, Up: Groups

3.7 Find a Group's Parent: nc_inq_grp_parent

Given ncid, find the ncid of the parent group.

When used with the root group, this function returns the NC_ENOGRP error (since the root group has no parent.)

Usage

     int nc_inq_grp_parent(int ncid, int *parent_ncid);
ncid
The group id.
parent_ncid
Pointer to an int. The ncid of the group will be copied there.

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENOGRP
No parent group found (i.e. this is the root group).
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.

Example

        if (nc_create(FILE_NAME, NC_NETCDF4, &ncid)) ERR;
        if (nc_def_grp(ncid, HENRY_VII, &henry_vii_id)) ERR;
     
        if (nc_inq_grp_parent(henry_vii_id, &parent_ncid)) ERR;
        if (parent_ncid != ncid) ERR;
        if (nc_close(ncid)) ERR;


Previous: nc_inq_grp_parent, Up: Groups

3.8 Create a New Group: nc_def_grp

Create a group. Its location id is returned in the new_ncid pointer.

Usage

     int nc_def_grp(int parent_ncid, char *name, int *new_ncid);
parent_ncid
The group id of the parent group.
name
The name of the new group.
new_ncid
A pointer to an int. The ncid of the new group will be placed there.

Errors

NC_NOERR
No error.
NC_EBADID
Bad group id.
NC_ENAMEINUSE
That name is in use. Group names must be unique within a group, and must not be the same as any variable or type in the group.
NC_EMAXNAME
Name exceed max length NC_MAX_NAME.
NC_EBADNAME
Name contains illegal characters.
NC_ENOTNC4
Attempting a netCDF-4 operation on a netCDF-3 file. NetCDF-4 operations can only be performed on files defined with a create mode which includes flag HDF5. (see nc_open).
NC_ESTRICTNC3
This file was created with the strict netcdf-3 flag, therefore netcdf-4 operations are not allowed. (see nc_open).
NC_EHDFERR
An error was reported by the HDF5 layer.
NC_EPERM
Attempt to write to a read-only file.
NC_ENOTINDEFINE
Not in define mode.

Example

           int root_ncid, a1_ncid;
           char grpname[] = "assimilation1";
     
           /* Get the ncid of the root group. */
           if ((res = nc_inq_ncid(root_ncid, NULL, &root_ncid)))
              return res;
     
           /* Create a group. */
           if ((res = nc_def_grp(root_ncid, grpname, &a1_ncid)))
              return res;


Next: , Previous: Groups, Up: Top

4 Dimensions


Next: , Previous: Dimensions, Up: Dimensions

4.1 Dimensions Introduction

Dimensions for a netCDF dataset are defined when it is created, while the netCDF dataset is in define mode. Additional dimensions may be added later by reentering define mode. A netCDF dimension has a name and a length. In a netCDF classic or 64-bit offset file, at most one dimension can have the unlimited length, which means variables using this dimension can grow along this dimension. In a netCDF-4 file multiple unlimited dimensions are supported.

There is a suggested limit (100) to the number of dimensions that can be defined in a single netCDF dataset. The limit is the value of the predefined macro NC_MAX_DIMS. The purpose of the limit is to make writing generic applications simpler. They need only provide an array of NC_MAX_DIMS dimensions to handle any netCDF dataset. The implementation of the netCDF library does not enforce this advisory maximum, so it is possible to use more dimensions, if necessary, but netCDF utilities that assume the advisory maximums may not be able to handle the resulting netCDF datasets.

Ordinarily, the name and length of a dimension are fixed when the dimension is first defined. The name may be changed later, but the length of a dimension (other than the unlimited dimension) cannot be changed without copying all the data to a new netCDF dataset with a redefined dimension length.

Dimension lengths in the C interface are type size_t rath