55# ' @param readin readin indicates the maps to be used. If CreateFlowmet.R is run it's own, this should point to the template which references the maps(or values)
66# ' used for the various levels and statevars. Otherwise, if run inside of RHESSysPreprocess, readin will use the map data from world_gen.R,
77# ' Streams map, and other optional maps, still need to be specified.
8- # ' @param asp_rules List of aspatial structure and inputs. Also can be path to rules file - must be used along with template input.
8+ # ' @param asprules List of aspatial structure and inputs. Also can be path to rules file - must be used along with template input.
99# ' @param road_width >0, defaults to 1.
1010# ' @inheritParams RHESSysPreprocess
1111# ' @author Will Burke
1212# ' @export
1313
1414CreateFlownet = function (flownet_name ,
1515 readin = NULL ,
16+ template = NULL ,
1617 type = " raster" ,
1718 typepars = NULL ,
18- asp_rules = NULL ,
19+ asprules = NULL ,
1920 streams = NULL ,
2021 overwrite = FALSE ,
2122 roads = NULL ,
@@ -39,11 +40,12 @@ CreateFlownet = function(flownet_name,
3940 if (! is.logical(overwrite )) {stop(" overwrite must be logical" )} # check overwrite inputs
4041 if (file.exists(flownet_name ) & overwrite == FALSE ) {stop(noquote(paste(" Flowtable" ,flownet_name ," already exists." )))}
4142
43+ template_list = template_read(template )
44+ template_clean = template_list [[1 ]] # template in list form
45+ var_names = template_list [[2 ]] # names of template vars
46+ map_info = template_list [[5 ]] # tables of maps and their inputs/names in the template
47+
4248 if (! wrapper & is.character(readin )) { # if run outside of rhessyspreprocess.R, and if readin is character. readin is the template (and path)
43- template_list = template_read(readin )
44- template_clean = template_list [[1 ]] # template in list form
45- var_names = template_list [[2 ]] # names of template vars
46- map_info = template_list [[5 ]] # tables of maps and their inputs/names in the template
4749 cfmaps = rbind(map_info ,c(" cell_length" ," none" ), c(" streams" ," none" ), c(" roads" ," none" ), c(" impervious" ," none" ),c(" roofs" ," none" ))
4850 } else if (wrapper | (! wrapper & is.matrix(readin ))) { # map info is passsed directly from world gen - either in wrapper or outside of wrapper and readin is matrix
4951 cfmaps = readin
@@ -116,22 +118,28 @@ CreateFlownet = function(flownet_name,
116118 raw_impervious_data = NULL
117119 if (! is.null(impervious )) {raw_impervious_data = map_list [[cfmaps [cfmaps [,1 ] == " impervious" ,2 ]]]}
118120
121+ # TODO - this should get cleaned up eventually - there's at least 2 different versions of the maps being used
122+ if (length(readmap @ data [,1 ]) == 1 ) {
123+ map_df = as.data.frame(readmap @ data ) # works for 1 patch world
124+ } else {
125+ map_df = as.data.frame(readmap ) # make data frame for ease of use
126+ }
127+
119128 # read aspatial rules if needed
120- if (! is.null(asp_rules ) && is.character( asp_rules )) {
129+ if (! is.null(asprules )) {
121130 asp_map = template_clean [[which(var_names == " asp_rule" )]][3 ] # get rule map/value
122- if (suppressWarnings(any(is.na(as.numeric(asp_map ))))) { # if it's a map
131+
132+ if (! " asp_rule" %in% notamap ) { # if it's a map
123133 asp_map = gsub(" .tif|.tiff" ," " ,asp_map )
124134 asp_mapdata = as.data.frame(readmap )[asp_map ]
125135
126136 # --- doing manipulation of the asp map if needed here ---
127- if (template_clean [[which(var_names == " asp_rule" )]][2 ] == " mode" ) {
128-
137+ if (template_clean [[which(var_names == " asp_rule" )]][2 ] == " mode" ) {
129138 # mode for aggregating by mode
130139 mode_fun = function (x ) {
131140 ux <- unique(x )
132141 ux [which.max(tabulate(match(x , ux )))]
133142 }
134-
135143 # fun to check if rules are all unique per patch (kinda slow but oh well) -----
136144 check_rules = function (patches , asp_rules ) {
137145 patches_u = unique(patches [! is.na(patches )])
@@ -155,28 +163,22 @@ CreateFlownet = function(flownet_name,
155163 map_info [map_info [, 1 ] == " strata" , 2 ]))
156164 level_names = unique(gsub(" .tiff|.tif|.asc" ," " ,level_names ))
157165
158- if (length(readmap @ data [,1 ]) == 1 ) {
159- map_df = as.data.frame(readmap @ data ) # works for 1 patch world
160- } else {
161- map_df = as.data.frame(readmap ) # make data frame for ease of use
162- }
163-
164- asp_maps = aggregate(map_df $ asprule , by = map_df [level_names ], FUN = mode_fun , simplify = T )
166+ asp_maps = aggregate(map_df [[cfmaps [cfmaps [,1 ] == " asp_rule" ,2 ]]], by = map_df [level_names ], FUN = mode_fun , simplify = T )
165167 names(asp_maps )[which(names(asp_maps ) == " x" )] = " asprule"
166168
167169 rules_out = check_rules(patches = asp_maps $ pch_30m1000 , asp_rules = asp_maps $ asprule )
168170 if (! is.null(rules_out )) {
169171 print(rules_out )
170172 stop(" Mode of rules was attempted but there are still patches with multiple rules. Check input maps." )
171173 }
172-
173174 asp_mapdata = asp_maps $ asprule
174175 }
175176
176- } else if (suppressWarnings(all(! is.na(as.numeric(asp_map ))))) { # if is a single number
177- asp_mapdata = as.numeric(asp_map )
177+ } else if (" asp_rule" %in% notamap ) { # if is a single number
178+ asp_mapdata = map_df [[cfmaps [cfmaps [,1 ] == " basin" ,2 ]]]
179+ asp_mapdata [! is.na(asp_mapdata )] = as.numeric(cfmaps [cfmaps [,1 ] == " asp_rule" ,2 ])
178180 }
179- asp_list = aspatial_patches(asprules = asp_rules , asp_mapdata = asp_mapdata )
181+ asp_list = aspatial_patches(asprules = asprules , asp_mapdata = asp_mapdata )
180182 }
181183
182184 # ------------------------------ Make flownet list ------------------------------
@@ -197,15 +199,9 @@ CreateFlownet = function(flownet_name,
197199 skip_hillslope_check = skip_hillslope_check )
198200
199201 # ------------------------------ Multiscale routing/aspatial patches ------------------------------
200- if (! is.null(asp_rules )) {
201- if (" asp_rule" %in% notamap ) {
202- map_list [[" asp_rule" ]] = raw_basin_data
203- map_list [[" asp_rule" ]][! is.na(map_list [[" asp_rule" ]])] = as.numeric(cfmaps [cfmaps [,1 ] == " asp_rule" ,2 ])
204- # cfmaps = rbind(cfmaps, c("asp_rule", "asp_rule"))
205- cfmaps [cfmaps [,1 ] == " asp_rule" , 2 ] = " asp_rule"
206- }
207-
208- CF1 = multiscale_flow(CF1 = CF1 , asp_maps = asp_maps , cfmaps = cfmaps , asp_list = asp_list )
202+ if (! is.null(asprules )) {
203+ patch_map = map_df [[cfmaps [cfmaps [,1 ] == " patch" ,2 ]]]
204+ CF1 = multiscale_flow(CF1 = CF1 , asp_map = asp_mapdata , patch_map = patch_map , asp_list = asp_list )
209205 }
210206
211207 # ---------- Flownet list to flow table file ----------
0 commit comments