# Earth Engine GFS: Calculating Hourly Precipitation

Any tutorial on the GFS model in Earth Engine describes filtering out the 6-hour precipitation intervals, to avoid double-counting the cumulative precipitation band. However, I cannot square the documentation with how to calculate the hourly precipitation itself.

The docs say:

'Cumulative precipitation at surface for the previous 1-6 hours, depending on the value of the "forecast_hours" property according to the formula ((F - 1) % 6) + 1 (and only for assets with forecast_hours > 0).

As a consequence, to calculate the total precipitation by hour X, double-counting should be avoided by only summing the values for forecast_hours that are multiples of 6 plus any remainder to reach X. It also means that to determine the precipitation for just hour X, one must subtract the value for the preceding hour unless X is the first hour in a 6-hour window.'

How exactly would I convert that to code? I have started something like so:

``````var dataset = ee.ImageCollection('NOAA/GFS0P25')
.filter(ee.Filter.date('2018-03-01', '2018-03-02'))
.filter(ee.Filter.neq('forecast_hours',0))
.select('total_precipitation_surface')
var ppt = dataset

print(ee.List(dataset))

//var interval_list = [1,2,3,4,5,7,8,9,10,11,13,14,15,16,17,19,20,21,22,23,25
//                      ,26,27,29,30,31,32,33,34,35,37]

var interval_list = [6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,
126,132,138,144,150,156,162,168,174,180,186,192,198,204,210,216,
220,226,232,240,252,264,276,288,300,312,324,336,348,360,372,384]

var non_six_gfs = dataset
.filter(ee.Filter.inList('forecast_hours',interval_list).not())

print(non_six_gfs.toList(3).get(-1))

var gfs_hourly = dataset.map(function(image){
var previous_int = ee.Image(ee.List(dataset).get(-1));
return image.subtract(previous_int)
})
``````

With the idea of filtering out the 6 hour intervals, and then subtracting the previous forecast hour from the current one, but I'm not sure how to do this and skip over those 6 hour intervals to avoid cutting into the previous cumulative period. I'd also like to avoid converting a image collection to a list, which is very computationally expensive.

You could do the desired operation with the following script. Nonetheless, I am getting an error with images from `'forecast_hours'` > 120, since 'forecast_hours' seem to increase in intervals of 3 hours after 120. Thus, I am not sure if you are going to be able to obtain the desired result. Anyway, hope the answer is useful.

``````var interval_list = ee.List.sequence(6, 120, 6)

var resul = interval_list.map(function(hour){
var h6 = ppt.filter(ee.Filter.eq('forecast_hours', ee.Number(hour).int())).first();
var hpre = ppt.filter(ee.Filter.eq('forecast_hours', ee.Number(hour).subtract(1).int())).first();
var resul = ee.Image(h6).subtract(ee.Image(hpre));
return resul;
})

resul = ee.ImageCollection.fromImages(resul);
``````