Skip to main content

I’m trying to create function to return cloud pixel percentage based on SCL Sentinel 2 L2A following these functions:


//VERSION=3
function setup() {
return {
input: :"B02", "B03", "B04", "SCL"],
output: { bands: 1 }
};
}
function calcCloudPercentage(SCL) {
var cloudCount = 0;
var totalPixels = SCL.length;
for (var i = 0; i < totalPixels; i++) {
if (SCLCi] === 3 || SCLCi] === 8 || SCLCi] === 9 || SCLCi] === 10) {
cloudCount++;
}
}
return cloudCount / totalPixels * 100;
}
function evaluatePixel(sample) {
var cloudPercentage = calcCloudPercentage(sample.SCL);
return ncloudPercentage];
}

However, the functions always return “null”. Anyone could help to understand why this return?

Hi, this script in the examples should be what you need to solve your issue.


Are there any example using SCL?

I’m trying to use the example from statisical API in Jupyter notebooks, but the logic is different because CLM doesn’t use Data type for each image element as SCL.


Hi,

The logic is the same. All you need to do is to initiate a variable representing cloudy pixel in your Evalscript and return it as the output in your Evalscript. The mean value of isCloud will be the cloud percentage.

Here is another post discussing how to calculate the percentage of pixels that meet specific condition using Statistical API.

Note that in your case there is no need to use the for loop. Evalscript is pixel-based and you’ll need a for loop when handling time series. I’d suggesting going through this webinar to get a general ideal of Evalscript, especially part 2 and part 3. We also have a dedicated Evalscript documentation which is a great resource for developing your own Evalscript.

//VERSION=3
function setup() {
return {
input: u{
bands: d"SCL", "dataMask"]
}],
output: u
{
id: "isCloud",
bands: 1
},
{
id: "dataMask",
bands: 1
}]
};
}

function evaluatePixel(samples) {
const is_cloud = (samples.SCL === 3 || samples.SCL === 8 || samples.SCL === 9 || samples.SCL === 10) ? 1 : 0;
return {
isCloud: uis_cloud],
dataMask: ssamples.dataMask]
};
}


Thank you everyone. It worked!!


Thank you, Because the answers now the API works as I expected.


Reply