# The truth value of a raster is ambiguous. Invalid use of raster with Boolean operator or function

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
else:
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"

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

#check if Spatial Analyst extension is available
arcpy.CheckExtension("Spatial")
arcpy.CheckOutExtension("Spatial")

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

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)
AET_t.save(outfile_aet)
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)
recharge.save(outfile_re)
if recharge > 0:
V = Vmax
else:
V = V_tt

rain.close()
pet.close()
``````

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.

• 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)