This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>Keywords: 199506251818.AA12345 Hi Jeremy, > I am currently trying to use the netCDF file format. > I was wondering if there were any example fortran programs > that showed how to write and read a simple using netCDF. > Althoug the user guide gives an outline on how to do this a > working concrete example would be much appreciated. The source distribution includes a test program in netcdf/fortran/ftest.f, but it's not a very good example since its purpose is to test all the Fortran interfaces. If you compose a simple CDL file, say "simple.cdl", to specify the structure of a simple netCDF dataset, you can use ncgen -f simple.cdl to generate Fortran code that will create the netCDF file. This will not produce a great example, but for automatically-generated code, it's not too bad. A program that generates Fortran code to read a netCDF file is available as user-contributed software from: ftp://ftp.unidata.ucar.edu/pub/netcdf/contrib/gennet.f I can't find any great complete Fortran example programs, but I've appended an example sent in from someone at NOAA/FSL who was inquiring about a bug. I hope this is of some help. --Russ ______________________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu program series C This program will open a series of netCDF-format MAPS model output. include 'netcdf.inc' implicit none integer i, n, times, ndims, filenum, nlevels c--Set number of dimensions, pressure levels and time levels here: c (nlevels is the number of interpolated levels for mae program input) parameter(ndims=2,n=39,times=8,filenum=13,nlevels=50) integer start(ndims), count(ndims), cdfid, divid, rcode, timeid integer ncnowrit, qid, qadvtid, tid, tadvtid, dayindex, timesize integer dims, vars, gatts, recid real div(n,times), extdiv(n,times), aveserqadvt(filenum,n,times) real q(n,times), qadvt(n,times), extq(n,times), extqadvt(n,times) real sertempc(filenum,n,times), tadvt(n,times), exttempc(n,times) real exttadvt(n,times), tempc(n,times), sertadvt(filenum,n,times) real serdiv(filenum,n,times), p(n), avesertadvt(filenum,n,times) real aveserdiv(filenum,n,times), serq(filenum,n,times), hr real serqadvt(filenum,n,times), seromegaprime(filenum,n,times) real exttempk(n,times), pmax, pmin, sertempk(filenum,n,times) character filename*70, date*3, name*128 c--Set desired max and min pressure levels for mae input file here: parameter(pmax=950.,pmin=250.,hr=60.*60.) open(unit=60,file='serdiv',form='formatted') open(unit=61,file='avserdiv',form='formatted') open(unit=62,file='sertempc',form='formatted') data start /1,1/ data count /n,times/ data p /1050.,1025.,1000.,975.,950.,925.,900.,875.,850.,825., + 800.,775.,750.,725.,700.,675.,650.,625.,600.,575.,550., + 525.,500.,475.,450.,425.,400.,375.,350.,325.,300.,275., + 250.,225.,200.,175.,150.,125.,100./ c print*,'Enter starting date as a string <month><day>:' c read(*,21) dayindex dayindex=129 do i=1,filenum if(dayindex.eq.132) dayindex = 201 filename = 'doug.nc' print*,'Processing ',filename,' ...' c Make errors non-fatal to allow us to do error handling. c The default is all errors are fatal = call ncpopt(NCVERBOS+NCFATAL) c call ncpopt(0) c call ncpopt(NCVERBOS) C NetCDF subroutine to open netCDF file. cdfid = ncopn(filename, ncnowrit, rcode) print*,'cdfid',cdfid C NetCDF subroutine determining actual dimension of observation times. timeid = ncdid(cdfid,'time', rcode) call ncdinq(cdfid, timeid, name, timesize, rcode) if ( rcode.ne.0 ) print*, 'Error getting time dimension' print *, 'ncdid/ncdinq result:' , name call ncinq(cdfid, dims, vars, gatts, recid, rcode) call ncdinq(cdfid, recid, name, timesize, rcode) if (rcode.ne.0 ) print*, 'Error getting time dimension...' c print*,'timeid',timeid print*,'recid',recid print*,'name is',name print*,'timesize is',timesize print*,'rcode is',rcode enddo end