Fanning Software Consulting

NetCDF File Won't Come Out of Define Mode

QUESTION: I've followed the directions on how to write a netCDF file in Ken Bowman's book almost verbatim, but when I run my code I get the following error.

   % NCDF_CONTROL: Attempt to take the file out of define mode 
    (ENDEF) failed. (NC_ERROR=-45) 

The error points to the line in the file where I am using the NCDF_Control command to exit the netCDF define mode and get into the netCDF data mode so I can actually write the variables to the file. Previously I have just been defining global attributes, dimensions, and the variables themselves. Do you have any idea what the problem is?

ANSWER: Yes, having stared at this problem for over three hours myself, I know exactly what the problem could be. Are you by any chance defining a _FillValue attribute for one of your variables? If so, there is a completely undocumented requirement that the data type of the fill value must match the data type of the variable it will be an attribute of. That is to say, if your variable (identified by variableID) is type FLOAT, then the _FillValue attribute must also be of type FLOAT. You may have to force the fill value to be the correct type by setting one of the type keywords. For example, like this.

   NCDF_ATTPUT, fileID, variableID, '_FillValue', Float(fillValue), /FLOAT

If there is a type mismatch, then you will not be able to take the netCDF file out of define mode and you will get an error like this, even though the offending line may be far away from the NCDF_Control command in the file. This is really an error that may be impossible to track down without the help of the good folks on the IDL newsgroup.

Version of IDL used to prepare this article: IDL 7.1.

Web Coyote's Guide to IDL Programming