Google Earth Engine – Fixing Mismatched Type for Band Error in Earth Engine

google-earth-engine

I'm trying to calculate the average Julian day of year for greenness decrease from the MODIS MCD12Q2 dataset. Following is the code and error, which comes from having to normalize the julian day within each year using the apply_offset() function. There are several similar questions on here with this error, but I can't figure out how to fix it here.

// The values of MCD12Q2 are "Days since Jan. 1, 2000"
// This is the offset needed to convert to the relative Julian day for the year
var all_doys = ee.List.sequence(1,366);
var apply_offset = function(image){
  var day_offset = image.date().difference('2000-01-01','day');
  return image.subtract(day_offset).multiply(-1);
}

var all_years = ee.ImageCollection('MODIS/MCD12Q2')
            .select(['Onset_Greenness_Decrease1'])
            .map(apply_offset);


print(all_years)
var mean_decrease = all_years.reduce(ee.Reducer.mean());
print(mean_decrease)

var viz_options = {
  min: 0,
  max: 365,
  palette: ['0f17ff', 'b11406', 'f1ff23'],
};
Map.addLayer(
    mean_decrease.select('Onset_Greenness_Decrease1_mean'), viz_options,
    'Mean Decrease');

The error:

Mean Decrease: Tile error: Expected a homogeneous image collection,
but an image with an incompatible band was encountered. 
Mismatched type for band 'Onset_Greenness_Decrease1':
Expected: Type<Float<-65169.0, 366.0>>.
  Actual: Type<Float<-64804.0, 731.0>>.
          Image ID: null
This band might require an explicit cast.

Best Answer

As the error says, just explicitly cast the new image band. The image showed correctly on the screen if I add casting toFloat:

var all_doys = ee.List.sequence(1,366);
var apply_offset = function(image){
  var day_offset = image.date().difference('2000-01-01','day');
  return image.subtract(day_offset).multiply(-1).toFloat();
};

Full code

Related Question