Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]

5 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 nonnegative 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:

After a summary of conventions used in describing the netCDF interfaces, the rest of this chapter presents a detailed description of the interfaces for these operations.

5.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_ERR subroutine in case an error was detected. For an example of such a subroutine, see Section 5.2 "Get error message corresponding to error status: NF90_STRERROR," page 30.

5.2 Get error message corresponding to error status: NF90_STRERROR

The function NF90_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

 function nf90_strerror(ncerr)
   integer, intent( in) :: ncerr
   character(len = 80)  :: nf90_strerror
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 to any system error message (as understood by the system strerror function), NF90_STRERROR returns a string indicating that there is no such error status.

Example

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

 subroutine handle_err(status)
   integer, intent ( in) :: status
   
   if(status /= nf90_noerr) then
     print *, trim(nf90_strerror(status))
     stop "Stopped"
   end if
 end subroutine handle_err

5.3 Get netCDF library version: NF90_INQ_LIBVERS

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

Usage

 function nf90_inq_libvers()
   character(len = 80) :: nf90_inq_libvers

Errors

This function takes no arguments, and returns no error status.

Example

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

 print *, trim(nf90_inq_libvers())

5.4 Create a NetCDF dataset: NF90_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 is 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.

Usage

 function nf90_create(path, cmode, ncid)
   character (len = *), intent(in   ) :: path
   integer,             intent(in   ) :: cmode
   integer, optional,   intent(in   ) :: initialsize
   integer, optional,   intent(inout) :: chunksize
   integer,             intent(  out) :: ncid
   integer                            :: nf90_create
path
The file name of the new netCDF dataset.
cmode
The creation mode. A zero value (or NF90_CLOBBER) specifies the default behavior: overwrite any existing dataset with the same file name and buffer and cache accesses for efficiency.
Otherwise, the creation mode is NF90_NOCLOBBER, NF90_SHARE, or IOR(NF90_NOCLOBBER, NF90_SHARE). Setting the NF90_NOCLOBBER flag means you do not want to clobber (overwrite) an existing dataset; an error (NF90_EEXIST) is returned if the specified dataset already exists. The NF90_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 NF_SHARE flag.
ncid
Returned netCDF ID.
The following optional arguments allow additional performance tuning.
initialsize
The initial size of the file (in bytes) at creation time. A value of 0 causes the file size to be computed when nf90_enddef is called.
chunksize
Controls a space versus time trade-off, 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.
The library chooses a system-dependent default value if NF90_SIZEHINT_DEFAULT is supplied as input. If the "preferred I/O block size" is available from the stat() system call as member st_blksize this value is used. Lacking that, twice the system pagesize is used. Lacking a call to discover the system pagesize, the default chunksize is set to 8192 bytes.
The chunksize is a property of a given open netcdf descriptor ncid, it is not a persistent property of the netcdf dataset.

Errors

If no errors were detected, NF90_CREATE returns the value NF90_NOERR. Possible causes of errors include:

Example

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:

 use netcdf
 implicit none
 integer :: ncid, status
 ...
 status = nf90_create(path = "foo.nc", cmode = nf90_noclobber, ncid = ncid)
 if (status /= nf90_noerr) call handle_err(status)

5.5 Open a NetCDF Dataset for Access: NF90_OPEN

The function NF90_OPEN opens an existing netCDF dataset for access in data mode.

Usage

 function nf90_open(path, mode, ncid, chunksize)
   character (len = *), intent(in   ) :: path
   integer,             intent(in   ) :: mode
   integer,             intent(  out) :: ncid
   integer, optional,   intent(inout) :: chunksize
   integer                            :: nf90_open
path
File name for netCDF dataset to be opened.
mode
A zero value (or NF90_NOWRITE) specifies the default behavior: open the dataset with read-only access, buffering and caching accesses for efficiency
Otherwise, the creation mode is NF90_WRITE, NF90_SHARE, or IOR(NF90_WRITE, NF90_SHARE). Setting the NF90_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 NF_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 NF90_SHARE flag.
ncid
Returned netCDF ID.
The following optional argument allows additional performance tuning.
chunksize
Controls a space versus time trade-off, 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.
The library chooses a system-dependent default value if NF90_SIZEHINT_DEFAULT is supplied as input. If the "preferred I/O block size" is available from the stat() system call as member st_blksize this value is used. Lacking that, twice the system pagesize is used. Lacking a call to discover the system pagesize, the default chunksize is set to 8192 bytes.
The chunksize is a property of a given open netcdf descriptor ncid, it is not a persistent property of the netcdf dataset.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status
 ...
 status = nf90_open(path = "foo.nc", cmode = nf90_nowrite, ncid = ncid)
 if (status /= nf90_noerr) call handle_err(status)

5.6 Put Open NetCDF Dataset into Define Mode: NF90_REDEF

The function NF90_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

 function nf90_redef(ncid)
   integer, intent( in) :: ncid
   integer              :: nf90_redef
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status
 ...
 status = nf90_open("foo.nc", nf90_write, ncid) ! Open dataset
 if (status /= nf90_noerr) call handle_err(status)
 ...
 status = nf90_redef(ncid)                       ! Put the file in define mode
 if (status /= nf90_noerr) call handle_err(status)

5.7 Leave Define Mode: NF90_ENDDEF

The function NF90_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 Section 5.12 "Set Fill Mode for Writes: NF90_SET_FILL," page 41). 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. See Chapter 9 "NetCDF File Structure and Performance," page 83, for a more extensive discussion.

Usage

 function nf90_enddef(ncid, h_minfree, v_align, v_minfree, r_align)
   integer,           intent( in) :: ncid
   integer, optional, intent( in) :: h_minfree, v_align, v_minfree, r_align
   integer                        :: nf90_enddef
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
The following arguments allow additional performance tuning. Note: these arguments expose internals of the netcdf version 1 file format, and may not be available in 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 the 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 following parameters allow one to control costs of future calls to nf90_redef or nf90_enddef by requesting that some space be available at the end of the section. The default value for both arguments is 0.
h_minfree
Size of the pad (in bytes) at the end of the "header" section.
v_minfree
Size of the pad (in bytes) at the end of the data section for fixed size variables.

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 NF90_ALIGN_CHUNK tells the library to use the chunksize (see above) as the align parameter. The default value for both arguments is 4 bytes.
v_align
The alignment of the beginning of the data section for fixed size variables.
r_align
The alignment of the beginning of the data section for variables which have an unlimited dimension (record variables).

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status
 ...
 status = nf90_create("foo.nc", nf90_noclobber, ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  !  create dimensions, variables, attributes
 status = nf90_enddef(ncid) 
 if (status /= nf90_noerr) call handle_err(status)

5.8 Close an Open NetCDF Dataset: NF90_CLOSE

The function NF90_CLOSE closes an open netCDF dataset. If the dataset is in define mode, NF90_ENDDEF will be called before closing. (In this case, if NF90_ENDDEF returns an error, NF90_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

 function nf90_close(ncid)
   integer, intent( in) :: ncid
   integer              :: nf90_close
ncid
netCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status
 ...
 status = nf90_create("foo.nc", nf90_noclobber, ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  !  create dimensions, variables, attributes
 status = nf90_close(ncid) 
 if (status /= nf90_noerr) call handle_err(status)

5.9 Inquire about an Open NetCDF Dataset: NF90_Inquire

The NF90_Inquire subroutine returns information about an open netCDF dataset, given its netCDF ID. The subroutine can be called from either define mode or data mode, and returns values for any or all of the following: 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.

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

Usage

 function nf90_Inquire(ncid, nDimensions, nVariables, nAttributes, &                   unlimitedDimId)
   integer,           intent( in) :: ncid
   integer, optional, intent(out) :: nDimensions, nVariables, nAttributes, &                                 unlimitedDimId
   integer                        :: nf90_Inquire
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
nDimensions
Returned number of dimensions defined for this netCDF dataset.
nVariables
Returned number of variables defined for this netCDF dataset.
nAttributes
Returned number of global attributes defined for this netCDF dataset.
unlimitedDimID
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.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status, nDims, nVars, nGlobalAtts, unlimDimID
 ...
 status = nf90_open("foo.nc", nf90_nowrite, ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  
 status = Nf90_Inquire(ncid, nDims, nVars, nGlobalAtts, unlimdimid) 
 if (status /= nf90_noerr) call handle_err(status)
 status = Nf90_Inquire(ncid, nDimensions = nDims, &                      unlimitedDimID = unlimdimid) 
 if (status /= nf90_noerr) call handle_err(status)

5.10 Synchronize an Open NetCDF Dataset to Disk: NF90_SYNC

The function NF90_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 NF90_SYNC after writing and the readers call NF90_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 NF90_SHARE flag, and then it will not be necessary to call NF90_SYNC at all. However, the NF90_SYNC function still provides finer granularity than the NF90_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 NF90_SHARE flag. Use of the NF90_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 NF90_SYNC before any subsequent access.

When calling NF90_SYNC, the netCDF dataset must be in data mode. A netCDF dataset in define mode is synchronized to disk only when NF90_ENDDEF is called. A process that is reading a netCDF dataset that another process is writing may call NF90_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

 function nf90_sync(ncid)
   integer, intent( in) :: ncid
   integer              :: nf90_sync
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90 _CREATE.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status
 ...
 status = nf90_open("foo.nc", nf90_write, ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  
 ! write data or change attributes
 ...  
 status = NF90_SYNC(ncid) 
 if (status /= nf90_noerr) call handle_err(status)

5.11 Back Out of Recent Definitions: NF90_ABORT

You no longer need to call this function, since it is called automatically by NF90_CLOSE in case the dataset is in define mode and something goes wrong with committing the changes. The function NF90_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 NF90_REDEF, the netCDF dataset is restored to its state before definition mode was entered and the dataset is closed.

Usage

 function nf90_abort(ncid)
   integer, intent( in) :: ncid
   integer              :: nf90_abort
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status, LatDimID
 ...
 status = nf90_open("foo.nc", nf90_write, ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  
 status = nf90_redef(ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  
 status = nf90_def_dim(ncid, "Lat", 18, LatDimID) 
 if (status /= nf90_noerr) then ! Dimension definition failed
   call handle_err(status)
   status = nf90_abort(ncid) ! Abort redefinitions
   if (status /= nf90_noerr) call handle_err(status)
 end if

5.12 Set Fill Mode for Writes: NF90_SET_FILL

This function is intended for advanced usage, to optimize writes under some circumstances described below. The function NF90_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 NF90_FILL or NF90_NOFILL. The default behavior corresponding to NF90_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. See Section 7.8 "Fill Values," page 67, for more information on the use of fill values. See Section 8.1 "Attribute Conventions," page 69, for information about how to define your own fill values.

The behavior corresponding to NF90_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 NF90_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 NF90_SET_FILL again to explicitly set the fill mode to NF90_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 NF90_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 NF90_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

 function nf90_set_fill(ncid, fillmode, old_mode)
   integer, intent( in) :: ncid, fillmode 
   integer, intent(out) :: old_mode
   integer              :: nf90_set_fill
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
fillmode
Desired fill mode for the dataset, either NF90_NOFILL or NF90_FILL.
old_mode
Returned current fill mode of the dataset before this call, either NF90_NOFILL or NF90_FILL.

Errors

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

Example

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

 use netcdf
 implicit none
 integer :: ncid, status, oldMode
 ...
 status = nf90_open("foo.nc", nf90_write, ncid) 
 if (status /= nf90_noerr) call handle_err(status)
 ...  
 ! Write data with prefilling behavior
 ...  
 status = nf90_set_fill(ncid, nf90_nofill, oldMode) 
 if (status /= nf90_noerr) call handle_err(status)
 ...
 !  Write data with no prefilling
 ...

[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Office of Programs University Corporation for Atmospheric Research (UCAR)   Unidata is a member of the UCAR Office of Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690