1

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)

1 Answer 1

0

Finally found one approach to do this. Interested if anyone has some ideas on how to do this by running the .map function on a ImageCollection to filter out the Images needed based on a list of year-month identifiers. This proposed approach would cut back on the coding needed to, say, calculate the annual total precipitation for a water year.

// 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)
  print('PPT',ppt);

// Set the list sequence based on the years of spring precip totals needed  
var year = ee.List.sequence(1992, 2021, 1)
  print('Year', year)

// Loop through the year list defined above to extract the monthly data needed within
// each year to calculate spring precip totals. 
var sprImgs = year.map(function(number){
  var marKey = ee.Number(number).format('%.0f').cat(ee.Number(3).format('%02d'));
  var aprKey = ee.Number(number).format('%.0f').cat(ee.Number(4).format('%02d'));
  var mayKey = ee.Number(number).format('%.0f').cat(ee.Number(5).format('%02d'));
  var title = ee.String('spr_ppt_').cat(ee.Number(number).format('%.0f'))
  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))).rename(title);
  return spring
})

// Combine the yearly, spring precip Images into a collection for export
var spring_ppt = ee.ImageCollection.fromImages(sprImgs)
  print('Spring Precip', spring_ppt)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.