B Expression syntax

 B.1 Details of expressions
 B.2 Mathematical functions provided
 B.3 Rules for expressions
 B.4 Operator precedence

This appendix discusses the syntax permitted for expressions passed to the expression parser. Expressions are input to CAT as argument EXPR of subroutine CAT_EIDNT (see Section 7).

Expressions have an algebraic format, and comprise: columns, vector column elements, parameters and constants linked by arithmetic operators and mathematical functions. For example, suppose that a catalogue contained scalar columns called x, y and z and parameters called p and q. Some valid expressions are:

x p x + p (x + y + 2) / (p + q) (2.0*x + y + 3.75*p) + 13.0) / (z + 1.8*q) (2)

Remember that in CAT column and parameter names are not case-sensitive. Thus the following column or parameter names would all be considered equivalent:


Vector column elements occur in expressions with their usual syntax: the name of the base column followed by the element number enclosed in square brackets. The first element in a vector is numbered one. For example, an expression to add two to the fourth element of vector FLUX would be ‘FLUX[4] + 2.0’.

B.1 Details of expressions

The arithmetic operators are:

+ addition,
- subtraction,
* multiplication,
/ division.

brackets (‘(’ and ‘)’) may be used as required.

B.2 Mathematical functions provided

Table 16 lists the mathematical functions which are provided. The letters denote data types permitted, coded as follows: B = BYTE, H = half INTEGER, I = INTEGER, R = REAL, D = DOUBLE PRECISION, C = CHARACTER, L = LOGICAL. The appearance of N as an argument means that any numeric type (BHIRD) is permitted, as a result it means that the type is the widest type of any of the arguments. R/D means that the result is REAL unless one or more arguments is of DOUBLE PRECISION type in which case D is the result.

Function Notes

B = BYTE(N) convert to BYTE data type
H = HALF(N) convert to INTEGER*2 data type
I = INT(N) convert to INTEGER data type
R = REAL(N) convert to REAL data type
D = DBLE(N) convert to DOUBLE PRECISION data type
I = NINT(N) convert to nearest INTEGER
N = MIN(N,N) the function must have precisely two arguments
N = MAX(N,N) the function must have precisely two arguments
N = MOD(N,N) remainder
N = ABS(N) absolute value
R/D = SQRT(N) square root
R/D = LOG(N) natural logarithm
R/D = LOG10(N) logarithm to the base 10
R/D = EXP(N) exponential
R/D = SIN(N) sine; argument in radians
R/D = COS(N) cosine; argument in radians
R/D = TAN(N) tangent; argument in radians
R/D = ASIN(N) arc-sine; result in radians
R/D = ACOS(N) arc-cosine; result in radians
R/D = ATAN(N) arc-tangent; result in radians
R/D = ATAN2(N,N) arc-tangent (two arguments) result in radians
I = IAND(I,I) bitwise logical AND
I = IOR(I,I) bitwise logical OR
I = XOR(I,I) bitwise logical exclusive OR
R/D = DTOR(N) degrees to radians conversion
R/D = RTOD(N) radians to degrees conversion
C = UPCASE(C) convert character string to upper case
C = STRIP(C) leading and trailing spaces are removed
C = SUBSTR(C,N,N) returns characters from positions argument 2
to argument 3 inclusive, with the positions
starting at 1
L = NULL(*) .TRUE. if argument is NULL
D = HMSRAD(N,N,N) converts 3 arguments hours, minutes and
seconds to radians
D = DMSRAD(C,N,N,N) first argument is the sign (‘+’ or ‘-’ ),
converts degrees, minutes and seconds to radians
D = GREAT(N,N,N,N) great circle distance between two spherical
coordinates. All the input arguments and the
return argument are in radians. The input arguments
are in the order: (α1, δ1,α2, δ2)
D = PANGLE(N,N,N,N) Position angle of point (α2, δ2) from point
(α1, δ1). All the input arguments and the return
argument are in radians. The input arguments are in
the order: (α1, δ1,α2, δ2)

Table 16: Mathematical functions which may be used in expressions

B.3 Rules for expressions

The expression string can contain constants, column and parameter names, operators, functions, and parentheses. In general the usual rules of algebra and Fortran should be followed, with some minor exceptions, as noted below.

Spaces are permitted between items, except that a function-name must be followed immediately by a left parenthesis. Spaces are not permitted within items such as names and numerical constants, but can be used within character strings and date/time values in curly braces.
Lower-case letters are treated everywhere as identical to the corresponding upper-case letter.
Column and parameter names can be up to CAT__SZCMP characters long, and may consist of letters, digits, and underscores, except that the first character must not be a digit.
Vector elements are supported but with a restricted syntax: they may consist of a name followed by an unsigned integer constant subscript enclosed in square brackets, for example FLUX[4] or MAGNITUDE[13]. The first element of the vector is numbered one.
CHARACTER constants may be enclosed in a pair of single or double quotes; embedded quotes of the same type may be denoted by doubling up on the quote character within the string, for example ’DON’’T’ or "DON""T".
LOGICAL constants may be .TRUE. or .FALSE. but abbreviations of these words are allowed down to .T. and .F.
Numerical constants may appear in any valid form for Fortran 77 (except that embedded spaces are not allowed). Some additional forms are also permitted, as shown below.
%Xstring %Ostring %Bstring for hexadecimal, octal and binary INTEGER constants respectively.
Angles in sexagesimal notation: colons must be used to separate items, for example hours:minutes:seconds (or degrees:minutes:seconds). If there is a leading sign then the value will be taken as degrees:minutes:seconds, otherwise hours:minutes:seconds. In either case the value is converted to RADIANS.
A date/time value may be given as a string enclosed in curly braces; a range of common formats are permitted, with order year-month-day or day-month-year, and the month as a number or three-character abbreviation. The time may follow with colons separating hours:minutes:seconds. Examples of some valid dates:

1992-JUL-26 12:34:56

Relational operators are supported in both Fortran 77 form (for example .GE. .NE.) as well as in the Fortran 90 forms (for example, >= /= ).
Single-symbol forms for .AND. .OR. and .NOT. are provided as an alternative: & |# respectively.
The dots may be left off the Fortran 77 forms of the relational operators and the logical operators .AND. and .OR. where spaces or parentheses separate them from names or constants, but the logical constants and the .NOT. operator need the enclosing dots to distinguish them from other lexical items in all cases.
INTEGER division does not result in truncation (as in Fortran) but produces a floating-point result. The NINT or INT function should be used (as appropriate) if an INTEGER result is required.
The functions MAX and MIN must have exactly two arguments.
All arithmetic is carried out internally in DOUBLE PRECISION (but the compiler works out the effective data type of the result using the normal expression rules).
Exponentiation is performed by log/exp functions, with use of ABS to avoid taking logs of negative arguments, thus -2**3 will come out as ‘+8’ not ‘-8’.

B.4 Operator precedence

The operator precedence rules are show in Table 17. The rules of Fortran 90 are used as far as possible; in this table the larger numbers denote higher precedence (tighter binding).

Precedence Function/operator

2 start/end of expression
4 ( )
6 ,
8 .EQV. .NEQV.
10 .OR.|
12 .AND. &
14 .NOT. #
16 .EQ. .GE. .GT. .LE. .LT. .NE.==   >=   >   <=   <   /=
20 //
22 + - (binary operators)
24 + - (unary operators)
26 * /
28 **
30 all functions

Table 17: Operator precedence rules

Note that all operators except ** associate from left to right, but ** and functions associate from right to left.