This is the code to generate an 36 length imagecollection from 2020 up to 2022:

var months = ee.List.sequence(1, 12);

// Create an image collection for each month
var monthlyImageCollection = ee.ImageCollection.fromImages(
  years.map(function(year) {
    return months.map(function(month) {
      var startDate = ee.Date.fromYMD(year, month, 1);
      var endDate = startDate.advance(1, 'month');

      var sentinel2 = ee.ImageCollection('COPERNICUS/S2_SR')
        .filterMetadata("CLOUDY_PIXEL_PERCENTAGE", 'less_than', 5)
        .filterDate(startDate, endDate)
        .set('year', year)
        .set('month', month);

      return sentinel2;

// Print the original and filtered collections
print("Original Collection", monthlyImageCollection);

but due to the clouds, there aren't any image in some month and there are some images with 0 bands in the results: enter image description here

is there any way to enhance my imagecolloction?

link to my code: https://code.earthengine.google.com/7c00ac10387a75dad7197f8205724dd4

best regards.

2 Answers 2


You should not use If() (ever), as it has sigificant performance implications.

You can simply filter on system:band_names to make sure all the images contain one of the expected bands:

collection.filter(ee.Filter.listContains("system:band_names", "B1"))

You don't need to remove null images in the Image Collection. You can skip the error message by using this function instead:

var addNDVIBand = function(image) {
  var opt = ee.List(image.bandNames()).size();
  var ndvi = ee.Image(image).normalizedDifference(['B8', 'B4']).rename('NDVI');
  return ee.Algorithms.If(opt, 

The function previously computes the size of the list of band names and if it is zero it returns a constant image and if not it calculates the corresponding NDVI and adds that band to the original collection. Complete code can be observed here.

After running it in GEE code editor, I got following result:

enter image description here

At the Tab Console it is clearly observed that NDVI was satisfactorily added where there were useful bands. A image with only one band was obtained in cases with zero bands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.