Hi,
Please see code below.
I need to substitute the value "90" in Expression with user input, that is: LSARqssedret
I have tried: Expression = "!Qs_Mt_a!" * float(LSARqssedret) but I'm not getting the right syntax within the expression.
Any help would be greatly appreciated.
Many thanks
import arcpy import string import math from arcpy import env arcpy.env.overwriteOutput=True from arcpy.sa import * arcpy.CheckOutExtension("Spatial") arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0) INWorkspace = arcpy.GetParameterAsText(0) StageAge = "Maas" LSARqssedret = arcpy.GetParameterAsText(1) GridPoints_ft2_qs = (INWorkspace + "\\" + StageAge + "_nodes_variable_ft_2") arcpy.AddField_management(GridPoints_ft2_qs,"MASS_g_a","DOUBLE") Expression = "!Qs_Mt_a! * 90" arcpy.CalculateField_management(GridPoints_ft2_qs,"Mass_g_a",Expression,"PYTHON")
Solved! Go to Solution.
Your current attempt isn't working because you are trying to multiply a string by a floating point number, which Python doesn't like so the CalculateField_management tool won't work either. Try rewriting Line 21 as:
Expression = "!Qs_Mt_a! * {}".format(float(LSARqssedret))
Your current attempt isn't working because you are trying to multiply a string by a floating point number, which Python doesn't like so the CalculateField_management tool won't work either. Try rewriting Line 21 as:
Expression = "!Qs_Mt_a! * {}".format(float(LSARqssedret))
It works perfectly now.
Much appreciated Joshua.
Hi again,
I'm dealing with a bit more complex expression now but I don't quite get it right.
I got the first part (in bold) but I cannot quite figure out the second part of the expression.
I would much appreciate some help.
Many thanks
import arcpy import string import math from arcpy import env arcpy.env.overwriteOutput=True from arcpy.sa import * arcpy.CheckOutExtension("Spatial") arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0) INWorkspace = arcpy.GetParameterAsText(0) StageAge = "Maas" TOCDrySedDen = arcpy.GetParameterAsText(1) Porosity = arcpy.GetParameterAsText(2) GridPoints_ft2_qs = (INWorkspace + "\\" + StageAge + "_nodes_variable_ft_2") arcpy.AddField_management(GridPoints_ft2_qs,"cm_yr","DOUBLE") Expression = ("!g_cm2_yr! / {}".format(float(TOCDrySedDen))) arcpy.CalculateField_management(GridPoints_ft2_qs,"cm_yr", Expression, "PYTHON_9.3")
# EXPRESSION CALCULATION: (!g_cm2_yr! / float(arcpy.GetParameterAsText(1))) *
((float(arcpy.GetParameterAsText(2))/100) / (1-float(arcpy.GetParameterAsText(2))/100))
You need to build the substitution string correctly to use with the format string function.
https://docs.python.org/2/library/functions.html#format
e.g.
Expression = "(!g_cm2_yr! / {0})*({1}/{2})".format(arcpy.GetParameterAsText(1),float(arcpy.GetParameterAsText(2))/100, 1-float(arcpy.GetParameterAsText(2)/100))
Thank you ever so much Owain. I understand now the logic behind it.
I was able to use your answer to the original question for parts of my calculation but now I'm running into an issue when trying to add up those parts. The goal is to multiply a field (scores) by a number inputted by the user (the weight) for six different fields, then sum those all together, and divide by another field. When I use the "+" symbol I believe it thinks I want to concatenate everything. I've tried using the sum function but get an error saying it can't sum strings. Here's my code.
# This script calculates the Composite Burden Score for each gas station in R3 using user-defined weights for the inputs.
# The Composite Burden Score is calculated by summing the weighted scores for SVI, Floodplain, Highway, Evacuation, Demand, and Supply and then dividing by the Community Resilience score.
from ntpath import join
import arcpy
from arcpy import env
from arcgis.gis import GIS
# Set Environments
env.workspace = arcpy.GetParameterAsText(0)
arcpy.env.overwriteOutput = True
env.extent = arcpy.GetParameterAsText(1)
# Set Parameters
# By default, each input is equally weighted at 1/6th (or 0.16666)
Gas_Stations = arcpy.GetParameterAsText(2)
SVI_weight = arcpy.GetParameterAsText(3) or 0.16666
Floodplain_weight = arcpy.GetParameterAsText(4) or 0.16666
Highway_weight = arcpy.GetParameterAsText(5) or 0.16666
Evacuation_weight = arcpy.GetParameterAsText(6) or 0.16666
Demand_weight = arcpy.GetParameterAsText(7) or 0.16666
Supply_weight = arcpy.GetParameterAsText(8) or 0.16666
Expression = (("!SVI_Score! * {}".format(float(SVI_weight))) + ("!Floodplain_Score! * {}".format(float(Floodplain_weight))) + ("!Highway_Score! * {}".format(float(Highway_weight))) + ("!Evacuation_Score! * {}".format(float(Evacuation_weight))) + ("!Demand_Score! * {}".format(float(Demand_weight))) + ("!Supply_Score! * {}".format(float(Supply_weight)))) / "!Resilience Score!"
# Calculated the Composite Burden Score
arcpy.CalculateField_management(Gas_Stations, "Composite_Burden_Score_Weight", Expression, "PYTHON")