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'?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
jblng
  • 57
  • 1
  • 6

1 Answers1

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");
Noel Gorelick
  • 11,405
  • 11
  • 26