Skip to content

Commit

Permalink
Continue the restructure
Browse files Browse the repository at this point in the history
Remove GCAM info in primary eq.
  • Loading branch information
realxinzhao committed Jun 15, 2024
1 parent a0617b2 commit 1bd6caf
Show file tree
Hide file tree
Showing 10 changed files with 2,381 additions and 750 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright 2019 Battelle Memorial Institute; see the LICENSE file.

#' module_xfaostat_L101_RawDataPreProc4_FBSH_CB
#' module_xfaostat_L101_RawDataPreProc4_FBSH_CBH
#'
#' Preprocess raw faostat data part 4 FBSH and CB
#' Preprocess raw faostat data part 4 FBSH and CBH
#'
#' @param command API command to execute
#' @param ... other optional parameters, depending on command
Expand All @@ -15,7 +15,7 @@
#' @importFrom tibble tibble
#' @importFrom tidyr complete drop_na gather nesting spread replace_na
#' @author XZ 2023
module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) {
module_xfaostat_L101_RawDataPreProc4_FBSH_CBH <- function(command, ...) {

MODULE_INPUTS <-
c(FAOSTAT_FILE = file.path(DIR_RAW_DATA_FAOSTAT, "FoodBalanceSheetsHistoric_E_All_Data_Normalized"),
Expand Down Expand Up @@ -106,7 +106,7 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) {
unit = "1000 tonnes") -> CBH1 # convert to Kton


## *FBSH_CB merge the two----
## *FBSH_CBH merge the two----
# load processed data

FBSH1 %>% distinct(item_code) %>%
Expand Down
6 changes: 3 additions & 3 deletions R/xfaostat_L105_DataConnectionToSUA.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {

FBSH_CBH_wide %>% gather_years() %>%
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>%
FAOSTAT_AREA_RM_NONEXIST() -> FBSH_CB
FAOSTAT_AREA_RM_NONEXIST() -> FBSH_CBH

TCL_wide %>% gather_years() %>%
filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>%
Expand Down Expand Up @@ -160,7 +160,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
# Merge Sudan regions to be consistent with data
# Mainly for storage data concerns
# And only keep data > min(FAOSTAT_Hist_Year_FBS)
for (.DF in c("SCL", "TCL_TM", "TCL_gross", "FBSH_CB", "FBS", "QCL_PROD")) {
for (.DF in c("SCL", "TCL_TM", "TCL_gross", "FBSH_CBH", "FBS", "QCL_PROD")) {
get(.DF) %>% filter(year >= min(FAOSTAT_Hist_Year_FBS)) %>%
# merge Sudan and South Sudan
FAO_AREA_DISAGGREGATE_HIST_DISSOLUTION_ALL(SUDAN2012_MERGE = T) %>%
Expand Down Expand Up @@ -645,7 +645,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {

assert_FBS_balance(.DF = Bal_new_all)

rm(TCL_gross, TCL_TM, SCL, FBS, FBSH_CB, FAO_items)
rm(TCL_gross, TCL_TM, SCL, FBS, FBSH_CBH, FAO_items)
rm(list = ls(pattern = "Bal_new_tier*"))


Expand Down
12 changes: 6 additions & 6 deletions R/xfaostat_L106_FoodMacroNutrient.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) {
"FBS_wide",
"OA",
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "FAO_an_items_cal_SUA"),
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "MAPPING_FAO_FBS_SUA"))
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_FAO_FBS_SUA"))

MODULE_OUTPUTS <-
c("SUA_food_macronutrient_rate")
Expand All @@ -39,7 +39,7 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) {
value_world <- value_reg <- Diff <- p_Diff <- quantile <- `Food supply quantity (kg/capita/yr)` <-
`Protein supply quantity (g/capita/day)` <- `Fat supply quantity (g/capita/day)` <-
FAO_an_items_cal_SUA <- Mcal_t <- fat_Perc <- protein_Perc <- FAO_FBS_code <-
SCL_item_code <- CPC_code <- MAPPING_FAO_FBS_SUA <- unit <- element <-
SCL_item_code <- CPC_code <- Mapping_FAO_FBS_SUA <- unit <- element <-
area_code <- item_code <- element_code <- OA <- FBS_wide <- SCL_wide <-
`Food supply (kcal/capita/day)` <- NULL

Expand Down Expand Up @@ -125,7 +125,7 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) {
#*******************************************

SCL %>% filter(element_code %in% c(261, 271, 281, 5141)) %>% #All 3 cal protein fats and food in ton
right_join(MAPPING_FAO_FBS_SUA %>%
right_join(Mapping_FAO_FBS_SUA %>%
filter(!is.na(CPC_code)) %>%
select(item_code = SCL_item_code, FAO_FBS_code, FBS_label),
by = "item_code") %>%
Expand Down Expand Up @@ -197,7 +197,7 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) {
#*******************************************

# Adding the 12 fish item from FBS
MAPPING_FAO_FBS_SUA %>% filter(is.na(CPC_code)) %>%
Mapping_FAO_FBS_SUA %>% filter(is.na(CPC_code)) %>%
select(item = FBS_label, item_code = FAO_FBS_code) -> Fish_item

FBS %>% right_join(Fish_item, by = c("item_code", "item")) -> FBS_fish
Expand Down Expand Up @@ -245,7 +245,7 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) {

rm(SUA_food_macronutrient, SUA_food_yearmean,
SUA_food_yearmean_fill, SUA_food_yearareamean,
OA, POP, SCL, FBS, MAPPING_FAO_FBS_SUA,
OA, POP, SCL, FBS, Mapping_FAO_FBS_SUA,
SUA_food_macronutrient_rate_nofish, FAO_an_items_cal_SUA)
rm(Fish_item)
rm(checkarea, checkitem, checkelement)
Expand All @@ -260,7 +260,7 @@ module_xfaostat_L106_FoodMacroNutrient <- function(command, ...) {
"FBS_wide",
"OA",
file.path(DIR_RAW_DATA_FAOSTAT, "FAO_an_items_cal_SUA"),
file.path(DIR_RAW_DATA_FAOSTAT, "MAPPING_FAO_FBS_SUA")) ->
file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_FAO_FBS_SUA")) ->
SUA_food_macronutrient_rate

# P.S. ----
Expand Down
Loading

0 comments on commit 1bd6caf

Please sign in to comment.