I have a code which creates a value for several different variables for a specific time period (usually over 1 month) and exports these as a CSV to my Google drive. You can find a link to the code here.

At the moment, if I want to create the code for different dates I just do this manually, by altering the startDate and endDate line in my code.

// select dates
 var startDate = ee.Date('2016-01-01');
 var endDate = ee.Date('2016-02-01');

 var startyear = 2016; // 
 var endyear = 2016;

 collection: Dat,
 description: 'Dat',
 selectors: cols,
 fileFormat: 'CSV'

The above would then create the data values for January 2016, and export it as a task which I then run in the google earth engine console.

I'm looking to automate this so I can get monthly values over a number of years, or values for specific months within each year, with out having to do this manually. So rather than changing the startDate, endDate, startyear and endyear manually, it would run the code and start again.

I'm aware in some coding systems you can do this with for loops, but I'm not familiar with them at all, and in Google Earth Engine these are not recommended.

Is it simple enough to set up a function that calculates the score for four months (e.g. for Jan, April, July and October) for a set number of years (say 2003-2019), so it would run the score for Jan 2003, then for April 2003, July 2003, etc?

1 Answer 1


What I've done in the past is loop over a list of indices that I use to index into lists of months, years, etc. For example, if I wanted get avergae NDVI for some region for select months in select years:

// say collect is a collection of Landsat images containing a band 'NDVI' and region is a geometry 
var collect = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR')

  // filter date 

  // filter PATH/ROW 
  .filterMetadata('WRS_ROW','equals', 25)

  // filter cloud cover
  // add NDVI band 
    return image.addBands(image.normalizedDifference(['B4','B3']).rename('NDVI'))

  // only NDVI band 

// define years and months I want to get NDVI for 
var years = ee.List([2000, 2003, 2006]) 
var months = ee.List([5,7,9]) 

// create lists to index into 
var yearsi = ee.List.repeat(years,months.length()).flatten().sort()
var monthsi = ee.List.repeat(months,years.length()).flatten()
print(yearsi) // 2012,2012,2012,2013,... 
print(monthsi) // 5,   7,   9,   5 , .... 

// map over a list of indices 
var collect_comp = ee.ImageCollection.fromImages(

      // get year and month for this iteration
      var y = yearsi.get(i)
      var m = monthsi.get(i)
      // create monthly mean composite 
      var comp = collect.select('NDVI')

          // filter date 
         // mean composite 
      // aggregate over a region 
      var ndvi_dict = comp.reduceRegion({reducer:ee.Reducer.mean(), scale: 30, geometry: region})
      // return composite image and set new metadata to be reduced NDVI. if there are no images in the filtered calendar range, return a flag (-99)
      return ee.Algorithms.If(ndvi_dict.size().eq(1),
        comp.set(ndvi_dict), comp.set('NDVI',-99))

In this example, collect_comp can then be exported as a table.

  • Thanks. Running this code however gives a syntax error in the line return comp.set(comp.reduceRegion({reducer:ee.Reducer.mean(), geometry: region})). I have several functions and variables that are produced in the code iIm trying to iterate over. Is it just a mater of wrapping this function around the rest of my code? Commented Nov 16, 2020 at 7:38
  • Thanks for your comment @mikejwilliamson. My code was a mess. It now runs. You are right - see if you can wrap it around your code. My understanding is that you cannot "export to drive" within a mapped function, so you must export results to a single .csv at the end.
    – korndog
    Commented Nov 16, 2020 at 17:33

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.