0

I have an image collection with precipitation data for one year, say TerraClimate data, with 12 images in the collection (one for each month), each containing average monthly precipitation data. Let's call this image collection "precip".

To get the minimum monthly precipitation at each location across the year one would simply do the following and apply a minimum reducer:

var minprecip = precip.min();

However, how would one go about getting the MONTH in which the minimum value occurred?

I.e. the resulting image would have values ranging from 1-12, depending on which image in the collection contained the minimum precipitation value for the pixel.

This seems like a relatively simple question, but I am unable to find any way to get a reducer key or otherwise figure out how to approach the problem.

1 Answer 1

0

First of all, ImageCollection.min() is a short-cut for reduce(ee.Reducer.min()). The min reducer has built-in ability to pass on more values than the one it chose the minimum of, when you set numInputs greater than 1. However, by default, it won't preserve the band names, so here's a bit of boilerplate to solve that:

function argminReduce(imageCollection) {
  var bandNames = imageCollection.first().bandNames();
  return imageCollection.reduce(
    ee.Reducer.min(bandNames.size())
      .setOutputs(bandNames)
  );
}

This will take an image collection and return an image, where each pixel has the minimum value for the first band and all other bands have the corresponding values from the same image.

Using it for your described purpose:

var images = terraClimate
  .filterDate('2020-01-01', '2020-12-31')
  .map(function (image) {
    var pr = image.select('pr');
    var monthValue = image.date().get('month');
    return pr.addBands(ee.Image.constant(monthValue).toInt().rename('month'));
  });

var minimum = argminReduce(images);
Map.addLayer(minimum, {bands: ['month'], min: 1, max: 12});

https://code.earthengine.google.com/6fcbb630e23dc8f05575af325eb36ade

(This is not intended to be a scientifically meaningful analysis of the dataset. This code is only intended to demonstrate the desired minimum operation.)

Your Answer

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

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