This guide explains how to create mathematical expressions to calculate custom spectral indices in the RAVI plugin. It also provides practical examples and important tips to avoid common mistakes.

## Available Bands

Sentinel-2 provides the following bands that you can use in your expressions:

Band (use it in the expression) | Name | Description | Wavelength | Spatial Resolution
--- | --- | --- | --- | ---
B1 | Coastal aerosol | Coastal aerosol | 443 nm | 60 m
B2 | Blue | Blue | 490 nm | 10 m
B3 | Green | Green | 560 nm | 10 m
B4 | Red | Red | 665 nm | 10 m
B5 | Red Edge 1 | Red Edge 1 | 705 nm | 20 m
B6 | Red Edge 2 | Red Edge 2 | 740 nm | 20 m
B7 | Red Edge 3 | Red Edge 3 | 783 nm | 20 m
B8 | NIR | Near Infrared | 842 nm | 10 m
B8A | Narrow NIR | Narrow Near Infrared | 865 nm | 20 m
B9 | Water vapor | Water vapor | 940 nm | 60 m
B11 | SWIR 1 | Shortwave Infrared 1 | 1610 nm | 20 m
B12 | SWIR 2 | Shortwave Infrared 2 | 2190 nm | 20 m

## Supported Operators

### Arithmetic Operators

- `+` (addition)
- `-` (subtraction)
- `*` (multiplication)
- `/` (division)
- `**` (exponentiation)

### Relational Operators

- `>` (greater than)
- `<` (less than)
- `>=` (greater than or equal to)
- `<=` (less than or equal to)
- `==` (equal to)
- `!=` (not equal to)

### Logical Operators

- `&&` (logical AND)
- `||` (logical OR)
- `!` (logical NOT)

## Available Mathematical Functions

- `sqrt(x)` - Square root of x
- `abs(x)` - Absolute value of x
- `min(x, y)` - Minimum value between x and y
- `max(x, y)` - Maximum value between x and y
- `log(x)` - Natural logarithm of x
- `exp(x)` - Exponential function \( e^x \)
- `sin(x)`, `cos(x)`, `tan(x)` - Trigonometric functions
- `round(x)` - Round x to the nearest integer
- `floor(x)` - Round x down to the nearest integer
- `ceil(x)` - Round x up to the nearest integer
- `pow(x, y)` - Raise x to the power of y

## Expression Examples

### NDVI (Normalized Difference Vegetation Index)

`(B8 - B4) / (B8 + B4)`

### EVI (Enhanced Vegetation Index)

`2.5 * ((B8 - B4) / (B8 + 6 * B4 - 7.5 * B2 + 1))`

### SAVI (Soil Adjusted Vegetation Index) with L=0.5

`(1 + 0.5) * ((B8 - B4) / (B8 + B4 + 0.5))`

### NDRE (Normalized Difference Red Edge)

`(B8 - B5) / (B8 + B5)`

### NDMI (Normalized Difference Moisture Index)

`(B8 - B11) / (B8 + B11)`

### Conditional Expression

This expression calculates NDVI only when the red band (B4) is greater than 0.2. Otherwise, it returns 0:

`(B4 > 0.2) ? ((B8 - B4) / (B8 + B4)) : 0`

This expression calculates NDMI only when the SWIR1 band (B11) is greater than 0.1. Otherwise, it returns -1:

`(B11 > 0.1) ? ((B8 - B11) / (B8 + B11)) : -1`

### Conditional Expression with Logical Operators

This expression calculates NDVI only when both the red band (B4) is greater than 0.2 and the NIR band (B8) is greater than 0.3. Otherwise, it returns 0:

`(B4 > 0.2 && B8 > 0.3) ? ((B8 - B4) / (B8 + B4)) : 0`

## Important Tips

- Automatic normalization: All bands are already divided by 10000 to convert from digital values to reflectance (0-1).
- Avoid division by zero: Ensure that the denominator in your expressions cannot be zero.
- Use parentheses: To ensure operations are executed in the correct order.
- Test your expression: Start with known indices and modify them to understand how they work.
- Follow Python conventions: Expressions must follow Python syntax, including the use of operators like `**` for exponentiation.

## Attention

When creating complex expressions, test them incrementally to ensure they work as expected. Extreme values or division by zero can cause unexpected results.