diff --git a/.gitignore b/.gitignore index 6d36915c..73db14b7 100644 --- a/.gitignore +++ b/.gitignore @@ -48,5 +48,6 @@ inst/extdata/GCAM/aglu/FAO/GCAMFAOSTAT* # Developer paper developertemp +data-raw/SciData/* #docs diff --git a/R/yfaostat_Traceable_FBS_DataExport.R b/R/yfaostat_Traceable_FBS_DataExport.R index 38791358..2708ea2e 100644 --- a/R/yfaostat_Traceable_FBS_DataExport.R +++ b/R/yfaostat_Traceable_FBS_DataExport.R @@ -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", @@ -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 diff --git a/inst/extdata/FAOSTAT/Mapping_gcamdata_SUA_PrimaryEquivalent.csv b/inst/extdata/FAOSTAT/Mapping_gcamdata_SUA_PrimaryEquivalent.csv index e5a3243e..591f0ead 100644 --- a/inst/extdata/FAOSTAT/Mapping_gcamdata_SUA_PrimaryEquivalent.csv +++ b/inst/extdata/FAOSTAT/Mapping_gcamdata_SUA_PrimaryEquivalent.csv @@ -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 diff --git a/inst/extdata/aglu/FAO/Mapping_SUA_PrimaryEquivalent.csv b/inst/extdata/aglu/FAO/Mapping_SUA_PrimaryEquivalent.csv index 1331c63c..a21b3d7b 100644 --- a/inst/extdata/aglu/FAO/Mapping_SUA_PrimaryEquivalent.csv +++ b/inst/extdata/aglu/FAO/Mapping_SUA_PrimaryEquivalent.csv @@ -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