[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]

NetCDF User's Guide for FORTRAN

Appendix D NetCDF 2 FORTRAN Transition Guide




Overview of FORTRAN interface changes

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.



The New FORTRAN Interface

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.



Function Naming Conventions

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:

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.



Type Conversion

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.



Error handling

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.



What's Missing?

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.



Other Changes

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.

NCABOR

NF_ABORT

NCACPY

NF_COPY_ATT

NCADEL

NF_DEL_ATT

NCAGT

NF_GET_ATT_DOUBLE, NF_GET_ATT_REAL, NF_GET_ATT_INT, NF_GET_ATT_INT1, NF_GET_ATT_INT2

NCAGTC

NF_GET_ATT_TEXT

NCAINQ

NF_INQ_ATT, NF_INQ_ATTID, NF_INQ_ATTLEN, NF_INQ_ATTTYPE

NCANAM

NF_INQ_ATTNAME

NCAPT

NF_PUT_ATT_DOUBLE, NF_PUT_ATT_REAL, NF_PUT_ATT_INT, NF_PUT_ATT_INT1, NF_PUT_ATT_INT2

NCAPTC

NF_PUT_ATT_TEXT

NCAREN

NF_RENAME_ATT

NCCLOS

NF_CLOSE

NCCRE

NF_CREATE

NCDDEF

NF_DEF_DIM

NCDID

NF_INQ_DIMID

NCDINQ

NF_INQ_DIM, NF_INQ_DIMLEN, NF_INQ_DIMNAME

NCDREN

NF_RENAME_DIM

NCENDF

NF_ENDDEF

NCGOPT

(none)

NCINQ

NF_INQ, NF_INQ_NATTS, NF_INQ_NDIMS, NF_INQ_NVARS, NF_INQ_UNLIMDIM

NCOPN

NF_OPEN

NCPOPT

(none)

NCREDF

NF_REDEF

NCSFIL

NF_SET_FILL

NCSNC

NF_SYNC

NCTLEN

(none)

NCVDEF

NF_DEF_VAR

NCVG1C

NF_GET_VAR1_TEXT

NCVGGC

NF_GET_VARM_TEXT, NF_GET_VARS_TEXT

NCVGT

NF_GET_VARA_DOUBLE, NF_GET_VARA_REAL, NF_GET_VARA_INT, NF_GET_VARA_INT1, NF_GET_VARA_INT2

NCVGT1

NF_GET_VAR1_DOUBLE, NF_GET_VAR1_REAL, NF_GET_VAR1_INT, NF_GET_VAR1_INT1, NF_GET_VAR1_INT2

NCVGTC

NF_GET_VARA_TEXT

NCVGTG

NF_GET_VARM_DOUBLE, NF_GET_VARM_REAL, NF_GET_VARM_INT, NF_GET_VARM_INT1, NF_GET_VARM_INT2, NF_GET_VARS_DOUBLE, NF_GET_VARS_REAL, NF_GET_VARS_INT, NF_GET_VARS_INT1, NF_GET_VARS_INT2

NCVID

NF_INQ_VARID

NCVINQ

NF_INQ_VAR, NF_INQ_VARDIMID, NF_INQ_VARNAME, NF_INQ_VARNATTS, NF_INQ_VARNDIMS, NF_INQ_VARTYPE

NCVP1C

NF_PUT_VAR1_TEXT

NCVPGC

NF_PUT_VARM_TEXT, NF_PUT_VARS_TEXT

NCVPT

NF_PUT_VARA_DOUBLE, NF_PUT_VARA_REAL, NF_PUT_VARA_INT, NF_PUT_VARA_INT1, NF_PUT_VARA_INT2

NCVPT1

NF_PUT_VAR1_DOUBLE, NF_PUT_VAR1_REAL, NF_PUT_VAR1_INT, NF_PUT_VAR1_INT1, NF_PUT_VAR1_INT2

NCVPTC

NF_PUT_VARA_TEXT

NCVPTG

NF_PUT_VARM_DOUBLE, NF_PUT_VARM_REAL, NF_PUT_VARM_INT, NF_PUT_VARM_INT1, NF_PUT_VARM_INT2, NF_PUT_VARS_DOUBLE, NF_PUT_VARS_REAL, NF_PUT_VARS_INT, NF_PUT_VARS_INT1, NF_PUT_VARS_INT2

NCVREN

NF_RENAME_VAR

(none)

NF_INQ_LIBVERS

(none)

NF_STRERROR


NetCDF User's Guide for FORTRAN - 7 Nov 1997

[Next] [Previous] [Top] [Contents] [Index] [netCDF Home Page] [Unidata Home Page]

<http://www.unidata.ucar.edu>