Compare image in imageCollection with another image GEE

I want to calculate the number of monthly in a year that have less than 10% of the annual precipitation of that year, using the CHIRPS daily precip dataset on GEE.

My logic with code below (initiated for YY-1998 and annual precip for 1998 code not shown)-

1) Calculate the monthly precip for year YYYY and save it as an imagecollection by_monthYY.

2) Write a conditional statement to compare each image in by_MonthYY with annual precipitation of same year, such that if the by_MonthYY <=10% annual precipitation, a value of 1 is assigned, else 0, all saved in an other image collection by_MonthdryYY.

3) Reduce by_MonthdryYY by sum, to get the total number of dry months for that year.

I need to do above for multiple years and finally reduce by mean to get the average number of dry months over a time period.

``````var chirps_timeperiod = chirps_complete.filterDate('1998-01-01', '1998-12-31'); //YY=1998

var month_sum98 = ee.List.sequence(0, 1*12).map(function(n) { // .sequence: number of years from starting year to present
var start = ee.Date('1998-01-01').advance(n, 'month'); // Starting date
var end = start.advance(1, 'month'); // Step by each iteration

return ee.ImageCollection(chirps_timeperiod)
.filterDate(start, end)
.sum()
.set('system:time_start', start.millis());
});
print(month_sum98);
var collection98 = ee.ImageCollection(month_sum98); //monthly precip for 1998

var replacement = ee.Image(1);
var conditional = function(image) { //image is map image collection of monthly precip by year separetly
return image.where(image.lte(0.1*(annualprecip_1998)), replacement);
};
var dryseason98=month_sum98.map(conditional);
``````

However, I get an error saying image.lte is not a function. I also used image.ee.Filter.lessThanOrEqual with error image.ee is undefined. What function should I use to compare the two images?

1 Answer

You didn't include a complete, running script, which makes it hard to help. By just looking at the code, I spot two problems:

• You probably should `collection98.map(conditional)` instead of `month_sum98.map(conditional)`.

• `0.1*(annualprecip_1998)` looks suspicious. If `annualprecip_1998` is an EE object, it would fail. You would have to `annualprecip_1998.multiply(0.1)`.

Based on your description, something like this might work:

``````var year = 1998

var chirps = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')

var monthlyCollection = ee.ImageCollection(
ee.List.sequence(0, 11).map(function (monthOffset) {
var start = ee.Date.fromYMD(year, 1, 1).advance(monthOffset, 'months')
var end = start.advance(1, 'month')
return chirps
.filterDate(start, end)
.sum()
.set('system:time_start', start.millis())
})
)

var annual = monthlyCollection.sum()
var dryThreshold = annual.multiply(0.1)

var numberOfDryMonths = monthlyCollection
.map(function (monthly) {
return monthly.updateMask(
monthly.lte(dryThreshold)
)
})
.reduce(ee.Reducer.count())
.int8()

Map.addLayer(numberOfDryMonths, {min: 0, max: 12, palette: 'blue,green,yellow,red'})
``````

https://code.earthengine.google.com/67b4dc53a2fe0a208362d41c75d61d35