Next: , Previous: Language-Types, Up: Variables


4.3 Create a Variable: NF90_DEF_VAR

The function NF90_DEF_VAR adds a new variable to an open netCDF dataset in define mode. It returns (as an argument) a variable ID, given the netCDF ID, the variable name, the variable type, the number of dimensions, and a list of the dimension IDs.

Usage

      function nf90_def_var(ncid, name, xtype, dimids, varid)
        integer,               intent( in) :: ncid
        character (len = *),   intent( in) :: name
        integer,               intent( in) :: xtype
        integer, dimension(:), intent( in) :: dimids
        integer,               intent(out) :: varid
        integer                            :: nf90_def_var
ncid
NetCDF ID, from a previous call to NF90_OPEN or NF90_CREATE.
name
Variable name. Must begin with an alphabetic character, followed by zero or more alphanumeric characters including the underscore ('_'). Case is significant.
xtype
One of the set of predefined netCDF external data types. The type of this parameter, NF90_TYPE, is defined in the netCDF header file. The valid netCDF external data types are NF90_BYTE, NF90_CHAR, NF90_SHORT, NF90_INT, NF90_FLOAT, and NF90_DOUBLE.
dimids
Vector of dimension IDs corresponding to the variable dimensions. For example, a vector of 2 dimension IDs specifies a matrix, 1 specifies a vector, and 0 means the variable is a scalar with no dimensions. If the ID of the unlimited dimension is included, it must be last. This argument is optional, and if absent specifies a scalar with no dimensions.
varid
Returned variable ID.

Errors

NF90_DEF_VAR returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error. Possible causes of errors include:

Example

Here is an example using NF90_DEF_VAR to create a variable named rh of type double with three dimensions, time, lat, and lon in a new netCDF dataset named foo.nc:

      use netcdf
      implicit none
      integer :: status, ncid
      integer :: LonDimId, LatDimId, TimeDimId
      integer :: RhVarId
      ...
      status = nf90_create("foo.nc", nf90_NoClobber, ncid)
      if(status /= nf90_NoErr) call handle_error(status)
      ...
      ! Define the dimensions
      status = nf90_def_dim(ncid, "lat", 5, LatDimId)
      if(status /= nf90_NoErr) call handle_error(status)
      status = nf90_def_dim(ncid, "lon", 10, LonDimId)
      if(status /= nf90_NoErr) call handle_error(status)
      status = nf90_def_dim(ncid, "time", nf90_unlimited, TimeDimId)
      if(status /= nf90_NoErr) call handle_error(status)
      ...
      ! Define the variable
      status = nf90_def_var(ncid, "rh", nf90_double, &
                            (/ LonDimId, LatDimID, TimeDimID /), RhVarId)
      if(status /= nf90_NoErr) call handle_error(status)