# Model Builder and Python : Boolean in Calculting Field box

572
3
11-08-2021 02:03 PM
New Contributor

Hello,

I built a Model Builder tool that performs a number of operations on a FGDB feature class that contains geochemical analyzes. I want to calculate a geochemical index called Eu/Eu * on the basis of the contents of europium (Eu), samarium (Sm) and gadolinium (Gd), according to the following formula: Eu/Eu * = (Eu/0.058)/math.sqrt((Sm/0.153) * (Gd/0.2055)).

To note :

•  Values below a detection limit are negative.

•  For a same element, this detection threshold varies depending on the analysis method. For example, for platinum Pt, it is -0.1 and -70.

•  Since the square root (in the equation) of a negative value does not exist, then my Model Builder tool crashes.

This is why I tried to introduce a small boolean python script in the "Calculate Field" box (figure below), so that if one of the 3 elements is negative, then it must then put zero in the Eu_Eu field created previously (Float type). Otherwise (i.e. actually all 3 are positive) it calculates the above formula.

def calcul(Eu_Eu):

if (!Eu!<=0 or !Gd!<=0 or !Sm!<=0):

return 0

else:

return (!Eu!/0.058)/math.sqrt((!Sm!/0.153)*(!Gd!/0.2055))

The problem is that, besides not really knowing if my script is correct (I'm just a beginner...), it puts me error 000989 (figure below) according to which there would be a problem with the syntax, which I unfortunately cannot locate. To my knowledge, field names should be placed between two exclamation points in a Python script, right? I say that because by putting "‘ ", the error disappears, but the calculation still crashes.

1 Solution

Accepted Solutions
Occasional Contributor III

Hey Mag

You have to add the the other fields that you want to use to your Function definition. Your def should be more like this

def calcul(Euval, GdVal,SmVal)

and to use the function you could use calcul(!Eu_Eu!,!Gd!,!Sm!)

Bonne chance!

3 Replies
Occasional Contributor III

Hey Mag

You have to add the the other fields that you want to use to your Function definition. Your def should be more like this

def calcul(Euval, GdVal,SmVal)

and to use the function you could use calcul(!Eu_Eu!,!Gd!,!Sm!)

Bonne chance!

MVP Esteemed Contributor

@DominicRoberge2  is absolutely right, the Python variables are used locally (unlike labeling expressions). I'd add that a more Pythonic approach is to beg for forgiveness instead of asking for permission. If the calculation throws an error for any reason, you'll get zero return to your Calculate Field results:

``````# expression
calcu(!Eu!, !Gd!, !Sm!)

# bloc du code
def calcu(Eu, Gd,  Sm):
try:
return  Eu / 0.058 / math.sqrt((Sm / 0.053) * Gd / 0.2055)
except:
return 0``````

New Contributor

Hello,

Thank you so much to both of you for your rapid reply and solutions! My problem is now resolved. I finally adopted the second method.

Regards!