I have extracted sea surface temperature(SST) images from Google Earth Engine for a specific time period and region

var start = ee.Date(2012-01-01);
  var end = ee.Date(start).advance(1,'month');
  var date_range = ee.DateRange(start,end);

var sstImg = ee.ImageCollection('NASA/OCEANDATA/MODIS-Aqua/L3SMI') 
.filterBounds(reefs_500) //Filter by Study Area
.filterDate(date_range) // startdate, enddate
.sort('system:time_start', false);

I want to calculate the long term mean SST per month for the region and then subtract that value from the image collection above.

I have calculated the mean for each month from 2002-2012 and have this as an image collection for 12 different images one per month.

var sst2002_12 = ee.ImageCollection('NASA/OCEANDATA/MODIS-Aqua/L3SMI')
    .filterDate(ee.Date('2002-01-01'), ee.Date('2012-12-31'))

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

var byMonth = ee.ImageCollection.fromImages(
      months.map(function (m) {
        return sst2002_12.filter(ee.Filter.calendarRange(m, m, 'month'))
                    .set('month', m);

How can I subtract only the mean from the image for the same month e.g. should the image come from January, it takes the Jan (or 01) value from the image collection?

I have this code to subtract a specific value from each image.

var series_daily = sstImg.map(function(image) {
    return image.subtract(mean)
    .set('system:time_start', image.get('system:time_start'));

But not sure how to adapt it so it only picks the correct value to do the subtracting from the list.

1 Answer 1


I would use ee.Date.get to get the month from each image in sstImg. Then I would use that information to filter byMonth. Example:

var series_daily = sstImg.map(function(image) {
    var m = image.date().get('month') // get month 
    var mean = byMonth.filter(ee.Filter.eq('month',m)).first() // filter by month 

    return image.subtract(mean)
    .set('system:time_start', image.get('system:time_start'));
  • Thanks, that makes sense. However I'm getting an error. Error in map(ID=A2016305): Image.subtract, argument 'image2': Invalid type. Expected type: Image<unknown bands>. Actual type: ImageCollection. My code is here if want to check it out code.earthengine.google.com/… Commented Feb 2, 2021 at 9:53
  • Hi @mikejwilliamson. Thanks for the heads-up. I have editted my code snippet. Hope it is de-bugged (still have not ran it; thank you for your link, but I was unable to access the script).
    – korndog
    Commented Feb 2, 2021 at 16:57
  • yeah I kind of figured out the issue and did the same thing, so that's great. Works fine now. Commented Feb 2, 2021 at 17:12

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.