I am trying to temporally reduce an image from 365 bands (daily) to 12 bands (monthly).
Context: I imported an image collection with Soil Moisture (SM) that had 8 values per day (every three hours). I first reduced it to average daily values to calculate SM anomalies with this formula: SManom = (SM-SMmean)/ SMstdev
Then I created a new variable (irrigation events) that is 1 when there is an irrigation event and 0 if not. So now I have an image with one band for each day, and I want to reduce it to accumulated monthly values. This is the code that I have for now, below it I will post what I tried.
(This is actually not what I want to achieve (accumulate irrigation events) but it is a working example, otherwise the entire code would be too long. )
var dataset = ee.ImageCollection("NASA/GLDAS/V021/NOAH/G025/T3H"),
geometry = ee.Geometry.Polygon(
[[[21.742985018165424, 52.321076580783036],
[21.742985018165424, 44.02344004522736],
[40.28790689316542, 44.02344004522736],
[40.28790689316542, 52.321076580783036]]], null, false);
//Import precipitation and soil moisture for a year.
var pcp = dataset.select('Rainf_tavg').filterDate('2019-01-01', '2020-01-01');
var sm = dataset.select('SoilMoi0_10cm_inst').filterDate('2019-01-01', '2020-01-01');
//Reduce SM to daily values and compute mean, std dev, and anomalies.
var sm_daily = ee.ImageCollection.fromImages(days.map(function(d) {
var filtered = sm.filter(ee.Filter.calendarRange({
start: d,
field: 'day_of_year'
}));
return filtered.mean().set('day', d);
}));
var sm_mean = sm_daily.mean();
var sm_stdev = sm_daily.reduce(ee.Reducer.stdDev());
var sm_img = sm_daily.toBands();
var sm_anom = (sm_img.subtract(sm_mean)).divide(sm_stdev);
//Reducing precipitation to daily values.
var pcp_daily = ee.ImageCollection.fromImages(days.map(function(d) {
var filtered = pcp.filter(ee.Filter.calendarRange({
start: d,
field: 'day_of_year'
}));
return filtered.mean().multiply(86400).set('day', d);
}));
var pcp_img = pcp_daily.toBands();
//Computing irrigation events.
var irrigation_event = pcp_img.lte(1).and(sm_anom.gte(1));
I tried to adapt the part of the code that I used to reduce to daily values but the fact that now it is an image rather than an image collection is problematic. The best solution I have found is to use slice on the image in order to return images from specifically selected bands and creating an image collection from those images. I think this could work but I'm having trouble selecting bands in an iterative manner. I also tried creating an array with lists that contain sequences of the days of each month but that didn't work since the lists have to be the same length.
var months = ee.List.sequence(1, 12);
//Here I want to map this function to the variable months but I can't seem to figure out how to select
//the right bands in every iteration.
var irr_jan_acc = ee.ImageCollection.fromImages(months.map(function(g) {
var filtered = irrigation_event.slice(g);
return filtered.reduce(ee.Reducer.sum());
}));
I am quite new to GEE and especially to iterations.