// UPDATE: The easiest way is now toBands().
var merged = MOD1.toBands();
// For completeness, this is the iterate() way:
var mergeBands = function(image, previous) {
return ee.Image(previous).addBands(image);
};
var merged = MOD1.iterate(mergeBands, ee.Image([]));
Looks like you were pretty close, and with the help of Nicholas Clinton's answer here we can arrive at a solution. The main changes I made were:
- removing unnecessary variable definitions (
first
and time0
)
- adjust how the function mapped to
months
is defined
- added a
years
variable for the same function mapping technique
All of that said, here's an updated script.
//calculate average monthly NDVI for Ukraine
//get MODIS NDVI
var collection = ee.ImageCollection('MODIS/006/MOD13A1').select('NDVI');
//import country boundaries
var countries = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017');
//Subset Ukraine feature from countries.
var ukr = ee.Feature(
countries
.filter(ee.Filter.eq('country_na', 'Ukraine'))
.first()
);
print("ukr",ukr);
//Use a function to iterate through months and calculate average NDVI
var months = ee.List.sequence(1, 12);
print("months",months);
var years = ee.List.sequence(2010, 2020);
print("years",years);
// Map filtering and reducing across year-month combinations and convert to ImageCollection
var yrMo = ee.ImageCollection.fromImages(
years.map(function (y) {
return months.map(function (m) {
return collection
.filter(ee.Filter.calendarRange(y, y, 'year'))
.filter(ee.Filter.calendarRange(m, m, 'month'))
.mean()
.set('year',y)
.set('month',m);
});
}).flatten());
print("yrMo",yrMo);
//Define chart parameters
var chartParam = {
title: 'Monthly average NDVI',
hAxis: {title: 'Time'},
vAxis: {title: 'Average NDVI'},
};
//Plot the chart
var chart = ui.Chart.image.seriesByRegion({
imageCollection: yrMo,
regions: ukr,
reducer: ee.Reducer.mean(),
scale: 500,
xProperty: 'system:index',
seriesProperty: 'PROJECT'
});
//Print chart to console
print(chart.setOptions(chartParam));
Best Answer
Ok, here's some code to solve your problem. Question 2 is addressed first with a 12-layer raster image being exported to your drive, then the second export is an example for a single year (example is with 2001).
Edit: wrong link. https://code.earthengine.google.com/5b44745d195c944c4c4de06ed68fe000