I am trying to write an iterative procedure using the GEE Code Editor. I initially calculate a variable "AR" and if this variable is greater than a specified threshold, then an iterative procedure is repeated until the AR value is below the threshold value. I am uncertain about the most efficient way to achieve this result given the client/server constraints of the IDE. In my example, I need to use a .getInfo()
call to retrieve the AR value, or else it will not be recognized and passed into the while loop. Unless there is a different way to do this? I understand this is not the best practice, especially within the loop. Please see my example code below...
var calculate_adjustment_ratio = function(x, y){
// ENTER TOTAL FROM MODELED DEFORESTATION
var modeled_deforestation = ee.Number(x)
// ENTER EXPECTED AMOUNT OF DEFORESTATION
var expected_deforestation = ee.Number(y)
// CALCULATE ADJUSTMENT RATIO
var AR = expected_deforestation.divide(modeled_deforestation)
print(AR, 'Adjustment Ratio')
return AR
}
// ENTER INITIAL INPUT PARAMETERS TO CALCULATE THE ADJUSMENT RATIO
var modeled_deforestation = 1.75
var expected_deforestation = 3
// CALCULATE INITIAL ADJUSTMENT RATIO (AR)
var AR = calculate_adjustment_ratio(modeled_deforestation,expected_deforestation).getInfo()
//////////////////////////////////////////////////////////////////////////
var update_modeled_deforestation = function(modeled_deforestation, update_deforestation){
var updated = ee.Number(modeled_deforestation).add(ee.Number(update_deforestation))
return updated
}
//////////////////////////////////////////////////////////////////////////
var maxIterations = 10
var iterationCount = 0
var adjustmentValue = 0.5
// IF THE INITIAL AR IS GREATER THAN 1.00001, ITERATE LOOP UNTIL AR >= 1.00001
while (AR > 1.00001 && iterationCount <= maxIterations) {
var modeled_deforestation_new = update_modeled_deforestation(modeled_deforestation, adjustmentValue)
AR = calculate_adjustment_ratio(modeled_deforestation_new,expected_deforestation).getInfo()
iterationCount++
adjustmentValue = adjustmentValue + 0.5;
// Print intermediate AR value
print("Intermediate AR (iteration " + iterationCount + "):", AR);
}
if (iterationCount <= maxIterations) {
print("AR should be less than 1.00001")
} else {
print("Maximum number of iterations reached");
}