|
|
|||
|
||||
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:
- Create, given dataset name and whether to overwrite or not.
- Open for access, given dataset name and read or write intent.
- Put into define mode, to add dimensions, variables, or attributes.
- Take out of define mode, checking consistency of additions.
- Close, writing to disk if required.
- Inquire about the number of dimensions, number of variables, number of global attributes, and ID of the unlimited dimension, if any.
- Synchronize to disk to make sure it is current.
- Set and unset nofill mode for optimized sequential writes.
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:
- a description of the purpose of the function;
- a Fortran 90 interface block that presents the type and order of the formal parameters to the function;
- a description of each formal parameter in the Fortran 90 interface;
- a list of possible error conditions; and
- an example of a Fortran 90 program fragment calling the netCDF function (and perhaps other netCDF functions).
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_strerrorErrors
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_err5.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_libversErrors
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_createErrors
If no errors were detected, NF90_CREATE returns the value NF90_NOERR. Possible causes of errors include:
- Passing a dataset name that includes a directory that does not exist.
- Specifying a dataset name of a file that exists and also specifying NF90_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.
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_openErrors
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_redefErrors
NF90_REDEF returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
- The specified netCDF dataset is already in define mode.
- The specified netCDF dataset was opened for read-only.
- The specified netCDF ID does not refer to an open netCDF dataset.
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_enddefErrors
NF90_ENDDEF returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
- The specified netCDF dataset is not in define mode.
- The specified netCDF ID does not refer to an open netCDF dataset.
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_closeErrors
NF90_CLOSE returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
- Define mode was entered and the automatic call made to NF90_ENDDEF failed.
- The specified netCDF ID does not refer to an open netCDF dataset.
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_InquireErrors
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:
- To minimize data loss in case of abnormal termination, or
- To make data available to other processes for reading immediately after it is written. But note that a process that already had the dataset open for reading would not see the number of records increase when the writing process calls NF90_SYNC; to accomplish this, the reading process must call NF90_SYNC.
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_syncErrors
NF90_SYNC returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
- The netCDF dataset is in define mode.
- The specified netCDF ID does not refer to an open netCDF dataset.
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_abortErrors
NF90_ABORT returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
- When called from define mode while creating a netCDF dataset, deletion of the dataset failed.
- The specified netCDF ID does not refer to an open netCDF dataset.
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 if5.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:
- 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.
- 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.
- 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_fillErrors
NF90_SET_FILL returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:
- The specified netCDF ID does not refer to an open netCDF dataset.
- The specified netCDF ID refers to a dataset open for read-only access.
- The fill mode argument is neither NF90_NOFILL nor NF90_FILL.
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 ...
| Contact Us Site Map Search Terms and Conditions Privacy Policy Participation Policy | ||||||
|
||||||