0

I have function which calculates monthly means from an image collection

var UK_SST = ee.ImageCollection('NOAA/CDR/SST_WHOI/V2').select('sea_surface_temperature')
    .filterDate(ee.Date('1990-01-01'), ee.Date('2019-01-01'))
    .filterBounds(ROI)


// month data for dataset

var startyear = 1990; 
var endyear = 2019; 

var startmonth = 1;
var endmonth = 12;

var startdate = ee.Date.fromYMD(startyear, startmonth, 1);
var enddate = ee.Date.fromYMD(endyear , endmonth, 1);

var years = ee.List.sequence(startyear, endyear);

var months = ee.List.sequence(1, 12);
var monthlySST =  ee.ImageCollection.fromImages(
  years.map(function (y) {
    return months.map(function(m) {
      var w = UK_SST.filter(ee.Filter.calendarRange(y, y, 'year'))
                .filter(ee.Filter.calendarRange(m, m, 'month'))
                .mean();
      return w.set('year', y)
          .set('month', m)
          .set('system:time_start', ee.Date.fromYMD(y, m, 1));

    });
  }).flatten()
);

I'm looking to set the band value as a property so I can export the image collection as a csv with the band values contained. However, when I try to set this

  .set("sst_mean", w.get("sea_surface_temperature"))

In this part of the function.

  return w.set('year', y)
          .set('month', m)
          .set("sst_mean", res.get("sea_surface_temperature"))
          .set('system:time_start', ee.Date.fromYMD(y, m, 1));

Nothing gets set. Any ideas?

1

Your w is an image where each pixel contains monthly means. Maybe you're looking the mean across your entire ROI? If so, you have to call w.reduceRegion().

Try something like this:

// Picking the scale from the first image in the collection
var scale = UK_SST.first().projection().nominalScale()

var months = ee.List.sequence(1, 12);
var monthlySST =  ee.FeatureCollection(
  years.map(function (y) {
    return ee.FeatureCollection(months.map(function(m) {
      var w = UK_SST.filter(ee.Filter.calendarRange(y, y, 'year'))
                .filter(ee.Filter.calendarRange(m, m, 'month'))
                .mean();
      var dict = w.reduceRegion({
        reducer: ee.Reducer.mean(),
        geometry: ROI,
        scale: scale,
        maxPixels: 1e13
      })
      var sst_mean = ee.Algorithms.If(
        dict.contains('sea_surface_temperature'),
        dict.get('sea_surface_temperature'),
        null
      )
      return ee.Feature(null, {
        'sst_mean': sst_mean,
          'year': y,
          'month': m,
          'system:time_start': ee.Date.fromYMD(y, m, 1)
      });
    }));
  })
).flatten().filter(ee.Filter.notNull(['sst_mean']));

https://code.earthengine.google.com/963e8883883e2b2b0444eaeea5c23a33

4
  • Thanks Daniel. Unfortunately when I run this I get the error 'User memory limit exceeded'. code.earthengine.google.com/4a36bb31a2f7ae260f91f18e77057e5a – mikejwilliamson Mar 20 '20 at 16:19
  • Try to export the results instead of printing. – Daniel Wiell Mar 20 '20 at 19:45
  • Exporting doesn't seem to work, file just continues. Could it be that the shapefile/asset i'm using for my ROI is too big? A much smaller shapefile from another region seems to work fine. – mikejwilliamson Mar 23 '20 at 9:30
  • That was the issue, so our code works perfectly! – mikejwilliamson Mar 23 '20 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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