I am using code from Nina Kattler to calculate the mean of max daily NDVI in particular districts in Mali over the growing season in 2019. The original code gives the mean NDVI over the growing season, and I have been struggling to figure out how to change it to calculate the mean of the maximum NDVIs from each day that MODIS takes an image.
To be more explicit, for each district I would like mean_max_NDVI = [sum(max_NDVIs from all images)/(number of images taken)]. My initial approach was to try and add the max method to the imageCollection, but this returns just an image which is not what I believe I need.
var gadm_level = '4';
var area = ee.FeatureCollection("users/geerootfold/gadm36_MLI_4");
print('area', area);
Map.centerObject(area, 6);
Map.addLayer(area, {}, 'area');
// Set the date
var year = '2019'
var startdate = year+'-05-01';
var enddate = year+'-09-30';
print(startdate)
// MODIS NDVI scale is 0.0001, so we need to correct the scale
var NDVI = function(image) {
return image.expression('float(b("NDVI")/10000)')
};
var modis = ee.ImageCollection('MODIS/006/MOD13Q1')
.filterBounds(area)
.filterDate(startdate,enddate);
var modisNDVI = modis.map(NDVI)
print('MODIS after .map', modisNDVI)
// This is an ImageCollection, containing a list of images that are within the startdata/enddate
// We want to make this a single image: a mosaic
//var mosaic = modisNDVI.mosaic()
// .clip(area);
//print('mosaic', mosaic);
var mosaic = modisNDVI.mosaic()
.clip(area);
print('mosaic', mosaic);
// Another alternative is to get the mean of the images from startdate/enddate:
//var mean = modisNDVI.mean()
// .clip(area);
//print('mean', mean);
// Set visualisation parameters
var colour = {
min: 0.0,
max: 1.0,
palette: [
'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301'
],
};
Map.addLayer(mosaic, colour, 'spatial mosaic');
// Add reducer output to the Features in the collection.
var reducers = ee.Reducer.mean().combine({
reducer2: ee.Reducer.stdDev(),
sharedInputs: true
});
var MeansOfFeatures = mosaic.reduceRegions({
collection: area,
reducer: reducers,
scale: 250,
});
print(ee.Feature(MeansOfFeatures.first()))
// Remove the content of .geo column (the geometry)
// https://gis.stackexchange.com/a/328257
var featureCollection = MeansOfFeatures.map(function(feat){
var nullfeat = ee.Feature(null)
return nullfeat.copyProperties(feat)
})
var size=featureCollection.size()
var toList = featureCollection.toList(size)
print(toList, 'toList')
// length + list
var idList = ee.List.sequence(0, size.subtract(1))
var newList = idList.map(function(x){
var index = idList.get(x)
var feat = toList.get(x)
return ee.Feature(feat).set('year', 2019).copyProperties(feat)
})
print(newList, 'newList')
featureCollection = ee.FeatureCollection(newList)
// Export the featureCollection (table) as a CSV file to your Google Drive account
Export.table.toDrive({
collection: featureCollection,
description: 'modis250_ndvi_TZA_adm'+gadm_level+'_'+year,
folder: 'GEE NDVI Output',
fileFormat: 'CSV'
});