/*
/* *************************************************************
/* USDA - ARS
/* Southwest Watershed Research Center
/* 2000 E. Allen Rd.
/* Tucson, AZ 85719
/* *************************************************************
/* Program: subws.aml
/*
/* Function: Automates the creation of subwatersheds within
/* a given watershed.
/*
/* History: Created 1997 by Scott Miller, USDA - ARS SWRC,
/* 2000 E. Allen Rd. Tucson, AZ 85719.
/* ph: 520-670-6380. miller@tucson.ars.ag.gov
/* *************************************************************
/* Routines called: none
/*
/* Requirements: flowaccumulation and flowdirection GRIDs
/*
/* Results: Creates a polygon map of the subwatershed
/* areas for the use of the ARDBSN model
/* *************************************************************
/* Usage: &r subws <watershed> <threshold>
/*
/* Arguments: threshold - # of cells that will be used to
/* determine where the streams are located.
/* *************************************************************
/*
&args size
&if [null %size%] &then &do
&type
&type Usage: &r subws <threshold>
&type
&return &inform
&end
&stat 9999
&if %:PROGRAM% ne GRID &then &do
&if %:PROGRAM% ne ARC &then quit
grid
&end
/* First go into GRID and create the stream network (and a polygon
/* coverage from that), then a preliminary subwatershed map based on the
/* intersections of the channels.
strm_%size% = con (dem6_acc > %size%, 1)
stream_%size% = streamline (strm_%size%, dem6_dir)
ppts_%size%a = streamlink (strm_%size%, dem6_dir)
sub_%size%a = watershed (dem6_dir, ppts_%size%a)
subws_%size%a = gridpoly (sub_%size%a)
q
/* Now use ArcEdit to select all the arcs and extract the tops of all
/* the channels. The reason to do this is that GRID's method (above)
/* does not create subwatersheds at the upland level, only for where
/* intersections take place. So we are getting the coordinates of where
/* the uplands start.
build stream_%size% node
ARCEDIT
&sv test = [show display]
EC stream_%size%
EDITFEATURE arc
/* First step is to select all the nodes that represent stream junctions
/* and export their x-, y-coordinates into usable format.
/* This is done in ArcEdit. Select all the tnodes in order. If no
/* arc is associated with a particular number, it should be selected,
/* since it is a terminal node.
ef node
&sv last [show maximum node]
ef arc
&sv num = 0
&sys touch nodes_%size%.txt
&label here
&sv num = %num% + 1
sel tnode# = %num%
&if %num% <> %last% &then &do
&sv sel = [show number select]
&if %sel% = 0 &then &sys echo [show node %num% coordinate] >> ~
nodes_%size%.txt
&goto here
&end
/* Get out of ArcEdit & create the file necessary to build a
/* subwatershed map in GRID, then go into GRID and do it.
quit
grid
/* Use the "snappour" procedure to ensure that we're in the lowest
/* point within 10m of the outlet.
ppts_%size%b = snappour (nodes_%size%.txt, dem6_acc, 10)
sub_%size%b = watershed (dem6_dir, ppts_%size%b)
subws_%size%b = gridpoly (sub_%size%b)
/* In Arc, append the 2 subwatershed maps together
quit
append subws_%size%
subws_%size%a
subws_%size%b
end
clean subws_%size%
createlabels subws_%size%
kill ppts_%size%a all
kill ppts_%size%b all
kill sub_%size%a all
kill sub_%size%b all
kill strm_%size% all
kill subws_%size%a all
kill subws_%size%b all
&return