NetCDF-Fortran 4.6.1
|
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:
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 Section 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.
NCERR
: An error status that might have been returned from a previous call to some netCDF function.
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), NF90_STRERROR returns a string indicating that there is no such error status.
Here is an example of a simple error handling function that uses NF90_STRERROR to print the error message corresponding to the netCDF error status returned from any netCDF function call and then exit:
The function NF90_INQ_LIBVERS returns a string identifying the version of the netCDF library, and when it was built.
This function takes no arguments, and returns no error status.
Here is an example using nf90_inq_libvers to print the version of the netCDF library with which the program is linked:
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.
path
: The file name of the new netCDF dataset.
cmode
: The creation mode flag. The following flags are available: NF90_CLOBBER, NF90_NOCLOBBER, NF90_SHARE, NF90_64BIT_OFFSET, NF90_NETCDF4, and NF90_CLASSIC_MODEL. (NF90_HDF5 is deprecated, use NF90_NETCDF4 instead).
A zero value (defined for convenience as NF90_CLOBBER) specifies: 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 in NetCDF Users’ Guide.
Setting NF90_NOCLOBBER 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 NF90_SHARE flag. (This only applies to netCDF-3 classic or 64-bit offset files.)
Setting NF90_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 in NetCDF Users’ Guide.
Setting the NF90_NETCDF4 flag causes netCDF to create a netCDF-4/HDF5 format output file.
Oring the NF90_CLASSIC_MODEL flag with the NF90_NETCDF4 flag causes the resulting netCDF-4/HDF5 file to restrict itself to the classic model - none of the new netCDF-4 data model features, such as groups or user-defined types, are allowed in such a file.
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. This is ignored for NetCDF-4/HDF5 files.
bufrsize
: 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 bufrsize is set to 8192 bytes.
The bufrsize is a property of a given open netcdf descriptor ncid, it is not a persistent property of the netcdf dataset.
This is ignored for NetCDF-4/HDF5 files.
cache_size
: If the cache_size is provided when creating a netCDF-4/HDF5 file, it will be used instead of the default (32000000) as the size, in bytes, of the HDF5 chunk cache.
cache_nelems
: If cache_nelems is provided when creating a netCDF-4/HDF5 file, it will be used instead of the default (1000) as the maximum number of elements in the HDF5 chunk cache.
cache_preemption
: If cache_preemption is provided when creating a netCDF-4/HDF5 file, it will be used instead of the default (0.75) as the preemption value for the HDF5 chunk cache.
comm
: If the comm and info parameters are provided the file is created and opened for parallel I/O. Set the comm parameter to the MPI communicator (of type MPI_Comm). If this parameter is provided the info parameter must also be provided.
info
: If the comm and info parameters are provided the file is created and opened for parallel I/O. Set the comm parameter to the MPI information value (of type MPI_Info). If this parameter is provided the comm parameter must also be provided.
NF90_CREATE returns the value NF90_NOERR if no errors occurred. Possible causes of errors include:
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:
The function NF90_OPEN opens an existing netCDF dataset for access.
path
: File name for netCDF dataset to be opened. This may be an OPeNDAP URL if DAP support is enabled.
mode
: A zero value (or NF90_NOWRITE) specifies: open the dataset with read-only access, buffering and caching accesses for efficiency
Otherwise, the open mode is NF90_WRITE, NF90_SHARE, or 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 NF90_SHARE flag is appropriate when one process may be writing the dataset and one or more other processes reading the dataset concurrently (note that this is not the same as parallel I/O); 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.
bufrsize
: This parameter applies only when opening classic format or 64-bit offset files. It is ignored for netCDF-4/HDF5 files.
It 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 bufrsize is set to 8192 bytes.
The bufrsize is a property of a given open netcdf descriptor ncid, it is not a persistent property of the netcdf dataset.
cache_size
: If the cache_size is provided when opening a netCDF-4/HDF5 file, it will be used instead of the default (32000000) as the size, in bytes, of the HDF5 chunk cache.
cache_nelems
: If cache_nelems is provided when opening a netCDF-4/HDF5 file, it will be used instead of the default (1000) as the maximum number of elements in the HDF5 chunk cache.
cache_preemption
: If cache_preemption is provided when opening a netCDF-4/HDF5 file, it will be used instead of the default (0.75) as the preemption value for the HDF5 chunk cache.
comm
: If the comm and info parameters are provided the file is opened for parallel I/O. Set the comm parameter to the MPI communicator (of type MPI_Comm). If this parameter is provided the info parameter must also be provided.
info
: If the comm and info parameters are provided the file is opened for parallel I/O. Set the comm parameter to the MPI information value (of type MPI_Info). If this parameter is provided the comm parameter must also be provided.
NF90_OPEN returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_OPEN to open an existing netCDF dataset named foo.nc for read-only, non-shared access:
Here is an example using NF90_OPEN to open an existing netCDF dataset for parallel I/O access. (Note the use of the comm and info parameters). This example is from test program nf_test/f90tst_parallel.f90.
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.
ncid
: netCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
NF90_REDEF returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_REDEF to open an existing netCDF dataset named foo.nc and put it into define mode:
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 NF90_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 in NetCDF Users Guide.
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 minfree 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 h_minfree and v_minfree is 0.
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 bufrsize (see above) as the align parameter. The default value for both v_align and r_align is 4 bytes.
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.
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).
NF90_ENDDEF returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
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:
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.
ncid
: NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
NF90_CLOSE returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_CLOSE to finish the definitions of a new netCDF dataset named foo.nc and release its netCDF ID:
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. An additional function, NF90_INQ_FORMAT, returns the (rarely needed) format version.
No I/O is performed when NF90_INQUIRE is called, since the required information is available in memory for each open netCDF dataset.
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.
format
: Returned integer indicating format version for this dataset, one of nf90_format_classic, nf90_format_64bit, nf90_format_netcdf4, or nf90_format_netcdf4_classic. These are rarely needed by users or applications, since thhe library recognizes the format of a file it is accessing and handles it accordingly.
Function NF90_INQUIRE returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_INQUIRE to find out about a netCDF dataset named foo.nc:
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.
ncid
: NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
NF90_SYNC returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_SYNC to synchronize the disk writes of a netCDF dataset named foo.nc:
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.
ncid
: NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
NF90_ABORT returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_ABORT to back out of redefinitions of a dataset named foo.nc:
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 Fill Values, for more information on the use of fill values. See Attribute Conventions in {No value for ‘n-man’}, 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:
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.
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.
NF90_SET_FILL returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
Here is an example using NF90_SET_FILL to set nofill mode for subsequent writes of a netCDF dataset named foo.nc: