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.
Hi Hui, > I am using netcdf function in fortran to read some data out from the netcdf > file. In my netcdf file, my data type is short. So in my fortran program, > I set the 'integer' type and use the function 'ncvgt' to read the data I > want. > I do not know why the output is much much biger than the real values. In > other words, I can not output the correct data. > The following is my script of fortran program and I could not find the > problem. > ****************************************** > program main > parameter (ndims=3) > parameter (nlat=67,nlon=65) > integer datain(nlon,nlat) > integer ncid,iv > integer rcode > integer start(3), edges(3) > data start/1,1,1/ > data edges/nlon,nlat,1/ > include 'netcdf.inc' > iv=5 > ncid = ncopn('../cal.ncep.1dom.nc',ncnowrit,rcode) > DO k =1,124 > start(3)=1 > call ncvgt(ncid,iv,start,edges,datain,rcode) > print *,datain(10,10) > enddo > end > ****************************************************************** > But when I tried to get a single value instead of a array, it is fine. > > Can you give me some hint? You are using the old version 2 Fortran-77 interface, which required that the size of the data on disk matched the size of the data in your program. In other words, if you use ncvgt to read in data from 16-bit integers, you had better be reading the data into an array of 16-bit integers in your program. So one possible fix for your problem is to just change integer datain(nlon,nlat) to integer*2 datain(nlon,nlat) if your Fortran compiler supports that notation to indicate an array of short (16-bit) integers. The Fortran-77 standard did not deal with integer sizes and provided no standard way to declare 16-bit integers, but many Fortran compilers support notations such as "integer*2" to indicate 2-byte (16-bit) integers. Alternatively, you could use the later version 3 f77 or f90 interface, both of which permit on-the-fly type conversion to 32-bit integers when you read the data. For example, in the f77 interface, you would leave the datain declaration alone but read the data with a call to ierr = nf_get_vara_int(ncid, iv, start, edges, datain) as documented in the Fortran-77 Users Guide http://www.unidata.ucar.edu/packages/netcdf/docs/netcdf-f77.html --Russ