1

I have this code that extracts the monthly time series of soil moisture, on radius defined by the buffer function:


function bufferPoints(radius, bounds) {
  return function(pt) {
    pt = ee.Feature(pt);
    return bounds ? pt.buffer(radius).bounds() : pt.buffer(radius);
  };
}
var ptsbuff = ee.FeatureCollection(point).map(bufferPoints(500, false));

// Create a chart for SoilMoi100_200cm_inst trend in point 1.
var dataset = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
          .filterBounds(point)
          .filterDate('2006-01-01', '2016-12-31')
          .select('SoilMoi100_200cm_inst');

var months = ee.List.sequence(1, 12);
var start_year = 2006;
var start_date = '2006-01-01';
var end_year = 2016;
var end_date = '2016-12-31';

var years = ee.List.sequence( start_year, end_year);


var byMonthYear =  ee.FeatureCollection(
  years.map(function (y) {
    return months.map(function(m) {
      var w = dataset.filter(ee.Filter.calendarRange(y, y, 'year'))
                    .filter(ee.Filter.calendarRange(m, m, 'month'))
                    .mean();
           
      var pointMean = w.reduceRegion({reducer:ee.Reducer.first(), geometry:ptsbuff,scale:500});  
      // set the dictionary as property and cast to an ee.Image, as setMulti returns an ee.Element.
      return ee.Feature(null).set("value",pointMean.get("SoilMoi100_200cm_inst")).set("year",y).set("month",m);
      

    });
  }).flatten()
);


// print the collection, point information
//print("feature collection",byMonthYear);

Export.table.toDrive({collection:byMonthYear,description:"csvExport"}) 

Since I am doing extractions on several radius (For example 250, 500, 1000, 1500, 2000...), I have to re-execute the code each time.
Is there a way to modify the code to extract the data simultaneously and automatically with several buffer radius.

1
  • I also ran the script in my answer with following extended list (250, 500, 1000, 1500, 2000) and result (with 660 lines) was obtained as expected in only 6 minutes.
    – xunilk
    Commented Jun 16, 2022 at 15:56

1 Answer 1

1

You only have to define a buffers list object and modify your script as follows for mapping it. In this case, I only used two distances (250, 500) for saving compute time. I defined an arbitrary point in France for running complete script.

// Create a chart for SoilMoi100_200cm_inst trend in point 1.
var dataset = ee.ImageCollection('NASA/GLDAS/V021/NOAH/G025/T3H')
          .filterBounds(point)
          .filterDate('2006-01-01', '2016-12-31')
          .select('SoilMoi100_200cm_inst');

var radius_lst = ee.List([250, 500]);

var colByRadius = ee.List(radius_lst).map(function (radius) {

  var months = ee.List.sequence(1, 12);
  var start_year = 2006;
  var start_date = '2006-01-01';
  var end_year = 2016;
  var end_date = '2016-12-31';

  var years = ee.List.sequence( start_year, end_year);

  var ptsbuff = ee.FeatureCollection(point).map(function (radius, bounds) {
    return function(pt) {
      pt = ee.Feature(pt);
    return bounds ? pt.buffer(radius).bounds() : pt.buffer(radius);
  };
  }(radius, false));

  var byMonthYear =  years.map(function (y) {
      return months.map(function(m) {
        var w = dataset.filter(ee.Filter.calendarRange(y, y, 'year'))
                    .filter(ee.Filter.calendarRange(m, m, 'month'))
                    .mean();
           
        var pointMean = w.reduceRegion(
          {reducer:ee.Reducer.first(), 
          geometry:ptsbuff, scale:500});  
      
      // set the dictionary as property and cast to an ee.Image, as setMulti returns an ee.Element.
      return ee.Feature(null).set("value",
                                  pointMean
                                    .get("SoilMoi100_200cm_inst"))
                                    .set("year",y)
                                    .set("month",m)
                                    .set("radius", radius);
      

      });
    }).flatten();

  return byMonthYear;

}).flatten();

// print the collection, point information
//print("feature collection", colByRadius);

Export.table.toDrive({collection:ee.FeatureCollection(colByRadius), description:"csvExport"}) 

After running above script in GEE code editor, I got a CSV file with 264 rows as follows. Transition for different radius can be observed inside red rectangle.

enter image description here

Editing Note:

Subsequently, I ran the script with following extended list (250, 500, 1000, 1500, 2000) and result (with 660 rows) was obtained as expected in 6 minutes.

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.