1

I have this code, it generate N ImageCollection and for each image a chart from NDVI in function a the months

This is the place of study:

var areaEst = ee.FeatureCollection("users/aleszczuk/Trabajo_Final_Especializacion/TF_Mascara_Suelos")

This is the collection:

var NDVICollection=ee.ImageCollection('MODIS/MOD13A1')
                         .filterDate('2000-01-01','2018-12-31');

a list

var list=NDVICollection.toList(20);

The loop a generator of charts:

for (var i=2000;i<=2018;i++){
    var image=ee.Image(list.get(i));
    var name1=ee.String('Grafico ')
      .cat(ee.String(ee.Number(i)))
      .getInfo();

    var NDVICollection=ee.ImageCollection('MODIS/006/MOD13Q1')
                         .filterDate(i+'-01-01',i+'-12-31')
                         .filterBounds(table).select('NDVI'); 
                         print(NDVICollection,i);


    var tempTimeSeries = ui.Chart.image.seriesByRegion(
                        NDVICollection, table, ee.Reducer.mean(), 'NDVI', 200, 'system:time_start', 'class')
                          .setChartType('ScatterChart')
                          .setOptions({
                                      title: 'NDVI por potrero año '+i,
                                      vAxis: {title: 'NDVI MODIS'},
                                      lineWidth: 1,
                                      pointSize: 4,
                                      series: {
                                              0: {color: 'FF0000'}, 
                                              1: {color: '00FF00'},
                                              2: {color: '0000FF'} 
}});
print(tempTimeSeries);
}

So far so good, the issue is that now I want to adapt the following line of code to generate collections of images and reduce them to months

That is to say 18 collections of 12 months instead of having the 23 data that are in the imagecollection's.

The code is:

//Function for reduce a temporal resolution a months
var months = ee.List.sequence(1, 12);

var NDVI_mensual = months.map(function(m) {
  // Filter to 1 month.
  var NDVI_men = NDVI.filter(ee.Filter.calendarRange(m, m, 'month')).mean();
  // add month band for MMax
  return  NDVI_men.addBands(ee.Image.constant(m).select([0],['month']).int8());
});

var NDVI_mensual = ee.ImageCollection(NDVI_mensual);

This code works well but it generates me a single image. I need 18, one per year. I need adapt the previous code and assemble it with the loop.

1 Answer 1

0

Trying to make the loop work with the following lines of code:

//Import collection
var NDVICollection=ee.ImageCollection('MODIS/MOD13A1')
                         .filterDate('2000-01-01','2018-12-31');
//create a list
var list=NDVICollection.toList(20);//depend on your list 

//Loop 
for (var i=2000;i<=2018;i++){
    var image=ee.Image(list.get(i));
    var name1=ee.String('Grafico ')
      .cat(ee.String(ee.Number(i)))
      .getInfo();

    var NDVICollection=ee.ImageCollection('MODIS/006/MOD13Q1')
                         .filterDate(i+'-01-01',i+'-12-31')
                         .filterBounds(table).select('NDVI'); 
    //print(NDVICollection,i);
    //Convert the collectio a 12 values per months

    var filtered = NDVICollection.filter(ee.Filter.calendarRange(1, 12, 'month')).mean();
    var filtered = filtered.addBands(ee.Image.constant(1-12).select([0],['month']).int8());
    var NDVI_mensual = ee.ImageCollection(filtered);
}});
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.