|
|
|||
|
||||
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
NetCDF User's Guide for FORTRAN
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.
Each interface description for a particular netCDF function in this and later chapters contains:
In the FORTRAN function prototype and formal parameter descriptions, output parameters (in which returned values will be stored) have lower case names, to distinguish them from input parameters named in upper case.
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: nc_strerror," page 30.
NF_STRERROR The function
NF_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.
CHARACTER*80 FUNCTION NF_STRERROR(INTEGER 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), nc_strerror returns
a string indicating that there is no such error status.
Here is an example of a simple error handling subroutine
that uses NF_STRERROR to print the
error message corresponding to the netCDF error status returned from any netCDF
function call and then exit:
INCLUDE 'netcdf.inc' ... SUBROUTINE HANDLE_ERR(STATUS) INTEGER STATUS IF (STATUS .NE. NF_NOERR) THEN PRINT *, NF_STRERROR(STATUS) STOP 'Stopped' ENDIF END
NF_INQ_LIBVERS
The function
NF_INQ_LIBVERS returns a string identifying
the version of the netCDF library, and when it was built.
CHARACTER*80 FUNCTION NF_INQ_LIBVERS()
This function takes no arguments, and thus no errors are possible in its invocation.
Here is an example using NF_INQ_LIBVERS
to print the version of the netCDF library with which the program is linked:
INCLUDE 'netcdf.inc' ... PRINT *, NF_INQ_LIBVERS()
NF_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.
INTEGER FUNCTION NF_CREATE (CHARACTER*(*) PATH, INTEGER CMODE, INTEGER ncid)
NF_CREATE returns the value NF_NOERR
if no errors occurred. Possible causes of errors include:
NF_NOCLOBBER.
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.inc' ... INTEGER NCID, STATUS ... STATUS = NF_CREATE('foo.nc', NF_NOCLOBBER, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_OPEN
The function
NF_OPEN opens an existing netCDF dataset
for access.
INTEGER FUNCTION NF_OPEN(CHARACTER*(*) PATH, INTEGER OMODE, INTEGER ncid)
NF_OPEN returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_OPEN
to open an existing netCDF dataset named foo.nc for read-only,
non-shared access:
INCLUDE 'netcdf.inc' ... INTEGER NCID, STATUS ... STATUS = NF_OPEN('foo.nc', 0, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_REDEF
The function
NF_REDEF puts an open netCDF dataset
into define mode, so dimensions, variables, and attributes can be added or
renamed and attributes can be deleted.
INTEGER FUNCTION NF_REDEF(INTEGER NCID)
NCID |
NetCDF ID, from a previous call to |
NF_REDEF returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_REDEF
to open an existing netCDF dataset named foo.nc and put it into
define mode:
INCLUDE 'netcdf.inc' ... INTEGER NCID, STATUS ... STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID) ! open dataset IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_REDEF(NCID) ! put in define mode IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_ENDDEF
The function
NF_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: nc_set_fill," page 39). 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 95, for a more extensive discussion.
INTEGER FUNCTION NF_ENDDEF(INTEGER NCID)
|
NetCDF ID, from a previous call to |
NF_ENDDEF returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_ENDDEFto
finish the definitions of a new netCDF dataset named foo.nc and
put it into data mode:
INCLUDE 'netcdf.inc' ... INTEGER NCID, STATUS ... STATUS = NF_CREATE('foo.nc', NF_NOCLOBBER, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... ! create dimensions, variables, attributes STATUS = NF_ENDDEF(NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_CLOSE
The function
NF_CLOSE closes an open netCDF dataset.
If the dataset is in define mode, NF_ENDDEF
will be called before closing. (In this case, if NF_ENDDEF
returns an error, NF_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.
INTEGER FUNCTION NF_CLOSE(INTEGER NCID)
|
netCDF ID, from a previous call to |
NF_CLOSE returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
NF_ENDDEF
failed.
Here is an example using NF_CLOSE
to finish the definitions of a new netCDF dataset named foo.nc
and release its netCDF ID:
INCLUDE 'netcdf.inc' ... INTEGER NCID, STATUS ... STATUS = NF_CREATE('foo.nc', NF_NOCLOBBER, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... ! create dimensions, variables, attributes STATUS = NF_CLOSE(NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_INQ
FamilyMembers of the NF_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, NF_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 FORTRAN, these functions include NF_INQ,
NF_INQ_NDIMS, NF_INQ_NVARS,
NF_INQ_NATTS, and NF_INQ_UNLIMDIM.
No I/O is performed when these functions are called, since the required information is available in memory for each open netCDF dataset.
INTEGER FUNCTION NF_INQ (INTEGER NCID, INTEGER ndims, INTEGER nvars,INTEGER ngatts, INTEGER unlimdimid) INTEGER FUNCTION NF_INQ_NDIMS (INTEGER NCID, INTEGER ndims) INTEGER FUNCTION NF_INQ_NVARS (INTEGER NCID, INTEGER nvars) INTEGER FUNCTION NF_INQ_NATTS (INTEGER NCID, INTEGER ngatts) INTEGER FUNCTION NF_INQ_UNLIMDIM (INTEGER NCID, INTEGER unlimdimid)
All members of the NF_INQ family return the value
NF_NOERR if
no errors occurred. Otherwise, the returned status indicates an error. Possible
causes of errors include:
Here is an example using NF_INQ
to find out about a netCDF dataset named foo.nc:
INCLUDE 'netcdf.inc' ... INTEGER STATUS, NCID, NDIMS, NVARS, NGATTS, UNLIMDIMID ... STATUS = NF_OPEN('foo.nc', NF_NOWRITE, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_INQ(NCID, NDIMS, NVARS, NGATTS, UNLIMDIMID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_SYNC
The function NF_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 NF_SYNC
after writing and the readers call NF_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 NF_SHARE
flag, and then it will not be necessary to call NF_SYNC
at all. However, the NF_SYNC function
still provides finer granularity than the NF_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 NF_SHARE
flag. Use of the NF_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 NF_SYNC
before any subsequent access.
When calling NF_SYNC, the netCDF
dataset must be in data
mode. A netCDF dataset in define mode is synchronized to disk only when NF_ENDDEF
is called. A process that is reading a netCDF dataset that another process
is writing may call NF_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.
INTEGER FUNCTION NF_SYNC(INTEGER NCID)
|
NetCDF ID, from a previous call to |
NF_SYNC returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_SYNC
to synchronize the disk writes of a netCDF dataset named foo.nc:
INCLUDE 'netcdf.inc' ... INTEGER STATUS, NCID ... STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... ! write data or change attributes ... STATUS = NF_SYNC(NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS)
NF_ABORT
You no longer need to call this function, since it is called automatically
by NF_CLOSE in case the dataset is
in define mode and something goes wrong with committing the changes. The function
NF_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 NF_REDEF,
the netCDF dataset is restored to its state before definition mode was entered
and the dataset is closed.
INTEGER FUNCTION NF_ABORT(INTEGER NCID)
|
NetCDF ID, from a previous call to |
NF_ABORT returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
Here is an example using NF_ABORT
to back out of redefinitions of a dataset named foo.nc:
INCLUDE 'netcdf.inc' ... INTEGER STATUS, NCID, LATID ... STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_REDEF(NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... STATUS = NF_DEF_DIM(NCID, 'LAT', 18, LATID) IF (STATUS .NE. NF_NOERR) THEN ! dimension definition failed CALL HANDLE_ERR(STATUS) STATUS = NF_ABORT(NCID) ! abort redefinitions IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ENDIF ...
NF_SET_FILL
This function is intended for advanced usage, to optimize writes under some
circumstances described below. The function NF_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 NF_FILL
or NF_NOFILL.
The default behavior corresponding to NF_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.16 "Fill Values," page 78, for more information on the
use of fill values. See Section 8.1
"Attribute Conventions," page 81, for information about how to define
your own fill values.
The behavior corresponding to NF_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 NF_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 NF_SET_FILL
again to explicitly set the fill mode to NF_NOFILL.
There are three situations where it is advantageous to set nofill mode:
NF_ENDDEF
and then write completely all non-record variables and the initial
records of all the record variables you want to initialize.
NF_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.
INTEGER FUNCTION NF_SET_FILL(INTEGER NCID, INTEGER FILLMODE, INTEGER old_mode)
NF_SET_FILL returns the value NF_NOERR
if no errors occurred. Otherwise, the returned status indicates an error.
Possible causes of errors include:
NF_NOFILL
nor NF_FILL.
Here is an example using NF_SET_FILL
to set nofill mode for subsequent writes of a netCDF dataset named foo.nc:
INCLUDE 'netcdf.inc' ... INTEGER NCID, STATUS, OMODE ... STATUS = NF_OPEN('foo.nc', NF_WRITE, NCID) IF (STATUS .NE. NF_NOERR) CALL HANDLE_ERR(STATUS) ... ! write data with default prefilling behavior ... OMODE = NF_SET_FILL(NCID, NF_NOFILL) ... ! 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 | ||||||
|
||||||