Understanding evalscript/V3 structure in custom scripts

Hi Forum,
I am trying to get my head around the way evalscripts work by converting a simple script to follow the V3 evalscript structure. So if I have the following script functioning in EO Browser:

let kndvi = Math.tanh(Math.pow(((B08 - B04) / (B08 + B04)), 2));

//Visualization, as used in EO Browser:
if (kndvi<-1.1) return [0,0,0];
else if (kndvi<-0.2) return [0.75,0.75,0.75];
else if (kndvi<-0.1) return [0.86,0.86,0.86];
else if (kndvi<0) return [1,1,0.88];
else if (kndvi<0.025) return [1,0.98,0.8];
else if (kndvi<0.05) return [0.93,0.91,0.71];
else if (kndvi<0.075) return [0.87,0.85,0.61];
else if (kndvi<0.1) return [0.8,0.78,0.51];
else if (kndvi<0.125) return [0.74,0.72,0.42];
else if (kndvi<0.15) return [0.69,0.76,0.38];
else if (kndvi<0.175) return [0.64,0.8,0.35];
else if (kndvi<0.2) return [0.57,0.75,0.32];
else if (kndvi<0.25) return [0.5,0.7,0.28];
else if (kndvi<0.3) return [0.44,0.64,0.25];
else if (kndvi<0.35) return [0.38,0.59,0.21];
else if (kndvi<0.4) return [0.31,0.54,0.18];
else if (kndvi<0.45) return [0.25,0.49,0.14];
else if (kndvi<0.5) return [0.19,0.43,0.11];
else if (kndvi<0.55) return [0.13,0.38,0.07];
else if (kndvi<0.6) return [0.06,0.33,0.04];
else return [0,0.27,0];

And I am trying to reorganize it to work with the evalscript structure of “setup” and “evaluatepixel” like this:

//VERSION=3
function setup() {
  return {
    input: ["B04", "B08"],
    output: { bands: 3 }
  };
}

function evaluatePixel(sample) {
let kndvi = Math.tanh(Math.pow((sample.B08 - sample.B04) / (sample.B08 + sample.B04)), 2);
if (kndvi<-1.1) return [0,0,0];
else if (kndvi<-0.2) return [0.75,0.75,0.75];
else if (kndvi<-0.1) return [0.86,0.86,0.86];
else if (kndvi<0) return [1,1,0.88];
else if (kndvi<0.025) return [1,0.98,0.8];
else if (kndvi<0.05) return [0.93,0.91,0.71];
else if (kndvi<0.075) return [0.87,0.85,0.61];
else if (kndvi<0.1) return [0.8,0.78,0.51];
else if (kndvi<0.125) return [0.74,0.72,0.42];
else if (kndvi<0.15) return [0.69,0.76,0.38];
else if (kndvi<0.175) return [0.64,0.8,0.35];
else if (kndvi<0.2) return [0.57,0.75,0.32];
else if (kndvi<0.25) return [0.5,0.7,0.28];
else if (kndvi<0.3) return [0.44,0.64,0.25];
else if (kndvi<0.35) return [0.38,0.59,0.21];
else if (kndvi<0.4) return [0.31,0.54,0.18];
else if (kndvi<0.45) return [0.25,0.49,0.14];
else if (kndvi<0.5) return [0.19,0.43,0.11];
else if (kndvi<0.55) return [0.13,0.38,0.07];
else if (kndvi<0.6) return [0.06,0.33,0.04];
else return [0,0.27,0];
}

The first version gives a nice visualization in various shades of green, eg https://sentinelshare.page.link/zhxG ; the second version returns no error message but all pixels in black: https://sentinelshare.page.link/bXSL.

What am I doing wrong?

The evalscript structure is fine, you are only missing a pair of parentheses in your kndvi calculation. It should work with this:

Math.tanh(Math.pow(((sample.B08 - sample.B04) / (sample.B08 + sample.B04)), 2));

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.