I am intended to apply moving average function on a list of area values but the code returns empty list? Anyone has any idea?
the code link: https://code.earthengine.google.com/5cf9d5a21af90784ae3b3dce79f4a36b
Map.centerObject(geometry)
var modis = imageCollection
.map(function(img){
var band = img.normalizedDifference(['sur_refl_b04','sur_refl_b02']).rename('ndwi');
var thr = band.gt(0.1);
var mask = thr.updateMask(thr);
var area_img = mask.multiply(ee.Image.pixelArea().divide(1e6));
var area_val = ee.Number(mask.reduceRegion({
reducer: ee.Reducer.sum(), geometry: geometry, scale: 1000
}).values().get(0))
var date = img.date().format('YYYY-MM-dd')
return area_img
.copyProperties(img, img.propertyNames())
.set('area',area_val)
.set('date',date)
});
var area = modis.aggregate_array('area')
print('original_area', area)
// Function to calculate moving average
var movingAverage = function(values, windowSize) {
// Initialize an empty array to store the moving average values
var movingAverageValues = [];
// Iterate through each value in the input array
for (var i = 0; i < values.length; i++) {
// Calculate the starting index of the window
var startIndex = Math.max(0, i - Math.floor(windowSize / 2));
// Calculate the ending index of the window
var endIndex = Math.min(values.length - 1, i + Math.ceil(windowSize / 2) - 1);
// Initialize a variable to store the sum of values within the window
var sum = 0;
// Iterate through values within the window and calculate their sum
for (var j = startIndex; j <= endIndex; j++) {
sum += values[j];
}
// Calculate the average of the values within the window and push it to the movingAverageValues array
movingAverageValues.push(sum / (endIndex - startIndex + 1));
}
// Return the array containing the moving average values
return movingAverageValues;
};
var smoothed_area = movingAverage(area,3)
print('smoothed_area', smoothed_area)