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.
Optional arguments allow additional settings for variables in netCDF-4/HDF5 files. These parameters allow data compression and control of the layout of the data on disk for performance tuning. These parameters may also be used to set the chunk sizes to get chunked storage, or to set the contiguous flag to get contiguous storage.
Variables that make use of one or more unlimited dimensions, compression, or checksums must use chunking. Such variables are created with default chunk sizes of 1 for each unlimited dimension and the dimension length for other dimensions, except that if the resulting chunks are too large, the default chunk sizes for non-record dimensions are reduced.
All parameters after the varid are optional, and only supported if netCDF was built with netCDF-4 features enabled, and if the variable is in a netCDF-4/HDF5 file.
function nf90_def_var(ncid, name, xtype, dimids, varid, contiguous, & chunksizes, deflate_level, shuffle, fletcher32, endianness, & cache_size, cache_nelems, cache_preemption) integer, intent(in) :: ncid character (len = *), intent(in) :: name integer, intent( in) :: xtype integer, dimension(:), intent(in) :: dimids integer, intent(out) :: varid logical, optional, intent(in) :: contiguous integer, optional, dimension(:), intent(in) :: chunksizes integer, optional, intent(in) :: deflate_level logical, optional, intent(in) :: shuffle, fletcher32 integer, optional, intent(in) :: endianness integer, optional, intent(in) :: cache_size, cache_nelems, cache_preemption integer :: nf90_def_var
ncid
name
xtype
dimids
If an integer is passed for this parameter, a 1-D variable is created.
If this parameter is not passed (or is a 1D array of size zero) it means the variable is a scalar with no dimensions.
For classic data model files, if the ID of the unlimited dimension is included, it must be first. In 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.
This argument is optional, and if absent specifies a scalar with no
dimensions.
varid
storage
If NF90_CHUNKED, then chunked storage is used for this variable. Chunk sizes may be specified with the chunksizes parameter. Default sizes will be used if chunking is required and this function is not called.
By default contiguous storage is used for fix-sized variables when
conpression, chunking, shuffle, and checksums are not used.
chunksizes
The total size of a chunk must be less than 4 GiB. That is, the product of all chunksizes and the size of the data (or the size of nc_vlen_t for VLEN types) must be less than 4 GiB. (This is a very large chunk size in any case.)
If not provided, but chunked data are needed, then default chunksizes
will be chosen. For more information see The NetCDF Users Guide.
shuffle
deflate_level
fletcher32
endianness
cache_size
cache_nelems
cache_preemption
NF90_DEF_VAR returns the value NF90_NOERR if no errors occurred. Otherwise, the returned status indicates an error.
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)
In the following example, from nf_test/f90tst_vars2.f90, chunking, checksums, and endianness control are all used in a netCDF-4/HDF5 file.
! Create the netCDF file. call check(nf90_create(FILE_NAME, nf90_netcdf4, ncid, cache_nelems = CACHE_NELEMS, & cache_size = CACHE_SIZE)) ! Define the dimensions. call check(nf90_def_dim(ncid, "x", NX, x_dimid)) call check(nf90_def_dim(ncid, "y", NY, y_dimid)) dimids = (/ y_dimid, x_dimid /) ! Define some variables. chunksizes = (/ NY, NX /) call check(nf90_def_var(ncid, VAR1_NAME, NF90_INT, dimids, varid1, chunksizes = chunksizes, & shuffle = .TRUE., fletcher32 = .TRUE., endianness = nf90_endian_big, deflate_level = DEFLATE_LEVEL)) call check(nf90_def_var(ncid, VAR2_NAME, NF90_INT, dimids, varid2, contiguous = .TRUE.)) call check(nf90_def_var(ncid, VAR3_NAME, NF90_INT64, varid3)) call check(nf90_def_var(ncid, VAR4_NAME, NF90_INT, x_dimid, varid4, contiguous = .TRUE.))