Request builder returns black tiff when visualizing average NDVIs

for the below posted evalscript and request object, i am calculating the ndvi and then calculate the averags.
i want to visualize the band that contain the averages, but response i receive in the request builder is a completly black tiff file.
would you please let me know why that is happeing and how to fix this issue?

request object:

{
  "input": {
	"bounds": {
	  "bbox": [
		12.44693,
		41.870072,
		12.541001,
		41.917096
	  ]
	},
	"data": [
	  {
		"dataFilter": {
		  "timeRange": {
			"from": "2024-06-26T00:00:00Z",
			"to": "2024-07-26T23:59:59Z"
		  }
		},
		"type": "sentinel-2-l2a"
	  }
	]
  },
  "output": {
	"width": 512,
	"height": 343.697,
	"responses": [
	  {
		"identifier": "default",
		"format": {
		  "type": "image/jpeg"
		}
	  }
	]
  },
  "evalscript": "\n//VERSION=3\n\nfunction setup() {\n  return {\n    input: [\"B04\", \"B08\", \"CLD\"],\n    output: {\n          id: \"default\",\n          bands: 1,\n          sampleType: \"FLOAT32\",\n          nodataValue: NaN,\n        },\n  };\n}\n\nfunction samplesParser(samples) {\n    const cloudFreeSamples = new Array();\n    const cloudySamples = new Array();\n\n    for (let i = 0; i < samples.length; i++){\n      if (samples[i].CLD > 0) {\n        cloudySamples.push(samples[i]);\n      } else {\n        cloudFreeSamples.push(samples[i]);\n      }\n    }\n    return [cloudySamples, cloudFreeSamples];\n  }\n\n  function calcAverage(samples) {\n    let sum = 0;\n    for(let i = 0;i < samples.length; i++) {\n      sum += samples[i];\n    }\n    let avg = sum / samples.length;\n    return avg;\n  }\n  \n  function calcNDVIForSamples(samples) {\n    const ndvis = new Array(samples.length).fill(NaN);\n\n    for(let i = 0;i < samples.length; i++) {\n      ndvis[i] = (samples[i].B08 - samples[i].B04) / (samples[i].B08 + samples[i].B04) ;\n    }\n    return ndvis;\n  }\n\nfunction evaluatePixel(samples) {\n  const parsedSamples = samplesParser(samples);\n  const ndvis = calcNDVIForSamples(parsedSamples[1]);\n  const averages = calcAverage(ndvis);\n  return [averages]\n}"
}

evalscript:

//VERSION=3

function setup() {
  return {
	input: ["B04", "B08", "CLD"],
	output: {
		  id: "default",
		  bands: 1,
		  sampleType: "FLOAT32",
		  nodataValue: NaN,
		},
  };
}

function samplesParser(samples) {
	const cloudFreeSamples = new Array();
	const cloudySamples = new Array();

	for (let i = 0; i < samples.length; i++){
	  if (samples[i].CLD > 0) {
		cloudySamples.push(samples[i]);
	  } else {
		cloudFreeSamples.push(samples[i]);
	  }
	}
	return [cloudySamples, cloudFreeSamples];
  }

  function calcAverage(samples) {
	let sum = 0;
	for(let i = 0;i < samples.length; i++) {
	  sum += samples[i];
	}
	let avg = sum / samples.length;
	return avg;
  }
  
  function calcNDVIForSamples(samples) {
	const ndvis = new Array(samples.length).fill(NaN);

	for(let i = 0;i < samples.length; i++) {
	  ndvis[i] = (samples[i].B08 - samples[i].B04) / (samples[i].B08 + samples[i].B04) ;
	}
	return ndvis;
  }

function evaluatePixel(samples) {
  const parsedSamples = samplesParser(samples);
  const ndvis = calcNDVIForSamples(parsedSamples[1]);
  const averages = calcAverage(ndvis);
  return [averages]
}

I think you are missing mosaicking: "TILE" in the setup() function.

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