# Field Calculator Expression Question (RePost)

Question asked by 406howard on Jan 28, 2016
Latest reply on Feb 4, 2016 by xander_bakker

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"
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):

###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.CalculateField_management(R1+years[0]+'_'+years[i+2]+Dam,fields[j], ????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