2. Preview
• What is an array and the NumPy package
– Creating arrays
– Array indexing
– Array inquiry
– Array manipulation
– Array operations
• What is (in) CDAT?
– Masked variables, axes
– Brief tour of vcdat
4. Creating arrays
• Use the array function on a list:
import numpy
a = numpy.array([[2, 3, -5],[21, -2, 1]])
• The array function will match the array type to
the contents of the list.
• To force a certain numerical type for the array,
set the dtype keyword to a type code:
a = numpy.array([[2, 3, -5],[21, -2, 1]],
dtype='d')
5. Creating arrays (cont.)
• Some common typecodes:
– 'd': Double precision floating
– 'f': Single precision floating
– 'i': Short integer
– 'l': Long integer
• To create an array of a given shape filled with zeros,
use the zeros function (with dtype being optional):
a = numpy.zeros((3,2), dtype='d')
• To create an array the same as range, use the
arange function (again dtype is optional):
a = numpy.arange(10)
6. Array indexing
• Like lists, element addresses start with zero, so the first
element of 1‐D array a is a[0], the second is a[1], etc.
• Like lists, you can reference elements starting from the end,
e.g., element a[-1] is the last element in a 1‐D array.
• Slicing an array:
– Element addresses in a range are separated by a colon.
– The lower limit is inclusive, and the upper limit is exclusive.
• Type the following in the Python interpreter:
import numpy
a = numpy.array([2, 3.2, 5.5, -6.4, -2.2, 2.4])
• What is a[1] equal to? a[1:4]? Share your answers
with your neighbor.
9. Array manipulation
• Reshape the array: numpy.reshape(a, (2,3))
• Transpose the array: numpy.transpose(a)
• Flatten the array into a 1‐D array: numpy.ravel(a)
• Repeat array elements: numpy.repeat(a,3)
• Convert array a to another type:
b = a.astype('f')
where the argument is the typecode for b.
• Try these commands out in your interpreter on an
array you already created and see if you get what you
expect.
10. Array operations: Method 1 (loops)
• Example: Multiply two arrays together, element‐by‐element:
import numpy
shape_a = numpy.shape(a)
product = numpy.zeros(shape_a, dtype='f')
a = numpy.array([[2, 3.2, 5.5, -6.4],
[3, 1, 2.1, 21]])
b = numpy.array([[4, 1.2, -4, 9.1],
[6, 21, 1.5, -27]])
for i in xrange(shape_a[0]):
for j in xrange(shape_a[1]):
product[i,j] = a[i,j] * b[i,j]
• Note the use of xrange (which is like range, but provides only
one element of the list at a time) to create a list of indices.
• Loops are relatively slow.
• What if the two arrays do not have the same shape?
11. Array operations: Method 2 (array syntax)
• Example: Multiply two arrays together, element‐by‐element:
import numpy
a = numpy.array([[2, 3.2, 5.5, -6.4],
[3, 1, 2.1, 21]])
b = numpy.array([[4, 1.2, -4, 9.1],
[6, 21, 1.5, -27]])
product = a * b
• Arithmetic operators are automatically defined to act element‐wise
when operands are NumPy arrays. (Operators have function
equivalents, e.g., product, add, etc.)
• Output array automatically created.
• Operand shapes are automatically checked for compatibility.
• You do not need to know the rank of the arrays ahead of time.
• Faster than loops.
19. What is CDAT?
• Designed for climate science data, CDAT was first released in 1997
• Based on the object‐oriented Python computer language
• Added Packages that are useful to the climate community and other
geophysical sciences
– Climate Data Management System (CDMS)
– NumPy / Masked Array / Metadata
– Visualization (VCS, IaGraphics, Xmgrace, Matplotlib, VTK, Visus, etc.)
– Graphical User Interface (VCDAT)
– XML representation (CDML/NcML) for data sets
• One environment from start to finish
• Integrated with other packages (i.e., LAS, OPeNDAP, ESG, etc.)
• Community Software (BSD open source license)
• URL: http://www‐pcmdi.llnl.gov/software‐portal (CDAT Plone site)
21. A NetCDF example and VCDAT
• Change directory to the following directory
cd /CAS_OBS/mo/sst/HadISST/
• Check the contents of the netcdf file
ncdump sst_HadISST_Climatology_1961-1990.nc | more
• Start vcdat
vcdat &
• Note what happens when you click on the “file”
pulldown arrow
• Select variable “sst”
• Press “plot”
22. Exercise 2: Opening a NetCDF file
import cdms2
DATAPATH = ‘/CAS_OBS/mo/sst/HadISST/’
f = cdms2.open(DATAPATH + ‘sst_HadISST_Climatology_1961-1990.nc’)
# You can query the file
f.listvariables()
# You can “access” the data through file variable
x = f[‘sst’]
# or read all of it into memory
y = f(‘sst’)
# You can get some information about the variables by
x.info()
y.info()
# You can also find out what class the object x or y belong to
print x.__class__
# Close the file
f.close()
24. cdtime module
• Import the cdtime module
import cdtime
• Relative time
r = cdtime.reltime(19, “days since 2011-5-1”)
• Component time
c = cdtime.comptime(2011, 5, 20)
• You can interchange between component and
relative time
c.torel(“days since 2011-1-1”)
r.tocomp()