Mosaicking for sentinel 2A

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.

Hi @aswinbio, in order to fix the evalscript you need to include in the input bands the “dataMask” :

bands: ["B01", "B02", "B03", "B04", "B08", "B05", "B06", "B07", "B8A", "B11", "B12", "dataMask"]

that you are using later in the if sentence to check the condition:

samples[i].dataMask == 1

And regarding the second question, it may be important to know if it would be useful for you to acquire the least cloud cover of the month, but only for the tile of that date. That means that you would not get the full bounding box you are requesting, you only would get the tile. For example:

Let me know if that solution would be ok for you

Thanks for the response @adrian.dipaolo. Yes, having one image per month should be fine. I can merge later, I assume.

So remember you can build your request to get the less cloud coverage image of a month

Screenshot from 2022-12-09 12-00-47

with mosaicking SIMPLE will work for getting the least cloud tile for that area in the month. But let me know if you need further information.

Thanks @adrian.dipaolo . At the moment, I use the method you suggested. I just wanted to automate this process!

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