1

I am quite new to GEE and I am having issues mixing client and server-side functions. I am importing a table with rain gauge coordinates and their associated IDs as a feature collection. I have defined a function that for each feature:

  1. Extract the coordinates, creates a buffer around the point and computes the time series of the mean aerosol absorving index from TROPOMI (Sentinel 5P).
  2. Extracts the ID.
  3. Exports the time series as a CSV called _aer_ai_timeseries.csv to Google Drive

The function works when I call it only on one feature, but not when I map it. From what I have been reading the problem is that mapping is a server-side function while export is a client-side function. However I am not being able to find a workaround.

What would be the best way to stil run the function on all features in my collection AND export all timeseries?

This is my function:

var AER_AI_series = function(feature){
  var lon = feature.geometry().coordinates().get(0);
  var lat = feature.geometry().coordinates().get(1);
  
  var point = ee.Geometry.Point(lon, lat)
  
  var geom = point.buffer(3500);
  
  ////////////////        //////////////////
  // IMAGE COLLECTION - SENTINEL 5P - AER_AI
  ////////////////        //////////////////
  
  var start_date_ai = '2018-07-01';
  var end_date_ai = '2022-11-01';
  
  var aer_ai = ee.ImageCollection('COPERNICUS/S5P/OFFL/L3_AER_AI')
    .select('absorbing_aerosol_index')
    .filterDate(start_date_ai, end_date_ai);

  
  //////
  // 
  //  CREATING TIME SERIES TO EXPORT TO GOOGLE DRIVE
  //
  //////
  var filteredCollection = aer_ai.select('absorbing_aerosol_index')
    .filter(ee.Filter.bounds(geom));
    
  var timeSeries = ee.FeatureCollection(filteredCollection.map(function(image) {
    var stats = image.reduceRegion({
      reducer: ee.Reducer.mean(),
      geometry: geom,
      scale: 3500,
      maxPixels: 1e10
    });
    
    // If there was no aer_ai value found, we set the ndvi to a NoData value -9999
    var aer_ai = ee.List([stats.get('absorbing_aerosol_index'), -9999])
      .reduce(ee.Reducer.firstNonNull());
   
    // Create a feature with null geometry and aer_ai value and date as properties
    var f = ee.Feature(null, {'aer_ai': aer_ai,
      'date': ee.Date(image.get('system:time_start')).format('YYYY-MM-dd')});
    return f;
  }));

  var name_st = feature.get('id');
  var name_file =  ee.String(name_st).cat('_aer_ai_timeseries');
  
  // Export to CSV
  Export.table.toDrive({
      collection: timeSeries,
      description: name_file.getInfo(),
      folder: 'Aerosol_data',
      fileNamePrefix: name_file.getInfo(),
      fileFormat: 'CSV'
  });

  return(0);
  
};
1
  • Please, mark as accepted below answer. It works for you.
    – xunilk
    Nov 27, 2022 at 14:30

1 Answer 1

1

You can do client-side iteration. You can for instance create a client-side list of the gauge IDs, and iterate over that, exporting the time-series for each. Note that I pass the client-side id to the function, so you don't have to getInfo() to create a client-side string for the description and fileNamePrefix export properties.

gauges.aggregate_array('id').evaluate(function (ids) {
  ids.map(function (id) {
    var gauge = gauges.filter(ee.Filter.eq('id', id))
    AER_AI_series(gauge, id)
  })
})

https://code.earthengine.google.com/c6157cee3336ffadebee9f8ef61641f9

2
  • Perfect, it works! Thanks a lot :)
    – Monica_EC
    Nov 25, 2022 at 19:50
  • I finally understood the use of 'evaluate'. Thanks.
    – xunilk
    Nov 27, 2022 at 14:29

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.