1

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.
  • The apply_offset() is where you have problems. If you don't map that to the ImageCollection everything else runs fine. Right now if you have something in 2017 it's not correcting to a modulo day of year, it's going to be some value in the thousands. – JepsonNomad Dec 21 '18 at 1:11
  • The values in MCD12Q2 are already in the thousands since they are "Days since Jan. 1, 2000" and the apply_offset() is meant to correct that to the actual Julian day. If you know how to make it work correctly can you please share the code? – Shawn Dec 21 '18 at 13:01
  • I just took out the .map(apply_offset()) from your original imageCollection fetch. I'd give Kuik's approach a shot. Edit: Just confirmed that Kuik's method works. – JepsonNomad Dec 21 '18 at 19:28
3

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

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.