Skip to content

Commit b5842b7

Browse files
committed
dones step 3, working on step 4
1 parent 4a3f287 commit b5842b7

File tree

1 file changed

+59
-34
lines changed

1 file changed

+59
-34
lines changed

R/fct_combine_mcs_cstock.R

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
#' ad <- read_xlsx(path = path, sheet = "AD_lu_transitions", na = "NA")
2626
#' usr <- read_xlsx(path = path, sheet = "user_inputs", na = "NA")
2727
#'
28-
#' res <- fct_combine_mcs_cstock(.ad = ad, .cs = cs, .usr = usr)
28+
#' res <- fct_combine_sims_C_allstock(.ad = ad, .cs = cs, .usr = usr)
2929
#' res |> filter(sim_no == 1)
3030
#'
3131
#' @export
32-
fct_combine_mcs_cstock <- function(.ad, .cs, .usr){
32+
fct_combine_sims_C_allstock <- function(.ad, .cs, .usr){
3333

3434
## !!! FOR TESTING ONLY - run example then assign ad, cs and usr to the input vars.
3535
# .ad <- ad
@@ -47,14 +47,17 @@ fct_combine_mcs_cstock <- function(.ad, .cs, .usr){
4747
## 1. simulate CF ####
4848

4949
if (is.numeric(.usr$c_fraction)) {
50-
sims_CF <- fct_make_mcs(
51-
.n_iter = .usr$n_iter,
52-
.pdf = .usr$c_fraction_pdf,
53-
.mean = round(.usr$c_fraction, 3),
54-
.se = round(.usr$c_fraction_se, 3),
55-
#.params = c(params$c_pdf_a, params$c_pdf_b, params$c_pdf_c),
56-
.trunc = .usr$trunc_pdf
57-
) |> round(3)
50+
sims_CF <- tibble(
51+
sim_no = 1:.usr$n_iter,
52+
CF = round(fct_make_mcs(
53+
.n_iter = .usr$n_iter,
54+
.pdf = .usr$c_fraction_pdf,
55+
.mean = round(.usr$c_fraction, 3),
56+
.se = round(.usr$c_fraction_se, 3),
57+
#.params = c(params$c_pdf_a, params$c_pdf_b, params$c_pdf_c),
58+
.trunc = .usr$trunc_pdf
59+
), 3)
60+
)
5861
}
5962

6063
## 2. simulate C elements ####
@@ -114,69 +117,91 @@ fct_combine_mcs_cstock <- function(.ad, .cs, .usr){
114117
dplyr::distinct()
115118

116119
## + Run loop
117-
combi_formulas <- pmap(combi, function(period, lu){
120+
combi_form <- purrr::pmap(combi, function(period, lu){
118121

119122
c_sub <- .cs |> filter(.data$c_period == period, .data$c_lu_id == lu)
120123

121124
c_check <- fct_check_pool(.c_sub = c_sub, .c_unit = .usr$c_unit, .c_fraction = .usr$c_fraction)
125+
c_form <- fct_make_formula(.c_check = c_check, .c_unit = .usr$c_unit)
122126

123-
})
127+
tibble(
128+
period = period,
129+
lu_id = lu,
130+
c_form = c_form
131+
)
124132

133+
}) |> purrr::list_rbind()
125134

135+
## CHECK
136+
# combi_form
126137

138+
## + Add to sims
139+
if (is.numeric(.usr$c_fraction)) {
140+
sims_C_form <- sims_C |>
141+
left_join(sims_CF, by = "sim_no") |>
142+
left_join(combi_form, by = c("period", "lu_id"))
143+
} else {
144+
sims_C_form <- sims_C |>
145+
left_join(combi_form, by = c("period", "lu_id"))
146+
}
127147

128-
#|>
129-
#dplyr::select("sim_no", "c_period", "lu_id", "C_all", "C_form", dplyr::everything())
148+
sims_C_all <- sims_C_form |>
149+
rowwise() |>
150+
mutate(
151+
C_all = if_else(c_form == "DG_ratio", NA_real_, round(eval(parse(text=c_form)), 3))
152+
) |>
153+
ungroup()
130154

131155
## CHECK
132-
# tt <- mcs_c |> dplyr::filter(.data$sim_no == 1)
156+
# tt <- sims_C_all |> dplyr::filter(.data$sim_no == 1)
157+
158+
## 4. Calculate C for degraded land uses
133159

134-
if ("DG_ratio" %in% unique(mcs_c$C_form)) {
160+
if ("DG_ratio" %in% unique(sims_C_all$c_form)) {
135161

136-
## Get pools used for DG
162+
## + Get pools used for DG
137163
if (.usr$dg_pool == "ALL") {
138164
dg_pool <- "C_all"
139165
} else {
140166
dg_pool <- stringr::str_split(.usr$dg_pool, pattern = ",") |> purrr::map(stringr::str_trim) |> unlist()
141167
}
142168
dg_pool_intact <- paste0(dg_pool, "_intact")
143169

144-
## Filter DG to modify formula and recalculate
145-
mcs_dg <- mcs_c |>
146-
dplyr::filter(.data$C_form == "DG_ratio") |>
170+
## + Filter DG to modify formula and recalculate
171+
sims_DG <- sims_C_all |>
172+
dplyr::filter(.data$c_form == "DG_ratio") |>
147173
dplyr::mutate(
148174
lu_intact = stringr::str_remove(.data$lu_id, pattern = .usr$dg_ext)
149175
)
150176

151-
mcs_join <- mcs_c |>
152-
dplyr::filter(.data$lu_id %in% unique(mcs_dg$lu_intact)) |>
177+
sims_C_intact <- sims_C_all |>
178+
dplyr::filter(.data$lu_id %in% unique(sims_DG$lu_intact)) |>
153179
dplyr::select("sim_no", lu_intact = "lu_id", !!!rlang::syms(dg_pool))
154180

155-
names(mcs_join)[!(names(mcs_join) %in% c("sim_no", "lu_intact"))] <- dg_pool_intact
181+
names(sims_C_intact)[!(names(sims_C_intact) %in% c("sim_no", "lu_intact"))] <- dg_pool_intact
156182

157-
mcs_dg2 <- mcs_dg |>
158-
dplyr::left_join(mcs_join, by = c("sim_no", "lu_intact")) |>
183+
sims_DG2 <- sims_DG |>
184+
dplyr::left_join(sims_C_intact, by = c("sim_no", "lu_intact")) |>
159185
dplyr::rowwise() |>
160186
dplyr::mutate(
161-
C_form = paste0(.data$C_form, " * (", paste0(dg_pool_intact, collapse = " + "), ")"),
162-
C_all = round(.data$C_all * sum(!!!rlang::syms(dg_pool_intact)), 3)
187+
c_form = paste0(.data$c_form, " * (", paste0(dg_pool_intact, collapse = " + "), ")"),
188+
C_all = round(.data$DG_ratio * sum(!!!rlang::syms(dg_pool_intact)), 3)
163189
) |>
164190
dplyr::ungroup() |>
165191
dplyr::select(-"lu_intact", -dplyr::all_of(dg_pool_intact))
166192

167-
mcs_c2 <- mcs_c |>
168-
dplyr::filter(.data$C_form != "DG_ratio") |>
169-
dplyr::bind_rows(mcs_dg2) |>
170-
dplyr::distinct()
193+
sims_C_all2 <- sims_C_all |>
194+
dplyr::filter(.data$c_form != "DG_ratio") |>
195+
dplyr::bind_rows(sims_DG2)
171196

172197
} else {
173-
mcs_c2 <- mcs_c
198+
sims_C_all2 <- sims_C_all
174199
}
175200

176-
mcs_c2
201+
sims_C_all2
177202

178203
## Check
179-
# tt <- mcs_c2 |> dplyr::filter(.data$sim_no == 1)
204+
# tt <- sims_C_all2 |> dplyr::filter(.data$sim_no == 1)
180205

181206
}
182207

0 commit comments

Comments
 (0)