Thank you to all who responded to this question earlier. I am re-posting a more specific question and hopefully my code will be easier to understand.
So, I have number of feature classes in a .gdb that have tree counts per acre in each polygon by year. I am iteratively unioning the features by year adding a field that is the sum of the previous tree count with the new tree count.
import arcpy
arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput=True
arcpy.env.workspace="C:\LPP\APS.gdb"
R1="R1ADS"
Dam="Damage"
years=['1999','2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012', '2013']
fields=['TPA_','Acres_','NoTrees_']
###R1+years(i)+Dam is the name of the feature in the .gdb that will be unioned
RangeYear=['1999_2000','1999_2001','1999_2002','1999_2003','1999_2004','1999_2005',
'1999_2006','1999_2007','1999_2008','1999_2009','1999_2010','1999_2011','1999_2012','1999_2013']
### RangeYear is a variable that I created for naming the unioned outfeature
#________________________________________________
###The statement below create first RangeYear feature to base iteration on in following loop. Because only two years are being added it requires a simple Field Calculator Expression to sum the trees per acre per year which I was able to figure out.
arcpy.Union_analysis([R1+years[0]+Dam,R1+years[1]+Dam],R1+years[0]+"_"+years[1]+Dam)
for j in range(0,3):
arcpy.AddField_management(R1+years[0]+"_"+years[1]+Dam,fields+RangeYear[0],"FLOAT","","","")
###I tried to but the statements below in the loop above by having the Field Calculator expressions in a list also, however
###the program would not allow me to iterate through that list so I plugged the expression into the parameters below and ran the tool one at a ###time.
arcpy.CalculateField_management(R1+years[0]+"_"+years[1]+Dam,fields[0]+RangeYear[0],'!TPA_1999!+!TPA_2000!',"PYTHON")
arcpy.CalculateField_management(R1+years[0]+'_'+years[1]+Dam,fields[0]+RangeYear[0],'!Shape_Area! * 0.00024710538',"PYTHON")
arcpy.CalculateField_management(R1+years[0]+'_'+years[1]+Dam,fields[0]+RangeYear[0],'!TPA_1999_2000!*!Acres_1999_2000!',"PYTHON")
###The loop below unions the remaining years the previous years with the next chronological year;
###adds three new field; calculates the new fields values and then move on to the next union.
for i in range(0,13) :
arcpy.Union_analysis([R1+years[0]+"_"+years[i+1]+Dam,R1+years[i+2]+Dam],R1+years[0]+"_"+years[i+2]+Dam)
for j in range(0,3):
arcpy.AddField_management(R1+years[0]+'_'+years[i+2]+Dam,fields+RangeYear[i+1],"FLOAT","","","")
arcpy.CalculateField_management(R1+years[0]+'_'+years[i+2]+Dam,fields, ????Expression????, "PYTHON")
###OK so the major crux here is that for TPA_1999_20XX, I need to add the previously unioned TPA_1999_20XX-1 to TPA_20XX.
###I have no idea how to do this. I suspect that once I figure this out, the next two expressions will be evident.
####################################Thanks for reading, sorry for being such a hack programmer!####################Field Calculator Expression Arcpy
Message was edited by: HOWARD WILLIAMS
What do you get when you print expression[0]. What is the content of the expression?
In the skeleton that I posted before it generate (I think) a valid syntax for the expression. In see that you are not using all the code that I posted. Maybe you could try the code below or share a part of the data to test if it works and will help to correct any errors.
Please try the code below (on a copy of your data to be sure) and post back any errors it generates:
def main():
import arcpy
arcpy.env.overwriteOutput=True
arcpy.env.workspace= r"C:\LPP\APS.gdb"
fields = ['TPA_','Acres_','NoTrees_']
expr = "'!TPA_{0}! + !TPA_{1}!','!Shape_Area! * 0.00024710538','!TPA_{2}! * !Acres_{2}!'"
years = [str(year) for year in range(1999, 2014)]
range_years = ['{0}_{1}'.format(years[0], year) for year in years[1:]]
# initial union
print "initial union"
lst_union = ["R1ADS{0}Damage".format(y) for y in years[:2]]
fc_out = "R1ADS{0}Damage".format(range_years[0])
print lst_union
print fc_out
prev_union = fc_out
arcpy.Union_analysis(lst_union, fc_out)
expr2 = expr.format(years[0], years[1], range_years[0])
expressions = expr2.split(',')
for j in range(0,3):
fld = fields+range_years[0]
print " - AddField", fld
print " - CalcField", fld, expressions
arcpy.AddField_management(fc_out, fields+range_years[0], "FLOAT")
arcpy.CalculateField_management(fc_out, fields+range_years[0], expressions, "PYTHON")
# other unions
print "\nother unions"
for y in years[2:]:
index = years.index(y)
lst_union = ["R1ADS{0}Damage".format(years[index-1]), "R1ADS{0}Damage".format(y)]
lst_union = [prev_union, "R1ADS{0}Damage".format(y)]
fc_out = "R1ADS{0}Damage".format(range_years[index-1])
prev_union = fc_out
print lst_union
print fc_out
expr3 = expr.format(range_years[index-2], y, range_years[index-1])
expressions2 = expr3.split(',')
arcpy.Union_analysis(lst_union, fc_out)
for j in range(0,3):
fld = fields+range_years[index-1]
print " - AddField", fld
print " - CalcField", fld, expressions2
arcpy.AddField_management(fc_out, fld, "FLOAT")
arcpy.CalculateField_management(fc_out, fields, expressions2, "PYTHON")
if __name__ == '__main__':
main()