NCL Home > Documentation > Functions > File I/O


Defines a list of variable names, variable types, and variable dimension names for a supported file.


	procedure filevardef (
		thefile   [1] : file,    
		var_names [*] : string,  
		var_types [*] : string,  
		dim_names [*] : string   



The reference to the file that you want to define the variables in. This reference must be created by the addfile function.


An array of variable names that will appear on the file.


An array of types for each var_names.


The set of dimensions which apply to all names in var_names .


This procedure defines one or more variables in a netCDF or HDF file to be created. var_names can contain multiple names of variables and var_types must contain a type for each variable name in var_names. All variables will be defined with the set of dimension names in dim_names. The dimensions must be in the order desired. The dimensions must also have been defined already either with the procedure filedimdef or by normal variable assignment.

See Also

fileattdef, filedimdef, filevarattdef


Example 1

Write one 2D array and two 3D arrays (with dimension names "lev", "lat", and "lon") to a netCDF file called "":

  f = addfile("","c")
  var_names2D = (/ "PS" /)
  var_names3D = (/ "T", "Q" /)
  varvar_types2D = (/ "float" /)
  varvar_types3D = (/ "float", "float" /)
  filevardef( f, var_names2D, varvar_types2D, (/  "lat", "lon" /) )
  filevardef( f, var_names3D, varvar_types3D, (/  "lev", "lat", "lon" /) )
"ncdump -h" produces the following output:
    netcdf myfile {
            lev = 10 ;
            lat = 73 ;
            lon = 144 ;
            float PS(lat, lon) ;
            float T(lev, lat, lon) ;
            float Q(lev, lat, lon) ;

Example 2

Write float and integer scalars "c" and "i" to a netCDF file. NCL uses the reserved dimension name "ncl_scalar" to identify a scalar value.

    f = addfile("","c")

    filevardef(f, (/"c","i"/), (/"float","integer"/),"ncl_scalar")
    i    = 2
    c    = 3.14
    f->c = c        ; or f->c = 3.14
    f->i = i        ; or f->i = 2
"ncdump" produces the following output:
    netcdf test {
            float c ;
            int i ;
     c = 3.14 ;
     i = 2 ;
Note: it is not actually necessary to use filevardef to define a scalar on a file. You can also do it with :
    f    = addfile("","c")
    c    = 5
    c!0  = "ncl_scalar"
    f->c = c
If "c" contains lots of attributes, then you may want to use a more efficient method for writing the scalar to a file:
   filevardef(f, "c", typeof(c), "ncl_scalar")
   filevarattdef(f,"c", c)   ; Copies c's attributes to a file.
   f->c =  (/c/)             ; "(/" and "/)" prevent the (re)writing of
                             ; c's attributes.