19
19
module_xfaostat_L107_FoodBalanceSheet <- function (command , ... ) {
20
20
21
21
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" ),
26
23
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" )
28
28
29
29
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 " )
32
32
33
33
if (command == driver.DECLARE_INPUTS ) {
34
34
return (MODULE_INPUTS )
@@ -626,7 +626,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
626
626
tidyr :: nest(data = - nest_level ) %> %
627
627
# we are now ready to recursively primarize APE commodities then aggregate to APE
628
628
Proc_primarize() - >
629
- L107.APE_after2010
629
+ L107.Traceable_FBS_PCe_2010Plus
630
630
631
631
632
632
Check_Balance_SUA <- function (.DF ){
@@ -679,7 +679,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
679
679
}
680
680
681
681
682
- Check_Balance_SUA(L107.APE_after2010 )
682
+ Check_Balance_SUA(L107.Traceable_FBS_PCe_2010Plus )
683
683
684
684
# # Done Section2 ----
685
685
# ****************************----
@@ -693,7 +693,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
693
693
# All food item with available macronutrient info from FAOSTAT are included
694
694
695
695
# 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
697
697
698
698
Mapping_gcamdata_SUA_PrimaryEquivalent %> %
699
699
select(APE_comm_Agg , item = source_item ) %> %
@@ -703,24 +703,23 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
703
703
left_join_error_no_match(Mapping_gcamdata_SUA_ItemCode , by = " item" ) - >
704
704
SUA_Items_APE
705
705
706
+ # food is uniquely mapped from SUA to APE_Comm
706
707
assertthat :: assert_that(
707
708
SUA_Items_APE %> % distinct(item_code ) %> % nrow() == SUA_Items_APE %> % nrow(),
708
709
msg = " Check duplicates in Mapping_gcamdata_SUA_PrimaryEquivalent SUA items"
709
710
)
710
711
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
714
713
Mapping_gcamdata_SUA_ItemCode %> %
715
714
filter(! item_code %in% unique(SUA_Items_APE $ item_code )) - > SUA_Items_NEC
716
715
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)
719
717
Mapping_gcamdata_SUA_ItemCode %> %
720
718
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" ) %> %
722
720
# For NA APE_comm_Agg: not elsewhere classified (NEC)
723
721
# 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
724
723
mutate(APE_comm_Agg = if_else(is.na(APE_comm_Agg ), " NEC" , APE_comm_Agg )) - >
725
724
SUA_Items_Food
726
725
@@ -740,6 +739,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
740
739
741
740
# ## b. Calculate SUA food Calories consumption by joining macronutrient rates and SUA food ----
742
741
742
+ # Get data ready first
743
743
FAO_SUA_Kt %> %
744
744
filter(element == " Food" , item_code %in% SUA_Items_Food $ item_code ) %> %
745
745
# ensure we at least have a complete series across time otherwise it may
@@ -760,47 +760,55 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
760
760
by = c(" item_code" , " macronutrient" )) %> %
761
761
mutate(macronutrient_value = if_else(is.na(macronutrient_value ),
762
762
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 ,
767
768
value = if_else(macronutrient %in% c(" fatperc" , " proteinperc" ),
768
769
value / 100 / 1000 , value )) %> % # unit from perc to Mt
769
770
select(- macronutrient_value , - macronutrient_value_World , - Food_Kt ) %> %
770
771
# 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
777
783
778
784
779
785
# ****************************----
780
786
# Produce outputs ----
781
- # *******************************
782
787
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
789
808
790
809
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
-
801
810
return_data(MODULE_OUTPUTS )
802
811
803
-
804
812
} else {
805
813
stop(" Unknown command" )
806
814
}
0 commit comments