Inspired from Climate Engine platform, I want to calculate Standardize Precipitation Index based on this paper, where each rainfall datasets are fitted to the gamma function to define the relationship of probability to rainfall.

I have try to write a code in GEE to calculate 3-month SPI, so far using simple equation where SPI = (current rainfall - LongTermAverage) / StDev spi = rainfall_now.subtract(ltamean).divide(ltastd). I have difficulties on how to fitted the data to gamma function in GEE, is there any reference or example about this?

Map.setCenter(109.738, -7.347, 7);

// We will compare for a 1-month period of CHIRPS data 
var start = 31+28; 

// Long term average (LTA) can be generated directly from the pre-calculated Rainfall
var lta = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
  .filter(ee.Filter.dayOfYear(start, start + 31))
  .filterDate('1981-01-01', '2019-12-31');

var ltamean = lta.select('precipitation').mean();
var ltastd = lta.reduce(ee.Reducer.stdDev());

// same for this year
var latest = ee.ImageCollection('UCSB-CHG/CHIRPS/PENTAD')
  .filter(ee.Filter.dayOfYear(start, start + 31))
  .filterDate('2020-01-01', '2020-05-01');

var rainfall_now = latest.select('precipitation').mean();

// add the various layers and layer combination 
Map.addLayer(rainfall_now, rainfall_viz, 'JFM 2020', false);
Map.addLayer(ltamean, rainfall_viz, 'LTA', false);
Map.addLayer(ltastd, rainfall_viz, 'STDev', false);

// Calculating rainfall anomaly and standardized precipitation index 
var anomaly = rainfall_now.divide(ltamean).multiply(100);
var spi = rainfall_now.subtract(ltamean).divide(ltastd);

// Define a colour table
var red2blue = ['a60027','de3e2c','fa8b50','ffd485','e1e1e1','d3edf5','90c4de','5183bd','313694']
var rainfall_viz = {min:0, max:25, palette: ['B8810B', 'CB8C08', 'FFE47E','FFF2C1', 'DAF7A6', 'D2FFFF', '2DE7E7', '00B9D6', '008BD6', '006399']};
var rainanom_viz = {min:0, max:200, palette: red2blue};
var spicolor = ['760005','ec0013','ffa938','fdd28a','fefe53','ffffff','a2fd6e','00b44a','008180','2a23eb','a21fec'];
var spi_viz = {min:-2, max:2, palette: spicolor};

// Add the image to the map using both the color ramp and interval schemes.
Map.addLayer(anomaly, rainanom_viz, 'Anomaly (JFM2020-LTA)');
Map.addLayer(spi, spi_viz, 'SPI JFM2020');

For reference, attached is the spreadsheet file, that I frequently used to calculate SPI3 that incorporate gamma function and normal inverse in the calculation.

2 Answers 2


That is a limitation with GEE. More details and code are provided here in this regards: https://un-spider.org/advisory-support/recommended-practices/recommended-practice-drought-monitoring-spi/step-by-step

  • the link is not working, I tried opening it many times. Any idea why is that so? or could you please save the page and share it,if possible.
    – HARSH
    Sep 4, 2023 at 7:37

In order to fit the gamma distribution (CDF) on rainfall data to calculate SPI, a few parameters need to be calculated as you have shared in the attached spreadsheet.

Now the main thing is to fit the gamma over it. The Gamma(x,a,b) is for the probability distribution function while when we are calculating SPI we need cumulative distribution function which required incomplete gamma function.

To calculate the first you have to calculate x/b and then you can use "gammainc" function available in GEE.

I am attaching my GEE Code where I have calculated each and every parameter, hope it will help you.



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.