0

I am trying to multiply a monthly imageCollection by a different number depending on the month. What I want is to multiply all the pixels of the first 12 months by 0.85, the following 12 months by 0.95, the following 12 by 0.65. All this for a total of 468 months from 1981 to 2019 (the example contains only 2 years, 24 months).

var monthly_sum = ee.List.sequence(0, 2*12).map(function(n) { // .sequence: number of years from starting year to present
  var start = ee.Date('1981-01-01').advance(n, 'month'); // Starting date
  var end = start.advance(1, 'month'); // Step by each iteration

  return ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY")
        .filterDate(start, end)
        .sum()
        .set('system:time_start', start.millis());
});


print(monthly_sum)

var my_list = ee.List([]);

var otherList = ee.List([0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.884748080310752,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95,
0.95
])

Which would be the easiest way to multiply each monthly image by the corresponding multiplicative factor creating a new imageCollection?

Thanks,

1
  • You should include the relevant (and possibly simplified) code in the question post, so that readers can see it without leaving the site. – Tyler Erickson Apr 30 '20 at 13:36
2

You can scale an image collection by a list of scaling factors using the following approach, which constructs a list of (image, scaling factor) pairs, and then multiplies them together.

// Copyright 2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0

var originalCollection = ee.ImageCollection([
  ee.Image(1.0).double(),
  ee.Image(2.0).double(),
  ee.Image(3.0).double(),
  ee.Image(4.0).double(),
]);
var scalingFactors = ee.List([
  1.0,
  10.0,
  100.0,
  1000.0,
]);

// Combine into a list of elements containing an image and a scaling factor.
var MAX_ELEMENTS = 1000;
var zippedList = originalCollection.toList(MAX_ELEMENTS).zip(scalingFactors);

// Map a function over the list to scale each image.
var scaledCollection = ee.ImageCollection.fromImages(
  zippedList.map(function (_list) {
    _list = ee.List(_list);
    var _img = ee.Image(_list.get(0));
    var _scale = _list.getNumber(1);
    var _scaledImage = _img.multiply(_scale);
    return _scaledImage.double();  // Set the data type, so the collection is consistent.
  })
);

// Verify the images are scaled by inspecting the map layer.
Map.addLayer(originalCollection, {}, 'originalCollection');
Map.addLayer(scaledCollection, {}, 'scaledCollection');

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.