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: Roger Ting <address@hidden> >Subject: Zero value variable >Organization: UCAR/Unidata >Keywords: 200403120402.i2C42ZrV006218 Hi Roger, > Does netcdf subroutine allow a variable with no value? Currently, when i > attempt to read a variable with empty value, it gives a negative value > which causes my program to crash. The return code is still zero even > though the value that been read in is obviously wrong > > > This is how the variable appears in the dump file. > > > No_Prof = _ ; > > > This is how i read in the value > rcode=NF_GET_VAR1_INT(fileid,nprofsid,1,No_Prof(option)) > call checkdataerr('Error in getting number of profile',rcode) > > > I did inquire the variable id before reading it. Shouldn't netcdf > subroutine return an error or zero value in this case? No, netCDF allows variables with no values, and specifies what happens when you try to read a value that has not been written: http://www.unidata.ucar.edu/packages/netcdf/guidef/guidef-12.html#HEADING12-1381 7.16 Fill Values What happens when you try to read a value that was never written in an open netCDF dataset? You might expect that this should always be an error, and that you should get an error message or an error status returned. You do get an error if you try to read data from a netCDF dataset that is not open for reading, if the variable ID is invalid for the specified netCDF dataset, or if the specified indices are not properly within the range defined by the dimension lengths of the specified variable. Otherwise, reading a value that was not written returns a special fill value used to fill in any undefined values when a netCDF variable is first written. You may ignore fill values and use the entire range of a netCDF external data type, but in this case you should make sure you write all data values before reading them. If you know you will be writing all the data before reading it, you can specify that no prefilling of variables with fill values will occur by calling NF_SET_FILL before writing. This may provide a significant performance gain for netCDF writes. The variable attribute _FillValue may be used to specify the fill value for a variable. Their are default fill values for each type, defined in the include file netcdf.inc: NF_FILL_CHAR, NF_FILL_INT1 (same as NF_FILL_BYTE), NF_FILL_INT2 (same as NF_FILL_SHORT), NF_FILL_INT, NF_FILL_REAL (same as NF_FILL_FLOAT), and NF_FILL_DOUBLE. ... This is not an error, because it often turns out to be useful to only write parts of a multidimensional variable and be able to access the variable later to determine which values were written and which values are "missing". For example, when storing sea surface temperature on a grid, missing values are often used for grid points that are over land. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu/staff/russ