Skip to content

Commit a40fc88

Browse files
committed
additional fixes for aspatial patches
1 parent 3f70f45 commit a40fc88

5 files changed

Lines changed: 78 additions & 16 deletions

File tree

R/CreateFlownet.R

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ----------

R/RHESSysPreprocess.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ RHESSysPreprocess = function(template,
153153
roofs = roofs,
154154
wrapper = wrapper,
155155
parallel = parallel,
156-
make_stream = make_stream)
156+
make_stream = make_stream,
157+
skip_hillslope_check = skip_hillslope_check)
157158

158159
# ---------- Run build_meta ----------
159160
# if (meta) {

R/aspatial_patches.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ aspatial_patches = function(asprules,asp_mapdata) {
4848
# strata data frames (num = patch ct) - rows are statevars, cols are strata
4949

5050
# ---------- build rulevars based on rules and map data ----------
51-
map_ids = unique(asp_mapdata)[[1]] # get rule IDs from map/input
51+
map_ids = unique(asp_mapdata) # get rule IDs from map/input
5252
map_id_tags = paste("rule_",map_ids,sep = "") # all map IDs concated w tags for referencing/reading in code
5353

5454
asp_vars = as.list(rep(0,length(map_ids))) # highest level list of the different rules

R/multiscale_flow.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
#' @param asp_list List of aspatial rules
88
# Will Burke 1/16/19
99

10-
multiscale_flow = function(CF1, map_list, cfmaps, asp_list) {
10+
multiscale_flow = function(CF1, asp_maps, cfmaps, asp_list) {
11+
12+
#nbr 121
13+
#nbr patch 11955
14+
# no times nbr asp ct is empty
15+
#which(lapply(CF1,"[[", 9) == 15660)
1116

1217
# functions for applys
1318
apply_patches = function(CFp) {
1419
id = paste0("rule_", unique(asp_map[which(raw_patch_data == CFp$PatchID)]))
1520
asp_count = ncol(rulevars[[id]]$patch_level_vars[1, ]) - 1 # get number of aspatial patches for current patch
1621
asp = c(1:asp_count)
17-
CFasp = lapply(asp, add_asp,CFp)
22+
CFasp = lapply(asp, add_asp,CFp, id)
1823
unlist(CFasp,recursive = F)
1924
return(CFasp)
2025
}
21-
add_asp = function(asp, CFp) {
26+
add_asp = function(asp, CFp, id) {
2227
CFasp = CFp
2328
CFasp$PatchID = CFp$PatchID * 100 + asp # aspatial patch ID is old patch ID *100 + aspatial number
2429
CFasp$Number = CFp$Number * 100 + asp # same modification to number
@@ -48,11 +53,11 @@ multiscale_flow = function(CF1, map_list, cfmaps, asp_list) {
4853
cat("Creating multiscale flowtable - this may take a moment with many patches")
4954

5055
# ----- Variable setup -----
51-
asp_map = map_list[[cfmaps[cfmaps[, 1] == "asp_rule", 2]]] # matrix of aspatial rules
56+
asp_map = asp_maps[[cfmaps[cfmaps[, 1] == "asp_rule", 2]]] # matrix of aspatial rules
5257

5358
patch_ID = unlist(lapply(CF1, "[[", 9)) # patch IDs from cf1
5459
numbers = unlist(lapply(CF1, "[[", 1)) # flow list numbers
55-
raw_patch_data = map_list[[cfmaps[cfmaps[, 1] == "patch", 2]]] # get patch matrix inside the function
60+
raw_patch_data = asp_maps[[cfmaps[cfmaps[, 1] == "patch", 2]]] # get patch matrix inside the function
5661
rulevars = asp_list # get rules - state variable overrides
5762
CF2 = list() # empty list for new flow list
5863

R/world_gen.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ world_gen = function(template,
9696
levels = as.data.frame(levels)
9797
colnames(levels) = c("w", "b", "h", "z", "p", "s")
9898

99+
# mode for aggregating by mode
100+
mode_fun = function(x) {
101+
ux <- unique(x)
102+
ux[which.max(tabulate(match(x, ux)))]
103+
}
99104

100105
# -------------------- Aspatial Patch Processing --------------------
101106
rulevars = NULL
@@ -108,8 +113,12 @@ world_gen = function(template,
108113
}
109114
rulevars = aspatial_patches(asprules = asprules, asp_mapdata = asp_mapdata)
110115

111-
if (is.data.frame(asp_mapdata)) { # add ruleID to levels matrix
112-
levels = cbind(levels, a = unname(as.matrix(asp_mapdata)))
116+
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)
118+
levels = merge(levels, asp_level[,c("h", "z", "p", "x")], by = c("h", "z", "p"), sort = F)
119+
levels = levels[c("w", "b", "h", "z", "p", "s", "x")]
120+
names(levels)[7] = "a"
121+
#levels = cbind(levels, a = unname(as.matrix(asp_mapdata)))
113122
} else if (is.numeric(asp_mapdata)) {
114123
levels = cbind(levels, a = rep(asp_mapdata,length(levels[,1])) )
115124
}
@@ -154,11 +163,7 @@ world_gen = function(template,
154163
} # end loop through n_basestations
155164

156165
# -------------------- Process Template + Maps --------------------
157-
# mode for aggregating by mode
158-
mode_fun = function(x) {
159-
ux <- unique(x)
160-
ux[which.max(tabulate(match(x, ux)))]
161-
}
166+
162167
# Build list based on operations called for by template
163168
statevars = vector("list",length(template_clean))
164169

0 commit comments

Comments
 (0)