I have a table that contains data that needs to be summed and the result needs to replace an existing value. I only want to sum the values if they are not -9999. The result would be put in the existing Total column.
Here is my table
Total | D1 | D2 |
---|---|---|
10 | 5 | 5 |
5 | -9999 | 5 |
5 | 5 | -9999 |
ImI am using ArcProArcGIS Pro Python 3 Calculate Field on the Total column. I thought that the following would solve my issue, but I believe my condition is not being set properly. My result is only replacing Total with D1. The sum is not occurring.
Expression
calc(!D1!,!D2!)
Code Block
def calc(D1 , D2 ):
if (D1) != -9999:
return (D1)
elif (D2) != -9999:
return (D1 + D2)
else:
return 0
This code is replacing a VB Script used in arcmap 10.8
a=0
If [d1]>=0 Then
a=a+ [d1]
End If
If [d2]>=0 Then
a=a+ [d2]
End If
Total = a
Any help is appreciated.
Based on the first comment by mikewatt
Is there a better way to write this script?
I could have up to 8 columns to sum?.
def calc(D1 , D2 ):
if (D1) != -9999 and (D2) != -9999:
return (D1 + D2)
elif (D1) != -9999 and (D2) == -9999:
return (D1)
elif (D2) != -9999 and (D1) == -9999:
return (D2)
else:
return 0