Go to the previous, next section.

Adding New Dimensions, Variables, Attributes

An existing netCDF file can be extensively altered. New dimensions, variables, and attributes can be added or existing ones renamed, and existing attributes can be deleted. Existing dimensions, variables, and attributes can be renamed. The following code template lists a typical sequence of calls to add new netCDF components to an existing file:

    ncopen           /* open existing netCDF file */
      ...
    ncredef          /* put it into define mode */
        ...
      ncdimdef       /* define additional dimensions (if any) */
        ...
      ncvardef       /* define additional variables (if any) */
        ...
      ncattput       /* define additional attributes (if any) */
        ...
    ncendef          /* check all definitions, leave define mode */
      ...
    ncvarput         /* provide values for new variables */
      ...
    ncclose          /* save netCDF file */

In FORTRAN, the corresponding sequence looks like this:

    NCOPN             !  open existing netCDF 
      ...
    NCREDF            !  put it into define mode 
        ...
      NCDDEF          !  define additional dimensions (if any) 
        ...
      NCVDEF          !  define additional variables (if any) 
        ...
      NCAPT or NCAPTC !  define additional attributes (if any) 
        ...
    NCENDF            !  check all definitions, leave define mode 
      ...
    NCVPT or NCVPTC   !  provide values for new variables 
      ...
    NCCLOS            !  save netCDF file 

A netCDF file is first opened by the ncopen (or NCOPN) call. This call puts you in data mode, which means existing data values can be accessed and changed, existing attributes can be changed (so long as they do not grow), but nothing can be added. To add new netCDF dimensions, variables, or attributes you must leave data mode and enter define mode, by calling ncredef (or NCREDF). In define mode, call ncdimdef (or NCDDEF) to define new dimensions, ncvardef (or NCVDEF) to define new variables (using the new dimensions), and ncattput (or NCAPT or NCAPTC) to assign new attributes to variables or enlarge old attributes.

You can leave define mode and reenter data mode, checking all the new definitions for consistency and committing the changes to disk, by calling ncendef (or NCENDF). If you do not wish to reenter data mode, just call ncclose (or NCCLOS), which will have the effect of first calling ncendef (or NCENDF).

Until the ncendef (or NCENDF) call, you may back out of all the redefinitions made in define mode and restore the previous state of the netCDF by calling ncabort (or NCABOR). You may also use the ncabort call to restore the netCDF to a consistent state if the call to ncendef (or NCENDF) fails. If you have called ncclose (or NCCLOS) from definition mode and the implied call to ncendef (or NCENDF) fails, ncabort (or NCABOR) will automatically be called to close the netCDF in its previous consistent state (before you entered define mode).

Go to the previous, next section.