I am trying to create a function to calculate the average Vegetation Condition Index

((mean NDVI of individual year)-(min NDVI from full period)) / ((max NDVI from full period) - (min NDVI from full period))

...for the months of March, April and May in each individual year between 2001 and 2020 for a part of Ukraine, and then plot this on a graph. My reference period for the index is 2001-2020. I have applied a mask to just run the script on agricultural land in one specific region of the country.

I think there is an issue with my actual function as it is not currently running properly and is throwing the following error:

enter image description here

//calculate average monthly NDVI for Ukraine
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(
   .filter(ee.Filter.eq('country_na', 'Ukraine'))

// Choose a reference period for your baseline
var reference = ee.ImageCollection(collection.filterDate('2001-01-01', '2020-12-31'))

//filter out images from specific date
//march, april, may (spring)
var spring = reference.filter(ee.Filter.dayOfYear(59,150));

//calulate min of full series (mam 2001-2020) for each pixel
var spring_min = spring.min();

//calculate max of full series (mam 2001-2020) for each pixel
var spring_max = spring.max();

//mam_2003_mean IS the average ndvi for specific season (2003 mam)
//clip to zap agrimask
var spring_agrimask_min = spring_min.multiply(zap_crop_mask);
var spring_agrimask_max = spring_max.multiply(zap_crop_mask);

//Use a function to iterate through months and calculate average Vegetation condition Index
var years = ee.List.sequence(2001, 2020);

var byYear = ee.ImageCollection.fromImages(
      years.map(function (y) {
          var year_mean = y.select(("NDVI")).mean();
          var year_mean_mask = year_mean.multiply(zap_crop_mask);
          var vci_top = year_mean_mask.subtract(spring_agrimask_min);
          var vci_bot = spring_agrimask_max.subtract(spring_arimask_min);
          var vci = vci_top.subtract(vci_bot);
        return vci.filter(ee.Filter.calendarRange(y, y, 'year'))
                    .set('year', y);

//Define chart parameters
var chartParam = {
 title: 'Annual average NDVI',
  hAxis: {title: 'Time'},
  vAxis: {title: 'Average NDVI'},

//Plot the chart
var chart = ui.Chart.image.seriesByRegion({
  imageCollection: byYear,
//  regions: ukr,
  reducer: ee.Reducer.mean(),
  scale: 500,
  xProperty: 'year',
  seriesProperty: 'PROJECT'
//Print chart to console

1 Answer 1


Structuring the function like this worked well

var summer_vci = ee.ImageCollection.fromImages(
      years.map(function (y) {
        return summer.filter(ee.Filter.calendarRange(y, y, 'year'))
                    .set('year', y);

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.