I have created a model in ArcGIS 10.2 that iterates through some feature classes in order to add a field 'Zmin10' (double) and calculate the field value based on a variable (called "Minimum") to which it adds a different value based on the feature class name ("outName").
Here is the model:
The Calculate Field Tool ('Recalculate Field Zmin10') is set as following:
Expression: shift( !Zmin10! )
Expression type: Python
Code Block:
def shift(Zmin10):
if %outName% == 'toc':
return (( float(%Minimum%) ) - 20)
elif %outName% == '2016':
return (( float(%Minimum%) ) - 40)
elif %outName% == '2017':
return (( float(%Minimum%) ) - 60)
else:
return (( float(%Minimum%) ) - 80)
The code block does not work at all for the feature class named 'toc', where I get the following error:
Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 2, in shift
NameError: global name 'toc' is not defined
For the other options, it always returns the value according to the "else" part of the statement, as if somehow it does not recognise the other values either.
I do not understand exactly where the code is wrong and why.
For more clarity, the Python script is attached:
# Import arcpy module
import arcpy
# Load required toolboxes
arcpy.ImportToolbox("Model Functions")
# Script arguments
Input_Folder = arcpy.GetParameterAsText(0)
if Input_Folder == '#' or not Input_Folder:
Input_Folder = "D:\\ArcGIS\\Scripts\\ModelTransects\\test_final\\03_SHP\\Tempor" # provide a default value if unspecified
Minimum = arcpy.GetParameterAsText(1)
if Minimum == '#' or not Minimum:
Minimum = "-157.3" # provide a default value if unspecified
# Local variables:
path_points_sel__1_ = Input_Folder
path_points_sel2 = path_points_sel__1_
path_points_sel__2_ = path_points_sel2
Name = Input_Folder
outName = Name
# Process: Iterate Feature Classes
arcpy.IterateFeatureClasses_mb(Input_Folder, "*points_sel*", "POINT", "RECURSIVE")
# Process: Calculate Value
arcpy.CalculateValue_management("changeName(\"%Name%\")", "def changeName(Name):\\n return str(Name[16:])", "String")
# Process: Add Field
arcpy.AddField_management(path_points_sel__1_, "Zmin10", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
# Process: Recalculate Field 'Zmin10'
arcpy.CalculateField_management(path_points_sel2, "Zmin10", "shift( !Zmin10!, %outName% )", "PYTHON", "def shift(Zmin10, outName):\\n outName = %outName%\\n if outName == 'toc':\\n return (( float(%Minimum%) ) - 20)\\n elif outName == '2016':\\n return (( float(%Minimum%) ) - 40)\\n elif outName == '2017':\\n return (( float(%Minimum%) ) - 60)\\n else:\\n return (( float(%Minimum%) ) - 80)")