@@ -122,10 +122,61 @@ CreateFlownet = function(flownet_name,
122122 if (suppressWarnings(any(is.na(as.numeric(asp_map ))))) { # if it's a map
123123 asp_map = gsub(" .tif|.tiff" ," " ,asp_map )
124124 asp_mapdata = as.data.frame(readmap )[asp_map ]
125+
126+ # --- doing manipulation of the asp map if needed here ---
127+ if (template_clean [[which(var_names == " asp_rule" )]][2 ] == " mode" ) {
128+
129+ # mode for aggregating by mode
130+ mode_fun = function (x ) {
131+ ux <- unique(x )
132+ ux [which.max(tabulate(match(x , ux )))]
133+ }
134+
135+ # fun to check if rules are all unique per patch (kinda slow but oh well) -----
136+ check_rules = function (patches , asp_rules ) {
137+ patches_u = unique(patches [! is.na(patches )])
138+ patches_i = lapply(patches_u , function (x , y ) {which(x == y )}, patches )
139+ outrules = lapply(patches_i , function (x , y ) {y [x ]}, asp_rules )
140+ lens = sapply(outrules , function (x ) {length(unique(x ))})
141+ lens_i = which(unlist(lens ) > 1 )
142+ if (length(lens_i ) > 0 ) {
143+ cat(" There are patches with multiple multiscale rules:\n " )
144+ out = data.frame (patch = patches [lens_i ], rules = sapply(outrules [lens_i ], function (x ) {paste(unique(x ), collapse = " , " ) }) )
145+ return (out )
146+ } else {
147+ return (NULL )
148+ }
149+ }
150+
151+ level_names = unname(c(map_info [map_info [, 1 ] == " basin" , 2 ],
152+ map_info [map_info [, 1 ] == " hillslope" , 2 ],
153+ map_info [map_info [, 1 ] == " zone" , 2 ],
154+ map_info [map_info [, 1 ] == " patch" , 2 ],
155+ map_info [map_info [, 1 ] == " strata" , 2 ]))
156+ level_names = unique(gsub(" .tiff|.tif|.asc" ," " ,level_names ))
157+
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 )
165+ names(asp_maps )[which(names(asp_maps ) == " x" )] = " asprule"
166+
167+ rules_out = check_rules(patches = asp_maps $ pch_30m1000 , asp_rules = asp_maps $ asprule )
168+ if (! is.null(rules_out )) {
169+ print(rules_out )
170+ stop(" Mode of rules was attempted but there are still patches with multiple rules. Check input maps." )
171+ }
172+
173+ asp_mapdata = asp_maps $ asprule
174+ }
175+
125176 } else if (suppressWarnings(all(! is.na(as.numeric(asp_map ))))) { # if is a single number
126177 asp_mapdata = as.numeric(asp_map )
127178 }
128- asp_rules = aspatial_patches(asprules = asp_rules , asp_mapdata = asp_mapdata )
179+ asp_list = aspatial_patches(asprules = asp_rules , asp_mapdata = asp_mapdata )
129180 }
130181
131182 # ------------------------------ Make flownet list ------------------------------
@@ -154,7 +205,7 @@ CreateFlownet = function(flownet_name,
154205 cfmaps [cfmaps [,1 ] == " asp_rule" , 2 ] = " asp_rule"
155206 }
156207
157- CF1 = multiscale_flow(CF1 = CF1 , map_list = map_list , cfmaps = cfmaps , asp_list = asp_rules )
208+ CF1 = multiscale_flow(CF1 = CF1 , asp_maps = asp_maps , cfmaps = cfmaps , asp_list = asp_list )
158209 }
159210
160211 # ---------- Flownet list to flow table file ----------
0 commit comments