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