0

I am extracting monthly CHIRPS precipitation data using an image stack, and want to add image dates to the band names. Before creating the stack, I created two new image properties, 'month'and 'year'. I want to concatenate these properties to the 'precipitation' band of each image in the collection, such that each band has the following format: 'precipitation_YYYY_MM'.

Script here

Per the answer from the question GEE - Add date to band names in ImageCollection, I have a function that takes the 'month'and 'year' properties of the image, and tries to concatenate that with the existing bandname:

var renamed = monthlycolmean.map(function(image) {
    var month = image.get('month');
    var year = image.get('year');
    return image.rename(image.bandNames().map(function(bandName){
      return ee.String(bandName).cat(month).cat(year);
    }));
  });

However, I get this error:

ImageCollection (Error)
Error in map(ID=0):
Image.rename: Invalid band name: 'precipitation1.01981.0'.

I have also tried to use image.getString() instead of image.get() to pull the month and year properties as strings, but that fails with the following error:

ImageCollection (Error)
Error in map(ID=0):
Element.getString: Unable to cast value to String.

How do I rename correctly so that bandnames are formatted like 'precipitation_YYYY_MM'?

1 Answer 1

1

You can't use "." in band names and without specifying any formatting, the numbers are getting printed at 1.0 and 1981.0. Use "_" instead and force the formatting to be integer:

var month = image.getNumber('month').format("_%d")
var year = image.getNumber('year').format("_%d");
1
  • Ah, thanks Noel! The formatting tip fixed my issue.
    – jblng
    Commented Dec 8, 2021 at 11:02

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.