0

the function .map applies a function to every individual image in an ImageCollection. And the function .iterate applies a function to one image and the output of the calculation done to the precedent image on an ImageCollection. The first only works with one image each time, and the second implies modifying each image and utilize it to any calculation with the next one.

I need a function that works like .iterate, but does not modify the precedent image. I just need to do: image (time -1) / image (time 0). I can not find a way to do it,

thanks for your help

i have tried,

var first = ee.List([
  ee.Image(1).set('system:time_start', time0).select([0], ['pc1'])
]);

var changeDET = function(image, list) {
  var previous = ee.Image(ee.List(list).get(-1));
  var change = previous.divide(image.select('pc1'))
    .set('system:time_start', image.get('system:time_start'));

  return ee.List(list).add(change);
};

var cumulative = ee.ImageCollection(ee.List(imageCollection.iterate(changeDET, first)))
  .sort('system:time_start', false)
javier Moreira
  • 65
  • 2
  • 10

1 Answers1

0

What you can do is to convert your imageCollection into a ee.List object, then map over that list with an index variable to access the previous image. Example code is below:

var length = yourImageCollection.size();
var list = yourImageCollection.toList(length);

var calculated_list = list.map(function(img) {
    var index = list.indexOf(img)
    img = ee.Image(img);
    var previousIndex = ee.Algorithms.If(index.eq(0), index, index.subtract(1));
    var previousImage = ee.Image(list.get(previousIndex)):
    var change = ee.Image(previousImage.divide(img)
                                       .copyProperties(img, ["system:time_start"]));
    return change;
})

I'm not sure what you want to do with the first image, so when map function reach the first image, previousIndex will equal to index. In other words, the first image will be divided by itself (as there is no image before it).

Hope this helps.

Kevin
  • 338
  • 1
  • 3
  • 9
  • thanks, @Kevin. Before I try it, actually the problem that you mentioned was the next one. I always get an imageCollection with the first (or possibly the last if sorted backward) with an image I don't want. How can I filter the collection without that image? there is not an `ee.Filter.first` function. thanks – javier Moreira Mar 28 '19 at 13:53
  • @javier, if the image you want to filter out does not share its date with other images, you can filter it using `filterDate` method of imageCollection. Note that this can only be done if that image is either the first or the last image by date. – Kevin Mar 29 '19 at 10:37
  • thanks @Kevin, i have thought of that solution, but, i will filter also on the images of interest, since they share the date. – javier Moreira Mar 29 '19 at 14:42