|
|
|||
|
||||
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
NetCDF User's Guide for FORTRAN
NetCDF version 3 includes a complete rewrite of the netCDF library. It is about twice as fast as the previous version. The netCDF file format is unchanged, so files written with version 3 can be read with version 2 code and vice versa.
The core library is now written in ANSI C. You must have an ANSI C compiler to compile this version. The FORTRAN interface is layered on top of the C interface using a different technique than was used in netCDF-2.
Rewriting the library offered an opportunity to implement improved C and FORTRAN interfaces that provide some significant benefits:
It is not necessary to rewrite programs that use the version 2 FORTRAN interface, because the netCDF-3 library includes a backward compatibility interface that supports all the old functions, globals, and behavior. We are hoping that the benefits of the new interface will be an incentive to use it in new netCDF applications. It is possible to convert old applications to the new interface incrementally, replacing netCDF-2 calls with the corresponding netCDF-3 calls one at a time.
Other changes in the implementation of netCDF result in improved portability, maintainability, and performance on most platforms. A clean separation between I/O and type layers facilitates platform-specific optimizations. The new library no longer uses a vendor-provided XDR library, which simplifies linking programs that use netCDF and speeds up data access significantly in most cases.
First, here's an example of FORTRAN code that uses the netCDF-2 interface:
! Use a buffer big enough for values of any type DOUBLE PRECISION DBUF(NDATA) REAL RBUF(NDATA) ... EQUIVALENCE (RBUF, DBUF), ... INT XTYPE ! to hold the actual type of the data INT STATUS ! for error status ! Get the actual data type CALL NCVINQ(NCID, VARID, ...,XTYPE, ...) ... ! Get the data CALL NCVGT(NCID, VARID, START, COUNT, DBUF, STATUS) IF(STATUS .NE. NCNOERR) THEN PRINT *, 'Cannot get data, error code =', STATUS ! Deal with error ... ENDIF IF (XTYPE .EQ. NCDOUBLE) THEN CALL DANALYZE(DBUF) ELSEIF (XTYPE .EQ. NCFLOAT) THEN CALL RANALYZE(RBUF) ... ENDIF
Here's how you might handle this with the new netCDF-3 FORTRAN interface:
! I want to use doubles for my analysis DOUBLE PRECISION DBUF(NDATA) INT STATUS ! So I use a function that gets the data as doubles. STATUS = NF_GET_VARA_DOUBLE(NCID, VARID, START, COUNT, DBUF) IF(STATUS .NE. NF_NOERR) THEN PRINT *, 'Cannot get data, ', NF_STRERROR(STATUS) ! Deal with error ... ENDIF CALL DANALYZE(DBUF)
The example above illustrates changes in function names, data type conversion, and error handling, discussed in detail in the sections below.
The netCDF-3 C library employs a new naming convention, intended to make
netCDF programs more readable. For example, the name of the function to rename
a variable is now NF_RENAME_VAR instead
of the previous NCVREN.
All netCDF-3 FORTRAN function names begin with
the NF_ prefix. The second part of
the name is a verb, like GET, PUT,
INQ (for inquire), or OPEN.
The third part of the name is typically the object of the verb: for example
DIM, VAR,
or ATT for functions dealing with
dimensions, variables, or attributes. To distinguish the various I/O operations
for variables, a single character modifier is appended to VAR:
VAR entire variable access
VAR1 single value access
VARA array or array section access
VARS strided access to a subsample
of values
VARM mapped access to values not
contiguous in memory
At the end of the name for variable and attribute functions, there is a component
indicating the type of the final argument: TEXT,
INT1, INT2,
INT, REAL,
or DOUBLE. This part of the function
name indicates the type of the data container you are using in your program:
character string, 1-byte integer, and so on.
Also, all PARAMETER
names in the public FORTRAN interface begin with
the prefix NF_. For example, the PARAMETER
which was formerly MAXNCNAM is now
NF_MAX_NAME,
and the former FILFLOAT is now NF_FILL_FLOAT.
As previously mentioned, all the old names are still supported for backward compatibility.
With the new interface, users need not be aware of the external type of numeric variables, since automatic conversion to or from any desired numeric type is now available. You can use this feature to simplify code, by making it independent of external types. The elimination of type punning prevents some kinds of type errors that could occur with the previous interface. Programs may be made more robust with the new interface, because they need not be changed to accommodate a change to the external type of a variable.
If conversion to or from an external numeric type is necessary, it is handled by the library. This automatic conversion and separation of external data representation from internal data types will become even more important in netCDF version 4, when new external types will be added for packed data for which there is no natural corresponding internal type, for example, arrays of 11-bit values.
Converting
from one numeric type to another may result in an error if the target type
is not capable of representing the converted value. (In netCDF-2, such overflows
can only happen in the XDR layer.) For example, a REAL
may not be able to hold data stored externally as an NF_DOUBLE
(an IEEE floating-point number). When accessing an array of values, an NF_ERANGE
error is returned if one or more values are out of the range of representable
values, but other values are converted properly.
Note that mere loss of precision in type conversion does not return an error. Thus, if you read double precision values into an INTEGER, for example, no error results unless the magnitude of the double precision value exceeds the representable range of INTEGERs on your platform. Similarly, if you read a large integer into a REAL incapable of representing all the bits of the integer in its mantissa, this loss of precision will not result in an error. If you want to avoid such precision loss, check the external types of the variables you access to make sure you use an internal type that has a compatible precision.
The new interface distinguishes arrays of characters intended to represent text strings from arrays of 8-bit bytes intended to represent small integers. The interface supports the internal types CHARACTER and INT1 intended for text strings and one byte integers.
The new interface handles errors differently than netCDF-2. In the old interface, the default behavior when an error was detected was to print an error message and exit. To get control of error handling, you had to call a function NCPOPT and to determine the cause of an error, you had to test the value of a returned error parameter.
In the new interface, functions return an integer status that indicates not
only success or failure, but also the cause of the error. The
library will never try to print anything, nor will it call exit
(unless you are using the netCDF version 2 compatibility functions). You will
have to check the function return status and do this yourself. We eliminated
these globals in the interest of supporting parallel (multiprocessor) execution
cleanly, as well as reducing the number of assumptions about the environment
where netCDF is used. The new behavior should provide better support for using
netCDF as a hidden layer in applications that have their own GUI interface.
NCLONG
and NF_INT Where the netCDF-2 interface used NCLONG
to identify an external data type corresponding to 32-bit integers, the new
interface uses NF_INT instead. NCLONG
is defined to have the same value as NF_INT
for backward compatibility, but it should not be used in new code. With new
64-bit platforms using long for 64-bit integers, we would like to reduce the
confusion caused by this name clash. Note that there is still no netCDF external
data type corresponding to 64-bit integers.
There is no function corresponding to the NCTLEN
function from the version 2 interface. The separation of internal and external
types and the new type-conversion interfaces make NCTLEN
unnecessary. Since users read into and write out of native types, knowledge
of the space required for native types is perfectly adequate to determine
how much space to allocate for a value.
In
the previous library, there was no checking that the characters used in the
name of a netCDF object were compatible with CDL restrictions. The ncdump
and ncgen utilities that use CDL permit only alphanumeric characters, "_"
and "-" in names. Now this restriction is also enforced by the
library for creation of new dimensions, variables, and attributes. Previously
existing components with less restrictive names will still work OK.
There are two new functions in netCDF-3 that don't correspond to any netCDF-2
functions: NF_INQ_LIBVERS and NF_STRERROR.
The version of the netCDF library in use is returned as a string by NF_INQ_LIBVERS.
An error message corresponding to the status returned by a netCDF function
call is returned as a string by the NF_STRERROR
function.
A new NF_SHARE
flag is available for use in an NF_OPEN
or NF_CREATE call, to suppress the
default buffering of accesses. The use of NF_SHARE
for concurrent access to a netCDF dataset means you don't have to call NF_SYNC
after every access to make sure that disk updates are synchronous. It is important
to note that changes to 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.
The version 2 interface had a single inquiry function, NCVINQ
for getting the name, type, and shape of a variable. Similarly, only
a single inquiry function was available for getting information about a dimension,
an attribute, or a netCDF dataset. When you only wanted a subset of this information,
you had to provide dummy arguments as placeholders
for the unneeded information. The new interface includes additional inquire
functions that return each item separately, so errors are less likely from
miscounting arguments.
The previous implementation returned an error when 0-valued
count components were specified in NCVPT
and NCVGT calls. This restriction
has been removed, so that now functions in the NF_PUT_VAR
and NF_GET_VAR families may be called
with 0-valued count components, resulting in no data being accessed. Although
this may seem useless, it simplifies some programs to not treat 0-valued counts
as a special case.
The previous implementation returned an error when the same dimension was used more than once in specifying the shape of a variable in ncvardef. This restriction is relaxed in the netCDF-3 implementation, because an autocorrelation matrix is a good example where using the same dimension twice makes sense.
In the new interface, units for the IMAP
argument to the NF_PUT_VARM and NF_GET_VARM
families of functions are now in terms of the number of data elements of the
desired internal type, not in terms of bytes as in the netCDF version-2 mapped
access interfaces.
Following is a table of netCDF-2 function names and names of the corresponding netCDF-3 functions. For parameter lists of netCDF-2 functions, see the netCDF-2 User's Guide.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]
| Contact Us Site Map Search Terms and Conditions Privacy Policy Participation Policy | ||||||
|
||||||