Variable types in CDAT
Goal: Learn about variable types in CDAT.
A variable is essentially a multidimensional data array, augmented with a domain, a set of attributes, and optionally a spatial and/or temporal coordinate system. It can be obtained either from a file, a collection of files, or as the result of computation. Correspondingly there are three types of variables in CDMS:
- A file variable is a variable associated with a single data file. Setting or referencing a file variable generates I/O operations
- A transient variable is an ‘in-memory’ object not associated with a file or dataset. Transient variables result from a computation or I/O operation.
- A dataset variable is a variable associated with a collection of files. Reference to a dataset variable reads data, possibly from multiple files. Dataset variables are read-only.
File variable:
# import necessary module
import cdms
# open the file to read, return "f" - the file object
f = cdms.open(’sample.nc’,’r+’)
# get a file variable u from the opened file
u = f.getVariable('u')
# or equivalent, u2 is also a file variable
u2 = f['u']
u
<Variable: u, file: sample.nc, shape: (1, 2, 80, 97)>
Transient variable:
# to get a transient variable use the following syntax:
u = f('u') # notice the patentesis are "( )" here
u
u
array(
array (1,2,80,97) , type = f, has 15520 elements)
Dataset variable:
# open many files described in the 'sample.xml' file
filepath = os.path.join(sys.prefix, 'sample_data/cdtest10.xml')
f2 = cdms.open(filepath)
f2.listall
<bound method Dataset.listall of <Dataset: 'cdtest10',
URI: '.../sample_data/cdtest10.xml',
mode: 'r', status: open>>
# check what variables exist
f2.listvariables()
['bounds_latitude', 'bounds_time', 'bounds_longitude',
'u', 't', 'v']
u3 = f2('u') # u3 is a dataset variable
# get all info about this variable
u.info()
*** Description of Slab u ***
id: u
shape: (48, 16, 32)
filename: cdtest10
missing_value: 0
comments:
grid_name: grid_16x32
grid_type: None
time_statistic:
long_name:
units: m/s
datatype: Int
name_in_file: u
Grid has Python id -0x491b9214.
Gridtype: generic
Grid shape: (16, 32)
Order: yx
** Dimension 1 **
id: time_48
Designated a time axis.
units: months since 2000-1
Length: 48
First: 0.0
Last: 59.0
Other axis attributes:
name_in_file: time
datatype: Double
partition: [[ 0,12,]
[12,24,]
[24,36,]
[36,48,] ]
isvar: true
Python id: -0x491b9394
** Dimension 2 **
id: latitude
Designated a latitude axis.
units:
Length: 16
First: -90.0
Last: 90.0
Other axis attributes:
datatype: Double
isvar: true
axis: Y
Python id: -0x491b9294
** Dimension 3 **
id: longitude
Designated a longitude axis.
units:
Length: 32
First: 0.0
Last: 348.75
Other axis attributes:
modulo: 360.0
datatype: Double
isvar: true
axis: X
topology: circular
Python id: -0x491b9374
*** End of description for u ***
To learn about a dataset variable see 'Opening Dataset' section of the CDAT Programing Tips and Tricks.