[Thread Prev][Thread Next][Index]
Re: Read cdf files using fortran
It is usually easy to read cdf files using fortran. A simple
subroutine which does that is pasted below, with comments. A
useful tool is the unix command ncdump -h filename.cdf, which
shows you the header information in the netcdf file. In the
example subroutine below, ncdump will give you the grid sizes
(nx,ny,nt).
The procedure is: 1) open the netcdf file; 2) read the variable
number in the cdf file from the header; 3) read the data;
4) close the file.
It is pretty straightforward for simple files. For more
complicated files, there is a lot of documentation in the
Ferret manual.
Billy K
-------------------------------------------------------------------------------------
subroutine cdf_read (fname,vname,nx,ny,nt,data)
include '/opt/local/include/netcdf.inc' ! system-specific
real data(nx,ny,nt) ! data grid to read
character *31 fname ! filename to read from
character *9 vname ! variable name to read
integer lower(3),upper(3) ! 3-dimensional array
integer dnum ! DNUM is the variable order number
integer ir ! IR is a flag for netcdf library calls
c....................IR is returned by each call to the netcdf library.
c....................IR will be 0 for a successful call, 1 otherwise.
c....................I'm not using IR here, else set flags to test it.
data lower/1,1,1/ ! lower contains the lower corner indices
upper(1)=nx ! upper contains the upper corner indices
upper(2)=ny
upper(3)=nt
c.......................open the netcdf file for reading only (NCNOWRIT)
ncid=ncopn(fname,NCNOWRIT,ir)
dnum=ncvid(ncid,vname,ir) ! get the variable number DNUM
call ncvgt(ncid,dnum,lower,upper,data,ir) ! read the data
call ncclos(ncid,ir) ! close the file
return
end
[Thread Prev][Thread Next][Index]
Dept of Commerce /
NOAA /
OAR /
PMEL /
TMAP
Contact Us | Privacy Policy | Disclaimer | Accessibility Statement