forked from DOI-USGS/sfrmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteStreamNetwork.py
214 lines (197 loc) · 6.43 KB
/
RouteStreamNetwork.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
203
204
205
206
207
208
209
210
211
212
213
# RouteStreamNetwork.py
# Description: Takes river_elev shapefile from AssignRiverElev.py
# and uses PlusFlow.dbf table (fromCOMID -> toCOMID) information
# to check routing of the whole network
#
# Requirements: os, sys, re, arcgisscripting, defaultdict
#
# Author: H.W. Reeves; USGS Michigan Water Science Center
# Date 1/29/2010
#
import os
import sys
import re
import arcgisscripting
from collections import defaultdict
# Global Input file for SFR utilities
infile="SFR_setup.in"
# Settings
#set elevation difference to assign same value to all cells
#so that check does not get messed up by rounding floats
eps=1.0e-02
# Get input files locations
infile=open(infile,'r').readlines()
inputs=defaultdict()
for line in infile:
if "#" not in line.split()[0]:
varname=line.split("=")[0]
var=line.split("=")[1].split()[0]
inputs[varname]=var.replace("'","") # strip extra quotes
ELEV=inputs["ELEV"] # 'river_w_elevations.shp'
FLOW=inputs["FLOW"] # 'PlusFlow_merge.dbf'
INTERSECT="river_explode.shp"
RIVER_ELEVS="river_elevs.dbf"
'''
print "\nJoining %s to %s; saving as %s (might take a minute)" %(RIVER_ELEVS,INTERSECT,ELEV)
arcpy.CopyFeatures_management(INTERSECT,ELEV)
arcpy.JoinField_management(ELEV,"FID",RIVER_ELEVS,"OLDFID")
'''
path=os.getcwd()
gp=arcgisscripting.create(9.3)
gp.Workspace=path
# sets over-write output option to true for output shapefile (river_elev.shp)
gp.overwriteoutput = 1
#delete any working layers if found
if gp.Exists("temp_lyr"):
gp.Delete_management("temp_lyr")
if gp.Exists("flow_lyr"):
gp.Delete_management("flow_lyr")
if gp.Exists("temp_table"):
gp.Delete_management("temp_table")
#read through river_elev shapefile and get list of all COMIDs; then
#read through NHDFlow and pull the from/to information for those COMIDs
comids=dict()
segments=gp.SearchCursor(ELEV)
for seg in segments:
#print str(seg.COMID)
comids[seg.COMID]=1
del seg
del segments
print "have all the comids"
#if check_network.txt (from and to comid list)
#does not exist, create on by setting maketable=1
lcount=0
maketable=1
lencomids = len(comids)
print "Creating to/from routing list..."
if maketable==1:
outfile=open("check_network.txt",'w')
outfile_FAILS = open("check_network_COM_FAILS.txt",'w')
for com in comids.iterkeys():
lcount+=1
percentdone=100.0*lcount/lencomids
print "%4.2f percent done" %(percentdone)
theQuery="FROMCOMID= %d" %(com)
print theQuery
gp.MakeTableView(FLOW,"temp_table",theQuery)
result=gp.GetCount("temp_table")
number=int(result.GetOutput(0))
print number
if number> 0:
selected=gp.SearchCursor("temp_table")
sel=selected.Next()
while sel:
if sel.TOCOMID in comids:
outfile.write("%d,%d\n" %(com,sel.TOCOMID))
else:
outfile.write("%d,99999\n" %(com))
sel=selected.Next()
else:
theQuery="TOCOMID= "+str(com)
print theQuery
gp.MakeTableView(FLOW,"temp_table",theQuery)
result=gp.GetCount("temp_table")
number=int(result.GetOutput(0))
print number
if number > 0:
selected=gp.SearchCursor("temp_table")
sel=selected.Next()
while sel:
if sel.FROMCOMID in comids:
outfile.write("%d,%d\n" %(sel.FROMCOMID,com))
else:
outfile.write("99999,%d\n" %(com))
sel=selected.Next()
else:
outfile_FAILS.write("%d,check routing, COMID in neither FROM or TO\n" %(com))
try:
outfile.close()
except:
print "problem closing outfile\n"
exit()
try:
outfile_FAILS.close()
except:
print "problem closing outfile_FAILS\n"
exit()
fromCOMIDlist=defaultdict(list)
#read in the file with information for clipped segments at the boundary
print "Processing clipped segments along domain boundary..."
CLIP=open("boundaryClipsRouting.txt",'r').readlines()
header = CLIP.pop(0) #header
clipto=dict()
clipfrom=dict()
for line in CLIP:
vals=line.strip().split(',')
fromin=int(float(vals[0]))
toin=int(float(vals[1]))
if fromin==99999:
clipto[toin]=1
if toin==99999:
clipfrom[fromin]=1
infile=open("check_network.txt",'r').readlines()
for line in infile:
vals=line.strip().split(",")
fromin=int(float(vals[0]))
toin=int(float(vals[1]))
if not toin in clipto:
if not fromin in clipfrom:
fromCOMIDlist[fromin].append(toin)
else:
fromCOMIDlist[fromin].append(99999)
#now have full from->to table as a dictionary of lists
#go through river_elev file COMID by COMID and check
#that next downstream COMID has lower river elevation
outfile=open("Flagged_comids.txt",'w')
outfile.write("COMID,DWNCOMID,MINELEV,DWNELEV\n")
gp.MakeFeatureLayer(ELEV,"flow_lyr")
total=len(comids)
icount=1
print "Checking for downstream routing..."
for com in comids.iterkeys():
print icount,"out of ",total
icount=icount+1
flag=0
theQuery="COMID= "+ str(com)
#print theQuery
if com==1798409:
print 'here I am'
gp.SelectLayerByAttribute("flow_lyr","NEW_SELECTION",theQuery)
gp.MakeFeatureLayer("flow_lyr","temp_lyr")
selected=gp.SearchCursor("temp_lyr")
sel=selected.Next()
maxelev=0.0
minelev=999999.
while sel:
if sel.ELEVAVE > maxelev:
maxelev=sel.ELEVAVE
if sel.ELEVAVE < minelev:
minelev=sel.ELEVAVE
sel=selected.Next()
del selected
i=0
while i<len(fromCOMIDlist[com]):
dwnstrm=fromCOMIDlist[com][i]
theQuery="COMID= %d" %(dwnstrm)
gp.SelectLayerByAttribute("flow_lyr","NEW_SELECTION",theQuery)
gp.MakeFeatureLayer("flow_lyr","temp_lyr")
selected=gp.SearchCursor("temp_lyr")
sel=selected.Next()
dwdnmax=0.0
dwdnmin=999999.
while sel:
if sel.ELEVAVE > dwdnmax:
dwdnmax=sel.ELEVAVE
if sel.ELEVAVE < dwdnmin:
dwdnmin=sel.ELEVAVE
sel=selected.Next()
del selected
if(dwdnmax > (minelev+0.01)):
outfile.write("%d,%d,%f,%f\n" %(com,dwnstrm,minelev,dwdnmax))
flag=1
i=i+1
if(flag == 0):
sys.stdout.write(str(com) + " is OK\n")
outfile.close()
gp.refreshcatalog(path)
del gp