1

I have some code, from the help of this post that calculates monthly averages per year for a region of interest and exports it to a csv.

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)

// Pick the scale from the first image
var scale = UK_SST.first().projection().nominalScale()

// month data for dataset

var startyear = 1990; 
var endyear = 2018; 

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.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: ROIEdit,
        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']));

Export.table.toDrive({
collection: monthlySST,
description: 'monthlySST',
fileFormat: 'CSV'
});

The region of interest is broken up into two regions. I have written code to add the ID name as a property to the ROI feature collection.

// adding names to ROI
var FeatCollectAsAList = ROI.toList(ROI.size());
print(FeatCollectAsAList, 'ROI');

// map a function over the list to rename the IDs
var ROIEdit = ee.FeatureCollection(FeatCollectAsAList.map(function(feat){
  var id = ee.Feature(feat).get('id');
  return ee.Feature(feat).set("system:index", id);
}));

I can't quite figure out how to add this to the export. I tried using the set function to add a new column as 'ROI' but it just came out empty.

 return ee.Feature(null, {
    'sst_mean': sst_mean,
     'year': y,
     'month': m,
     'system:time_start': ee.Date.fromYMD(y, m, 1),
     'ROI: RIEdit.get('id')
  })

I could just export the two regions separately and join them together but I thought it might be simple enough to add the ROI as an extra column.

Your Answer

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

Browse other questions tagged or ask your own question.