Appendix 1: Readnet.for created from cdf2fortran
Sample file produced by the cdf2fortran utility with the netCDF file 'rcm00683.cdf' found in the WOCE CD ROM 2. Note that this fortran routine can be easily generalised to read all of the cm data records on this CD set.
c-----------------------------------------------------------------------
c
c readnet.f
c This file is a fortran template file designed to read the given
c netCDF file into memory.
c
c History:
c Date Name Action
c --------- ------------ --------------------------------------------
c ?? ??? ?? cids Created.
c
c-----------------------------------------------------------------------
c Do not forget to include the -I path_to_netcdf_includes in your
c compile statement
c Required includes.
c include 'netcdf.inc'
include '/usr/local/netcdf/include/netcdf.inc'
c Define Variables.
c Variable ids run sequentially from 1 to nvars = 08
parameter (nvars = 8) ! number of variables
parameter (nrec= 36434) ! change this 'to generalize
parameter (ndims = 1) ! number of dimensions
parameter (itime = 36434)
integer*4 rcode ! error code
integer*4 recdim ! record dimension
real*8 date(nrec)
real*8 time(nrec)
real*4 speed(nrec)
real*4 direction(nrec)
real*4 u(nrec)
real*4 v(nrec)
real*4 temperature(nrec)
real*4 pressure(nrec)
integer*4 start(ndims) ! hyperslab starting index
integer*4 count(ndims) ! hyperslab count from start
integer vdims(ndims) ! max # of var dims
character*1024 strbuf ! string buffer for var
! and attr names
c Open netCDF file.
ncid=ncopn('rcm00683.cdf',ncnowrit,rcode)
c Get info on the record dimension for this file.
call ncinq(ncid,ndims,nvars,ngatts,recdim,rcode)
call ncdinq(ncid,recdim,strbuf,nrecs,rcode)
c nrecs now contains the # of records for this file
c Retrieve data for date variable.
call ncvinq(ncid, 1,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 1,start,count,date,rcode)
c Retrieve data for time variable.
call ncvinq(ncid, 2,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 2,start,count,time,rcode)
c Retrieve data for speed variable.
call ncvinq(ncid, 3,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 3,start,count,speed,rcode)
c Retrieve data for direction variable.
call ncvinq(ncid, 4,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 4,start,count,direction,rcode)
c Retrieve data for u variable.
call ncvinq(ncid, 5,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 5,start,count,u,rcode)
c Retrieve data for v variable.
call ncvinq(ncid, 6,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 6,start,count,v,rcode)
c Retrieve data for temperature variable.
call ncvinq(ncid, 7,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 7,start,count,temperature,rcode)
c Retrieve data for pressure variable.
call ncvinq(ncid, 8,strbuf,nctype,nvdim,vdims,nvatts,rcode)
lenstr=1
do j=1,nvdim
call ncdinq(ncid,vdims(j),strbuf,ndsize,rcode)
lenstr=lenstr*ndsize
start(j)=1
count(j)=ndsize
end do
call ncvgt(ncid, 8,start,count,pressure,rcode)
c ******************************************
c Begin writing statements to use the data.
c ******************************************
c End Program.
stop
end