0

I am working with rainfall monthly totals for a 30-year period using CHIRPS pentad.

//1.Reference Period
var historical = ee.List.sequence(1991, 2020)
var months = ee.List.sequence(1, 5)
//print(historical)
    
//2.Map over the years and create a monthly totals collection
var monthlyImages = historical.map(function(year){
  return months.map(function(month){
    var filtered = chirps
    .filter(ee.Filter.calendarRange(year, year, 'year'))
    .filter(ee.Filter.calendarRange(month, month, 'month'))
    var monthly = filtered.sum();
    return monthly.set({'month': month, 'year': year})
  })
}).flatten()

print(monthlyImages)

//3. Create an Image Collection for each month of each year
var monthlyCol = ee.ImageCollection.fromImages(monthlyImages)
print(monthlyCol)

//4. Convert to Feature Collection
var montlyRainfall = ee.FeatureCollection(monthlyImages)

// Export.table.toDrive({
//   collection: monthlyRainfall,
//   fileNamePrefix: 'rainfallbymonth',
//   fileFormat: 'CSV'}) 

I have been able to get an Image Collection with 150 images for the months I require. I am looking to export each monthly totals(sum) to a table and obtain results from my geometry.

Transforming my Image Collection to a Feature Collection and exporting to CSV has worked with the correct dates but it just doesn't contain the values from my 'precipitation' band which is the sum that I filtered in the first code of block.

I understand I need to transform my image collection to a feature collection and tell it to assign the dictionary values to my table I just have not figured out how to do it. I am also not sure if I need to Reduce.sum again even though I have filtered my monthly images totals using .sum already.

1 Answer 1

0

To transform your image collection to a feature collection your statement:

//4. Convert to Feature Collection
var montlyRainfall = ee.FeatureCollection(monthlyImages)

it doesn't nothing.

You need, for instance, to use 'reduceRegion' method by employing as parameters a reducer, a geometry and a scale; as it can be observed at following lines:

//4. Convert to Feature Collection
var monthlyRainfall = ee.FeatureCollection(monthlyCol.map(function (img){
  
  var value = ee.Image(img).reduceRegion(ee.Reducer.first(),
                                         geometry,
                                         scale)
                            .get('precipitation'); 
  
  var month = ee.Image(img).get("month");
  var year = ee.Image(img).get("year");
  
  var date = ee.String(ee.Number(year).int()).cat('_').cat(ee.Number(month).int());
  
  return ee.Feature(null).set('precipitation', value)
                         .set('date', date);
  
}));

print(monthlyRainfall);

 Export.table.toDrive({
   collection: monthlyRainfall,
   fileNamePrefix: 'rainfallbymonth',
   fileFormat: 'CSV'});

Above function was included in this complete code and, after running it and its corresponding task, you have a CSV file as follows. You can observe the precipitation at corresponding point (chosen arbitrarily in US) by month and year.

enter image description here

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.