1

I would like to apply an expression to an image based on conditions. The part of my code that I need to do this in is where i need to calculate runoff Q2 based on values of AMC and S:

var runoff_func = function(image) {

  var AMC = image.select('precipitation_1')
  var ppt = image.select('precipitation')
  var AMCreplaced = S_image2_slope_corrected.where(AMC.lte(13), S_image1);
  var AMCreplaced2 = AMCreplaced.where(AMC.gt(28), S_image3)
  var s_value = AMCreplaced2.select('S_image2_slope_corrected')

  // adjusted Q based on Verma et al., 2018

  var Q2 = image.expression(
    '((ppt-0.12*S_image + 0.19*(sqrt(ppt*S_image))*(ppt-0.12*S_image +0.38*S_image))/(ppt+0.88*S_image)+(0.38*S_image))', {
      'ppt': image.select('precipitation'),
      'S_image': AMCreplaced2.select('S_image2_slope_corrected')
    });

  var Q3 = Q2.where(ppt.lt(s_value.multiply(0.2)), zeroImage)
  return Q3.clip(Drakensberg).rename('runoff')
    .copyProperties(image, ["system:time_start"]);
};
var runoff = MergedRain_AMC.map(runoff_func)

print(runoff)

I would like to calculate Q2 based on the following conditions:

If ppt <= (0.12*S_image2 - 0.19*S_image2) then Q = 0
If (0.12*S_image2 -P)<= 0.19 *S_image2 <0.12*S_image2_slope_corrected  then Q = ((ppt-0.12*S_image + 0.19*(sqrt(ppt*S_image))*(ppt-0.12*S_image +0.38*S_image))/(ppt+0.88*S_image)+(0.38*S_image))
If 0.12*S_image2 <= 0.19*S_image2<= 1.12*S_image2 then

Q = ppt (1- (1.12*S_image2 - 0.19*S_image2)**2/ppt*(1.12*S_image2 - 0.19*S_image2)+S_image2*(S_image2+0.19*S_image2))

Here is the link to my code:

https://code.earthengine.google.com/?scriptPath=users%2Fmorriskolawole%2Ftest%3ASCS_CN_Verma%20method_cathment

1 Answer 1

2

You can use ee.Image.where() for conditionals. Note that if more than one condition match, the last will get applied. Using ee.Image.expression() makes the conditions a bit more readable when they're complex. To make it more concise, I created a function to evaluate your specific variables.

var Q = ee.Image(-1) // Value to use when none of the conditions match
  .where(
    evaluate('ppt < (0.12*S_image2 - 0.19*S_image2)'), 
    0
  )
  .where(
    evaluate('(0.12*S_image2 -P)<= 0.19 *S_image2 <0.12*S_image2_slope_corrected'), 
    evaluate('((ppt-0.12*S_image + 0.19*(sqrt(ppt*S_image))*(ppt-0.12*S_image +0.38*S_image))/(ppt+0.88*S_image)+(0.38*S_image))')
  )
  .where(
    evaluate('0.12*S_image2 <= 0.19*S_image2<= 1.12*S_image2'), 
    evaluate('ppt*(1- (1.12*S_image2 - 0.19*S_image2)**2/ppt*(1.12*S_image2 - 0.19*S_image2)+S_image2*(S_image2+0.19*S_image2))')
  )

function evaluate(expression) {
  return ee.Image().expression(expression, {
    ppt: ppt,
    S_image: S_image,
    S_image2: S_image2,
    S_image2_slope_corrected: S_image2_slope_corrected,
    P: P
  })
}

https://code.earthengine.google.com/d2579a461fad8ca25c8bef674aee7524

1
  • Thank you very much! problem solved
    – Rozy
    Commented Aug 31, 2022 at 12:22

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.