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.
Oops, forgot to CC: support-netcdf on this when I sent it ... > From: address@hidden (Charlie Zender) > Organization: NCAR / CGD > Keywords: 199411300111.AA07035 Hi Charlie, > i'm unclear how one should treat scalar variables with netcdf. > by scalar variable i simply mean a single number. > my method is to define them as variables with 0 dimensions, > write them out with ncvarput1, and read them in with ncvarget1 > with the mindex argument set to 1. this method seems to work > in that the scalar value shows up in the netcdf file, ... That's a correct method, although if you're using Fortran as your P.S. says, you must be using NCVPT1 and NCVGT1. Also in the case of scalars it doesn't matter what value you use for MINDEX since its value isn't needed so its just a placeholder. > ... but > i just noticed that i'm unable to read it back in with > ncvarget or ncvarget1. it always returns zero instead of the > scalar stored in the file. is this because i've screwed up? Maybe, although I'd have to see an example of how are trying to access the data to be sure. > do i have to define scalar variables as one dimensional variables > whose dimension length is one? No, scalars are 0-dimensional variables and they don't have a dimension length. > an example would be helpful OK, here's a Fortran example that reads and writes a scalar value: program fexample include 'netcdf.inc' integer iret * netCDF id integer cdfid * variable ids integer zid * data variables real z * enter define mode cdfid = nccre ('z.nc', NCCLOB, iret) * define variables zid = ncvdef (cdfid, 'z', NCFLOAT, 0, 0, iret) * leave define mode call ncendf(cdfid, iret) * store into z. It doesn't matter what the mindex argument is for a scalar, so we * just use "0" for a placeholder call ncvpt1(cdfid, zid, 0, 3.14159, iret) * read from netCDF scalar variable call ncvgt1(cdfid, zid, 0, z, iret) * verify value was read correctly print *, z end -- Russ Rew UCAR Unidata Program address@hidden P.O. Box 3000 http://www.unidata.ucar.edu/ Boulder, CO 80307-3000