I am trying to run the following script to get cloudless mosiacs for each month. However, the evalscript outputs error.
//VERSION=3
var scenes_list = [];
function setup() {
return {
input: [{
// Add the dataMask band for data filtering
bands: ["B01", "B02", "B03", "B04", "B08", "B05", "B06", "B07", "B8A", "B11", "B12"],
units: "DN"
}],
output: {
bands: 11
},
// Use TILE instead of SIMPLE so the metadata can be accessed by `scenes` object
mosaicking: Mosaicking.TILE
};
}
// Extract the metadata of the tiles
function updateOutputMetadata(scenes, inputMetadata, outputMetadata) {
let metadata = [];
for (i=0; i<scenes_list.length; i++) {
let acquisition_object = {
"date": {},
"tile_id":{},
"cloud_coverage":{}
};
Object.assign(acquisition_object, {"date": scenes.tiles[i].date});
Object.assign(acquisition_object, {"tile_id": scenes.tiles[i].tileOriginalId});
Object.assign(acquisition_object, {"cloud_coverage": scenes.tiles[i].cloudCoverage});
metadata.push(acquisition_object)
}
outputMetadata.userData = {
"metadata": metadata
}
}
function evaluatePixel(samples) {
let index_for_mosaicking = get_index_for_mosaicking(samples);
if (!scenes_list.includes(index_for_mosaicking)) {
scenes_list.push(index_for_mosaicking)
}
return [samples[index_for_mosaicking].B01,samples[index_for_mosaicking].B02,samples[index_for_mosaicking].B03,samples[index_for_mosaicking].B04,samples[index_for_mosaicking].B08,samples[index_for_mosaicking].B05,samples[index_for_mosaicking].B06,samples[index_for_mosaicking].B07,samples[index_for_mosaicking].B8A,samples[index_for_mosaicking].B11,samples[index_for_mosaicking].B12];
}
// Define a function to get the index of sample with the least cloud coverage
function get_index_for_mosaicking(samples) {
let sample_with_data = [];
for (i=0; i < samples.length; i++) {
// Select the index only if there is data
if (samples[i].dataMask == 1) {
sample_with_data.push(i);
}
}
// Return the first one which has the least cloud coverage
return sample_with_data[0];
}
Also, I am interested in how to acquire the image with least cloud cover in a month, but without mosaicking. At the moment, simple mosaicking yields results, where I suspect the different acquisition from multiple orbits seems to alter the image.
Is there a workaround to get one single acquisition but with least cloud cover in a month.