The purpose of this work was to create accumulated watershed boundaries for each 12-digit HUC in the watershed boundary dataset. The work started in July 2015 shortly after the WBD team completed a major cleanup of the TOHUC codes.
There are three functions that have significant logic in them. 1) fromHUC_finder, 2) HUC_aggregator, and 3) unionHUC.
1) Given a list of TOHUCs and a HUC to analyze, fromHUC_finder returns all the HUCs that flow into the HUC in consideration.
2) Given a HUC to analyze and all the potential fromHUC lists upstream of it, HUC_aggregator recursively walks the fromHUC list upstream collecting as it goes.
3) Given a HUC to analyze, it's list of upstream HUCs from HUC_aggregator, and the contents of a shapefile of the local HUC boundaries in an R spatialPolygons format, unionHUC selects and aggregates all the polygons into a single polygon.
A fourth function is included in the package. It is a runner function for use with a parallelized function like parApply. It is a stand alone orchestration function to be run in a new R environment and has no significance other than allowing the code to run on many CPUs controlled from a single instance running the snow parallel R package.
For the sake of putting the kernel of this inline, here's the recursive function.