NCL Home > Documentation > Functions > File I/O

getfilevaratts

Returns all attribute names associated with a variable on the given file.

Prototype

	function getfilevaratts (
		thefile [1] : file,    
		varname [1] : 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.

varname

The name of the variable.

Description

This function returns a one-dimensional array of attributes for a file variable. It returns a missing value if the variable doesn't contain any attributes, or if the variable is not defined.

Note: since addfiles returns a variable of type "list", you must subscript the list before calling this function.

See Also

getfilevarnames, getfilevardimsizes, getfilevardimnames, getfilevardims, getfilevartypes, getfileatts, addfile, addfiles

Examples

Example 1

    g       = addfile("foo.nc" ,"r")
    slpAtts = getfilevaratts(g,"slp")
    print(slpAtts)

Output:

Variable: slpAtts
Type: string
Total Size: 56 bytes
            14 values
Number of Dimensions: 1
Dimensions and sizes:   [14]
Coordinates: 

(0)     long_name
(1)     valid_range
(2)     actual_range
(3)     units
(4)     add_offset
(5)     scale_factor
(6)     missing_value
(7)     precision
(8)     least_significant_digit
(9)     var_desc
(10)    dataset
(11)    level_desc
(12)    statistic
(13)    parent_stat

Example 2

To print the the variable's attributes on one line, use str_join:

    g = addfile("atmos2.nc" ,"r")
    print(str_join(getfilevaratts(g,"T"),", "))

Output:

(0)     long_name, units, time_op

Example 3

Consider a list of WRF output files starting with the prefix "wrfout_d01_2008-09". To retrieve the attributes associated with a variable across all files, you must subscript the list variable returned by addfiles:

  wrf_files = systemfunc("ls wrfout_d01_2008-09*")
  f = addfiles(wrf_files+".nc","r")
  print(getfilevaratts(f[0],"P")

Output:

(0)     FieldType
(1)     MemoryOrder
(2)     description
(3)     units
(4)     stagger
(5)     coordinates
Example 4

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