Skip to content

Commit 5b3f8ba

Browse files
committed
Improve PCe mapping and add it to data export for Sci Data
1 parent ed7696a commit 5b3f8ba

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ inst/extdata/GCAM/aglu/FAO/GCAMFAOSTAT*
4848
# Developer
4949
paper
5050
developertemp
51+
data-raw/SciData/*
5152

5253
#docs

R/yfaostat_Traceable_FBS_DataExport.R

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
2121
c(FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent"),
2222
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
2323
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_FAO_iso_reg"),
24-
FILE = file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_ItemCode"),
2524
"L105.Bal_new_all",
2625
"L107.Traceable_FBS_PCe_2010Plus",
2726
"L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus")
2827

2928
MODULE_OUTPUTS <-
3029
c(CSV = "Metadata_GCAMFAOSTAT_Traceable_FBS",
30+
CSV = "Nested_Mapping_SUA_To_Traceable_FBS",
3131
CSV = "SUA_2010_2022",
3232
CSV = "SUA_Food_Calorie_Macronutrient_2010_2022",
3333
CSV = "Traceable_FBS_PCe_2010_2022",
@@ -58,27 +58,72 @@ module_yfaostat_Traceable_FBS_DataExport <- function(command, ...) {
5858
# Helper functions ----
5959
Metadata_GCAMFAOSTAT_Traceable_FBS <- NULL
6060
add_to_output_meta <- function(.df,
61-
.datasetname){
62-
63-
.df %>% gather_years() -> .df
64-
if ("year" %in% names(.df)) {
65-
YearRange = paste0(.df %>% distinct(year) %>% min," to ", .df %>% distinct(year) %>% max)
66-
} else{
67-
YearRange = "BaseYears"
61+
.datasetname,
62+
.skip_stat = FALSE){
63+
64+
if (.skip_stat == FALSE) {
65+
66+
.df %>% gather_years() -> .df
67+
if ("year" %in% names(.df)) {
68+
YearRange = paste0(.df %>% distinct(year) %>% min," to ", .df %>% distinct(year) %>% max)
69+
} else{
70+
YearRange = "BaseYears"
71+
}
72+
73+
Metadata_GCAMFAOSTAT_Traceable_FBS %>%
74+
bind_rows(
75+
data.frame(dataset = .datasetname,
76+
nReg = .df %>% distinct(iso) %>% nrow,
77+
nItem = .df %>% distinct(item) %>% nrow,
78+
Years = YearRange)
79+
) -> .df1
80+
} else {
81+
82+
Metadata_GCAMFAOSTAT_Traceable_FBS %>%
83+
bind_rows(
84+
data.frame(dataset = .datasetname)
85+
) -> .df1
6886
}
6987

70-
Metadata_GCAMFAOSTAT_Traceable_FBS %>%
71-
bind_rows(
72-
data.frame(dataset = .datasetname,
73-
nReg = .df %>% distinct(iso) %>% nrow,
74-
nItem = .df %>% distinct(item) %>% nrow,
75-
Years = YearRange)
76-
) -> .df1
77-
7888
assign("Metadata_GCAMFAOSTAT_Traceable_FBS", .df1, envir = Curr_Envir)
7989
}
8090

8191

92+
# * Mapping_PrimaryEquivalent_nested_SUA ----
93+
94+
Mapping_gcamdata_SUA_PrimaryEquivalent %>%
95+
transmute(aggregated_PCe_item = APE_comm_Agg, PCe_item = APE_comm,
96+
nest_level, source_item0 = source_item, sink_item0 = sink_item) %>%
97+
distinct() %>%
98+
left_join_error_no_match(
99+
Mapping_gcamdata_SUA_ItemCode %>%
100+
distinct(source_item0 = item, source_item_code = item_code, source_item = item_new_2024), by = "source_item0") %>%
101+
left_join_error_no_match(
102+
Mapping_gcamdata_SUA_ItemCode %>%
103+
distinct(sink_item0 = item, sink_item_code = item_code, sink_item = item_new_2024), by = "sink_item0") %>%
104+
transmute(aggregated_PCe_item, PCe_item,
105+
nest_level, source_item, source_item_code, sink_item, sink_item_code) %>%
106+
add_title("Nested_Mapping_SUA_To_Traceable_FBS") %>%
107+
add_units("none") %>%
108+
add_comments("gcamfaostat Export CSV") %>%
109+
add_precursors(file.path(DIR_RAW_DATA_FAOSTAT, "Mapping_gcamdata_SUA_PrimaryEquivalent")) ->
110+
Nested_Mapping_SUA_To_Traceable_FBS
111+
112+
output_csv_data(
113+
gcam_dataset = Nested_Mapping_SUA_To_Traceable_FBS,
114+
out_filename = "Nested_Mapping_SUA_To_Traceable_FBS",
115+
col_type_nonyear = "ccicici",
116+
title = "Mapping of SUA items to aggregated primary commodity equivalent products by level of nestings",
117+
unit = "NA",
118+
code = "NA",
119+
description = "Data is compiled and generated by gcamfaostat.",
120+
out_dir = DIR_OUTPUT_CSV,
121+
GZIP = F)
122+
123+
add_to_output_meta(.df = Nested_Mapping_SUA_To_Traceable_FBS,
124+
"Nested_Mapping_SUA_To_Traceable_FBS", .skip_stat = TRUE)
125+
126+
82127
# Zeros were removed in all dataset in exporting to reduce size
83128
# We updated item names to the latest when applicable
84129

inst/extdata/FAOSTAT/Mapping_gcamdata_SUA_PrimaryEquivalent.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ Milk and products,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Milk,
331331
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
332332
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
333333
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
334-
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
335334
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
336335
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
337336
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

inst/extdata/aglu/FAO/Mapping_SUA_PrimaryEquivalent.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Milk, whole conden
326326
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow",Yoghurt,,Milk - (Excluding excluding Butter butter) and products,0.739010126,0.857196004,TRUE
327327
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
328328
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
329-
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
330329
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
331330
Dairy,"Milk, whole fresh cow",1,TRUE,"Milk, whole fresh cow","Butter, cow milk",,"Butter, Ghee and products",0.739010126,0.857196004,TRUE
332331
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

0 commit comments

Comments
 (0)