0

I am interested in calculating Monthly NDVI anomaly using Landsat. My objective will calculate long term monthly NDVI with all Landsat collection 2 sensors. In this simpler example I am only using Landsat 8 for one year.

I first gather, filter, map and return Landsat NDVI image collection.

var collection = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
.filterBounds(aoi)
.filterDate(startDate,endDate)
.filter(ee.Filter.calendarRange(startJulian,endJulian))
.select(['SR_B[1-7]','^ST_B.*','QA_PIXEL','QA_RADSAT'])
.map(maskL89sr)
.select('^SR_B.*')
.map(addNDVI);

Then after calculating my date variables, I create monthly mean ndvi collection. Then I group by mean and reduce to calculate the ndvi anomaly.

var means = ee.ImageCollection(ee.List.sequence(1, 12)
  .map(function(m) {
    return collection.filter(ee.Filter.calendarRange(m, m,'month'))
        .mean()
        .set('month', m);
}));

var dates = monthList.map(function(index) {
  return startDate.advance(index, 'month');
});

// Group by month, and then reduce within groups by mean();
// the result is an ImageCollection with one image for each month
var byMonth = ee.ImageCollection.fromImages(
  dates.map(function(date) {
    var beginning = date;
    var end = ee.Date(date).advance(1, 'month');
    var mean = collection.filterDate(beginning, end)
    .mean()
    .set('date', date);
    var nBands = mean.bandNames().size()
    
    var month = ee.Date(date).getRelative('month','year').add(1);
    return mean.subtract(
      means.filter(ee.Filter.eq('month', month)).first())
      .set('date', date)
      .set('nBands',nBands);
}));

The error I get is Image.subtract: If one image has no bands, the other must also have no bands. Got 0 and 1. I tried to correct this by adding a band indicating band size called nBands and filtered the collection to have an nband > 0..but to no avail. Here is the reproducible code.

1 Answer 1

1

The error you encountered occurred due to attempting to filter images outside the date range of the image collection containing NDVI mean values. While the collection variable comprises images from January 1, 2015 to December 31, 2015, the dates list includes dates extending until January 1, 2016.

You can fix it by getting a list of 12 months (0 to 11):

var monthList = ee.List.sequence(0,nMonths.subtract(1));

I hope this helps you.

2
  • Thanks that answer worked beautifully, that makes sense! I now have to figure out how make this code run faster so I can export results and do next stepss. Any suggestions on appropriate code review channels are appreciated.
    – pbaltezar
    Commented Apr 8 at 17:27
  • 1
    I suggest you follow the coding best practices guidelines. Changes such as avoiding if/else loops and clip operations should speed up your code.
    – ciranzo
    Commented Apr 8 at 19:23

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.