Google Earth Engine – Extract SST by Month Per Year Using JavaScript

google-earth-enginejavascriptmodistime series

I'm trying to plot a chart and get data from Google Earth Engine. I'm using MODIS-Aqua/L3SMI data in Earth Engine.

I have used Earth Engines in built functions to compare average sea surface temperatures by day of year per year. However, it's pretty busy and would like to calculate an average per month then plot the different years of the data set. Using the code here I've been able to get an average per month over all years in the data set.

var sst = ee.ImageCollection('NASA/OCEANDATA/MODIS- 
Aqua/L3SMI').select('sst').filterDate(ee.Date('2013-01-01'), ee.Date('2017- 
12-31'))

var byMonth = ee.ImageCollection.fromImages(
months.map(function (m) {
return sst.filter(ee.Filter.calendarRange(m, m, 'month'))
            .select(0).mean()
            .set('month', m);
 }));

Can this code be altered to pull out the year information as well, so I can get the data by per month per year at all?

Best Answer

var sst = ee.ImageCollection('NASA/OCEANDATA/MODIS-Aqua/L3SMI')
    .select('sst')
    .filterDate(ee.Date('2013-01-01'), ee.Date('2017-12-31'))

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

var byMonthYear = ee.ImageCollection.fromImages(
  years.map(function(y) {
    return months.map(function (m) {
      return sst
        .filter(ee.Filter.calendarRange(y, y, 'year'))
        .filter(ee.Filter.calendarRange(m, m, 'month'))
        .mean()
        .set('month', m).set('year', y);
  });
}).flatten());
print(byMonthYear)
Related Question