Implementing the ESA SNAP FUI Water-Colour Algorithm in GEE
What this software is
This page is an algorithm-porting tutorial: it ports the FUI water-colour index algorithm built into the ESA SNAP software to the Google Earth Engine (GEE) platform. SNAP (Sentinel Application Platform) is free, open-source software developed by ESA for the Sentinel missions of the Copernicus programme. The FUI water-colour index, integrated in SNAP's Optical / Thematic Water Processing / FU Classification module, classifies water colour into 21 grades from deep blue to reddish brown, based on the Forel-Ule colour scale.
Computing a single scene locally in SNAP is convenient, but a long time-series analysis requires downloading and processing many scenes, which is time-consuming. Ported to GEE, the algorithm can use the platform's massive data and computing power to compute long time series of water colour. The page provides the core Java source code of SNAP's FuAlgo, figures of the band coefficients and hue-angle correction coefficients for different sensors, the JavaScript implementation of FUI retrieval for the Landsat series and Sentinel-2 in GEE, and the FUI lookup table with the RGB and hexadecimal colour codes of every grade.
What you can use it for
- Understanding the FUI computation flow: tristimulus values XYZ, chromaticity coordinates, hue angle and its correction, and lookup-table grading
- Computing the FUI water-colour index of Landsat and Sentinel-2 imagery directly in GEE with the provided code
- Mapping FUI grades (1–21) to RGB colours for mapping, using the lookup table
1. ESA SNAP and the FUI water colour index
SNAP (Sentinel Application Platform) is free, open-source software developed by the European Space Agency for the Sentinel data missions of the Copernicus programme. For a detailed introduction, click here.
SNAP integrates the implementations of many algorithms. The one I will introduce is the FUI water colour index, which is integrated in the Optical / Thematic Water Processing / FU Classification module. Together with water temperature, salinity and transparency, water colour is one of the oldest time-series water quality data. The three main optical substances in water — chlorophyll, non-algal suspended matter and CDOM — together with the absorption and scattering of the water molecules themselves, jointly determine the colour the water presents. Based on the Forel-Ule colour scale, water colour is divided into 21 grades from deep blue to reddish brown. Research shows that, because it covers a very wide range of optical characteristics of natural water bodies, the FUI water colour index has strong potential and advantages in monitoring and retrieving water quality at regional and global long time scales.

Systematic research on the FUI water colour index and hue angle based on satellite images began with the European Citclops project in 2012. In that project, Wernand and Novoa et al. gradually improved the colour spectral analysis of the Forel-Ule colorimetric solutions, and built the complete CIE chromaticity coordinate points of the FUI index. The following is the calculation flow of the FUI water colour index based on satellite imagery.

For different remote sensing images, the colour tristimulus values XYZ are calculated according to different band coefficients, and are normalised into a two-dimensional point coordinate (chrx, chry). The included angle α between it and the equal-energy white point (x = y = 1/3), rotating anticlockwise along the positive X-axis direction, is calculated and called the hue angle. The hue angle is reclassified, according to the FUI index lookup table, into the 21 grades of the FUI water colour index, completing the calculation of the FUI water colour index (it is worth noting that, for different sensors, due to the discrete characteristics of their own bands, the centre wavelengths of the R, G and B bands do not correspond one-to-one with the R (645), G (555) and B (469) defined by the CIE-XYZ system, so the hue angle calculated from multispectral images will inevitably have a certain systematic error compared with the integration result of hyperspectral spectra, and hue angle correction is needed).
The above algorithm is already built into ESA SNAP; inputting an image for computation directly gives the calculation result. When we analyse the water colour situation of a lake or reservoir, we generally need to calculate its long time-series water colour index. Although SNAP can conveniently calculate the FU result of a single scene, long time-series analysis and computation require downloading and processing many scenes, and local computation is time-consuming and laborious. Therefore, we consider porting the algorithm built into ESA SNAP to the GEE platform. The source code of SNAP is open-sourced on GitHub, and the core code of the FU calculation can be found:
package org.esa.s3tbx.fu;
import com.bc.ceres.core.Assert;
class FuAlgo {
private static final double HUE_MIN = 45.0;
private static final double HUE_MAX = 234.0;
public static final byte MAX_FU_VALUE = 21;
public static final byte MIN_FU_VALUE = 0;
//等能白光点
private static final double CONST_WHITE_POINT = 0.333333;
//FUI指数查找表
private static final double[] ANGLE_OF_TRANSITIONS = new double[]{
232.0, 227.168, 220.977, 209.994, 190.779, 163.084, 132.999,
109.054, 94.037, 83.346, 74.572, 67.957, 62.186, 56.435,
50.665, 45.129, 39.769, 34.906, 30.439, 26.337, 22.741, 19.0, 19.0
};
private DominantLambdaLookup lambdaLookup;
//三刺激值
private double[] x3Factors;
private double[] y3Factors;
private double[] z3Factors;
//色度角校正值
private double[] polyCoeffs;
FuAlgo(Instrument instrument, boolean includeDominantLambda) {
x3Factors = instrument.getXFactors();
y3Factors = instrument.getYFactors();
z3Factors = instrument.getZFactors();
polyCoeffs = instrument.getPolynomCoefficients();
if (includeDominantLambda) {
lambdaLookup = new DominantLambdaLookup();
}
}
// just for testing
FuAlgo() {
}
void setPolyCoeffs(double[] polyCoeffs) {
this.polyCoeffs = polyCoeffs;
}
void setZ3Factors(double[] z3Factors) {
this.z3Factors = z3Factors;
}
void setY3Factors(double[] y3Factors) {
this.y3Factors = y3Factors;
}
void setX3Factors(double[] x3Factors) {
this.x3Factors = x3Factors;
}
public FuResult compute(double[] spectrum) {
final double x3 = getTristimulusValue(spectrum, x3Factors);
final double y3 = getTristimulusValue(spectrum, y3Factors);
final double z3 = getTristimulusValue(spectrum, z3Factors);
final double denominator = x3 + y3 + z3;
final double chrX = x3 / denominator;
final double chrY = y3 / denominator;
double hue = getHue(chrX, chrY);
final double polyCorr;
if (hue < HUE_MIN || hue > HUE_MAX) {
hue = Double.NaN;
polyCorr = Double.NaN;
} else {
final double hue100 = hue / 100;
polyCorr = getPolyCorr(hue100, polyCoeffs);
}
FuResultImpl result = new FuResultImpl();
result.x3 = x3;
result.y3 = y3;
result.z3 = z3;
result.chrX = chrX;
result.chrY = chrY;
result.hue = hue;
result.polyCorr = polyCorr;
result.hueAngle = hue + polyCorr;
result.fuValue = getFuValue(result.hueAngle);
if (lambdaLookup != null) {
result.domLambda = lambdaLookup.getDominantLambda(result.hueAngle);
}
return result;
}
static byte getFuValue(final double hueAngle) {
if (Double.isNaN(hueAngle)) {
return MIN_FU_VALUE;
}
for (byte i = 0; i < ANGLE_OF_TRANSITIONS.length; i++) {
if (hueAngle > ANGLE_OF_TRANSITIONS[i]) {
return i;
}
}
return MAX_FU_VALUE;
}
double getTristimulusValue(double[] spectrum, double[] factors) {
if (spectrum.length != factors.length) {
throw new IllegalArgumentException("The spectrum must have equal length as factors.");
}
double summation = 0;
for (int i = 0; i < spectrum.length; i++) {
summation = (spectrum[i] * factors[i]) + summation;
}
return summation;
}
double getHue(double chrX, double chrY) {
double atan2 = Math.atan2((chrY - CONST_WHITE_POINT), (chrX - CONST_WHITE_POINT));
final double hue = (180 * atan2) / Math.PI;
return hue < 0 ? hue + 360 : hue;
}
double getPolyCorr(double hue100, double[] constPolyHue) {
Assert.argument(constPolyHue.length == 6, "constPolyHue.length == 6");
double value = 0.0;
for (int i = 0; i < constPolyHue.length; i++) {
if ((i + 1) == constPolyHue.length) {
value += constPolyHue[i];
break;
}
value += constPolyHue[i] * Math.pow(hue100, 5 - i);
}
return value;
}
}2. Implementation of the FUI retrieval algorithm in GEE
Referring to the parameters for different sensors in the papers, the FUI water colour retrieval based on the Landsat series and Sentinel-2 can be implemented in GEE. The figure below shows the band coefficients corresponding to different sensors when calculating the tristimulus values.

The figure below shows the hue angle correction coefficients of different sensors.

According to the band coefficients in the papers, combined with the open-source code of SNAP, the FUI calculation flow can conveniently be ported to the GEE platform. With the help of the massive data and powerful computing ability of the GEE platform, the long time-series water colour results of a place can be easily calculated.
var getFU = function(image){
var X = image.expression('11.053 * b1 + 6.950 * b2 + 51.135 * b3 + 34.457 * b4',{
b1:image.select('B1'),
b2: image.select('B2'),
b3: image.select('B3'),
b4: image.select('B4')
}).rename('X');
var Y = image.expression('1.32 * b1 + 21.053 * b2 + 66.023 * b3 + 18.034 * b4',{
b1:image.select('B1'),
b2: image.select('B2'),
b3: image.select('B3'),
b4: image.select('B4')
}).rename('Y');
var Z = image.expression('58.038 * b1 + 34.931 * b2 + 2.606 * b3 + 0.016 * b4',{
b1: image.select('B1'),
b2: image.select('B2'),
b3: image.select('B3'),
b4: image.select('B4')
}).rename('Z');
var chrx = X.divide((X.add(Y).add(Z))).rename('chrx');
var chry = Y.divide((X.add(Y).add(Z))).rename('chry');
var atan2 = chrx.subtract(0.3333).atan2((chry).subtract(0.3333)).rename('atan2');
var hue_pre = atan2.multiply(ee.Number(180)).divide(Math.PI).rename('hue_pre');
var hue = hue_pre < 0 ? (hue_pre + 360) : hue_pre.rename('hue');
var a = hue.divide(100).rename('a');
var cor = a.pow(5).multiply(-52.16).add(a.pow(4).multiply(373.81))
.add(a.pow(3).multiply(-981.3)).add(a.pow(2).multiply(1134.19))
.add(a.multiply(-533.61)).add(76.72).rename('cor');
var hueAngle = hue.add(cor).rename('hueAngle');
/*
var angle_tran = ee.List([232.0, 227.168, 220.977, 209.994, 190.779,
163.084, 132.999,109.054, 94.037, 83.346, 74.572, 67.957, 62.186, 56.435,50.665,
45.129, 39.769, 34.906, 30.439, 26.337, 22.741, 19.0, 19.0]);
*/
/*
static byte getFuValue(final double hueAngle) {
for (byte i = 0; i < ANGLE_OF_TRANSITIONS.length; i++) {
if (hueAngle > ANGLE_OF_TRANSITIONS[i]) {
return i;
}
}
return MAX_FU_VALUE;
}
*/
var FU = a
.where(hueAngle.gt(19.0).and(hueAngle.lte(22.741)),1)
.where(hueAngle.gt(22.741).and(hueAngle.lte(26.337)),2)
.where(hueAngle.gt(26.337).and(hueAngle.lte(30.439)),3)
.where(hueAngle.gt(30.439).and(hueAngle.lte(34.906)),4)
.where(hueAngle.gt(34.906).and(hueAngle.lte(39.769)),5)
.where(hueAngle.gt(39.769).and(hueAngle.lte(45.129)),6)
.where(hueAngle.gt(45.129).and(hueAngle.lte(50.665)),7)
.where(hueAngle.gt(50.665).and(hueAngle.lte(56.435)),8)
.where(hueAngle.gt(56.435).and(hueAngle.lte(62.186)),9)
.where(hueAngle.gt(62.186).and(hueAngle.lte(67.957)),10)
.where(hueAngle.gt(67.957).and(hueAngle.lte(74.572)),11)
.where(hueAngle.gt(74.572).and(hueAngle.lte(83.346)),12)
.where(hueAngle.gt(83.346).and(hueAngle.lte(94.037)),13)
.where(hueAngle.gt(94.037).and(hueAngle.lte(109.054)),14)
.where(hueAngle.gt(109.054).and(hueAngle.lte(132.999)),15)
.where(hueAngle.gt(132.999).and(hueAngle.lte(163.084)),16)
.where(hueAngle.gt(163.084).and(hueAngle.lte(190.779)),17)
.where(hueAngle.gt(190.779).and(hueAngle.lte(209.994)),18)
.where(hueAngle.gt(209.994).and(hueAngle.lte(220.977)),19)
.where(hueAngle.gt(220.977).and(hueAngle.lte(227.168)),20)
.where(hueAngle.gt(227.168),21).rename('FU');
return image.addBands(FU);
}The following are the FUI index lookup table and the RGB data and colour codes corresponding to the different grades of FUI:

| FUI | R | G | B | Hexadecimal colour code | FUI | R | G | B | Hexadecimal colour code |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 33 | 88 | 188 | #2158BC | 12 | 148 | 182 | 96 | #94B660 |
| 2 | 49 | 109 | 197 | #316DC5 | 13 | 165 | 188 | 118 | #A5BC75 |
| 3 | 50 | 124 | 187 | #327CBB | 14 | 170 | 184 | 109 | #AAB86D |
| 4 | 75 | 128 | 160 | #4B80A0 | 15 | 173 | 181 | 95 | #ADB55F |
| 5 | 86 | 143 | 150 | #568F96 | 16 | 168 | 169 | 101 | #A8A965 |
| 6 | 109 | 146 | 152 | #6D9298 | 17 | 174 | 159 | 92 | #AE9F5C |
| 7 | 105 | 140 | 134 | #698C86 | 18 | 179 | 160 | 83 | #B3A053 |
| 8 | 117 | 158 | 114 | #759E75 | 19 | 175 | 138 | 68 | #AF8A44 |
| 9 | 123 | 166 | 84 | #7BA654 | 20 | 164 | 96 | 10 | #A4600A |
| 10 | 125 | 174 | 56 | #7DAE38 | 21 | 164 | 83 | 8 | #A45308 |
| 11 | 149 | 182 | 69 | #95B645 |
Attached is a small experiment I did in GEE before: https://code.earthengine.google.com/7b0b97aa01a0f86dcf64e10e1dc4869f
In addition, the doctoral thesis of Wang Shenglei of the Chinese Academy of Sciences mentioned another calculation method for the hue angle and a new FUI index lookup table, and elaborated on the hue angle correction method in detail. Interested students can explore it by themselves.
Reference paper addresses
- https://www.mdpi.com/2072-4292/10/2/180
- https://www.mdpi.com/1424-8220/15/10/25663
- http://www.jeos.org/index.php/jeos_rp/article/view/13057
- https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0063766
- https://kns-cnki-net-443.v.cnu.edu.cn/kcms/detail/detail.aspx?dbcode=CDFD&dbname=CDFDLAST2019&filename=1018995624.nh&uniplatform=NZKPT&v=8jR0NvYJM7ur0xgkduGfdTUjQK55ycqt2kmy2mmpMUjOJcydchiAcvB9pLactvP7
