1

I am trying to calculate a index of monthly EVI from modis dataset, but am receiving a "Parameter 'index' is required" error. I have found another answer about the question, but it doesn't work.

var table = ee.FeatureCollection("projects/ee-qsz/assets/Boreal_merge");

// import dataset
var collection = ee.ImageCollection('MODIS/MOD09GA_006_EVI')
  .filterDate('2003-01-01', '2011-12-31');
  
var clippedCollection = collection.map(function(image){
  return image.clip(table);
});
print('rawdata', clippedCollection);

// calculate monthly average EVI
var monthlyMean = function(year, month) {
  var startDate = ee.Date.fromYMD(year, month, 1);
  var endDate = startDate.advance(1, 'month');

  var monthlyImages = clippedCollection.filterDate(startDate, endDate)
    .mean()
    .set('system:time_start', startDate.millis())
    .set('system:time_end', endDate.millis());

  return monthlyImages;
};

var months = ee.List.sequence(1,12);
print(months);
var monthlyEvi = ee.ImageCollection.fromImages(
  ee.List.sequence(2003, 2011).map(function(year){
    return months.map(function(month){
      var evi = monthlyMean(year, month);
      var startDate = ee.Date.fromYMD(year, month, 1);
      var evi2 = evi.set('system:time_start', startDate);
      return evi2.set('month', month);
    });
  }).flatten()
);
print('Monthly EVI', monthlyEvi);

//visualize
var colorizedVis = {
  min: 0,
  max: 1,
  palette: [
    'ffffff', 'ce7e45', 'df923d', 'f1b555', 'fcd163', '99b718', '74a901',
    '66a000', '529400', '3e8601', '207401', '056201', '004c00', '023b01',
    '012e01', '011d01', '011301'
  ],
};
Map.setCenter(-7.03125, 31.0529339857, 2);
Map.addLayer(formattedEvi, colorizedVis, 'Colorized');

//test
var testyear = 2008;
var eviYear = monthlyEvi.filter(ee.Filter.calendarRange(testyear, testyear, 'year'));

// calculate index d
var d = ee.Number(2).multiply(Math.PI).divide(12);
print('d',d);
// calculate cos term
var costerm = ee.List.sequence(0, 11).map(function(i){
  return (d.multiply(i)).cos(); 
});
print('cos', costerm.get(0));

var mx_col =  ee.ImageCollection.fromImages(
     months.map(function(month) {
     var eviMonth = eviYear.filter(ee.Filter.eq('month', month)).first();
     var L = eviMonth.select('EVI');
     var mx = L.multiply(ee.Number(costerm.get(month-1)));
     return mx.set('system:time_start', eviMonth.get('system:time_start'));
   }).flatten()
  );

print('mx_col', mx_col);

When I output every variable such as mx, L, etc. outside of the map function it outputs successfully, but when using the map loop and generating the imagecollection I get the error. I'm a beginner in GEE.

1 Answer 1

0

The problem is here:

.
.
.
     var mx = L.multiply(ee.Number(costerm.get(month-1)));
.
.
.

Fixing as follows:

var mx = L.multiply(ee.Number(costerm.get(ee.Number(month).subtract(1))));

running my own version of your code (your asset is unavailable), I got a result without any error.

1
  • Hi @xunilk, thank you very much! I modified the code the way you did and it works successfully!
    – qingsong
    Commented Jul 18, 2023 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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