2

I'm looking to sum precipitation for a period of 10-days prior to a specified date. This is just an example - in fact, I have around 60 different dates and I don't want to select each the date manually as done below. Is there a way to iterate through the dates?

// CSV file
var coord = ee.FeatureCollection("users/eginamalaj/site_test");

// Select sampling date

var datesampl = '2018-05-06';
var s1 = coord.filter(ee.Filter.eq('date', datesampl));

// Assign time interval
var dates = new Date(datesampl); 
var start = new Date(dates.getTime() - 10*24*60*60*1000); // 10-days before
var end = new Date(dates.getTime() - 1*24*60*60*1000); // exclude sampling day 

// Get image and sum for the time interval
var precImage = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
                   .filterBounds(coord)
                   .filterDate(start, end) 
                   .select('precipitation')
                   .sum();
                   
// Extract precipitation for the sampling sites
var precSample = precImage.sampleRegions({
    collection: s1,
    scale: 30,
    geometries: true
});

//////// Repeat above with a different date

var datesampl2 = '2018-09-12'
var s2 = coord.filter(ee.Filter.eq('date', datesampl2));
var dates2 = new Date(datesampl2); 
var start2 = new Date(dates2.getTime() - 10*24*60*60*1000); // 10-days before
var end2 = new Date(dates2.getTime() - 1*24*60*60*1000); // exclude sampling day 
var precImage2 = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
                   .filterBounds(coord)
                   .filterDate(start, end) 
                   .select('precipitation')
                   .sum();
var precSample2 = precImage2.sampleRegions({
    collection: s1,
    scale: 30,
    geometries: true
});

                
// Combine them
var combined = precSample.merge(precSample2);
print(combined);

Code and data: https://code.earthengine.google.com/cd0a2c60fb8ddefbb9508abfc1a806bb

2 Answers 2

2

You do this by mapping over the feature collection. Unfortunately I think you didn't give access to your script to others, so I can't test it. But this should work:

// CSV file
var coord = ee.FeatureCollection("users/eginamalaj/site_test");

// Select sampling date

var results = coord.map(function(feature){
  var date = ee.Date(feature.get('date'))
  var start = date.advance(-10, 'day')
  var end = date.advance(-1, 'day')
  // Get image and sum for the time interval
  var precImage = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
                     .filterBounds(coord)
                     .filterDate(start, end) 
                     .select('precipitation')
                     .sum();
                     
  // Extract precipitation for the sampling sites
  var summed = precImage.reduceRegion({
      reducer: ee.Reducer.sum(),
      geometry: feature.geometry(),
      scale: 30
  });
  return feature.set(summed)
})

print(results)

Most of the time, if you have a workflow that works for a single element out of an ImageCollection or FeatureCollection it is quite straight forward to use the same workflow and automatically map over the entire Collection

5
  • Thanks so much @JonasV! I see what you did there. The s1 in the collection (sampleRegions) creates an error. I tried using the original coord file but the returned FeatureCollection is empty. Can you please have another look? I made the link and table public (sorry about that!).
    – egm
    Commented Mar 1, 2021 at 15:06
  • Ah, sorry. One question before I update my answer: Do you mean to use sampleRegions(). Is your end goal just to have the total precipitation sum for every feature? Or do you want all the precipitation sums that intersect with your feature individually?
    – JonasV
    Commented Mar 1, 2021 at 15:21
  • The end goal is to have a sum precipitation in each site (feature) for 10 days before the sampling date. So I'll need a sum precipitation for each site - date combination in the CSV list. If there is another way of doing it without sampleRegion that's great. I didn't know how else to get the precipitation at that exact feature/site location. Thanks so much again for looking into this!
    – egm
    Commented Mar 1, 2021 at 16:35
  • You can do it with .reduceRegion() instead. It makes it a bit easier and conceptually more correct I think. I updated my answer, have a look.
    – JonasV
    Commented Mar 2, 2021 at 7:26
  • Thank you so much!!! Your solution now does exactly what I was looking for. Cheers
    – egm
    Commented Mar 2, 2021 at 18:29
0

I found the problem and fixed it this way

feature = ee.Feature(feature.set('precipitations', max_prcp.get('prcp'),
                                 'sol_rad', mean_sol_radiation.get('srad'),
                                 'temp_max', mean_maxtemp.get('tmax')));  
return feature

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.