Here is a typical sequence of netCDF calls used to create a new netCDF dataset:
NF_CREATE ! create netCDF dataset: enter define mode ... NF_DEF_DIM ! define dimensions: from name and length ... NF_DEF_VAR ! define variables: from name, type, dims ... NF_PUT_ATT ! assign attribute values ... NF_ENDDEF ! end definitions: leave define mode ... NF_PUT_VAR ! provide values for variable ... NF_CLOSE ! close: save new netCDF dataset
Only one call is needed to create a netCDF dataset, at which point you will be in the first of two netCDF modes. When accessing an open netCDF dataset, it is either in define mode or data mode. In define mode, you can create dimensions, variables, and new attributes, but you cannot read or write variable data. In data mode, you can access data and change existing attributes, but you are not permitted to create new dimensions, variables, or attributes.
One call to NF_DEF_DIM is needed for each dimension created. Similarly, one call to NF_DEF_VAR is needed for each variable creation, and one call to a member of the NF_PUT_ATT family is needed for each attribute defined and assigned a value. To leave define mode and enter data mode, call NF_ENDDEF.
Once in data mode, you can add new data to variables, change old values, and change values of existing attributes (so long as the attribute changes do not require more storage space). Single values may be written to a netCDF variable with one of the members of the NF_PUT_VAR1 family, depending on what type of data you have to write. All the values of a variable may be written at once with one of the members of the NF_PUT_VAR family. Arrays or array cross-sections of a variable may be written using members of the NF_PUT_VARA family. Subsampled array sections may be written using members of the NF_PUT_VARS family. Mapped array sections may be written using members of the NF_PUT_VARM family. (Subsampled and mapped access are general forms of data access that are explained later.)
Finally, you should explicitly close all netCDF datasets that have been opened for writing by calling NF_CLOSE. By default, access to the file system is buffered by the netCDF library. If a program terminates abnormally with netCDF datasets open for writing, your most recent modifications may be lost. This default buffering of data is disabled by setting the NF_SHARE flag when opening the dataset. But even if this flag is set, changes to attribute values or changes made in define mode are not written out until NF_SYNC or NF_CLOSE is called.