Using the link here I've managed to calculate a weekly mean anomaly for several regions of interest.
I'm hoping to calculate a cumulative mean anomaly, then work out how many consecutive weeks have been over a specific value (say 1) in the past 12 weeks from a specific date.
I attempted to calculate the cumulative anomaly using the tutorial on the GEE pages (here). However I'm having a few issues.
I calculated a mean for sea surface temperature and calculated daily anomaly using this code
var collection = ee.ImageCollection('NASA/OCEANDATA/MODIS-Aqua/L3SMI')
.select('sst')
var reference = collection.filterDate('2002-01-01', '2012-12-31')
.sort('system:time_start', false);
print('reference', reference)
// Compute the mean of the first 10 years.
var mean = reference.mean();
var series = collection.filterDate('2018-01-01', '2018-12-
31').map(function(image) {
return image.subtract(mean).set('system:time_start',
image.get('system:time_start'));
});
I then calculated an average weekly anomaly using this code
var startDate = '2018-01-01';
var endDate = '2018-12-31';
var weekDifference = ee.Date(startDate).advance(1,
'week').millis().subtract(ee.Date(startDate).millis());
var listMap = ee.List.sequence(ee.Date(startDate).millis(),
ee.Date(endDate).millis(), weekDifference);
//function to get a weekly composite
function getWeeklysstComposite(date) {
// Only include sst for consistent compositing.
var sstWeekly = ee.ImageCollection(series)
.filterBounds(PB)
.filterDate(date, date.advance(1, 'week'))
.select('sst');
var composite = sstWeekly.mean()
.set('system:time_start', date.millis(), 'dateYMD',
date.format('YYYY-MM-dd'),
'numbImages', sstWeekly.size());
return composite;
}
var sstWeeklyANMLY =
ee.ImageCollection.fromImages(listMap.map(function(dateMillis){
var date = ee.Date(dateMillis);
return getWeeklysstComposite(date);
}));
Using the iterate function tutorial I tried to calculate a cumulative mean weekly anomaly
var time0 = reference.first().get('system:time_start');
var first = ee.List([
ee.Image(0).set('system:time_start', time0).select([0], ['sst'])
]);
var accumulate = function(image, list) {
var previous = ee.Image(ee.List(list).get(-1));
var added = image.add(previous)
.set('system:time_start', image.get('system:time_start'));
return ee.List(list).add(added);
};
var cumulative = ee.ImageCollection(ee.List(sstWeeklyANMLY.iterate(accumulate, first)));
However I'm getting this error
ImageCollection (Error)
Image.add: If one image has no bands, the other must also have no bands. Got
0 and 1.
What is going wrong here and how can I then, once I have a cumulative anomaly, calculate how many of the weeks in the preceding 12 preceding a certain date were over a specific threshold (say 4)?
The link to all my code in google earth engine is here
get(-1)as the iterate function gets the next element and returns the running total. – Kuik Oct 07 '19 at 15:50