Skip to content

Commit e0c1104

Browse files
committed
Adding extraction rates as output in L107 and exported as csv
1 parent a7ad121 commit e0c1104

File tree

2 files changed

+111
-10
lines changed

2 files changed

+111
-10
lines changed

R/xfaostat_L107_FoodBalanceSheet.R

+37-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#' @param command API command to execute
88
#' @param ... other optional parameters, depending on command
99
#' @return Depends on \code{command}: either a vector of required inputs, a vector of output names, or (if
10-
#' \code{command} is "MAKE") all the generated outputs: \code{L107.FAO_Food_Macronutrient_All},
11-
#' \code{FAO_Food_MacronutrientRate_2010_2019_MaxValue}
10+
#' \code{command} is "MAKE") all the generated outputs: \code{L107.Traceable_FBS_PCe_2010Plus},
11+
#' \code{L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus}, \code{L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus}
1212
#' @details This chunk aggregates supply utilization accounts into food balance sheet items
1313
#' @importFrom assertthat assert_that
1414
#' @importFrom dplyr summarize bind_rows filter if_else inner_join left_join mutate rename select n group_by_at
@@ -28,6 +28,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
2828

2929
MODULE_OUTPUTS <-
3030
c("L107.Traceable_FBS_PCe_2010Plus",
31+
"L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus",
3132
"L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus")
3233

3334
if(command == driver.DECLARE_INPUTS) {
@@ -642,12 +643,38 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
642643
# we will literally nest by nest level to avoid constant subseting
643644
# although we end up needed to unnest at times as well so ultimately,
644645
# it likely makes little difference in performance
645-
tidyr::nest(data = -nest_level) %>%
646+
tidyr::nest(data = -nest_level) ->
647+
FAO_SUA_Kt_R_nested
648+
646649
# we are now ready to recursively primarize APE commodities then aggregate to APE
650+
FAO_SUA_Kt_R_nested %>%
647651
Proc_primarize() ->
648652
L107.Traceable_FBS_PCe_2010Plus
649653

650654

655+
# also keep gross extraction rates
656+
# here is a simplified func of Proc_primarize but only binding
657+
# extraction rates across nests
658+
lapply(max(FAO_SUA_Kt_R_nested$nest_level):1,
659+
function(.curr_nest){
660+
661+
FAO_SUA_Kt_R_nested %>%
662+
filter(nest_level == .curr_nest) %>%
663+
pull(data) %>%
664+
first() ->
665+
DF_CURR_NEST
666+
667+
Mapping_gcamdata_SUA_PrimaryEquivalent_ID %>%
668+
distinct(APE_comm, nest_level, source_item, sink_item) %>%
669+
inner_join(
670+
Get_GROSS_EXTRACTION_RATE(DF_CURR_NEST, FAO_SUA_Kt_R_nested) %>%
671+
mutate(nest_level = .curr_nest) %>%
672+
spread(bal_source, extraction_rate) , by = c("APE_comm", "nest_level") )
673+
674+
}) %>% bind_rows() ->
675+
L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus
676+
677+
651678
Check_Balance_SUA <- function(.DF){
652679

653680
NULL -> element -> GCAM_commodity -> Import -> Export -> Production -> Food ->
@@ -857,6 +884,13 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
857884
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus
858885

859886

887+
L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus %>%
888+
add_title("L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus") %>%
889+
add_units("NA") %>%
890+
add_comments("Extraction rates by source and sink items and nest level, FAO countries, APE commoditeis, for 2010 +") %>%
891+
same_precursors_as(L107.Traceable_FBS_PCe_2010Plus) ->
892+
L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus
893+
860894
return_data(MODULE_OUTPUTS)
861895

862896
} else {

R/yfaostat_Traceable_FBS_DataExport.R

+74-7
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
2323
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg"),
2424
"L105.Bal_new_all",
2525
"L107.Traceable_FBS_PCe_2010Plus",
26-
"L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus")
26+
"L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus",
27+
"L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus")
2728

2829
MODULE_OUTPUTS <-
2930
c(CSV = "Metadata_GCAMFAOSTAT_Traceable_FBS",
3031
CSV = "Nested_Mapping_SUA_To_Traceable_FBS",
3132
CSV = "SUA_2010_2022",
3233
CSV = "SUA_Food_Calorie_Macronutrient_2010_2022",
3334
CSV = "Traceable_FBS_PCe_2010_2022",
34-
CSV = "Traceable_FBS_Food_Calorie_Macronutrient_2010_2022")
35+
CSV = "Traceable_FBS_Food_Calorie_Macronutrient_2010_2022",
36+
CSV = "Traceable_FBS_Extraction_Rate_2010_2022")
3537

3638
if(command == driver.DECLARE_INPUTS) {
3739
return(MODULE_INPUTS)
@@ -111,7 +113,7 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
111113

112114
output_csv_data(
113115
gcam_dataset = Nested_Mapping_SUA_To_Traceable_FBS,
114-
out_filename = "Nested_Mapping_SUA_To_Traceable_FBS",
116+
out_filename = "Nested_Mapping_SUA_To_Traceable_FBS" %>% paste0("_", Sys.Date()),
115117
col_type_nonyear = "ccicici",
116118
title = "Mapping of SUA items to aggregated primary commodity equivalent products by level of nestings",
117119
unit = "NA",
@@ -148,7 +150,7 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
148150

149151
output_csv_data(
150152
gcam_dataset = SUA_2010_2022,
151-
out_filename = "SUA_2010_2022",
153+
out_filename = "SUA_2010_2022" %>% paste0("_", Sys.Date()),
152154
col_type_nonyear = "ccifc",
153155
title = "Supply utilization accounts for all FAO items in 2010 - 2022",
154156
unit = "1000 tonnes",
@@ -177,7 +179,7 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
177179

178180
output_csv_data(
179181
gcam_dataset = SUA_Food_Calorie_Macronutrient_2010_2022,
180-
out_filename = "SUA_Food_Calorie_Macronutrient_2010_2022",
182+
out_filename = "SUA_Food_Calorie_Macronutrient_2010_2022" %>% paste0("_", Sys.Date()),
181183
col_type_nonyear = "ccicc",
182184
title = "SUA: food energy and macronutrient in 2010 - 2022",
183185
unit = "Mt or Mkcal",
@@ -206,7 +208,7 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
206208

207209
output_csv_data(
208210
gcam_dataset = Traceable_FBS_PCe_2010_2022,
209-
out_filename = "Traceable_FBS_PCe_2010_2022",
211+
out_filename = "Traceable_FBS_PCe_2010_2022" %>% paste0("_", Sys.Date()),
210212
col_type_nonyear = "cccc",
211213
title = "Traceable FBS: supply utilization accounts in primary equivalent for APE items in 2010 - 2022",
212214
unit = "1000 tonnes",
@@ -235,7 +237,7 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
235237

236238
output_csv_data(
237239
gcam_dataset = Traceable_FBS_Food_Calorie_Macronutrient_2010_2022,
238-
out_filename = "Traceable_FBS_Food_Calorie_Macronutrient_2010_2022",
240+
out_filename = "Traceable_FBS_Food_Calorie_Macronutrient_2010_2022" %>% paste0("_", Sys.Date()),
239241
col_type_nonyear = "cccc",
240242
title = "Traceable FBS: food energy and macronutrient in 2010 - 2022",
241243
unit = "Mt or Mkcal",
@@ -248,6 +250,71 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
248250
"Traceable_FBS_Food_Calorie_Macronutrient_2010_2022")
249251

250252

253+
254+
255+
256+
# * Traceable_FBS_Extraction_Rate_2010_2022 ----
257+
# generate a base mapping to join table
258+
# there is some info from the mapping to be used to update the rates
259+
# E.g., output specific
260+
261+
Mapping_gcamdata_SUA_PrimaryEquivalent %>%
262+
distinct(APE_comm_Agg, APE_comm, nest_level, source_item,
263+
sink_item, sink_item, source_item,
264+
output_specific_extraction_rate,
265+
min_extraction_rate = extraction_rate_Q25, Q25asMin ) %>%
266+
filter(nest_level >= 1) %>%
267+
mutate(min_extraction_rate = if_else(Q25asMin == F, 0, min_extraction_rate)) ->
268+
Extraction_Rate_Mapping
269+
270+
Extraction_Rate_Mapping %>%
271+
replace_na(list(output_specific_extraction_rate = 1)) %>%
272+
full_join(L107.Traceable_FBS_PCe_Gross_Extraction_Rates_2010Plus,
273+
by = c("APE_comm", "nest_level", "source_item", "sink_item")) %>%
274+
left_join_error_no_match(
275+
Mapping_gcamdata_FAO_iso_reg %>% select(region_ID, iso), by = "region_ID") %>%
276+
transmute(aggregated_PCe_item = APE_comm_Agg, PCe_item = APE_comm, iso, year,
277+
nest_level, source_item, sink_item,
278+
output_specific_extraction_rate,
279+
ER_imported = bal_import,
280+
ER_domestic = bal_domestic_current,
281+
ER_lagged = bal_domestic_lag ) %>%
282+
gather(ER, value, ER_imported, ER_domestic, ER_lagged) %>%
283+
# incorporate output_specific_extraction_rate into ER
284+
mutate(value = value * output_specific_extraction_rate) %>%
285+
select(-output_specific_extraction_rate) %>%
286+
rename(source_item0 = source_item, sink_item0 = sink_item) %>%
287+
left_join_error_no_match(
288+
Mapping_gcamdata_SUA_ItemCode %>%
289+
distinct(source_item0 = item, source_item_code = item_code, source_item = item_new_2024), by = "source_item0") %>%
290+
left_join_error_no_match(
291+
Mapping_gcamdata_SUA_ItemCode %>%
292+
distinct(sink_item0 = item, sink_item_code = item_code, sink_item = item_new_2024), by = "sink_item0") %>%
293+
transmute(aggregated_PCe_item, PCe_item, iso, year,
294+
nest_level, source_item, sink_item, extraction_point = ER, value) %>%
295+
filter(is.finite(value)) %>%
296+
add_title("Traceable_FBS_Extraction_Rate_2010_2022") %>%
297+
add_units("none") %>%
298+
add_comments("gcamfaostat Export CSV") %>%
299+
add_precursors(file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent")) %>%
300+
spread(year, value)->
301+
Traceable_FBS_Extraction_Rate_2010_2022
302+
303+
output_csv_data(
304+
gcam_dataset = Traceable_FBS_Extraction_Rate_2010_2022,
305+
out_filename = "Traceable_FBS_Extraction_Rate_2010_2022" %>% paste0("_", Sys.Date()),
306+
col_type_nonyear = "cciiccc",
307+
title = "Extraction rates used for compiling the traceable FBS dataset",
308+
unit = "NA",
309+
code = "NA",
310+
description = "Data is compiled and generated by gcamfaostat.",
311+
out_dir = DIR_OUTPUT_CSV,
312+
GZIP = F)
313+
314+
add_to_output_meta(.df = Traceable_FBS_Extraction_Rate_2010_2022 %>% rename(item = sink_item),
315+
"Traceable_FBS_Extraction_Rate_2010_2022")
316+
317+
251318
# Metadata ----
252319
output_csv_data(
253320
gcam_dataset = Metadata_GCAMFAOSTAT_Traceable_FBS,

0 commit comments

Comments
 (0)