I'm working with monthly precipitation data from PRISM Climate Group for the time period Oct. 1991 to Sept. 2021. The monthly images are currently in an imageCollection and I'm trying to calculate the spring precipitation (ppt) total (March, April, May) for each year. Ideally, this process would create a new imageCollection with each image representing the spring ppt total for each year. These new images would then be exported in a raster format for use with my statistical analysis in R.
I'm relatively new to Google Earth Engine and I've yet to find any examples of script performing the process described above to help with developing my own. I've tried multiple approaches including creating a custom function applied using the .map function. I've achieved the same process in R, but after seeing the processing speed of GEE for the first time, I would like to figure this out in this new coding language. This will help develop similar annual seasonal metrics of other climate variables needed for my research.
Below is the updated version of my script to calculate annual spring precipitation totals. I am still receiving errors with the map function at the end of this script:
// Load in state boundaries and filter for western states
var states = ee.FeatureCollection('TIGER/2018/States').filter(
ee.Filter.inList("NAME", ['Washington', 'Oregon', 'California','Idaho',
'Nevada','Montana','Wyoming','Utah','Colorado','New Mexico','Arizona','South Dakota']))
// Dissolve separate states into one western layer
var west = states.union();
// Map.addLayer(west);
// Load in PRISM data
var ppt = ee.ImageCollection("OREGONSTATE/PRISM/AN81m")
.select(['ppt'])
.filter(ee.Filter.gte('system:index', '199110'))
.filter(ee.Filter.lte('system:index', '202109'));
// Create a function that loops through imageCollections and clips based on a geometry
function clp(img) {
return img.clip(west)
}
var ppt = ppt.map(clp)
//.toBands();
//print(ppt)
//Map.addLayer(ppt.filter(ee.Filter.eq('system:index','202109')))
//print('PPT',ppt);
//print('All band names', ppt.bandNames());
// Calculate spring precipitation totals (MAM) for each year
var year = ee.List.sequence(1992, 2021, 1).map(function(number){return ee.Number(number)})
var sprImages = ppt.map(function(year){
var marKey = ee.String(year).cat('05')
var aprKey = ee.String(year).cat('06')
var mayKey = ee.String(year).cat('07')
var mar = ppt.filter(ee.Filter.eq('system:index', marKey)).select(0).toBands();
var apr = ppt.filter(ee.Filter.eq('system:index', aprKey)).select(0).toBands();
var may = ppt.filter(ee.Filter.eq('system:index', mayKey)).select(0).toBands();
var spring = ee.Image(mar.add(apr.add(may)));
return spring;
});
var spring_ppt = ee.ImageCollection.fromImages(sprImages)
print('Spring Precip', spring_ppt)