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: <address@hidden> >Subject: Help! - NetCDF >Organization: UCAR/Unidata >Keywords: 200109061203.f86C3J125207 Hi, > I have an array of integers with values from 0 to 255. > I put them in NC_BYTE variables with the C function > nc_put_vara_uchar(). But I when open the NetCDF file with > ncgen, I find that all integers with values greater than 128 > are changed to -127. Could you kindly tell me is there > any method to solve this problem? I will be very grateful > to you. Thanks! I assume you meant that you opened the file with ncdump rather than ncgen? (The ncdump program shows the values in a netCDF file, but ncgen merely converts a CDL file into a binary netCDF file or generates a C or Fortran program that creates an equivalent netCDF file.) When 8-bit data is stored in a netCDF file, it is just stored as an 8-bit integer. In particular there is no netCDF type for "signed 8-bit integer" versus "unsigned 8-bit integer" as there is in C, partly because some of the languages that read and write netCDF data, for example Fortran, have no type corresponding to unsigned integer data. As it says in the netCDF User's Guide (section 3.1 netCDF external data types): It is possible to interpret byte data as either signed (-128 to 127) or unsigned (0 to 255). However, when reading byte data to be converted into other numeric types, it is interpreted as signed. The ncdump program similarly displays integer data as signed, since there is no signed or unsigned property associated with the data in the file. However, you can still access the data as unsigned by using the function nc_get_vara_uchar(), and you will get back exactly the same unsigned values that you stored with nc_put_vara_uchar(). --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu