open-geocomputing / OpenEarthEngineLibrary

https://www.open-geocomputing.org/OpenEarthEngineLibrary/
127 stars 38 forks source link

error about MovingWindows and SavatskyGolayTimeFilter #16

Closed zicai1 closed 1 year ago

zicai1 commented 1 year ago

i have two questions,the first one is about MovingWindows,the code is

var merge_vi = l8_vi.merge(s2_vi).sort('system:time_start')
var snow_ = merge_vi.reduce(ee.Reducer.percentile([5])).clip(ROI)
var merge = merge_vi.map(function(img){
  var image = ee.Algorithms.If(ee.Number(img.get('doy')).gt(60).and(ee.Number(img.get('doy')).lt(320)),img,img.unmask(snow_))
  return ee.Image(image).copyProperties(img,img.propertyNames())
})
merge = ee.ImageCollection(merge)
print('merge',merge)
var smooth_vi=oeel.ImageCollection.movingWindow(merge,ee.Filter.maxDifference(1000*3600*24*20, 'system:time_start', null, 'system:time_start'))
                                  .select(['NDVI_mean','EVI_mean','NIRv_mean','kNDVI_mean'],['NDVI','EVI','NIRv','kNDVI']);
print('smooth_vi',smooth_vi)

the snow_ is an image that can be add the map,is also have four bands,when i run the code is report error:Invalid string length.But when I change the parameter of img.mask(snow) to a constant 0.1, the code can run normally. the other question is about SavatskyGolayTimeFilter,I asked this question before and it was solved, but when I added some processing steps, it reported an error, I don't know why, I think my code is fine

var join = ee.Join.saveFirst({
  matchKey:'smooth',
})
var filter = ee.Filter.equals({leftField: 'system:time_start',
  rightField: 'system:time_start'})
var join_vi = join.apply(merge_vi,smooth_vi,filter)
var residual = join_vi.map(function(img){
  var res = ee.Image(img).subtract(ee.Image(img.get('smooth')))
  return res.copyProperties(img,['system:time_start','doy','date','crs'])
})
var vi_sd = ee.ImageCollection(residual).reduce(ee.Reducer.stdDev())
var sd_throld = vi_sd.abs().multiply(1.5)
var join1 = ee.Join.saveFirst({
  matchKey:'res',
})
var join_vi1 = join1.apply(join_vi,residual,filter)
var vi_col = join_vi1.map(function(img){
  var ori = ee.Image(img);
  var smooth = ee.Image(img.get('smooth'))
  var res = ee.Image(img.get('res')).abs()
  var mask = res.lt(sd_throld)
  var vi = ori.updateMask(mask).unmask(smooth)
  return vi
})
vi_col = ee.ImageCollection(vi_col).sort('system:time_start')
var merge_sg = sg_smooth(merge_vi,extract_band)
print('merge_sg',merge_sg)
function sg_smooth(col,bandname){
  var col = col.select(bandname);
  var sg_col = oeel.ImageCollection.SavatskyGolayFilter(col,                     //SG滤波函数
      ee.Filter.maxDifference(1000*3600*24*45, 'system:time_start', null, 'system:time_start'),//@ collection:你准备平滑的影像数据集
      function(infromedImage,estimationImage){
        return ee.Image.constant(ee.Number(infromedImage.get('system:time_start'))//@ filter:滑动窗口大小
          .subtract(ee.Number(estimationImage.get('system:time_start'))));},//@ distanceFunction:距离函数
      4,[bandname]).sort('system:time_start');//@ degree:拟合多项式的次数       //@ bandOfInterest:你准备平滑的影像数据波段

  sg_col = sg_col.map(function(img){
    var smooth = img.select(ee.String('d_0_').cat(bandname)).rename(ee.String('sg_').cat(bandname));       //对平滑后的波段进行重命名
    // var daoshu = img.select(ee.String('d_1_').cat(bandname)).rename(ee.String('f1_').cat(bandname)).multiply(2000000000);
    return img.select(bandname).addBands([smooth])                                     //把重命名的新波段添加到影像中
  })

  return sg_col
}

I just want to filter out outliers with residuals greater than 1.5 S.D, sg_smooth(merge_vi,extract_band) If the first parameter is filled with merge_vi, no error will be reported, and if vi_col is filled, an error will be reported.the vi_col is no promble,it can be print,it works in another program,When I don't do outlier filtering, it can run,I don't know where the problem is.

mgravey commented 1 year ago

Hi, it's really hard do debug only based on the code, in the future think about adding a link to you code, and to share any required asset.

1) My advise do not use if even not ee.Algorithms.If, if you want to fill gaps of an image with another, then stack both(or more) images in an imageCollection, and do a mosaic() reduction.

2) if it reports an error, then provide the error message 😄

zicai1 commented 1 year ago

This is a job I haven't done and it's not appropriate to expose the code now, can I email you