How to eliminate cloudy pixels from the tiff

For the evalscript posted below, i introduced the method cloudy() to eliminate the cloudy pixels from the tiff. in other words, i want have a tiff file contains ndvi values where
the pixels values in the tiff are not covered with clouds.

at the run time, i receive empty tiff.
can you please point out what i am doing wron?

eval-script:

`//VERSION=3
  // Script to extract a time series of NDVI values using 
  // Sentinel 2 Level 2A data and  metadata file.
  
  function setup() {
	return {
	  input: [{
		bands: ["B04", "B08", "CLD"],
		units: "DN"
	  }],
	  output: [
	{
	  id: "default",
	  bands: 1,
	  sampleType: "FLOAT32",
	  nodataValue: NaN,
	},
  ],
	  mosaicking: Mosaicking.ORBIT
	}
  }

  function cloudy(samples) {
	let isCloudy = false;
	samples.forEach((sample) => {
	  if (sample.CLD > 0) isCloudy = true;
	});
	return isCloudy;
  }

  function evaluatePixel(samples) {
	if (samples.length < 1) return [NaN];
	if (cloudy(samples)) return [NaN];

	// Precompute an array to contain NDVI observations
	var n_observations = samples.length;
	let ndvi = new Array(n_observations).fill(NaN);
  
  //Fill the array with NDVI values
	samples.forEach((sample, index) => {
	  ndvi[index] = (sample.B08 - sample.B04) / (sample.B08 + sample.B04) ;
	});
					  
	// get average over the collected ndvi values:
	let sum = 0;
	for(let i = 0;i < ndvi.length; i++) {
	  sum += ndvi[i];
	}
		
	let avg = sum / ndvi.length;
	
	return [avg];
  }`

@burkhard.golla

Your script works for me (meaning for a random image I picked, I got some pixels back)! I guess your cloud masking is a bit too aggressive for the area you are looking at: for a pixel, if any of the dates has a cloud detected, you return NaN. Maybe if your use-case allows it, you could consider simply removing the cloudy pixels from the NDVI calculation?

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