I am calculating the SPI from the precipitation data for 12 month period of database "UCSB-CHG/CHIRPS/DAILY". I has implemented various stages to calculate the final SPI. Here the final SPI image types shows its 'ImageCollection'. I want to visualize each spi image over the map. error "linear: Layer error: Number.multiply, argument 'right': Invalid type. Expected type: Number. Actual type: Image<[precipitation]>."


var CHIRPS = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY");

var roi = 
        [[[68.16642515954577, 25.162738638764438],
          [68.16642515954577, 20.21844152685595],
          [74.75822203454577, 20.21844152685595],
          [74.75822203454577, 25.162738638764438]]], null, false);

var StYear = 1981;var StMonth = 1;
var EnYear = 1982;var EnMonth=5;
//-----------------CALCULATION OF 12 MONTH SPAN IMAGES---------
var IMean=ee.ImageCollection([]); 
var IMean=ee.ImageCollection([]);
var ILog=ee.ImageCollection([]);
var i;
var Exit=0;
while (Exit===0){
  var StDate=ee.Date.fromYMD(StYear,StMonth,1);

  var tEnYear=StYear+1;

  var EnDate=ee.Date.fromYMD(tEnYear,StMonth,1);//##advance the end date by 12 month
  print('Date='+StYear.toString()+'-'+StMonth.toString()+' to '+tEnYear.toString()+'-'+StMonth.toString());
  var I1 = ee.Image((CHIRPS).filter(ee.Filter.date(StDate,EnDate)).filterBounds(roi).select('precipitation').mean());
  var I1A=I1.clip(roi);
  IMean=IMean.merge(I1A);//## merge image with collection
  var IL=(I1A).log();
  var I1L=(IL.clip(roi)).log()
  if ((tEnYear==EnYear) && (StMonth==EnMonth)){
print('Mean Image Collection=',IMean,IMean.name())

//calculate mean of mean images and sum of log
var MSize=IMean.size();
var N=MSize.getInfo();
print('Size ImageCollection Mean=',N)

//--- Calculate Mean of all Monthly images
var FinalMean=ee.ImageCollection(IMean).mean();//8.86, J4
print('Mean of Images Calculated, ',FinalMean,FinalMean.name());
// Calculate Log of the sum of images
var LogofMean=(FinalMean).log();
print('Log of mean image calculated, ',LogofMean,LogofMean.name())

//-- sum of all log images
var SumofLog=ee.ImageCollection(ILog).sum();//717.33
print('Sum of log of individual calculation calculated, ',SumofLog,SumofLog.name())
//---calculate A
var A=ee.Image(LogofMean.subtract(SumofLog.divide(N)));//A
print('A Calculated,',A.name())
//#calculate alpha, beta
var s1=ee.Image((ee.Number(1).divide(ee.Number(4).multiply(A))))
var s2=ee.Image((ee.Number(1).add((ee.Number(1).add(A.multiply(4).divide(3))).sqrt())))
var alpha=ee.Image(s1.multiply(s2))
print('alpha calculated, ',alpha.name())

var beta=FinalMean.divide(alpha)
print('beta calculated, ',beta.name())
// calculate Gamma CDF
var z=FinalMean.divide(beta)
var GammaCDF=ee.Image(ee.Number(z).gammainc(alpha))
var SPI=ee.Image(ee.Number(-1).multiply(((ee.Number(2).sqrt())
var SPI2=ee.ImageCollection(SPI.copyProperties());
print("SPI Calculated=",SPI2.name())//to print object type

var visParams = {
  "palette":[ "FF595E","FF924C","FFCA3A","C5CA30","8AC926",
//var img1 = ee.Image((CHIRPS).filter(ee.Filter.date(StDate,EnDate)).filterBounds(roi).select('precipitation').mean());
var SPISlice = ee.Image(SPI2.first());
print('SPLICE Type= ',SPISlice.name())
Map.addLayer(SPISlice, visParams, 'linear')

//---CONSOLE OUTPUT BELOW--------- enter image description here

1 Answer 1


You are mixing up different object types. So multiplying numbers and images in your case, you can't mix them up in earth engine. For example:

var S1 = ee.Image((ee.Number(1).divide(ee.Number(4).multiply(A))))

should be something like:

var s1_2 =  ee.Image(ee.Image.constant(1).divide(ee.Image.constant(4).multiply(4)))

As soon as you say ee.Number(1) you can't simply divide it by an ee.Image() object, like your variable A. Because divide (and multiply etc.) then expect an number as well.

See: https://developers.google.com/earth-engine/apidocs/ee-image-constant

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.