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.
>To: address@hidden >From: Jongil Han <address@hidden> >Subject: Re: 20001017: attributes "friend" are not allowed >Organization: UCSD >Keywords: 200010171538.e9HFcl422289 Jongil, > By the way, could you send me a sample fortran program to read netcdf > data set if you have? As a beginer, it's not easy to figure out and be > familiar with netcdf data set. A sample program that uses netcdf library > and utility would be very helpful. There are now two Fortran interfaces, one for Fortran-77 and one for Fortran-90. The latter is contained in the netcdf-3.5 beta release, and will be in the final netCDF-3.5 release to be made available in the next week or two. It is described at http://www.unidata.ucar.edu/packages/netcdf/f90/index.html But I assume you want to use the Fortran 77 interface in netCDF 3.4, which is also available in netCDF 3.5. An overview of the netCDF calls needed to read data is in the "Use of the NetCDF Library" chapter of the "NetCDF User's Guide for FORTRAN" at http://www.unidata.ucar.edu/packages/netcdf/guidef/guidef-9.html#HEADING9-0 See especially section 4.2 and 4.3. I've also appended an example of a Fortran program that reads netCDF data. You can generate examples of Fortran programs that write netCDF data by first creating a "CDL file" (a text version of a binary netCDF file, see the User's Guide) and then using the ncgen utility with the "-f" option to generate the Fortran code needed to create the corresponding netCDF file. There is also a large test program in the nf_test/ directory that demonstrates every feature of the interface ... --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu program fgennc include 'netcdf.inc' * error status return integer iret * netCDF id integer ncid * dimension ids integer m_dim integer n_dim integer r_dim * dimension lengths integer m_len integer n_len integer r_len parameter (m_len = 3) parameter (n_len = 2) parameter (r_len = NF_UNLIMITED) * variable ids integer w_id integer x_id integer y_id integer z_id * rank (number of dimensions) for each variable integer w_rank integer x_rank integer y_rank integer z_rank parameter (w_rank = 1) parameter (x_rank = 2) parameter (y_rank = 1) parameter (z_rank = 2) * variable shapes integer w_dims(w_rank) integer x_dims(x_rank) integer y_dims(y_rank) integer z_dims(z_rank) * data variables real w(m_len) real x(n_len, m_len) * open file iret = nf_open('md.nc', NF_NOWRITE, ncid) call check_err(iret) * read w array as reals iret = nf_inq_varid(ncid, 'w', w_id) call check_err(iret) iret = nf_get_var_real(ncid, w_id, w) call check_err(iret) print *, w(1), w(2), w(3) iret = nf_close(ncid) call check_err(iret) end subroutine writerecs(ncid,y_id,z_id) * netCDF id integer ncid * variable ids integer y_id integer z_id include 'netcdf.inc' * error status return integer iret * netCDF dimension sizes for dimensions used with record variables integer n_len parameter (n_len = 2) * rank (number of dimensions) for each variable integer y_rank integer z_rank parameter (y_rank = 1) parameter (z_rank = 2) * starts and counts for array sections of record variables integer y_start(y_rank), y_count(y_rank) integer z_start(z_rank), z_count(z_rank) * data variables integer y_nr parameter (y_nr = 2) real y(y_nr) integer z_nr parameter (z_nr = 2) real z(n_len, z_nr) data y /-1, -2/ data z /-11, -12, -21, -22/ * store y y_start(1) = 1 y_count(1) = y_nr iret = nf_put_vara_real(ncid, y_id, y_start, y_count, y) call check_err(iret) * store z z_start(1) = 1 z_start(2) = 1 z_count(1) = n_len z_count(2) = z_nr iret = nf_put_vara_real(ncid, z_id, z_start, z_count, z) call check_err(iret) end subroutine check_err(iret) integer iret include 'netcdf.inc' if (iret .ne. NF_NOERR) then print *, nf_strerror(iret) stop endif end