1

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);
 }));

enter image description here

Is there a way of altering this code so it can be averages by month per year and plotted? SO you get different lines on the plot per year which can be used as a visual comparison?

mikejwilliamson
  • 405
  • 1
  • 7
  • 17

1 Answers1

4

To calculate and average value for every month of every year, you would want to map over the possible months and just filter each iteration in the map by that month (here is another post about nesting map functions in EE). For your specific example, here is how I would do it:

var startDate = ee.Date('2013-01-01'); // set start time for analysis
var endDate = ee.Date('2017-12-31'); // set end time for analysis

// calculate the number of months to process
var nMonths = ee.Number(endDate.difference(startDate,'month')).round();

var point = ee.Geometry.Point([-87.02617187499999, 28.05714582901274]);
var sst = ee.ImageCollection('NASA/OCEANDATA/MODIS-Aqua/L3SMI').select('sst')
            .filterDate(startDate, endDate);

var byMonth = ee.ImageCollection(
  // map over each month
  ee.List.sequence(0,nMonths).map(function (n) {
    // calculate the offset from startDate
    var ini = startDate.advance(n,'month');
    // advance just one month
    var end = ini.advance(1,'month');
    // filter and reduce
    return sst.filterDate(ini,end)
                .select(0).mean()
                .set('system:time_start', ini);
}));

print(byMonth);

Map.addLayer(ee.Image(byMonth.first()),{min: 15, max: 35},'SST');

// plot full time series
print(
  ui.Chart.image.series({
    imageCollection: byMonth,
    region: point,
    reducer: ee.Reducer.mean(),
    scale: 1000
  }).setOptions({title: 'SST over time'})
);

// plot a line for each year in series
print(
  ui.Chart.image.doySeriesByYear({
    imageCollection: byMonth,
    bandName:'sst',
    region: point,
    regionReducer: ee.Reducer.mean(),
    scale: 1000
  }).setOptions({title: 'SST over time'})
);

Here is the link to the code: https://code.earthengine.google.com/bb2f654d443d70a91fa89c8fb3cf601d

I wasn't too sure on what you were looking for in your charting so I provided two options: (1) a full time series plot and (2) a plot by DOY like you have above with a line for each year.

I hope this helps!

Kel Markert
  • 807
  • 4
  • 12