We are trying to identify areas where NDVI values were below 0.3 throughout the active growing season. I have modified the script that I found on this forum to meet our need.
Currently, I (think) am averaging the NDVI values, which I am not certain is the correct approach. Also, the script uses four bands as input, which also should not be needed. How do I use only three bands (B4, B4, and dataMask) for generating the desired output?
Appreciate any help.
//VERSION=3
function setup () {
return {
input: ["B02", "B03", "B04", "B08", "dataMask"],
output: {
bands: 4,
sampleType: 'UINT8'
},
mosaicking: "ORBIT"
}
}
function calcNDVI(sample) {
var denom = sample.B04+sample.B08;
return ((denom!=0) ? (sample.B08-sample.B04) / denom : 0.0);
}
function avgSamples(s){
var sum=0
for(var i=0; i<s.length; i++)
if (calcNDVI(s[i]) < .3)
{
sum+=calcNDVI(s[i])
}
let avg = sum/s.length
return avg
}
function evaluatePixel(s) {
let val=avgSamples(s)
return [255, 0, 0, ((val < 0.3) && (val > 0.01))* 255]
}
function preProcessScenes (collections) {
collections.scenes.orbits = collections.scenes.orbits.filter(function (orbit) {
return (new Date(orbit.dateFrom) < new Date("2022-05-01T00:00:00Z")) ||
(new Date(orbit.dateFrom) >= new Date("2022-10-31T00:00:00Z"))
})
return collections
}