|
5.0 Using FORTRAN to
read neCDF files
The simplest way to create
the Fortran code you need to read a netCDF is to use the fortran programs cdf2fortran.f
(at the above site or
http://www-c4.ucsd.edu/~cids/software/visual.html )
or gennet.for (
www.unidata.ucar.edu/packages/netcdf/contrib.html and
http://www.coaps.fsu.edu/WOCE/html/wcdtools.htm
). These programs read a netCDF file and create another fortran program with
the required calls to the netCDF libraries. This machine generated program only
needs to be compiled (with the correct include file and netCDF libraries). When
executed this code fragment can then read the netCDF file and is ready for the
fortran code to be added to undertake the analysis required by the user. The
variable names reflect the internal structure of the netCDF file and this machine
generated code is quite readable by humans.
5.1 Worked example of
cdf2fortran
The cdf2fortran.f code has
been tested on the samples of the netCDF files for the WOCE Global Data V2.
Retrieve cdf2fortran tar file and use the make file to compile the code. You
will have to check that the include directory and the LIBS directory are correct
for your particular installation of the netCDF libraries. For Windows machines
some extra work may be needed to get these routines to compile. On Unix/Linux
machines to compile cdf2fortran just type
> make cdf2fortran
To execute the cdf2fortran
code
> cdf2fortran rcm00683.cdf
Generated fortran program
called readnet.f
>
Either edit readnet.f file
and correct the "include 'netcdf.inc'" file for your particular installation
and compile
> f77 readnet.f
or compile using the -I
option to get the correct include file and the -l option to get the correct
library files
> f77 -c readnet.f -I/usr/local/netcdf/include
> f77 -o readnet readnet.o
-l/usr/local/netcdf/lib/libnetcdf.a
Sometimes there is a problem
at compilation because the variable names may have the same name as a parameter
name (eg Dimension time(time)). Changing the parameter name fixes this
problem and now you should have a complete fortran code able to read your particular
netCDF file. The result of compiling the above for a sample of the curent meter
data on WOCE Global Data CD ROM2 is shown in Appendix
1 but is equally applicable to the netcdf files in WOCE Global Data V3.
Although this piece of
code is quite long (compared to the examples shown below) it is reliable and
is quick to develop compared reading files that are not self-describing (eg
plain ascii files). The sample code fragment can be generalised to read the
netCDF data for all of the different current meter data. The program gennet.for
works in a very similar way and produces a slightly better documented fortran
code. See Appendix 4 for fixing a bug
in the readnet.for programs created by gennet.for.
|