Skip to content

Commit 9a7b5cb

Browse files
committed
Fixes to multiscale flowtable, should work now
1 parent b775f8b commit 9a7b5cb

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

R/CreateFlownet.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ CreateFlownet = function(flownet_name,
119119
# read aspatial rules if needed
120120
if (!is.null(asprules)) {
121121
asp_map = template_clean[[which(var_names == "asp_rule")]][3] # get rule map/value
122+
patch_map = map_df[[cfmaps[cfmaps[,1] == "patch",2]]] # set for use later - overwrite if using mode
122123

123124
if (suppressWarnings(is.na(as.numeric(asp_map)))) { # if it's a map
124125
asp_map = gsub(".tif|.tiff","",asp_map)
@@ -157,7 +158,10 @@ CreateFlownet = function(flownet_name,
157158
asp_maps = aggregate(map_df[[cfmaps[cfmaps[,1] == "asp_rule",2]]], by = map_df[level_names], FUN = mode_fun, simplify = T)
158159
names(asp_maps)[which(names(asp_maps) == "x")] = "asprule"
159160

160-
rules_out = check_rules(patches = asp_maps$pch_30m1000, asp_rules = asp_maps$asprule)
161+
patch_map = asp_maps[[cfmaps[cfmaps[,1] == "patch",2]]]
162+
163+
rules_out = check_rules(patches = patch_map, asp_rules = asp_maps$asprule)
164+
161165
if (!is.null(rules_out)) {
162166
print(rules_out)
163167
stop("Mode of rules was attempted but there are still patches with multiple rules. Check input maps.")
@@ -191,7 +195,6 @@ CreateFlownet = function(flownet_name,
191195

192196
# ------------------------------ Multiscale routing/aspatial patches ------------------------------
193197
if (!is.null(asprules)) {
194-
patch_map = map_df[[cfmaps[cfmaps[,1] == "patch",2]]]
195198
CF1 = multiscale_flow(CF1 = CF1, asp_map = asp_mapdata, patch_map = patch_map, asp_list = asp_list)
196199
}
197200

R/RHESSysPreprocess.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ RHESSysPreprocess = function(template,
142142
}
143143

144144
CreateFlownet(flownet_name = flownet_name,
145-
#readin = readin,
146145
template = template,
147146
type = type,
148147
typepars = typepars,

R/multiscale_flow.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99

1010
multiscale_flow = function(CF1, asp_map, patch_map, asp_list) {
1111

12-
#nbr 121
13-
#nbr patch 11955
14-
# no times nbr asp ct is empty
12+
#nbr 329
13+
#nbr patch 329
14+
# nbr_asp_ct is empty
1515
#which(lapply(CF1,"[[", 9) == 15660)
1616

1717
# functions for applys
1818
apply_patches = function(CFp) {
19+
print(asp_list)
1920
id = paste0("rule_", unique(asp_map[which(raw_patch_data == CFp$PatchID)]))
20-
asp_count = ncol(rulevars[[id]]$patch_level_vars[1, ]) - 1 # get number of aspatial patches for current patch
21+
asp_count = ncol(asp_list[[id]]$patch_level_vars[1, ]) - 1 # get number of aspatial patches for current patch
2122
asp = c(1:asp_count)
2223
CFasp = lapply(asp, add_asp,CFp, id)
2324
unlist(CFasp,recursive = F)
@@ -28,7 +29,7 @@ multiscale_flow = function(CF1, asp_map, patch_map, asp_list) {
2829
CFasp$PatchID = CFp$PatchID * 100 + asp # aspatial patch ID is old patch ID *100 + aspatial number
2930
CFasp$Number = CFp$Number * 100 + asp # same modification to number
3031
CFasp$PatchFamilyID = CFp$PatchID # retain old patch ID as patch family ID
31-
CFasp$Area = CFp$Area * as.numeric(rulevars[[id]]$patch_level_vars[rulevars[[id]]$patch_level_vars[, 1] == "pct_family_area", asp + 1]) # change area
32+
CFasp$Area = CFp$Area * as.numeric(asp_list[[id]]$patch_level_vars[asp_list[[id]]$patch_level_vars[, 1] == "pct_family_area", asp + 1]) # change area
3233
# this output is an actual mess but whatever
3334
nbr_out = mapply(add_nbrs, nbr = CFp$Neighbors, gamma = CFp$Gamma_i, slope = CFp$Slopes, border = CFp$Border_perimeter, SIMPLIFY = F)
3435
CFasp$Neighbors = unlist(lapply(nbr_out, "[[", "new_nbrs"))
@@ -41,12 +42,12 @@ multiscale_flow = function(CF1, asp_map, patch_map, asp_list) {
4142
add_nbrs = function(nbr, gamma, slope, border) {
4243
nbr_patch = patch_ID[numbers == nbr] # patch ID from number
4344
nbr_id = paste0("rule_", unique(asp_map[which(raw_patch_data == nbr_patch)])) # get rule ID
44-
nbr_asp_ct = ncol(rulevars[[nbr_id]]$patch_level_vars[1, ]) - 1
45+
nbr_asp_ct = ncol(asp_list[[nbr_id]]$patch_level_vars[1, ]) - 1
4546
new_slopes = rep(slope, nbr_asp_ct)
4647
new_borders = rep(border, nbr_asp_ct)
4748
new_nbrs = nbr * 100 + c(1:nbr_asp_ct)
4849
# original gamma is multiplied by respective areas of the new patches - should sum to original
49-
new_gammas = gamma * as.numeric(rulevars[[nbr_id]]$patch_level_vars[rulevars[[nbr_id]]$patch_level_vars[, 1] == "pct_family_area", 1 + c(1:nbr_asp_ct)])
50+
new_gammas = gamma * as.numeric(asp_list[[nbr_id]]$patch_level_vars[asp_list[[nbr_id]]$patch_level_vars[, 1] == "pct_family_area", 1 + c(1:nbr_asp_ct)])
5051
return(list("new_nbrs" = new_nbrs, "new_gammas" = new_gammas, "new_slopes" = new_slopes, "new_borders" = new_borders))
5152
}
5253

@@ -56,7 +57,7 @@ multiscale_flow = function(CF1, asp_map, patch_map, asp_list) {
5657
patch_ID = unlist(lapply(CF1, "[[", 9)) # patch IDs from cf1
5758
numbers = unlist(lapply(CF1, "[[", 1)) # flow list numbers
5859
raw_patch_data = patch_map
59-
rulevars = asp_list # get rules - state variable overrides
60+
#rulevars = asp_list # get rules - state variable overrides
6061
CF2 = list() # empty list for new flow list
6162

6263
loop_ver = F

0 commit comments

Comments
 (0)