forked from dtarb/TauDEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlopeAreaStreamDefinition.py
203 lines (169 loc) · 6.93 KB
/
SlopeAreaStreamDefinition.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Script Name: SlopeAreaStreamDefinition
#
# Created By: David Tarboton
# Date: 9/29/11
# Import ArcPy site-package and os modules
import arcpy
import os
import subprocess
# Inputs
inlyr = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(inlyr)
p = str(desc.catalogPath)
arcpy.AddMessage("\nInput D8 Flow Direction Grid: " + p)
inlyr1 = arcpy.GetParameterAsText(1)
desc = arcpy.Describe(inlyr1)
sca = str(desc.catalogPath)
arcpy.AddMessage("Input D-Infinity Contributing Area Grid: " + sca)
inlyr2 = arcpy.GetParameterAsText(2)
desc = arcpy.Describe(inlyr2)
slp = str(desc.catalogPath)
arcpy.AddMessage("Input Slope Grid: " + slp)
masklyr = arcpy.GetParameterAsText(3)
if arcpy.Exists(masklyr):
desc = arcpy.Describe(masklyr)
mask = str(desc.catalogPath)
arcpy.AddMessage("Input Mask Grid: " + mask)
ogrlyr = arcpy.GetParameterAsText(4)
if arcpy.Exists(ogrlyr):
desc = arcpy.Describe(ogrlyr)
shfl1 = str(desc.catalogPath)
extn = os.path.splitext(shfl1)[1] # get extension of a file
# if extention is shapfile do not convert into gjson other wise convert
if extn == ".shp":
shfl = shfl1
else:
arcpy.AddMessage("Extracting json outlet file from: " + shfl1)
basename = os.path.basename(shfl1) # get last part of the path
dirname = os.path.dirname(p) # get directory
arcpy.env.workspace = dirname # does not work without specifying the workspace
arcpy.FeaturesToJSON_conversion(shfl1, basename + ".json") # convert feature to json
shfl = os.path.join(dirname, basename + ".json")
arcpy.AddMessage("Using Outlets : " + shfl)
fellyr = arcpy.GetParameterAsText(5)
if arcpy.Exists(fellyr):
desc = arcpy.Describe(fellyr)
fel = str(desc.catalogPath)
arcpy.AddMessage("Input Pit Filled Elevation Grid for Drop Analysis: " + fel)
ad8lyr = arcpy.GetParameterAsText(6)
if arcpy.Exists(ad8lyr):
desc = arcpy.Describe(ad8lyr)
ad8 = str(desc.catalogPath)
arcpy.AddMessage("Input D8 Contributing Area Grid for Drop Analysis: " + ad8)
contcheck = arcpy.GetParameterAsText(7)
arcpy.AddMessage("Edge contamination checking: " + contcheck)
slpexp = arcpy.GetParameterAsText(8)
arcpy.AddMessage("Slope Exponent(m): " + slpexp)
areaexp = arcpy.GetParameterAsText(9)
arcpy.AddMessage("Area Exponent(n): " + areaexp)
thresh = arcpy.GetParameterAsText(10)
arcpy.AddMessage("Threshold(T): " + thresh)
# Input Number of Processes
inputProc=arcpy.GetParameterAsText(11)
arcpy.AddMessage("Number of Processes: " + inputProc)
# Outputs
src = arcpy.GetParameterAsText(12)
arcpy.AddMessage("Output Stream Raster Grid: " + src)
sa = arcpy.GetParameterAsText(13)
arcpy.AddMessage("Output Source Area Grid: " + sa)
ssa = arcpy.GetParameterAsText(14)
arcpy.AddMessage("Output Maximum Upslope Grid: " + ssa)
drp = arcpy.GetParameterAsText(15)
if arcpy.Exists(drp):
arcpy.AddMessage("Output Drop Analysis Table: " + drp)
usedroprange = arcpy.GetParameterAsText(16)
arcpy.AddMessage("Select Threshold by Drop Analysis: " + usedroprange)
minthresh = arcpy.GetParameterAsText(17)
arcpy.AddMessage("Minimum Threshold Value: " + minthresh)
maxthresh = arcpy.GetParameterAsText(18)
arcpy.AddMessage("Maximum Threshold Value: " + maxthresh)
numthresh = arcpy.GetParameterAsText(19)
arcpy.AddMessage("Number of Threshold Values: " + numthresh)
logspace = arcpy.GetParameterAsText(20)
arcpy.AddMessage("Logarithmic Spacing: " + logspace)
# Construct first command
cmd = 'mpiexec -n ' + inputProc + ' SlopeArea -slp ' + '"' + slp + '"' + ' -sca ' + '"' + sca + '"' + \
' -sa ' + '"' + sa + '"' + ' -par ' + slpexp + ' ' + areaexp
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
arcpy.CalculateStatistics_management(sa)
# Construct second command
cmd = 'mpiexec -n ' + inputProc + ' D8FlowPathExtremeUp -p ' + '"' + p + '"' + ' -sa ' + '"' + sa + '"' + \
' -ssa ' + '"' + ssa + '"'
if arcpy.Exists(ogrlyr):
cmd = cmd + ' -o ' + '"' + shfl + '"'
##if maximumupslope == 'false':
## cmd = cmd + ' -min '
if contcheck == 'false':
cmd = cmd + ' -nc '
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
arcpy.CalculateStatistics_management(ssa)
if (usedroprange == 'true') and arcpy.Exists(ogrlyr):
# Construct third command
cmd = 'mpiexec -n ' + inputProc + ' DropAnalysis -fel ' + '"' + fel + '"' + ' -p ' + '"' + p + '"' + \
' -ad8 ' + '"' + ad8 + '"'
cmd = cmd + ' -ssa ' + '"' + ssa + '"' + ' -o ' + '"' + shfl + '"' + ' -drp ' + '"' + drp + '"' + \
' -par ' + minthresh + ' ' + maxthresh + ' ' + numthresh + ' '
if logspace == 'false':
cmd = cmd + '1'
else:
cmd = cmd + '0'
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
# (threshold,rest)=line.split(' ',1)
drpfile = open(drp,"r")
theContents = drpfile.read()
beg, threshold = theContents.rsplit(' ', 1)
drpfile.close()
# Construct fourth command
cmd = 'mpiexec -n ' + inputProc + ' Threshold -ssa ' + '"' + ssa + '"' + ' -src ' + '"' + src + '"'
if (usedroprange == 'true') and arcpy.Exists(ogrlyr):
cmd = cmd + ' -thresh ' + threshold
else:
cmd = cmd + ' -thresh ' + thresh
if arcpy.Exists(masklyr):
cmd = cmd + ' -mask ' + '"' + mask + '"'
arcpy.AddMessage("\nCommand Line: " + cmd)
# Submit command to operating system
os.system(cmd)
# Capture the contents of shell command and print it to the arcgis dialog box
process=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
message = "\n"
for line in process.stdout.readlines():
if isinstance(line, bytes): # true in Python 3
line = line.decode()
message = message + line
arcpy.AddMessage(message)
arcpy.CalculateStatistics_management(src) # remove converted json file
if arcpy.Exists(ogrlyr):
extn_json = os.path.splitext(shfl)[1] # get extension of the converted json file
if extn_json == ".json":
os.remove(shfl)