1

I tried to plot the NDVI Time Series graph with the following code. but got this error

Error:

Computation timed out.

Code:

// Importing the Shape File of Area:

var Dehradun = ee.FeatureCollection('projects/ee-omkarthesis2022/assets/India_Dist')
                .filter('NAME_2 == "Dehradun"');

Map.centerObject(Dehradun, 8)        
/*-------------------------------------- // CloudMask-----------------------------------------*/

function mask2Clouds(image){
  var QA = image.select('QA60')
   
  var cloudBitMask = (1 << 10);                            // Bits 10 and 11 cloud and cirrus 
  var cirrusBitMask = (1 << 11);
  
  var mask = QA.bitwiseAnd(cloudBitMask).eq(0)            // setting both to zero indictes clear condition
              .and(QA.bitwiseAnd(cirrusBitMask).eq(0));
  
  return image.updateMask(mask).divide(10000)
              .copyProperties(image)
              .set('system:time_start',
              image.get('system:time_start'));
}

var start_date= '2016-06-01'
var end_date= '2016-11-30'

var Sen2 = ee.ImageCollection('COPERNICUS/S2')
              .sort('CLOUDY_COVER', false)
              .filterDate(start_date, end_date)
              .filterBounds(Dehradun)
              .map(mask2Clouds)
              
/*----------------------------------- // Computation NDVI----------------------------------------*/
                   
var NDVI_Sen2 = Sen2.map (function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']))
                           .addBands(image.metadata('system:time_start')
                          // .divide(1e18)
                           .rename('time'))
})


function smoother(t){
  // helper function to apply linear regression equation
  function applyFit(img){
      return img.select('time').multiply(fit.select('scale')).add(fit.select('offset'))
              .set('system:time_start',img.get('system:time_start')).rename('nd');
  }
  t = ee.Date(t);
  
  var window = NDVI_Sen2.filterDate(t.advance(-windowSize,'day'),t.advance(windowSize,'day'));
    
  var fit = window.select(['time','nd'])
    .reduce(ee.Reducer.linearFit());
    
  return window.map(applyFit).toList(5);
}

// function to reduce time stacked linear regression results
// requires that a variable 'fitIC' exists from the smooter function

function reduceFits(t){
  t = ee.Date(t);
  return fitIC.filterDate(t.advance(-windowSize,'day'),t.advance(windowSize,'day'))
              .mean().set('system:time_start',t.millis()).rename('nd');
}

var dates = NDVI_Sen2.aggregate_array('system:time_start');

var windowSize = 30; //days on either sides

var fitIC = ee.ImageCollection(dates.map(smoother).flatten());

var smoothed = ee.ImageCollection(dates.map(reduceFits));
// print('smoothed',smoothed)

// var smooth_mos = smoothed.mosaic()

// merge original and smoothed data into one image collection for plotting
// var joined = ee.ImageCollection(smoothed.select(['nd'],['smoothed'])
//                 .merge(NDVI_Sen2.select(['nd'],['original'])));

/*------------------------------------// Plot the Graph----------------------------------------*/

var chart = ui.Chart.image.series({
  imageCollection: smoothed,
  region: Dehradun,
  reducer: ee.Reducer.mean(),
  scale: 20
}).setOptions({title: 'NDVI over time'});

print(chart);

Code link: https://code.earthengine.google.co.in/eae1f20818b09f37a14b4232738aec0d

1 Answer 1

1

It is an scale problem (the value of 20 is very low for smoothed; it has a value about of 100,000). It can be fixed in following code lines:

print(smoothed);
var scale = smoothed.first().projection().nominalScale();

print("scale", scale);

Map.addLayer(smoothed.first());

var chart = ui.Chart.image.series({
  imageCollection: smoothed,
  region: Dehradun,
  reducer: ee.Reducer.mean(),
  scale: scale
}).setOptions({title: 'NDVI over time'});

print(chart);

Assuming an arbitrary area for Dehradun (your asset was not available for me), after running complete code, I could get the chart in a few seconds as follows:

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.