NCL Home > Documentation > Functions > File I/O

getfilevartypes

Returns the types of the named variables stored in the given file.

Prototype

	function getfilevartypes (
		thefile [1] : file,    
		var         : string   
	)

	return_val [*] :  string

Arguments

thefile

A reference to a file created from a call to addfile or addfiles. The file referenced must be one in the supported file format list.

var

A list of variable names in file referenced by thefile.

Description

Given a file referenced by thefile, this function returns the type of each variable name listed. A missing value is returned for any variable name that doesn't exist in the file.

See Also

getfilevaratts, getfilevardimnames, getfilevardimsizes
getfilevarnames, addfile, addfiles

Examples

Example 1

Open a netCDF file, retrieve all the variables names, and returns the type of each variable.

  f = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/Ustorm.cdf","r")

  varNames = getfilevarnames(f)           ; all variable names
  varTypes = getfilevartypes(f,varNames)  ; each variable type

  print("Type of '" + varNames + "' is " + varTypes)

Output:

(0)     Type of 'reftime' is character
(1)     Type of 'lon' is float
(2)     Type of 'lat' is float
(3)     Type of 'timestep' is integer
(4)     Type of 'u' is float

Example 2

Open a netCDF file and determine the type of a specified variable. If it is of type "float" or "double" then read directly. If it is of type "short" or "byte" use short2flt or byte2flt functions to unpack the data.

  load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"

  f = addfile("foo.nc", "r")

  varType = getfilevartypes(f,"SST") 

  if (varType.eq."float" .or. varType.eq."double") then
      sst = f->SST
  end if

  if (varType.eq."short") then
      sst = short2flt(f->SST )
  end if
Example 3

For each variable on a file, print its type, attributes, dimension names, and dimension sizes. It's possible for a variable to not contain dimension names and/or attributes, so the any and ismissing functions are used to check for this:

  f = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/Ustorm.cdf","r")

  vnames = getfilevarnames(f)           ; all variable names
  nvars  = dimsizes(vnames)

  do nv = 0,nvars-1
    vtype  = getfilevartypes(f,vnames(nv))
    vatts  := getfilevaratts(f,vnames(nv))       ; The := is important because these
    dnames := getfilevardimnames(f,vnames(nv))   ; variables may be a different size
    dsizes := getfilevardimsizes(f,vnames(nv))   ; the next time through the loop

;   print("==================================================")
    print("Variable '" + vnames(nv) + "'")
    print("  type            : " + vtype)
    if(.not.any(ismissing(dnames))) then
      print("  dimension names : " + str_join(dnames,","))
    else
      print("  dimension names : ")
    end if
    print("  dimension sizes : " + str_join(""+dsizes,","))
    if(.not.any(ismissing(vatts))) then
      print("  attributes      : " + str_join(vatts,","))
    else
      print("  attributes      : ")
    end if
 end do

Output:

Variable 'u'
  type            : float
  dimension names : timestep,lat,lon
  dimension sizes : 64,33,36
  attributes      : _FillValue
Variable 'timestep'
  type            : integer
  dimension names : timestep
  dimension sizes : 64
  attributes      : 
Variable 'lat'
  type            : float
  dimension names : lat
  dimension sizes : 33
  attributes      : 
Variable 'lon'
  type            : float
  dimension names : lon
  dimension sizes : 36
  attributes      : 
Variable 'reftime'
  type            : character
  dimension names : timelen
  dimension sizes : 20
  attributes      : units,long_name