I'm having trouble with an if/else statement in my for loop.

I have a raster Vmax with a constant value, another raster V which has a value of 0 in the beginning and will change during execution of the loop. Furthermore I load two text files with rain and ET-data. The idea is to calculate a recharge depending on the change of V which in turn depends on rain and ET. So something like that in a loop:

AET_t = PET*(V/Vmax)
V_tt = V + prec - AET_t
if V_tt > Vmax:
    recharge = V_tt - Vmax
    V = Vmax
    recharge = 0
    V = V_tt

This is my code:

#import modules and packages
import arcpy
from arcpy.sa import *
import os.path

#set workspace
arcpy.env.workspace = r"C:\Users\kern\Documents\model\test_area"

#load input rasters
Vmax = Raster(r"\input.gdb\Vmax")
V = Raster(r"\input.gdb\V")

#check if Spatial Analyst extension is available
#retrieve license

#open rain and PET textfile
rain = open("rain.txt")
pet = open("Epot.txt")
#create list of daily rain and PET data values
rain_list = rain.readlines()
pet_list = pet.readlines()

from itertools import izip, count
for i, p, et in izip(count(),rain_list, pet_list):
    prec = CreateConstantRaster(p, "FLOAT", "5", Vmax)
    PET = CreateConstantRaster(et, "FLOAT", "5", Vmax)
    output_aet = r"C:\Users\kern\Documents\model\test_area\AET.gdb"
    outfile_aet = os.path.join (output_aet, "aet" + str(i))
    output_re = r"C:\Users\kern\Documents\model\test_area\recharge.gdb"
    outfile_re = os.path.join (output_re, "re" + str(i))
    AET_t = PET*(V/Vmax)
    prj_file = r"C:\Users\kern\Documents\model\model_input_5x5\a_aplis.prj"
    arcpy.DefineProjection_management(outfile_aet, prj_file)
    V_tt = V + prec - AET_t
    recharge = Con(V_tt > Vmax, V_tt - Vmax, 0)
    if recharge > 0:
        V = Vmax
        V = V_tt


And this is the error:

File "C:\Users\kirn\Documents\MSc_Arbeit\model\test_area\WaterBalance.py", line 46, in if recharge > 0: ValueError: The truth value of a raster is ambiguous. Invalid use of raster with Boolean operator or function. Check the use of parentheses where applicable.

It seems the problem is the V = Vmax and V = V_tt. But how can I assign Vmax and V_tt respectively to V in another way? I found this https://geonet.esri.com/thread/79949 but couldn't really make head of it.

  • 1
    At the end, what is the intended meaning of a Python if statement that compares a raster to a constant?? Python is gently trying to tell you that this is nonsense. You probably wanted to use a raster calculation (using Con) at this juncture. – whuber Mar 20 '15 at 16:18

In your question it appears that you want to assign the values of each cell of V based on the value of rainfall at that cell. arcpy doesn't allow you (as a numpy array would) to compare a raster to a constant.

Also note that in each iteration of your loop V is being completely overwritten, and then the results aren't being saved anywhere, so once you get to the end of the function the raster \input.gdb\V won't have changed, and the value of V will be whatever value was written in the last iteration.

To resolve the error you're getting you can use the CreateConstantRaster method again to create a zero raster, and then a Con statement to compare and produce an output on a cell by cell basis:

zeros = CreateConstantRaster(O, "INTEGER", "5", Vmax)
V = Con(recharge > zeros, Vmax, V_tt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.