Google Earth Engine – Calculating Long-term Mean NDVI Anomaly in Time Series

google-earth-enginendvitime series

I want to calculate the longterm mean NDVI anomaly at a specific geometry point buffered to 1000m. I have calculated the longterm mean NDVI for the 20 year period. Because I want to calculate NDVI anomalies per year, I have calculated the monthly mean NDVI for the year 2020 as my first observation period.

The 20 year longterm mean NDVI and the monthly mean NDVI scripts are working as can be seen from the timeseries plots generated.

I am getting an error at line 224 of the script calculating the NDVI anomalies where I am trying to write a function that returns the anomalies using the formula below;

Ndvi_anomalies = monthly mean NDVI – longterm mean NDVI

Looking forward to your help and suggestions.

Here is the link to the code; https://code.earthengine.google.com/19e96a3fcda5b52fa84f1ed96ccf1dc8

Best Answer

You are going about your problem the wrong way. If you look at the documentation of the subtract() function (in the Docs tab next to Scripts) you'll see that is applicable to ee.Arrays, ee.Images and ee.Numbers, but not ee.ImageCollections. If you want a result for each month, you have to map through your image collection so that it runs the operation one image at the time, for example:

var Ndvi_anomalies =  months.map(function(m) {
  return mthmeanNDVI.filterMetadata('month', 'equals', m).first()
    .subtract(ltmean.filterMetadata('month', 'equals', m).first())
    .clip(swarmpoint).set('month', m);
});