Select to view content in your preferred language

Model Builder and Python : Boolean in Calculting Field box

781
3
Jump to solution
11-08-2021 02:03 PM
Mag_Geol2016
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.

Mag_Geol2016_0-1636407921990.png

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.

Mag_Geol2016_1-1636408299050.png

Mag_Geol2016_2-1636408318112.png

Mag_Geol2016_3-1636408336677.png

 

Thank you in advance for your precious help!

 

 

 

0 Kudos
1 Solution

Accepted Solutions
DominicRoberge2
Frequent Contributor

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)

...rest of your code

 

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

Bonne chance!

 

 

 

View solution in original post

3 Replies
DominicRoberge2
Frequent Contributor

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)

...rest of your code

 

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

Bonne chance!

 

 

 

curtvprice
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

 

 

Mag_Geol2016
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!

0 Kudos