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" #load input rasters Vmax = Raster(r"\input.gdb\Vmax") V = Raster(r"\input.gdb\V") #check if Spatial Analyst extension is available arcpy.CheckExtension("Spatial") #retrieve license 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 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) 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.