Unidata - To provide the data services, tools, and cyberinfrastructure leadership that advance Earth system science, enhance educational opportunities, and broaden participation. Unidata
         
  advanced  
 

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


6.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. If the file is a NetCDF-4/HDF5 file, the additional types NF_UBYTE, NF_USHORT, NF_UINT, NF_INT64, NF_UINT64, and NF_STRING may be used, as well as a user defined type ID.
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. For expanded model netCDF4/HDF5 files, there may be any number of unlimited dimensions, and they may be used in any element of the dimids array.

If the ID of the unlimited dimension is included, it must be first. 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)
 
 
  Contact Us     Site Map     Search     Terms and Conditions     Privacy Policy     Participation Policy
 
National Science Foundation (NSF) UCAR Office of Programs University Corporation for Atmospheric Research (UCAR)   Unidata is a member of the UCAR Office of Programs, is managed by the University Corporation for Atmospheric Research, and is sponsored by the National Science Foundation.
P.O. Box 3000     Boulder, CO 80307-3000 USA     Tel: 303-497-8643     Fax: 303-497-8690