Skip to content

Commit 8966e35

Browse files
committed
More MSR/aspatial fixes
Hopefully MSR should be working correctly now, both with single value and with map as rules input
1 parent 9f8b811 commit 8966e35

9 files changed

Lines changed: 79 additions & 45 deletions

R/CreateFlownet.R

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
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

1414
CreateFlownet = 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 ----------

R/RHESSysPreprocess.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ RHESSysPreprocess = function(template,
127127
asprules = asprules)
128128

129129
readin = world_gen_out[[1]]
130-
asp_rules = world_gen_out[[2]]
130+
#asp_rules = world_gen_out[[2]]
131131

132132
# ---------- Run CreateFlownet ----------
133133
cat("Begin CreateFlownet.R")
@@ -143,9 +143,10 @@ RHESSysPreprocess = function(template,
143143

144144
CreateFlownet(flownet_name = flownet_name,
145145
readin = readin,
146+
template = template,
146147
type = type,
147148
typepars = typepars,
148-
asp_rules = asp_rules,
149+
asprules = asprules,
149150
streams = streams,
150151
overwrite = overwrite,
151152
roads = roads,

R/aspatial_patches.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' @param asp_mapdata map data or value, indicating the rule IDs being used
44
#' @author Will Burke
55

6-
aspatial_patches = function(asprules,asp_mapdata) {
6+
aspatial_patches = function(asprules, asp_mapdata) {
77

88
# some functions
99
# splits a vector up at a specific character
@@ -49,7 +49,8 @@ aspatial_patches = function(asprules,asp_mapdata) {
4949

5050
# ---------- build rulevars based on rules and map data ----------
5151
map_ids = unique(asp_mapdata) # get rule IDs from map/input
52-
map_id_tags = paste("rule_",map_ids,sep = "") # all map IDs concated w tags for referencing/reading in code
52+
if (is.list(map_ids)) {map_ids = unlist(map_ids)}
53+
map_id_tags = paste0("rule_", as.numeric(map_ids)) # all map IDs concated w tags for referencing/reading in code
5354

5455
asp_vars = as.list(rep(0,length(map_ids))) # highest level list of the different rules
5556
# strata_index = as.list(rep(0,length(map_ids))) # get rid of in this version i think

R/multiscale_flow.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#'
33
#' Modifies an existing flowtable (R list) to work with multisacle routing.
44
#' @param CF1 Flowtable list object created from make_flow_list
5-
#' @param map_list List containing input maps as matrices
6-
#' @param cfmaps Table of map types and file names
5+
#' @param asp_map Map of the aspatial rules
6+
#' @param patch_map Map of the patches
77
#' @param asp_list List of aspatial rules
88
# Will Burke 1/16/19
99

10-
multiscale_flow = function(CF1, asp_maps, cfmaps, asp_list) {
10+
multiscale_flow = function(CF1, asp_map, patch_map, asp_list) {
1111

1212
#nbr 121
1313
#nbr patch 11955
@@ -53,11 +53,9 @@ multiscale_flow = function(CF1, asp_maps, cfmaps, asp_list) {
5353
cat("Creating multiscale flowtable - this may take a moment with many patches")
5454

5555
# ----- Variable setup -----
56-
asp_map = asp_maps[[cfmaps[cfmaps[, 1] == "asp_rule", 2]]] # matrix of aspatial rules
57-
5856
patch_ID = unlist(lapply(CF1, "[[", 9)) # patch IDs from cf1
5957
numbers = unlist(lapply(CF1, "[[", 1)) # flow list numbers
60-
raw_patch_data = asp_maps[[cfmaps[cfmaps[, 1] == "patch", 2]]] # get patch matrix inside the function
58+
raw_patch_data = patch_map
6159
rulevars = asp_list # get rules - state variable overrides
6260
CF2 = list() # empty list for new flow list
6361

R/world_gen.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ world_gen = function(template,
114114
rulevars = aspatial_patches(asprules = asprules, asp_mapdata = asp_mapdata)
115115

116116
if (is.data.frame(asp_mapdata)) { # add ruleID to levels df
117-
asp_level = aggregate(asp_mapdata$asprule.tiff, by = as.list(levels), FUN = mode_fun)
117+
asp_level = aggregate(asp_mapdata[[1]] , by = as.list(levels), FUN = mode_fun)
118118
levels = merge(levels, asp_level[,c("h", "z", "p", "x")], by = c("h", "z", "p"), sort = F)
119119
levels = levels[c("w", "b", "h", "z", "p", "s", "x")]
120120
names(levels)[7] = "a"

man/CreateFlownet.Rd

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/RHESSysPreprocess.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/make_flow_list.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/multiscale_flow.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)