AnsweredAssumed Answered

Python If Statements in Field Calculator

Question asked by darrochkaye on Mar 11, 2016
Latest reply on Apr 12, 2020 by

Hello all,


I am trying to convert a nested IF statement from Excel into Python via the Field Calculator in ArcMap. The data is wind data which has two fields: U and V; and the calculation is to be entered in the DIR field. These are vectors can be applied with triganometry to create wind direction in degrees. In Excel, the DIR field has the following formula:


...where column C is the U field and column D is the V field.


I've been playing all day with this and so far, I have the following:


def winddir(DIR):
    if !V! > 0:
        !DIR! = ((180/math.pi)*math.atan(!U!/!V!)+180)
    elif !U! < 0 and !V! < 0:
        !DIR! = ((180/math.pi)*math.atan(!U!/!V!))
    elif !U! > 0 and V < 0:
        !DIR! = ((180/math.pi)*math.atan(!U!/!V!)+360)
    elif !V! == 0 and !U! > 0:
        !DIR! = 270
    elif !V! == 0 and !U! < 0:
        !DIR! = 90





I keep getting the generic error:

ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 2)


I'm really pulling my hair out with this as I've tried tweaking the syntax but I cannot resolve it. Any assistance would be greatly appreciated.