I am working with Sentinel-2 data in GEE from the JavaScript API. I need to perform a chain of simple processes (including: search, pixel masking, band selection) and then export to my Drive. I need to perform this process for several periods of time (actually months) and save each FeatureCollection in a folder. These periods are listed below:

var periods = ee.List([
              ee.DateRange('2019-01-01', '2019-01-31'),
              ee.DateRange('2019-02-01', '2019-02-28'),
              ee.DateRange('2019-03-01', '2019-03-31')]);

I am able to carry out the process successfully for a single period:

var period1 = ee.DateRange('2019-01-01', '2019-01-31');

// Import and filter Sentinel-2 data.
var s2_data = ee.ImageCollection("COPERNICUS/S2_SR")
                                .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 80)

// Generate 'clear_sky' Sentinel-2 images using SCL.
var s2_clear_sky = function(image){
  var scl = image.select('SCL');
  var clear_sky_pixels = scl.eq(4).or(scl.eq(5)).or(scl.eq(6)).or(scl.eq(11));
  return image.updateMask(clear_sky_pixels);
var s2_data = s2_data.map(s2_clear_sky);
var s2_data_exp = s2_data.select(['B2', 'B3', 'B4']);

// Export data to drive
var batch = require('users/fitoprincipe/geetools:batch');
batch.Download.ImageCollection.toDrive(s2_data_exp, '2019-01-01_2019-01-31', 
                {scale: 10, 
                 region: extent, 
                 type: 'float'});

However I don't know how I can do this for all my periods in the periods list.

I am facing two main problems:

  1. How to apply the process for all the ee.DateRange of the periods list.

  2. How to send each period in the form of ee.String to the batch.Download.ImageCollection.toDrive function.

I apologize in advance if my approach is not adequate to deal with the problem. I am new to GEE. Could anyone please assist me on upgrading the code?

I found this related issue but I can't understand: ImageCollection Exporting by years

1 Answer 1


If you use a client-side object instead of server-side objects like ee.List and ee.DateRange, you can simply iterate your periods client-side and use client-side operations to generate your folder name based on each period. You can read up on this in the docs.

You do get some warnings outputted by batch.Download.ImageCollection.toDrive, but that's not something I can debug. You already got those with your original script.

Note that there will be quite a few click to submit all your tasks.

var batch = require('users/fitoprincipe/geetools:batch')
var extent = Map.getBounds(true)
var periods = [
  ['2019-01-01', '2019-01-31'],
  ['2019-02-01', '2019-02-28'],
  ['2019-03-01', '2019-03-31']


function exportPeriod(period) {
  // Import and filter Sentinel-2 data.
  var s2_data = ee.ImageCollection("COPERNICUS/S2_SR")
    .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 80)
    .filterDate(period[0], period[1])
  // Generate 'clear_sky' Sentinel-2 images using SCL.
  var s2_clear_sky = function(image) {
    var scl = image.select('SCL')
    var clear_sky_pixels = scl.eq(4).or(scl.eq(5)).or(scl.eq(6)).or(scl.eq(11))
    return image.updateMask(clear_sky_pixels)
  var s2_data = s2_data.map(s2_clear_sky)
  var s2_data_exp = s2_data.select(['B2', 'B3', 'B4'])
  var folder = period[0] + '_' + period[1]
  // Export data to drive
  batch.Download.ImageCollection.toDrive(s2_data_exp, folder, {
    scale: 10,
    region: extent,
    type: 'float'


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.