0

Anyone can help me to understand why GEE report this question? Here is my code:


    var table = ee.FeatureCollection("projects/ee-qsz/assets/boreal_id1");
    print('table', table);
    
    // 定义时间范围
    var startYear = 2003;
    var endYear = 2011;
    var months = ee.List.sequence(1, 12);
    var years = ee.List.sequence(startYear, endYear);
    
    // 导入MODIS的EVI数据集
    var collection = ee.ImageCollection('MODIS/MOD09GA_006_EVI')
      .filterDate('2003-01-01', '2011-12-31');
      
    var clippedCollection = collection.map(function(image){
      return image.clip(table);
    });
    print('rawdata', clippedCollection);
    
    // 创建一个函数,用于计算每个月的均值
    var monthlyMean = function(year, month) {
      var startDate = ee.Date.fromYMD(year, month, 1);
      var endDate = startDate.advance(1, 'month');
      var monlyfirstImage = clippedCollection.filterDate(startDate, endDate).first();
      var monthlyImages = clippedCollection.filterDate(startDate, endDate)
        .mean()
        .copyProperties(monlyfirstImage, ["system:time_start"])
        .copyProperties(monlyfirstImage, ["system:time_end"]);
        /*
        .set('system:time_start', startDate.millis())
        .set('system:time_end', endDate.millis());
        */
      return monthlyImages;
    };
    
    // 按年月调用函数,创建月度EVI时间序列
    var months = ee.List.sequence(1,12);
    print(months);
    var monthlyEvi = ee.ImageCollection.fromImages(
      ee.List.sequence(2003, 2011).map(function(year){
        return months.map(function(month){
          var evi = monthlyMean(year, month);
          // 设置时间属性为每个月的第一天
          var startDate = ee.Date.fromYMD(year, month, 1);
          var evi2 = evi;
          return evi2.set('month', month);
        });
      }).flatten()
    );
    print('Monthly EVI', monthlyEvi);
    
    // 定义一个函数,将时间戳转换为可读日期格式
    var formatTime = function(image) {
      var startTime = ee.Date(image.get('system:time_start'));
      var formattedTime_start = startTime.format('YYYY-MM-dd');
      var image_month = image.date().get('month');
      var endTime = ee.Date(image.get('system:time_end'));
      var formattedTime_end = endTime.format('YYYY-MM-dd');
      var image1 = image.set('system:time_start', formattedTime_start);
      var image2 = image1.set('system:time_end', formattedTime_end);
      return image2;
    };
    
    // 对每个图像应用 formatTime 函数
    var formattedEvi = monthlyEvi.map(formatTime);
    
    print('Formatted Monthly EVI', formattedEvi);
    
    //可视化
    var colorizedVis = {
      min: -1,
      max: 1,
      palette: [
        'ffffff', 'ce7e45', 'df923d', 'f1b555', 'fcd163', '99b718', '74a901',
        '66a000', '529400', '3e8601', '207401', '056201', '004c00', '023b01',
        '012e01', '011d01', '011301'
      ],
    };
    Map.setCenter(-7.03125, 31.0529339857, 2);
    // Map.addLayer(formattedEvi, colorizedVis, 'Colorized');
    
    
    // 计算系数d
    var d = ee.Number(2).multiply(Math.PI).divide(12);
    print('d',d);
    
    // 预先计算cos和sin项
    var costerm = ee.List.sequence(0, 11).map(function(i){
      return (d.multiply(i)).cos(); 
    });
    print('costerm',costerm);
    var sinterm = ee.List.sequence(0, 11).map(function(i){
      return (d.multiply(i)).sin(); 
    });
    
    
    // 定义计算季节性指数 SI 的函数
    var calculateSI = function(year) {
      var myDate = ee.Date.fromYMD(year, 1, 1);
      
      // 获取该年份的EVl数据
      var eviYear = formattedEvi.filter(ee.Filter.calendarRange(year, year, 'year'));
      //print('Formatted yearly EVI',eviYear);
    
      // 计算季节性指数 SI
      var mx_col = []; 
      var my_col = [];
    
      for (var i = 0; i < 12; i++) {
        var month = i+1;
        var eviMonth = eviYear.filter(ee.Filter.eq('month', month)).first();
        var L = eviMonth.select('EVI');
        var mx_i = L.multiply(ee.Number(costerm.get(i)))
                .copyProperties(eviMonth, ["system:time_start"])
                .copyProperties(eviMonth, ["system:time_end"]);
        var my_i = L.multiply(ee.Number(sinterm.get(i)))
                .copyProperties(eviMonth, ["system:time_start"])
                .copyProperties(eviMonth, ["system:time_end"]);     
        mx_col.push(mx_i);
        my_col.push(my_i);
      }
      
      mx_col = ee.ImageCollection(mx_col);
      my_col = ee.ImageCollection(my_col);
      
      var mx = ee.Image(mx_col.mean().copyProperties(eviYear.first(), ["system:time_start"]));
      var my = ee.Image(my_col.mean().copyProperties(eviYear.first(), ["system:time_start"]));
      
      var si =  mx.expression(
        'sqrt(mx * mx + my * my)',
        {mx: mx, my: my}
        );
    
      return si.rename(['SI']).copyProperties(eviYear.first(), ["system:time_start"]);
      //return mx;
    };
    var test = ee.Image(calculateSI(2008));
    print('test', test);
    //print('test2',test.select('SI'));
    //Map.addLayer(test.select('SI'), colorizedVis, 'test');
    
    // 计算每年的季节性指数 SI
    var siCollection = ee.ImageCollection.fromImages(years.map(function(year) {
      return calculateSI(year);
    }));
    
    print('si', siCollection);
    var Vis = {
      min: -1.0,
      max: 1.0,
    };
    
    Map.addLayer(siCollection,Vis, 'siCollection');
    var exportArgs = Export.image.toDrive({
      image: test,
      description: 'SI_2008',
      scale: 500,
      region: table.geometry().bounds(),
      fileFormat: 'GeoTIFF',  
      fileNamePrefix: 'SI_2008'
    });

I have tryed to return mx,my and si. When I return mx and add it to map, there are no errors, but when I return my or si, GEE reported this error (in function calculateSI, line 97).

Expected a homogeneous image collection, but an image with an incompatible band was encountered. Mismatched type for band 'EVI': Expected type: Float<-0.0, 0.0>. Actual type: Float<-0.49999999999999994, 0.49999999999999994>. Image ID: 1 This band might require an explicit cast.

3
  • I see no mx, my, or si return in your code. I copied and this script works without issue, so we can't reproduce your error. Please update the question and add the specific lines your code fails
    – aldo_tapia
    Commented Jul 17, 2023 at 12:43
  • Hi aldo, thanks for your answer sincerely. I am sorry I didn't report specific line of the error. And I can now tell you that the mx, mx and si are in the function calculateSI, which in line 97. You didn't find error when you run the code because I return mx in calculateSI. And that's why I am confused, when I return si or my in the funciton, I get the error.
    – qingsong
    Commented Jul 17, 2023 at 14:48
  • I guess maybe its a value error in the calculateion of my, but I can't find where it is now.
    – qingsong
    Commented Jul 17, 2023 at 14:59

1 Answer 1

0

I think I solve the problem. The problem is here:

var my_i = L.multiply(ee.Number(sinterm.get(i)))
                .copyProperties(eviMonth, ["system:time_start"])
                .copyProperties(eviMonth, ["system:time_end"]);  

GEE can't identify the type of the image. So I added a line of code:

my_i = ee.Image(my_i).toFloat();

And then it works!

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.