Skip to content

Commit

Permalink
Improve PCe mapping and add it to data export for Sci Data
Browse files Browse the repository at this point in the history
  • Loading branch information
realxinzhao committed Dec 12, 2024
1 parent ed7696a commit 5b3f8ba
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ inst/extdata/GCAM/aglu/FAO/GCAMFAOSTAT*
# Developer
paper
developertemp
data-raw/SciData/*

#docs
77 changes: 61 additions & 16 deletions R/yfaostat_Traceable_FBS_DataExport.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
c(FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent"),
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg"),
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
"L105.Bal_new_all",
"L107.Traceable_FBS_PCe_2010Plus",
"L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus")

MODULE_OUTPUTS <-
c(CSV = "Metadata_GCAMFAOSTAT_Traceable_FBS",
CSV = "Nested_Mapping_SUA_To_Traceable_FBS",
CSV = "SUA_2010_2022",
CSV = "SUA_Food_Calorie_Macronutrient_2010_2022",
CSV = "Traceable_FBS_PCe_2010_2022",
Expand Down Expand Up @@ -58,27 +58,72 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
# Helper functions ----
Metadata_GCAMFAOSTAT_Traceable_FBS <- NULL
add_to_output_meta <- function(.df,
.datasetname){

.df %>% gather_years() -> .df
if ("year" %in% names(.df)) {
YearRange = paste0(.df %>% distinct(year) %>% min," to ", .df %>% distinct(year) %>% max)
} else{
YearRange = "BaseYears"
.datasetname,
.skip_stat = FALSE){

if (.skip_stat == FALSE) {

.df %>% gather_years() -> .df
if ("year" %in% names(.df)) {
YearRange = paste0(.df %>% distinct(year) %>% min," to ", .df %>% distinct(year) %>% max)
} else{
YearRange = "BaseYears"
}

Metadata_GCAMFAOSTAT_Traceable_FBS %>%
bind_rows(
data.frame(dataset = .datasetname,
nReg = .df %>% distinct(iso) %>% nrow,
nItem = .df %>% distinct(item) %>% nrow,
Years = YearRange)
) -> .df1
} else {

Metadata_GCAMFAOSTAT_Traceable_FBS %>%
bind_rows(
data.frame(dataset = .datasetname)
) -> .df1
}

Metadata_GCAMFAOSTAT_Traceable_FBS %>%
bind_rows(
data.frame(dataset = .datasetname,
nReg = .df %>% distinct(iso) %>% nrow,
nItem = .df %>% distinct(item) %>% nrow,
Years = YearRange)
) -> .df1

assign("Metadata_GCAMFAOSTAT_Traceable_FBS", .df1, envir = Curr_Envir)
}


# * Mapping_PrimaryEquivalent_nested_SUA ----

Mapping_gcamdata_SUA_PrimaryEquivalent %>%
transmute(aggregated_PCe_item = APE_comm_Agg, PCe_item = APE_comm,
nest_level, source_item0 = source_item, sink_item0 = sink_item) %>%
distinct() %>%
left_join_error_no_match(
Mapping_gcamdata_SUA_ItemCode %>%
distinct(source_item0 = item, source_item_code = item_code, source_item = item_new_2024), by = "source_item0") %>%
left_join_error_no_match(
Mapping_gcamdata_SUA_ItemCode %>%
distinct(sink_item0 = item, sink_item_code = item_code, sink_item = item_new_2024), by = "sink_item0") %>%
transmute(aggregated_PCe_item, PCe_item,
nest_level, source_item, source_item_code, sink_item, sink_item_code) %>%
add_title("Nested_Mapping_SUA_To_Traceable_FBS") %>%
add_units("none") %>%
add_comments("gcamfaostat Export CSV") %>%
add_precursors(file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent")) ->
Nested_Mapping_SUA_To_Traceable_FBS

output_csv_data(
gcam_dataset = Nested_Mapping_SUA_To_Traceable_FBS,
out_filename = "Nested_Mapping_SUA_To_Traceable_FBS",
col_type_nonyear = "ccicici",
title = "Mapping of SUA items to aggregated primary commodity equivalent products by level of nestings",
unit = "NA",
code = "NA",
description = "Data is compiled and generated by gcamfaostat.",
out_dir = DIR_OUTPUT_CSV,
GZIP = F)

add_to_output_meta(.df = Nested_Mapping_SUA_To_Traceable_FBS,
"Nested_Mapping_SUA_To_Traceable_FBS", .skip_stat = TRUE)


# Zeros were removed in all dataset in exporting to reduce size
# We updated item names to the latest when applicable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Milk,
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow",Yoghurt,,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Whey, fresh",,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Cheese, whole cow milk",,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Milk, skimmed cow",,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow",Cream fresh,,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Butter, cow milk",,"Butter, Ghee and products",0.739010126,0.857196004,TRUE
Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Ghee, butteroil of cow milk",,"Butter, Ghee and products",0.739010126,0.857196004,TRUE
Expand Down
1 change: 0 additions & 1 deletion inst/extdata/aglu/FAO/Mapping_SUA_PrimaryEquivalent.csv
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Milk, whole conden
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow",Yoghurt,,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Whey, fresh",,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Cheese, whole cow milk",,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Milk, skimmed cow",,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow",Cream fresh,,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Butter, cow milk",,"Butter, Ghee and products",0.739010126,0.857196004,TRUE
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Ghee, butteroil of cow milk",,"Butter, Ghee and products",0.739010126,0.857196004,TRUE
Expand Down

0 comments on commit 5b3f8ba

Please sign in to comment.