# Oft-calc

### From Open Foris Wiki

**oft-calc Raster image calculator **

**Description:** based on an input raster file, oft-calc creates an output raster file as result of a simple calculation between the original bands. The bands used for the calculation must be all stacked in the input raster file.

**Usage:** (Version 2.0 ) oft-calc [-inv] [-of format] [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/
CInt16/CInt32/CFloat32/CFloat64}] [-um maskfile] [-{Z/M/Q/C/L/X/M}] <inputfile> <outputfile>

-inv the notation of the equations has changed in version 2.0. In case you want to use the old notations, please use the -inv option.

-of format. Any GDAL output format can be specified. If not specified, output format will be tif.

-ot output data type. If not specified, output data type will be the same as input data type.

-{Z/M/Q/C/L/X} to speed up the processing by reading n lines at the time. The number of lines has to be specified by using Z, M, Q, C, L or X.

Z=2000 M=1000 Q=500 C=100 L=50 X=10

-um mask. If a raster file is provided as a mask, only pixels with value different than 0 in the mask will be used for the calculation.

After defining the first line, following parameters will be asked:

*Number of output bands*

*Input postfix equations*

*Band 1:* The equation for output band 1 has to be specified. The input bands are referred to with #. The implemented operators between input bands include:

+ addition - subtraction / division * multiplication = equals to < less than > larger than ! not equal to ? if clause M maximum of two values m minimum of two values B bit level operator e natural logarithm c pixel column coordinate r pixel row coordinate ^ power e natural logarithm x base-e exponential function

## Contents |

## Examples: Operators

- Get Example data set
- For this exercise following tools are used:
**oft-calc**

#### Addition

Simple band addition: band1 + band2

oft-calc in_image out_image //hit return after defining this line 1 //this number defines the number of bands your out_image will have; hit return again #1 #2 + //type your clause and hit return. The equation is band1 + band2

#### Division

band1 / band2

oft-calc in_image out_image 1 #1 #2 / //The equation is band1 / band2.

//Note that if the equation was band2 / band1, the clause would be #2 #1 /

#### Equals to

if pixel value of band1 equals 0 then set it to 0, otherwise to 1

oft-calc in_image out_image 1 // if(?) band1 = 0 (#1 0 =) then 0 otherwise 1 (1 0) #1 0 = 1 0 ?

#### Boolean

- You can also use boolean larger than operator to determine if #1 > #2

#1 #2 >

#### The usage of the IF clause

if band1 > 50, output=1 else output=0. This also creates also a simple mask containing 1 for pixels of interest and 0 for background

oft-calc in_image out_image 1 #1 50 > 0 1 ? //if(''?'') band1 > 50 (''#1 50 >'') then 1 otherwise 0 (''0 1'') if band1 + band2 = 2, output=1 else output=0

oft-calc in_image out_image 1 #1 #2 + 2 = 0 1 ? //if(''?'') band1 + band2 (''#1 #2 +'') = 2 (''2 ='') then 1 otherwise 0 (''0 1'') if band1 > 50 or band2 > 50, output=1 else output=0

oft-calc in_image out_image 1 #1 50 > #2 50 > 0 1 ? 1 ? //if band1 > 50 (''#1 50 >'') then 1 (''1 ?'') otherwise if band2 > 50 (''#2 50 >'') then 1 otherwise 0 (''0 1 ?'')

## Examples on Applications

#### NDVI

Calculate the NDVI for a Landsat image (band3 = Red band, band4 = NIR Band)

oft-calc -ot Float32 in_image out_image 1 #4 #3 - #4 #3 + / // band4-band3 (#4 #3 -) / band4+band3 (#4 #3 +)

Note that the band4 in the input layerstack image should be the NIR band and the band 3, the Red band. Note also that the output data type should be specified as Float32 in order to have output values from -1 to 1

Oft-ndvi.bash also creates a NDVI image using (NIR-VIS) / (NIR + VIS) . See Oft-ndvi.bash

#### NBR - Normalised Burn Ratio

NBR highlights areas that have burned using Landsat TM.

Calculate the NBR for a Landsat image:

oft-calc -ot Float32 in_image out_image 1 #4 #7 - #4 #7 + / //(b4-b7) / (b4+b7)

In addition, the differenced NBR (dNBR) technique is a form of Change Detection which is used to index the severity of a fire.

Calculate the differenced (or delta) dNBR for NBR_prefire - NBR_postfire:

**Note**: as you can't have two separate input files, one for NBR_prefire and a second for NBR_postfire, you need to combine the two output_bands into one file before applying the equation (band 1 (#1) containing information on NBR_prefire and band 2 (#2) containing info on NBR_postfire):

oft-calc in_image out_image 1 #1 #2 - //band 1 (#1) contains info on NBR_prefire and band 2 (#2) contains NBR_postfire

#### Average of bands

Compute an average of bands 1,2 and 3 of an image:

oft-calc in_image out_image 1 #1 #2 + #3 + 3 / // band1 + band 2 (#1 #2 +) + band3 (#3 +) divided by 3 (3 /)

#### Build a mask from LEDAPS QA layer

- Bit level operators: does the first bit of band 2 equal to 1?

1 #2 B

- to build a mask from LEDAPS QA layer:

1 #1 B 0 2 #1 B 4 #1 B + 8 #1 B + 9 #1 B + 12 #1 B + < 2 1 ? 1 ?

which becomes

1 #1 B : if bit one of band 1 equals to 1 0 : constant 2 #1 B : if bit 2 of band 1 equals to 1 4 #1 B : if bit 4 of band 1 equals to 1 + : sum up the previous two terms 8 #1 B : if bit 8 of band 1 equals to 1 + : sum up previous two terms 9 #1 B : if bit 9 of band 1 equals to 1 + : sum up previous two terms 12 #1 B : if bit 12 of band 1 equals to 1 + : sum up previous two terms < : if previous term is smaller than 2 : output 2 (if clause false) 1 : output 1 (if clause true) ? : if 1 : output 1 (if clause true) ? : if

Now, what happens in practice, is the following:

1) Check bit 1 and record 0 if its is false and 1 if it is true 2) Check bits 2,4,8,9 and 12 and return their sum 3) if output of 2) is larger than zero (second line above) return 1 else return 2 4) if output of 1) is 1 return 1 else return output of 3)

Back to Open Foris Toolkit Main Page