0

I have a feature collection of about 80,000 tiles, and I am trying to calculate the annual maximum drought severity index score for each tile using TerraClimate data. TerraClimate is an image collection, so I started by transforming it into a stack of images.


var drought_M = ee.ImageCollection("IDAHO_EPSCOR/TERRACLIMATE").select("pdsi")
.filter(ee.Filter.date('2000-01-01', '2012-12-31'));

//This function creates a stack of images from the image collection
function newCollectionToImage(collection){
var stack = ee.Image(collection.iterate(function(img, prev) {
return ee.Image(prev).addBands(img);
}, ee.Image(1)));
 
stack = stack.select(ee.List.sequence(1, stack.bandNames().size().subtract(1)));
return stack;
}

//This function renames the image collection bands
var drought_M_rename = drought_M.map(function(img){
var y = ee.String(ee.Date(img.get('system:time_start')).get("year"));
return img.set("year",y).rename(y);
});

//Apply the function to create a stack of images
var droughtImageStack = newCollectionToImage(drought_M_rename);

How would I take this stack of images (or the image collection) and perform an image reduction that produces the annual maximum score?

2
  • This question can be closed because you have two requirements. Please, edit for having only one (by the way, the second is opinion based; another motive to be closed).
    – xunilk
    Apr 13 '21 at 16:43
  • I fixed this post so that it only asked one question. Moving the second question to a separate post.
    – C. Ashley
    Apr 13 '21 at 17:03
0

I post here what I'd do (I discarded your code). The explanation is in the code

var drought_M = ee.ImageCollection("IDAHO_EPSCOR/TERRACLIMATE");

// add YEAR property to each image
var drought_MY = drought_M.map(function(img){
  return img.set('YEAR', img.date().get('year'))
})

// create a list of years
var years = ee.List.sequence(2000, 2012)

// iterate over years
var drought = ee.Image(years.iterate(function(year, img){
  year = ee.Number(year).toInt()
  img = ee.Image(img)
  // filter collection by YEAR
  var d = drought_MY.filter(ee.Filter.eq('YEAR', year))
                    .select('pdsi')
  // compute year's max
  var dmax = d.max().rename(year.format())
  return img.addBands(dmax)
}, ee.Image(1)))

// discard first band (coming from ee.Image(1))
var years_str = years.map(function(y){return ee.Number(y).toInt().format()})
drought = drought.select(years_str) 

// do what ever you want with the drought layer
Map.addLayer(drought, {min:-500, max:500})

https://code.earthengine.google.com/4c5825498199751b60117509406c59c6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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