# Cloud pixel percentage

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 (SCL[i] === 3 || SCL[i] === 8 || SCL[i] === 9 || SCL[i] === 10) {
cloudCount++;
}
}
return cloudCount / totalPixels * 100;
}
function evaluatePixel(sample) {
var cloudPercentage = calcCloudPercentage(sample.SCL);
return [cloudPercentage];
}
``````

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.

William, 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 @tecnologia ,

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: [{
}],
output: [
{
id: "isCloud",
bands: 1
},
{
bands: 1
}]
};
}

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