0

I have a function that calculates the average rainfall over the first 6 months of the year over a 30 year period. The function receives a geometry, and returns the mean of pixels inside the geometry.

In some cases, with small polygons (about the size of a pixel) the result is NULL. it is not always the case, in tests I have made considerably smaller polygons that have given a result. Here is a code snippet that creates the null value.

var geometry = /* color: #0b4a8b */ee.Geometry.Polygon(
        [[[24.36479088838034, -33.36746983993103],
          [24.36024158347382, -33.42516561247864],
          [24.438305535991674, -33.4257574802331],
          [24.43181381987206, -33.37553285357873]]]);

var rain_temp = ee.ImageCollection("ECMWF/ERA5_LAND/MONTHLY_AGGR");

Map.addLayer(geometry);
Map.centerObject(geometry);
var avgRainfall6Coldest = function(polygon, startMonth){
  var startYear = 1990;
  var endYear = 2020;
  
  var rain_tempYears = rain_temp.filterDate(ee.Date.fromYMD(startYear,1,1),ee.Date.fromYMD(endYear,1,1));
  
  rain_tempYears = rain_tempYears.filterBounds(polygon);
  var monthlyAverages = function(month){
    return rain_tempYears.filter(ee.Filter.calendarRange(month,month,"month")).reduce(ee.Reducer.mean());
  };
  
  var yearlyAvg = ee.ImageCollection(ee.List.sequence(1,6).map(monthlyAverages)).reduce(ee.Reducer.sum());
  var imageVisParam = {"opacity":1,"bands":["total_precipitation_sum_mean_sum"],"min":0,"max":0.600,"gamma":1};
  Map.addLayer(yearlyAvg,imageVisParam);
  print(rain_tempYears.aggregate_count_distinct("year"));
  
  return yearlyAvg.select("total_precipitation_sum_mean_sum").reduceRegion(ee.Reducer.mean(),polygon);

};



print(avgRainfall6Coldest(geometry));



Link to the script in Earth Engine. https://code.earthengine.google.com/ec3cfbafb787c71658497e7b910d9acf

Any idea why this is occurring, and could there be a workaround?

1 Answer 1

0

Adding scale for the projection to work on inside .reduceRegion()should fix this here is a working version

Added line 11

var scale = rain_temp.first().projection().nominalScale().aside(print,'the scale of ERA5')

and line 31

.reduceRegion(ee.Reducer.mean(),polygon, scale)

Another thing to pay attention to when reducing over polygon is how pixel fractions are calculated/weighted see here and for more about scale here

1
  • I was suspicious that it might be related to scale, but sometimes it would work, and sometimes not. Thanks for the code. It worked like a charm. Commented Aug 7 at 18:40

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.