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.

1 Answer 1


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

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.