Skip to content

Commit ed7696a

Browse files
committed
update driver_drake to add data export plans to clean cache and finish FBS data exporting
Bump up version number to v1.1 and
1 parent 2a07365 commit ed7696a

12 files changed

+311
-988
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: gcamfaostat
22
Type: Package
33
Title: Prepare, process, and synthesize FAOSTAT data for global agroeconomic and multisector dynamic modeling
4-
Version: 1.0.1
5-
Date: 2024-10-17
4+
Version: 1.1.0
5+
Date: 2024-12-1
66
Authors@R: c(person("Xin", "Zhao", email = "xin.zhao@pnnl.gov", role = c("cre", "aut"), comment = c(ORCID = "0000-0002-1801-4393")),
77
person("Maksym", "Chepeliev", role = "aut", comment = c(ORCID = "0000-0001-8585-2314")),
88
person("Pralit", "Patel", role = "aut", comment = c(ORCID = "0000-0003-3992-1061")),

R/driver.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ driver_drake <- function(
419419
# assign output path (csv_dir) to DIR_OUTPUT_CSV in the package environment
420420
assign(x = "DIR_OUTPUT_CSV", value = csv_dir, envir = rlang::pkg_env("gcamfaostat"))
421421
dir.create(DIR_OUTPUT_CSV, showWarnings = FALSE, recursive = TRUE)
422+
423+
# clean data export chunk so that the data will be always exported
424+
drake::clean("module_yfaostat_Traceable_FBS_DataExport")
422425
}
423426

424427
# We merely suggest drake as we can still run the data system via driver

R/xfaostat_L107_FoodBalanceSheet.R

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
2020

2121
MODULE_INPUTS <-
22-
c("TM_bilateral_wide",
23-
"L105.Bal_new_all",
24-
"L106.SUA_food_macronutrient_rate",
25-
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent"),
22+
c(FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent"),
2623
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
27-
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg") )
24+
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg"),
25+
"TM_bilateral_wide",
26+
"L105.Bal_new_all",
27+
"L106.SUA_food_macronutrient_rate")
2828

2929
MODULE_OUTPUTS <-
30-
c("L107.APE_after2010",
31-
"L107.FAO_Food_Macronutrient_All")
30+
c("L107.Traceable_FBS_PCe_2010Plus",
31+
"L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus")
3232

3333
if(command == driver.DECLARE_INPUTS) {
3434
return(MODULE_INPUTS)
@@ -626,7 +626,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
626626
tidyr::nest(data = -nest_level) %>%
627627
# we are now ready to recursively primarize APE commodities then aggregate to APE
628628
Proc_primarize() ->
629-
L107.APE_after2010
629+
L107.Traceable_FBS_PCe_2010Plus
630630

631631

632632
Check_Balance_SUA <- function(.DF){
@@ -679,7 +679,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
679679
}
680680

681681

682-
Check_Balance_SUA(L107.APE_after2010)
682+
Check_Balance_SUA(L107.Traceable_FBS_PCe_2010Plus)
683683

684684
## Done Section2 ----
685685
#****************************----
@@ -693,7 +693,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
693693
# All food item with available macronutrient info from FAOSTAT are included
694694

695695
# a. Get all GCAM SUA items from the mapping by binding both source and sink items
696-
# about 486 items (out of 530) used in GCAM
696+
# 533 items
697697

698698
Mapping_gcamdata_SUA_PrimaryEquivalent %>%
699699
select(APE_comm_Agg, item = source_item) %>%
@@ -703,24 +703,23 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
703703
left_join_error_no_match(Mapping_gcamdata_SUA_ItemCode, by = "item") ->
704704
SUA_Items_APE
705705

706+
# food is uniquely mapped from SUA to APE_Comm
706707
assertthat::assert_that(
707708
SUA_Items_APE %>% distinct(item_code) %>% nrow() == SUA_Items_APE %>% nrow(),
708709
msg = "Check duplicates in Mapping_gcamdata_SUA_PrimaryEquivalent SUA items"
709710
)
710711

711-
# highly processed products or other products are not included in GCAM
712-
# (e.g., wine, infant food, or other nonfood items etc.)
713-
712+
# check: there are two items with empty accounts
714713
Mapping_gcamdata_SUA_ItemCode %>%
715714
filter(!item_code %in% unique(SUA_Items_APE$item_code)) -> SUA_Items_NEC
716715

717-
# b. There are 426 FAO food items, all included in FAO_SUA_Kt_2010to2022 (530 items)
718-
# SUA_Items_Food includes both GCAM and NonGCAM(NEC)
716+
# b. There are 432 FAO food items, all included in FAO_SUA_Kt_2010to2022 (535 items)
719717
Mapping_gcamdata_SUA_ItemCode %>%
720718
filter(item_code %in% unique(L106.SUA_food_macronutrient_rate$item_code)) %>%
721-
left_join(SUA_Items_APE %>% select(-item), by = "item_code") %>%
719+
left_join_error_no_match(SUA_Items_APE %>% select(-item), by = "item_code") %>%
722720
# For NA APE_comm_Agg: not elsewhere classified (NEC)
723721
# So we would know % of food calories not included in GCAM commodities
722+
# we later updated mapping to account for NEC there already; no No NA here
724723
mutate(APE_comm_Agg = if_else(is.na(APE_comm_Agg), "NEC", APE_comm_Agg)) ->
725724
SUA_Items_Food
726725

@@ -740,6 +739,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
740739

741740
### b. Calculate SUA food Calories consumption by joining macronutrient rates and SUA food ----
742741

742+
# Get data ready first
743743
FAO_SUA_Kt %>%
744744
filter(element == "Food", item_code %in% SUA_Items_Food$item_code) %>%
745745
# ensure we at least have a complete series across time otherwise it may
@@ -760,47 +760,55 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
760760
by = c("item_code", "macronutrient")) %>%
761761
mutate(macronutrient_value = if_else(is.na(macronutrient_value),
762762
macronutrient_value_World,
763-
macronutrient_value),
764-
# calculate total Cal, protein and fat in food
765-
# value was in 1000 ton or 10^ 9 g
766-
value = macronutrient_value * Food_Kt,
763+
macronutrient_value) ) %>%
764+
# computation
765+
# calculate total Cal, protein and fat in food
766+
# value was in 1000 ton or 10^ 9 g
767+
mutate(value = macronutrient_value * Food_Kt,
767768
value = if_else(macronutrient %in% c("fatperc", "proteinperc"),
768769
value / 100 /1000, value)) %>% # unit from perc to Mt
769770
select(-macronutrient_value, -macronutrient_value_World, -Food_Kt) %>%
770771
# rename element with units
771-
mutate(macronutrient = case_when(
772-
macronutrient == "calperg" ~ "MKcal",
773-
macronutrient == "fatperc" ~ "MtFat",
774-
macronutrient == "proteinperc" ~ "MtProtein" )) %>%
775-
left_join_error_no_match(Area_Region_Map, by = "area_code") ->
776-
L107.FAO_Food_Macronutrient_All
772+
mutate(
773+
element = case_when(
774+
macronutrient == "calperg" ~ "Dietary energy",
775+
macronutrient == "fatperc" ~ "Fat",
776+
macronutrient == "proteinperc" ~ "Protein" ),
777+
unit = case_when(
778+
macronutrient == "calperg" ~ "Mkcal",
779+
macronutrient == "fatperc" ~ "Mt",
780+
macronutrient == "proteinperc" ~ "Mt" )) %>%
781+
select(-macronutrient)->
782+
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus
777783

778784

779785
#****************************----
780786
# Produce outputs ----
781-
#*******************************
782787

783-
# GCAM_AgLU_SUA_APE_1973_2019 %>%
784-
# add_title("GCAM_AgLU_SUA_APE_1973_2019") %>%
785-
# add_units("kton") %>%
786-
# add_comments("Supply utilization balance for GCAM commodities and regions in primary equivalent") %>%
787-
# add_precursors(MODULE_INPUTS) ->
788-
# GCAM_AgLU_SUA_APE_1973_2019
788+
L107.Traceable_FBS_PCe_2010Plus %>%
789+
add_title("L107.Traceable_FBS_PCe_2010Plus") %>%
790+
add_units("kton") %>%
791+
add_comments("Updated FBS: Supply utilization balance in primary equivalent, FAO countries, APE commoditeis, for 2010 +") %>%
792+
add_precursors("TM_bilateral_wide",
793+
"L105.Bal_new_all",
794+
file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent"),
795+
file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
796+
file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg")) ->
797+
L107.Traceable_FBS_PCe_2010Plus
798+
799+
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus %>%
800+
add_title("L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus") %>%
801+
add_units("MKcal, MtFat, MtProtein") %>%
802+
add_comments("Dietary energy and macronutrients for food supply in PCe, FAO countries, APE commoditeis, for 2010 +") %>%
803+
add_precursors("L106.SUA_food_macronutrient_rate",
804+
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent"),
805+
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
806+
file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg")) ->
807+
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus
789808

790809

791-
792-
#
793-
# L107.FAO_Food_Macronutrient_All %>%
794-
# add_title("GCAM_AgLU_SUA_APE_1973_2019") %>%
795-
# add_units("MKcal, MtFat, MtProtein") %>%
796-
# add_comments("Macronutrient consumption values connected to food consumption in GCAM_AgLU_SUA_APE_1973_2019") %>%
797-
# add_precursors(MODULE_INPUTS) ->
798-
# L107.FAO_Food_Macronutrient_All
799-
#
800-
801810
return_data(MODULE_OUTPUTS)
802811

803-
804812
} else {
805813
stop("Unknown command")
806814
}

R/xfaostat_L999_DataExport.R

Lines changed: 0 additions & 95 deletions
This file was deleted.

R/yfaostat_GCAM_CSVExport.R renamed to R/yfaostat_GCAM_DataExport.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright 2019 Battelle Memorial Institute; see the LICENSE file.
22

3-
#' module_yfaostat_GCAM_CSVExport
3+
#' module_yfaostat_GCAM_DataExport
44
#'
55
#' Generate supply utilization balance in primary equivalent
66
#'
@@ -15,7 +15,7 @@
1515
#' @importFrom tibble tibble
1616
#' @importFrom tidyr complete drop_na gather nesting spread replace_na
1717
#' @author XZ Nov2023
18-
module_yfaostat_GCAM_CSVExport <- function(command, ...) {
18+
module_yfaostat_GCAM_DataExport <- function(command, ...) {
1919

2020
MODULE_INPUTS <-
2121
c("CS",
@@ -583,7 +583,7 @@ module_yfaostat_GCAM_CSVExport <- function(command, ...) {
583583
col_type_nonyear = "ciic",
584584
title = "Information of dataset exported by gcamfaostat",
585585
unit = "NA",
586-
description = "Data is preprocessed and generated by gcamfaostat v1.0.1",
586+
description = "Data is preprocessed and generated by gcamfaostat v1.1.0",
587587
code = "NA",
588588
out_dir = DIR_OUTPUT_CSV)
589589

0 commit comments

Comments
 (0)