18
18
module_xfaostat_L105_DataConnectionToSUA <- function (command , ... ) {
19
19
20
20
MODULE_INPUTS <-
21
- c(FILE = file.path(DIR_RAW_DATA_FAOSTAT , " FAO_items " ),
21
+ c(FILE = file.path(DIR_RAW_DATA_FAOSTAT , " Mapping_gcamdata_FAO_items " ),
22
22
" L102.QCL_PROD" ,
23
23
" L102.QCL_AN_LIVEANIMAL_MEATEQ" ,
24
24
" TCL_wide" ,
@@ -39,7 +39,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
39
39
year <- value <- Year <- Value <- FAO_country <- iso <- NULL # silence package check.
40
40
SCL_wide <- element_code <- element <- area_code <- item_code <- area <-
41
41
item <- unit <- FBS_wide <- FBSH_CBH_wide <- TCL_wide <- TM_bilateral_wide <-
42
- L102.QCL_PROD <- FAO_items <- tier <- QCL <- oil <-
42
+ L102.QCL_PROD <- Mapping_gcamdata_FAO_items <- tier <- QCL <- oil <-
43
43
cake <- SCL_item_oil <- SCL_item_cake <- cake_rate <- cake_rate_world <-
44
44
DS_key_coproduct_item <- Production <- Import <- Export <- DS_demand <-
45
45
DS_production <- CoproductRate <- L102.QCL_AN_LIVEANIMAL_MEATEQ <- `Closing stocks` <-
@@ -384,14 +384,14 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
384
384
385
385
386
386
387
- # 3. Process items in FAO_items to get Balanced SUA data ----
387
+ # 3. Process items in Mapping_gcamdata_FAO_items to get Balanced SUA data ----
388
388
# # 3.1 Bal_new_tier1 ----
389
389
# Tier1 includes 209 = 210-1 items with best sources e.g. bilateral trade (TM) prodstat (QCL) and supply-utilization-account (SCL)
390
390
# Note that item 237 Oil soybean was moved from Tier1 to Tier2 to use SCL for production due to Brazil data issue in QCL
391
391
# SCL has balanced data processed by FAO but the quality was poor with low consistency
392
392
393
393
394
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 1 ) %> % pull(item_code )) %> %
394
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 1 ) %> % pull(item_code )) %> %
395
395
SUA_TEMPLATE_LEFT_JOIN(" QCL" ) %> %
396
396
SUA_TEMPLATE_LEFT_JOIN(" TM" ) %> %
397
397
SUA_TEMPLATE_LEFT_JOIN(" SCL" ) %> %
@@ -402,7 +402,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
402
402
select(- QCL , - TCL , - SCL ) %> %
403
403
# Adjust for balance across all dimensions
404
404
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
405
- left_join(FAO_items %> % select(item_code , item ), by = " item_code" ) - >
405
+ left_join(Mapping_gcamdata_FAO_items %> % select(item_code , item ), by = " item_code" ) - >
406
406
Bal_new_tier1
407
407
408
408
@@ -411,15 +411,15 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
411
411
# # 3.2 Bal_new_tier2 ----
412
412
# Tier2 includes 204 items that had no data or low quality data in QCL so used production from SCL
413
413
414
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 2 ) %> % pull(item_code )) %> %
414
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 2 ) %> % pull(item_code )) %> %
415
415
SUA_TEMPLATE_LEFT_JOIN(" TM" ) %> %
416
416
SUA_TEMPLATE_LEFT_JOIN(" SCL" ) %> %
417
417
mutate(value = case_when(
418
418
element %in% c(" Export" , " Import" ) ~ TCL ,
419
419
element %in% SCL_element_new ~ SCL ) ) %> %
420
420
select(- TCL , - SCL ) %> %
421
421
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
422
- left_join(FAO_items %> % select(item_code , item ), by = " item_code" ) - >
422
+ left_join(Mapping_gcamdata_FAO_items %> % select(item_code , item ), by = " item_code" ) - >
423
423
Bal_new_tier2
424
424
425
425
assert_FBS_balance(Bal_new_tier2 )
@@ -429,7 +429,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
429
429
# Tier3 includes 21 items that had QCL but no bilateral trade data
430
430
# so use gross trade from SCL
431
431
432
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 3 ) %> % pull(item_code )) %> %
432
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 3 ) %> % pull(item_code )) %> %
433
433
SUA_TEMPLATE_LEFT_JOIN(" SCL" ) %> %
434
434
# light cleaning here since more missing data were seen for this group
435
435
# set NA stock variation to zero to avoid fill NA later
@@ -446,15 +446,15 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
446
446
select(- QCL , - SCL ) %> %
447
447
GROSS_TRADE_ADJUST(.MIN_TRADE_PROD_RATIO = 0.01 ) %> %
448
448
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
449
- left_join(FAO_items %> % select(item_code , item ), by = " item_code" ) - >
449
+ left_join(Mapping_gcamdata_FAO_items %> % select(item_code , item ), by = " item_code" ) - >
450
450
Bal_new_tier3
451
451
452
452
assert_FBS_balance(Bal_new_tier3 )
453
453
454
454
# # 3.4 Bal_new_tier4 ----
455
455
# Tier4 includes 40 items included in SCL but not in Tier1-3
456
456
457
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 4 ) %> % pull(item_code )) %> %
457
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 4 ) %> % pull(item_code )) %> %
458
458
SUA_TEMPLATE_LEFT_JOIN(" SCL" ) %> %
459
459
# light cleaning here since more missing data were seen for this group
460
460
rename(value = SCL ) %> %
@@ -466,7 +466,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
466
466
# Gross trade is adjusted since the source was not bilateral trade
467
467
GROSS_TRADE_ADJUST(.MIN_TRADE_PROD_RATIO = 0.01 ) %> %
468
468
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
469
- left_join(FAO_items %> % select(item_code , item ), by = " item_code" ) - >
469
+ left_join(Mapping_gcamdata_FAO_items %> % select(item_code , item ), by = " item_code" ) - >
470
470
Bal_new_tier4
471
471
472
472
assert_FBS_balance(.DF = Bal_new_tier4 )
@@ -475,7 +475,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
475
475
# # 3.5 Bal_new_tier5 ----
476
476
# Tier5 includes 12 fish items from FBS and FBSH. Item code came from FBS as well
477
477
478
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 5 ) %> % pull(item_code )) %> %
478
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 5 ) %> % pull(item_code )) %> %
479
479
SUA_TEMPLATE_LEFT_JOIN(" FBS" ) %> %
480
480
mutate(value = if_else(is.na(value ) & element == " Stock Variation" , 0 , value )) %> %
481
481
group_by(area_code , item_code , element ) %> %
@@ -485,7 +485,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
485
485
replace_na(list (value = 0 )) %> %
486
486
GROSS_TRADE_ADJUST(.MIN_TRADE_PROD_RATIO = 0.01 ) %> %
487
487
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
488
- left_join(FAO_items %> % select(item_code , item ), by = " item_code" ) - >
488
+ left_join(Mapping_gcamdata_FAO_items %> % select(item_code , item ), by = " item_code" ) - >
489
489
Bal_new_tier5
490
490
491
491
assert_FBS_balance(.DF = Bal_new_tier5 )
@@ -496,7 +496,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
496
496
# "Rice, paddy (rice milled equivalent)" removed as not needed and excluded by FAOSTAT in 2023
497
497
# 773 (Flax, processed but not spun) is changed to 771 (Flax, raw or retted)
498
498
499
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 6 ) %> % pull(item_code )) %> %
499
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 6 ) %> % pull(item_code )) %> %
500
500
SUA_TEMPLATE_LEFT_JOIN(" QCL" ) %> %
501
501
SUA_TEMPLATE_LEFT_JOIN(" TM" , .DS_TM_Assert_Item = F ) %> %
502
502
SUA_TEMPLATE_LEFT_JOIN(" TCL_gross" , .DS_TM_Assert_Item = F ) %> %
@@ -509,23 +509,23 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
509
509
replace_na(list (value = 0 )) %> %
510
510
GROSS_TRADE_ADJUST(.MIN_TRADE_PROD_RATIO = 0.01 ) %> %
511
511
spread(element , value ) %> %
512
- # Processing to add demand based on DS_demand in FAO_items
512
+ # Processing to add demand based on DS_demand in Mapping_gcamdata_FAO_items
513
513
# Only an exclusive use is assumed
514
- mutate(Processed = if_else(item_code %in% c(FAO_items %> %
514
+ mutate(Processed = if_else(item_code %in% c(Mapping_gcamdata_FAO_items %> %
515
515
filter(tier == 6 , grepl(" Processed" , DS_demand )) %> %
516
516
pull(item_code ) ) & (Production + Import - Export ) > 0 ,
517
517
(Production + Import - Export ), 0 ),
518
- Food = if_else(item_code %in% c(FAO_items %> %
518
+ Food = if_else(item_code %in% c(Mapping_gcamdata_FAO_items %> %
519
519
filter(tier == 6 , grepl(" Food" , DS_demand )) %> %
520
520
pull(item_code ) ) & (Production + Import - Export ) > 0 ,
521
521
(Production + Import - Export ), 0 ),
522
- `Other uses` = if_else(item_code %in% c(FAO_items %> %
522
+ `Other uses` = if_else(item_code %in% c(Mapping_gcamdata_FAO_items %> %
523
523
filter(tier == 6 , grepl(" Other" , DS_demand )) %> %
524
524
pull(item_code ) ) & (Production + Import - Export ) > 0 ,
525
525
(Production + Import - Export ), 0 )) %> %
526
526
gather(element , value , - area_code , - item_code , - year ) %> %
527
527
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
528
- left_join(FAO_items %> % select(item_code , item ), by = " item_code" ) - >
528
+ left_join(Mapping_gcamdata_FAO_items %> % select(item_code , item ), by = " item_code" ) - >
529
529
Bal_new_tier6
530
530
531
531
assert_FBS_balance(.DF = Bal_new_tier6 )
@@ -543,7 +543,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
543
543
filter(element == " Production" ) %> %
544
544
mutate(value = value * 1000 ) %> % # convert units back to tonne!!!
545
545
# Join to keep Tier 8 items
546
- right_join(FAO_items %> % filter(tier == 7 ) %> %
546
+ right_join(Mapping_gcamdata_FAO_items %> % filter(tier == 7 ) %> %
547
547
# Get co-production rate from DS_production which is uniform across regions
548
548
mutate(CoproductRate = as.numeric(gsub(" Coproduction_Rate \\ (|)" ," " , DS_production ))) %> %
549
549
select(item_code , item , coproduct_item = DS_key_coproduct_item , coproduct_item_code = DS_key_coproduct_item_code , CoproductRate ),
@@ -553,21 +553,21 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
553
553
554
554
555
555
# ## 3.7.2 Process to get Bal_new_tier7 ----
556
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 7 ) %> % pull(item_code )) %> %
556
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 7 ) %> % pull(item_code )) %> %
557
557
SUA_TEMPLATE_LEFT_JOIN(" QCL_Coproduct" ) %> %
558
558
replace_na(list (value = 0 )) %> %
559
559
spread(element , value ) %> %
560
- # Processing to add demand based on DS_demand in FAO_items
560
+ # Processing to add demand based on DS_demand in Mapping_gcamdata_FAO_items
561
561
# Only an exclusive use is assumed
562
- mutate(Feed = if_else(item_code %in% c(FAO_items %> % filter(tier == 7 , grepl(" Feed" , DS_demand )) %> %
562
+ mutate(Feed = if_else(item_code %in% c(Mapping_gcamdata_FAO_items %> % filter(tier == 7 , grepl(" Feed" , DS_demand )) %> %
563
563
pull(item_code ) ) & Production > 0 ,
564
564
Production , 0 ),
565
- `Other uses` = if_else(item_code %in% c(FAO_items %> % filter(tier == 7 , grepl(" Other" , DS_demand )) %> %
565
+ `Other uses` = if_else(item_code %in% c(Mapping_gcamdata_FAO_items %> % filter(tier == 7 , grepl(" Other" , DS_demand )) %> %
566
566
pull(item_code ) ) & Production > 0 ,
567
567
Production , 0 ) ) %> %
568
568
gather(element , value , - area_code , - item_code , - year ) %> %
569
569
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
570
- left_join(FAO_items %> % select(item , item_code ), by = " item_code" ) - >
570
+ left_join(Mapping_gcamdata_FAO_items %> % select(item , item_code ), by = " item_code" ) - >
571
571
Bal_new_tier7
572
572
573
573
assert_FBS_balance(.DF = Bal_new_tier7 )
@@ -609,7 +609,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
609
609
APE_live_an_MeatEQ
610
610
611
611
# ## 3.8.2 Process to get Bal_new_tier9 ----
612
- Get_SUA_TEMPLATE(.ITEM_CODE = FAO_items %> % filter(tier == 8 ) %> % pull(item_code )) %> %
612
+ Get_SUA_TEMPLATE(.ITEM_CODE = Mapping_gcamdata_FAO_items %> % filter(tier == 8 ) %> % pull(item_code )) %> %
613
613
SUA_TEMPLATE_LEFT_JOIN(" APE_live_an_MeatEQ" ) %> %
614
614
spread(element , value ) %> %
615
615
# only keep net openning stock in the study period
@@ -618,7 +618,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
618
618
ungroup() %> %
619
619
gather(element , value , - area_code , - item_code , - year ) %> %
620
620
SUA_bal_adjust %> % # Unit is converted to 1000 tonnes!
621
- left_join(FAO_items %> % select(item , item_code ), by = " item_code" ) - >
621
+ left_join(Mapping_gcamdata_FAO_items %> % select(item , item_code ), by = " item_code" ) - >
622
622
Bal_new_tier8
623
623
624
624
assert_FBS_balance(.DF = Bal_new_tier8 )
@@ -646,15 +646,15 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
646
646
647
647
assert_FBS_balance(.DF = L105.Bal_new_all )
648
648
649
- rm(TCL_gross , TCL_TM , SCL , FBS , FBSH_CBH , FAO_items )
649
+ rm(TCL_gross , TCL_TM , SCL , FBS , FBSH_CBH , Mapping_gcamdata_FAO_items )
650
650
rm(list = ls(pattern = " Bal_new_tier*" ))
651
651
652
652
653
653
L105.Bal_new_all %> %
654
654
add_title(" L105.Bal_new_all" ) %> %
655
655
add_units(" Ktonne" ) %> %
656
656
add_comments(" Preprocessed FAO SUA 2010 - 2021" ) %> %
657
- add_precursors(file.path(DIR_RAW_DATA_FAOSTAT , " FAO_items " ),
657
+ add_precursors(file.path(DIR_RAW_DATA_FAOSTAT , " Mapping_gcamdata_FAO_items " ),
658
658
" L102.QCL_PROD" ,
659
659
" L102.QCL_AN_LIVEANIMAL_MEATEQ" ,
660
660
" TCL_wide" ,
@@ -676,7 +676,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
676
676
# Update log
677
677
# The old tier 5 was not needed (oil seed cake) as the data is available
678
678
679
- # ***Generate/check FAO_items ----
679
+ # ***Generate/check Mapping_gcamdata_FAO_items ----
680
680
681
681
# Curr_Envir <- environment()
682
682
#
@@ -737,7 +737,7 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
737
737
# filter(item_code != 30) -> Tier6
738
738
#
739
739
# Tier6 %>% left_join(
740
- # FAO_items %>% filter(tier == 6) %>% select(item_code, DS_trade, DS_production, DS_demand, DS_key_coproduct_item)
740
+ # Mapping_gcamdata_FAO_items %>% filter(tier == 6) %>% select(item_code, DS_trade, DS_production, DS_demand, DS_key_coproduct_item)
741
741
# ) %>% replace_na(list(DS_demand = "Other use only")) -> Tier6
742
742
#
743
743
# Tier6 %>% inner_join(
@@ -750,14 +750,14 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
750
750
# mutate(DS_production = "QCL") -> Tier6
751
751
#
752
752
#
753
- # FAO_items %>% filter(tier %in% 8) %>%
753
+ # Mapping_gcamdata_FAO_items %>% filter(tier %in% 8) %>%
754
754
# select(item_code, item, DS_trade, DS_production, DS_demand, DS_key_coproduct_item) %>% mutate(tier = 8) %>%
755
755
# filter(!grepl("rice|maize|hempseed|linseed|kapok|poppy|safflower", item)) %>%
756
756
# mutate(coproduct_item_code = c(274, 278, 332, 340))-> Tier7
757
757
#
758
758
# # "Oil of olive residues|Jojoba oil|Cake of cottonseed|Other oil of vegetable origin, crude n.e.c."
759
759
#
760
- # FAO_items %>% filter(tier %in% 9) %>%
760
+ # Mapping_gcamdata_FAO_items %>% filter(tier %in% 9) %>%
761
761
# select(item_code, item) %>% mutate(tier = 9) -> Tier8
762
762
#
763
763
# Tier1 %>% mutate(DS_trade = "TM", DS_production = "QCL", DS_demand = "SCL") %>%
@@ -769,9 +769,9 @@ module_xfaostat_L105_DataConnectionToSUA <- function(command, ...) {
769
769
# bind_rows(Tier7) %>%
770
770
# bind_rows(Tier8) -> Tier_All
771
771
#
772
- # FAO_items %>% filter(tier %in% 1:9) %>% anti_join(Tier_All, by = c("item_code"))
773
- # Tier_All %>% anti_join(FAO_items , by = c("item_code"))
772
+ # Mapping_gcamdata_FAO_items %>% filter(tier %in% 1:9) %>% anti_join(Tier_All, by = c("item_code"))
773
+ # Tier_All %>% anti_join(Mapping_gcamdata_FAO_items , by = c("item_code"))
774
774
#
775
- # Tier_All -> FAO_items
775
+ # Tier_All -> Mapping_gcamdata_FAO_items
776
776
#
777
777
0 commit comments