Working with Data

This section covers the ways PV-WAVE works with data. PV-WAVE has been designed so that you can concentrate on the science, engineering, or analysis problem at hand, rather than on syntactical or programming details. Let’s begin by taking a look at:

dynamic data typing

common data types

array-oriented operations

variables

subscripts

using operators

expressions

Dynamic Data Typing

Every variable in PV-WAVE has an attribute known as data type. The data types accepted by PV-WAVE are outlined in Basic Data Types. However, you do not have to declare the data type when you define a variable. When a command statement or expression is entered, PV-WAVE automatically determines the data type of the variable based on the context of its use. Thus, data typing in PV-WAVE is dynamic.

In evaluating expressions, PV-WAVE automatically assumes the data type with the most precision and keeps track of it. Because PV-WAVE evaluates expressions containing operands of different types in the most accurate manner possible, the type of data contained in a variable may change during a work session.

 

Note

Arrays are an exception to the dynamic typing rules. Once the type of an array is established, often by the rules discussed in this section, that type is fixed. Inserting a value into an array that is larger than the array's type can express does not cause the array's type to change. Instead, the value wraps when the limit for the data type is reached. For example:

; create a BYTE array

a = [1b,2b,3b]

; insert a value outside the range of a PV-WAVE BYTE

a(0) = 260

; print the value

PRINT, a(0)

4

; the value has wrapped at the limit of the BYTE data type

; (255)

Array-oriented Operations

In addition to data type, every variable has another attribute called its structure. The structure of an evaluated expression may be either a scalar or an array. A scalar is a single value. An array is a collection of elements, all of the same data type, with one to eight dimensions. For example, one-dimensional arrays might hold time series data, two-dimensional arrays may consist of image or surface data, and three-dimensional arrays may contain a series of frames for an animation or cross sections of a volume.

Writing PV-WAVE routines is simplified since they can accept scalar or array arguments with no change in notation. PV-WAVE determines the structure of the data used and handles operations accordingly.

To facilitate the use of arrays, a set of array-oriented operators are included with PV-WAVE. These allow the specification of complicated computations at a high level using concise array expressions rather than scalars, loops, and IF statements.

Variables

Variables are the building blocks of PV-WAVE. Think of them as your data. In fact, we use these two terms interchangeably throughout the remainder of this document. Quite simply, variables are named locations for storing and retrieving your data.

Defining Variables

A single PV-WAVE variable name can refer to a combination of:

strings

floating-point numbers

integers

complex numbers

scalars

arrays

other data types

In general, a variable is defined simply by equating the variable to an expression using the = symbol. Variable names must begin with a letter, and can be up to 127 characters in length. The second and following characters may be letters, digits, or the symbols _ and $. The following illustrates a very simple variable definition:

; The following statement defines a variable, A. Until assigned a
; new value, whenever you refer to A, the value 15 is used.
A = 3 * 5

When a variable is set to the value of an expression, the type and structure of the variable are copied from the result of the expression. If an existing variable is set to the value of an expression with a different data type, the variable type is changed to match the data type of the expression. For example:

; Replace the type, structure, and value of variable ABC with
; those attributes resulting from the evaluation of the 
; expression XYZ + 1.
ABC = XYZ + 1

Structure Variables

Structure variables allow grouping of related items of different types in a single variable. This is similar to structures in the C language and records in Pascal.

For instance, to process a star catalog, each entry for a star can include the star name, right ascension, declination, and an intensity measured each month over the last 12 months. The structure is defined as:

Star = {Catalog, Name: ' ', Ra: 0.0, Dec: 0.0, Inten: Fltarr(12)}

This statement defines a structure of type Catalog and stores it in the variable Star. This structure contains four tags: Name, Ra, Dec, and Inten. The first tag contains a string; the following two tags contain floating-point scalars, and the fourth tag contains a 12-element floating-point array of one dimension.

Subscripts

Individual elements within an array are referred to by subscripts.

Subscripts provide a means of selecting one or more elements of an array variable. These values are stored in selected array elements. When a subscript reference is used, only the matching element in the array is affected.

Vectors are one-dimensional arrays. In PV-WAVE, the subscript of the first element is zero. For example, if a variable Temperature is a 100-point vector, Temperature(0) is the first point, Temperature(1) is the second point, and Temperature(99) is the last point.

Subscripts in 2D Arrays

Two-dimensional arrays contain data that is addressed by specifying both a column and a row number. The first subscript denotes the column while the second subscript is the row number. Note that this is consistent with FORTRAN and opposite from normal linear algebra. This allows the X value to appear first when subscripting pixel images. For example, if a variable Image contains a 512-by-512 matrix, the following locations are specified by the subscripts:

 

Image(0, 0)

The first point in the first row and column; typically the upper left corner of a matrix or the lower left in an image.

Image(i, j)

The element in column i, row j.

Image (511, 511)

The last element in the last column of the matrix; typically the lower right corner of a matrix or the upper right corner of an image.

Subscript Ranges

Subscript ranges are used to select a subarray from within an array by specifying the starting and ending subscripts in each dimension. The starting and ending values are separated by a colon. Some subarrays are:

 

Temperature(0:4)

A five-element vector containing the first five points of Temperature.

Temperature(50:*)

A fifty-point vector composed of those elements of Temperature from Temperature(50) to Temperature(99); the * denotes the last element.

Image(9, 0:4)

A five-element column vector taken from the first five rows of the tenth column of Image.

Image(100, *)

A 512-element column vector made by extracting every element of the 101st column; the *, when used by itself as a subscript, denotes all points along that dimension.

Array Subscripts

Arrays may also be used as subscripts to select elements of other arrays. If each element of the array is used as a subscript, the result has the same dimension as the subscript array and the type of the subscripted array. For example, in the equation:

new_vec=vec([3, 5, 7, 9])

new_vec is a four-element vector containing the elements vec(3), vec(5), vec(7), and vec(9).

Using Operators

Operators are used to combine variables and constraints into expressions. The operators in PV-WAVE include:

standard operators (add, subtract, multiply, divide, exponential, MOD)

relational operators (EQ, NE, GT, LT, LE, GE)

Boolean operators (AND, OR, NOT, XOR)

In addition, PV-WAVE has an expanded set of operators to perform such operations as:

finding minima and maxima

selecting scalars and subarrays from arrays

concatenating scalars to form arrays

An example of a PV-WAVE operator is the matrix multiplication operator, represented by the # symbol. The example of matrix multiplication below takes two DO loops in FORTRAN. It’s a simple, one-line command PV‑WAVE:

PRINT, [0, 1, 2, 3] # [0, 1, 2, 3]

The result would print as follows:

0 0 0 0
0 1 2 3
0 2 4 6
0 3 6 9

Expressions

Expressions are composed of:

variables

constants

operators

that describe how information is to be computed. Expressions may include variables; however, unlike variables, expressions have no name provide no lasting storage information. Typical expressions are:

4
ABC/5
SIN(A*PI)