Previous: The Need for Bad Pixels

## Recognition and Processing of Bad Pixels

Having been introduced into an array, bad pixels must be recognisable by subsequent algorithms, which must be capable of taking suitable action to allow for the missing pixel values (normally, this will mean disregarding them in an appropriate manner).

For purposes of recognition, bad pixels are assigned a unique value, appropriate to their numeric type and reserved for this purpose. This value is identified by a symbolic name of the form VAL__BADx, where ``x'' identifies the numeric type, as follows:

(note the use of a double underscore character in this naming convention). These symbolic constants, along with others relating to the HDS primitive numeric types, are defined in the include file PRM_PAR (see SUN/39). The appropriate symbolic name should be used for the numeric type being processed.

To give an example of how the need for bad pixels inevitably arises in practice, consider the following simple algorithm to divide one single-precision (i.e. `_REAL') array A by another B, to give a result in the array C:

```      INTEGER I, EL
REAL A( EL ), B( EL ), C( EL )

...

DO 1 I = 1, EL
C( I ) = A( I ) / B( I )
1    CONTINUE
```

This algorithm is obviously flawed, because if any element of B is zero, then it will fail. However, we can test for this case, and assign bad pixels to affected elements of the output array as follows:

```*  Define the VAL__BADx constants.
INCLUDE 'PRM_PAR'

...

DO 1 I = 1, EL

*  Test for division by zero and assign a bad value to the output array.
IF ( B( I ) .EQ. 0.0 ) THEN

*  Otherwise, calculate the result normally.
ELSE
C( I ) = A( I ) / B( I )
END IF
1    CONTINUE
```

Subsequent algorithms will then need to recognise these bad pixels and take appropriate action. Normally, if a bad pixel is encountered as input to an algorithm, it should automatically generate a corresponding bad pixel for output, a process known as bad pixel propagation. The above algorithm could therefore be adapted to recognise bad pixels in either of the input arrays (A and B) and to propagate them, as follows:

```      INCLUDE 'PRM_PAR'

...

DO 1 I = 1, EL

*  If either input pixel is bad, then so is the output pixel.
IF ( A( I ) .EQ. VAL__BADR .OR. B( I ) .EQ. VAL__BADR ) THEN

*  Check for division by zero.
ELSE IF ( B( I ) .EQ. 0.0 ) THEN

*  Otherwise, calculate the result normally.
ELSE
C( I ) = A( I ) / B( I )
END IF
1    CONTINUE
```

Different action may be required in other algorithms, but the process illustrated here is typical.