GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列

  上一期我们介绍了Sentinel-2 Level-2A数据在逐月时间序列方面的研究。今天我们接着介绍日均值时间序列的计算。大家主要学习计算的方法就可以,替换其他数据试试可不可行。

首先我们先把上期遇到的小问题解决一下:

  对,就这个图,NDVI的图例在使用"system:index"的时候,序号没有设置好。

GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列_第1张图片

  研究了好久还是没办法,所以小编决定换一个chart类型。ui.Chart.image.series

var years = ee.List.sequence(2020, 2020);
var months = ee.List.sequence(1, 12);
var days = ee.List([1, 30]).toString();
var S2_monthlymeanNDVI =  ee.ImageCollection.fromImages(
  years.map(function (y) {
    return months.map(function(m) {
    return NDVI.filter(ee.Filter.calendarRange(y,y, 'year')).filter(ee.Filter.calendarRange(m, m, 'month')).mean().set('year', y).set('month', m).set('system:time_start', ee.Date.fromYMD(y, m, 1)).set('system:index', days);
    });
  }).flatten()
);
var monthlychart = ui.Chart.image.series(S2_monthlymeanNDVI, geometry,ee.Reducer.mean(),500)
              .setChartType('LineChart').setOptions({
                interpolateNulls: true,
                title: 'NDVI time series',
                hAxis: {title: 'Date'},
                vAxis: {title: 'NDVI',viewWindowMode: 'explicit', viewWindow: {max: 0.7,min: 0.3,},gridlines: {count: 10,}},
                trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}});
// Display.
print(monthlychart);

  SO?

GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列_第2张图片

  学艺不精,琢磨了好久还是没有把index设置好,所以索性就不用它了。大家如果有好的办法,可以教教小编,谢谢啦~公众号回复122701了即可获取完整代码。

  好了,进入今天的正题。我们从上期的介绍也可以看出,同一天有好多景影像,关键是每幅影像计算的结果还不一样。因此我们需要把每天平均再研究,这样相对来说好一点。

//还是老样子哈,以广东省2020年为目标
var geometry = ee.FeatureCollection('users/ZhengkunWang/guangdongsheng')
Map.centerObject(geometry,6)
var colorizedVis = {
  min: -0.8,
  max: 0.8,
  palette: ['blue', 'white', 'green'],
};

//使用QA波段去云
function maskS2clouds(image) {
  var qa = image.select('QA60');
  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000).set(image.toDictionary(image.propertyNames()));
}

//因为全年影像太多了,为了计算快一点,这次选择七月份做研究
var S2_COL = ee.ImageCollection("COPERNICUS/S2")
                       .filterDate("2020-07-01", "2020-07-31")
                       .filterBounds(geometry)
                       .map(maskS2clouds)
                       .map(function(image){
                         var ndvi = image.normalizedDifference(["B8","B4"]).rename('NDVI')
                         return image.addBands(ndvi)
                       })
                       .select('NDVI');
print(S2_COL)
Map.addLayer(S2_COL.mean().clip(geometry), colorizedVis, 'NDVI image');

结果如图:

GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列_第3张图片

  既然要得到日均值,那我们首先要设置一个以一天为单位的间隔,然后再递进计算。


//设置时间属性,以1天为间隔单位
var interval = 1;
var increment = 'day';
var start = '2020-07-01';
// make a list of start years
var startDate = ee.Date(start);
var secondDate = startDate.advance(interval, increment).millis();
var increase = secondDate.subtract(startDate.millis());
var list = ee.List.sequence(startDate.millis(), ee.Date('2020-07-31').millis(), increase);
// monthly composite
var dailyMean =  ee.ImageCollection.fromImages(list.map(function(date){
  return S2_COL.select('NDVI').filterDate(ee.Date(date), ee.Date(date).advance(interval, increment))
           .mean().set('system:time_start',ee.Date(date).millis());
}));
//可以看看七月份一共计算到多少幅日均值
print(dailyMean)
//绘制时间序列
var chart = ui.Chart.image.series({
    imageCollection: dailyMean,
    region: geometry,
    reducer: ee.Reducer.mean(),
    scale: 500
    }).setOptions({
      interpolateNulls: true,
      lineWidth: 2,
      title: 'NDVI Daily Time Seires',
      vAxis: {title: 'NDVI'},
      hAxis: {title: 'Date'},
      trendlines: { 0: {title: 'NDVI_trend',type:'linear', showR2: true,  color:'red', visibleInLegend: true}}
    });
print(chart);

GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列_第4张图片

 GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列_第5张图片

  到此我们就计算完成了,可以看到计算完的结果还不错。而且不知道什么原因,有些日子里是没有计算成功的,不知道是不是去云之后影像缺失的问题。

  公众号回复:122801 即可获取上述代码。如果真的可以帮到你,记得给小编点个赞哦~

参考博客:

  本期没有参考,全是手工学习的。

更多精彩内容请关注:

GEEer成长日记六:Sentinel-2计算逐日NDVI时间序列_第6张图片

 

你可能感兴趣的:(Google,earth,engine,google,earth,javascript,时序模型,云计算)