Skip to content

Instantly share code, notes, and snippets.

@ryan-hill
Forked from debboutr/walk_up.py
Created October 19, 2017 16:50
Show Gist options
  • Save ryan-hill/a81b471920531e680859c692d24930bb to your computer and use it in GitHub Desktop.
Save ryan-hill/a81b471920531e680859c692d24930bb to your computer and use it in GitHub Desktop.
recursive function to walk up flow table. NOTE: this will not handle braided flowlines!
from StreamCat_functions import dbf2DF
pre = 'D:/NHDPlusV21/NHDPlusGL/NHDPlus04'
fline = dbf2DF('D%s/NHDSnapshot/Hydrography/NHDFlowline.dbf' % pre)
flow = dbf2DF('%s/NHDPlusAttributes/PlusFlow.dbf' pre)[['TOCOMID','FROMCOMID']]
def recurs(val, ups):
print val
ups = ups + flow.ix[flow.TOCOMID == val].FROMCOMID.tolist()
if 0 in ups:
ups.remove(0)
# here is where you would calculate whatever value that you want to pass through
area = fline.ix[fline.COMID == val].LENGTHKM.values[0]
# in this case I just grabbed length of the flowline
if ups:
return area + recurs(ups.pop(), ups)
else:
return area
recurs(4796914,[])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment