-
Notifications
You must be signed in to change notification settings - Fork 0
/
population-estimates-tool.py
99 lines (87 loc) · 3.13 KB
/
population-estimates-tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# population-estimates.py
# produce population estimates
# Kytt MacManus
# 2-11-13
# import libraries
import os, arcpy
import datetime
# set counter
startTime = datetime.datetime.now()
# define input table
##inTable = r"\\Dataserver0\gpw\GPW4\Gridding\Country\inputs\npl.gdb\npl_age_sex_proportions_2011"
inTable = arcpy.GetParameterAsText(0)
# define output workspace
##outWS = r"\\Dataserver0\gpw\GPW4\Gridding\Country\inputs\npl.gdb"
outWS = arcpy.GetParameterAsText(1)
# define growth rate table
##grTable = r"\\Dataserver0\gpw\GPW4\Gridding\Country\inputs\npl.gdb\npl_admin3_growth_rate"
grTable = arcpy.GetParameterAsText(2)
# define join field
##joinField = "UCID3"
joinField = arcpy.GetParameterAsText(3)
# define transfer attributes
attributes = ["YEARTO2000","YEARTO2005","YEARTO2010",
"YEARTO2015","YEARTO2020","AGR"]
##attributes = arcpy.GetParameterAsText(4)
# define outTable
outTable = outWS + os.sep + os.path.basename(inTable).replace(os.path.basename(inTable)[4:],"estimates")
# copy new table
try:
arcpy.Copy_management(inTable,outTable)
arcpy.AddMessage("Created " + outTable)
except:
arcpy.GetMessages()
# join fields
try:
arcpy.JoinField_management(outTable,joinField,grTable,joinField,attributes)
except:
arcpy.GetMessages()
# define target years
targetYears = ["2000","2005","2010","2015","2020"]
# iterate again
for year in targetYears:
arcpy.AddMessage("considering " + year)
# perform estimates. first project the total population to the reference year
# add field to outTable
try:
eField = "E_ATOTPOPBT_" + year
arcpy.AddField_management(outTable,eField,"DOUBLE")
except:
arcpy.GetMessages()
# construct calcExpression
calcExpression = "!ATOTPOPBT! * math.exp( !AGR! * !YEARTO" + year + "! )"
# perform calculation
try:
arcpy.CalculateField_management(outTable,eField,calcExpression,"PYTHON_9.3")
arcpy.AddMessage("calculated " + eField)
except:
arcpy.GetMessages()
# if year is 2010, then perform age estimates by applying proportions
if year == "2010":
# list fields
fieldList = arcpy.ListFields(outTable,"*PROP")
# iterate fields
for fld in fieldList:
field = fld.name
# define newField
newField = "E_" + field.replace("PROP",year)
# add newField
try:
arcpy.AddField_management(outTable,newField,"DOUBLE")
except:
arcpy.GetMessages()
# define calculation expression
propCalc = "!" + eField + "! * !" + field + "!"
# perform calculation
try:
arcpy.CalculateField_management(outTable,newField,propCalc,"PYTHON_9.3")
arcpy.AddMessage("calculated " + newField)
except:
arcpy.GetMessages()
## # delete proportion field
## try:
## arcpy.DeleteField_management(outTable,field)
## except:
## arcpy.GetMessages()
arcpy.AddMessage("completed calculations")
arcpy.AddMessage(datetime.datetime.now() - startTime)