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:
=IF(D2>0,((180/PI())*ATAN(C2/D2)+180),IF(AND(C2<0,D2<0),((180/PI())*ATAN(C2/D2)),IF(AND(C2>0,D2<0),((180/PI())*ATAN(C2/D2)+360),IF(AND(D2=0,C2>0),270,IF(AND(D2=0,C2<0),90,0)))))
...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:
codeblock:
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
DIR =
winddir(DIR)
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.
Thanks,
DK
Great. It works for me. Very much appreciated
["Extreme", "High", "Moderate", "Low", "Very Low"][!Value!-1]
Because DIR doesnt appear in the winddir function , I removed the parameter in the def-statement.
I guess you're working in the python-window, so please try this:
arcpy.env.workspace= "A:\\test" # Change to the data source directory tables = arcpy.ListFeatureClasses('*') #all shapes inside the directory #ALTERNATIVE if tables wanted # tables = arcpy.ListFiles('*.dbf') #all dBase-files inside the directory len(tables) # just to controll, should be ~50 for table in tables: #loop through all files # with arcpy.da.UpdateCursor(table,["DIR","U","V"]) as cursor: for row in cursor: #loop through all rows u,v = row[-2:] row[0] = winddir(u,v) #new DIR-value associated cursor.updateRow(row) #rewrite the row in the table
You can replace line 11&12 by this expression:
row[0] = winddir(*row[-2:])