Skip to content

Commit

Permalink
fix environments
Browse files Browse the repository at this point in the history
  • Loading branch information
realxinzhao committed Aug 18, 2023
1 parent 44ca9e3 commit ca841a4
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 90 deletions.
5 changes: 2 additions & 3 deletions R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ OUTPUT_Export_CSV <- TRUE
## Fao raw data folder
DIR_RAW_DATA_FAOSTAT <- "inst/extdata/aglu/FAO/FAOSTAT"
## Output GCAM csv
#DIR_OUTPUT_CSV <- "inst/extdata/aglu/FAO/temp"
DIR_OUTPUT_CSV <- "outputs/CSV"
dir.create(file.path(DIR_OUTPUT_CSV), showWarnings = FALSE)
DIR_OUTPUT_CSV <- file.path("outputs", "CSV")
dir.create(DIR_OUTPUT_CSV, recursive = T, showWarnings = FALSE)


# Historical years of focus ----
Expand Down
11 changes: 6 additions & 5 deletions R/xfaostat_L101_RawDataPreProc1_QCL.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,25 @@ module_xfaostat_L101_RawDataPreProc1_QCL <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)
Curr_Envir <- environment()


if(Process_Raw_FAO_Data == FALSE) {

# Prebuilt data is read here ----
# Load from Prebuilt data ----
QCL_wide <- extract_prebuilt_data("QCL_wide")
QCL_area_code_map <- extract_prebuilt_data("QCL_area_code_map")

} else {

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)


# *[QCL] FAOSTAT Production and area ----

## Load raw data
FAOSTAT_load_raw_data(DATASETCODE = "QCL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT)
FAOSTAT_load_raw_data(DATASETCODE = "QCL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir)

QCL %>% distinct(element_code, element)

Expand Down
15 changes: 8 additions & 7 deletions R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,28 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)
Curr_Envir <- environment()


if(Process_Raw_FAO_Data == FALSE) {

# Prebuilt data is read here ----
# Load from Prebuilt data ----
PP_wide <- extract_prebuilt_data("PP_wide")
PD <- extract_prebuilt_data("PD")
OA <- extract_prebuilt_data("OA")

} else {

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)

# Get area code ----
QCL_area_code <- QCL_area_code_map %>% distinct(area_code) %>% pull()


# *[PP] Producer price ----

FAOSTAT_load_raw_data(DATASETCODE = "PP", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT)
FAOSTAT_load_raw_data(DATASETCODE = "PP", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir)
# check data
PP %>% distinct(element, element_code, unit)

Expand Down Expand Up @@ -128,7 +129,7 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) {
# [PD] FAO_GDP_deflators ----
#**************************************

FAOSTAT_load_raw_data(DATASETCODE = "PD", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT)
FAOSTAT_load_raw_data(DATASETCODE = "PD", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir)
# read in Taiwan values as FAO does not have Taiwan price data
# GDP_deflator_Taiwan

Expand Down Expand Up @@ -183,7 +184,7 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) {

# *[OA]: Population ----

FAOSTAT_load_raw_data(DATASETCODE = "OA", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT)
FAOSTAT_load_raw_data(DATASETCODE = "OA", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir)

OA %>% distinct(element, element_code)
OA %>% distinct(item, item_code)
Expand Down
13 changes: 7 additions & 6 deletions R/xfaostat_L101_RawDataPreProc3_SCL_FBS.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ module_xfaostat_L101_RawDataPreProc3_SCL_FBS <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)
Curr_Envir <- environment()


if(Process_Raw_FAO_Data == FALSE) {
Expand All @@ -50,20 +48,23 @@ module_xfaostat_L101_RawDataPreProc3_SCL_FBS <- function(command, ...) {

} else {

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)

# Get area code ----
QCL_area_code <-
QCL_area_code_map %>% distinct(area_code) %>% pull()

## *[SCL] SUA: supply utilization accounting ----

FAOSTAT_load_raw_data("SCL") # SUA 2010+
FAOSTAT_load_raw_data("SCL", .Envir = Curr_Envir) # SUA 2010+

SCL %>% distinct(element, element_code, unit)


if (is.character(SCL$item_code)){

FAOSTAT_load_raw_data("SCL", GET_MAPPINGCODE = "ItemCodes")
FAOSTAT_load_raw_data("SCL", GET_MAPPINGCODE = "ItemCodes", .Envir = Curr_Envir)

SCL %>% rename(cpc_code = item_code) %>%
left_join_error_no_match(
Expand Down Expand Up @@ -117,7 +118,7 @@ module_xfaostat_L101_RawDataPreProc3_SCL_FBS <- function(command, ...) {
## *[FBS] new food balance sheet (2010-) ----

## Load raw data
FAOSTAT_load_raw_data("FBS") # New FBS 2010+
FAOSTAT_load_raw_data("FBS", .Envir = Curr_Envir) # New FBS 2010+
FBS %>% distinct(element, element_code, unit)

FBS %>% filter(
Expand Down
10 changes: 6 additions & 4 deletions R/xfaostat_L101_RawDataPreProc4_FBSH_CB.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----
Curr_Envir <- environment()

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)


if(Process_Raw_FAO_Data == FALSE) {
Expand All @@ -50,6 +49,9 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) {

} else {

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)

## Get area code ----
QCL_area_code <-
QCL_area_code_map %>% distinct(area_code) %>% pull()
Expand All @@ -59,7 +61,7 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) {

## *[FBSH] old food balance sheet (-2013) ----

FAOSTAT_load_raw_data("FBSH") # Old FBS -2013
FAOSTAT_load_raw_data("FBSH", .Envir = Curr_Envir) # Old FBS -2013
FBSH %>% distinct(element, element_code, unit)
# Keep population (old)
FBSH %>%
Expand All @@ -80,7 +82,7 @@ module_xfaostat_L101_RawDataPreProc4_FBSH_CB <- function(command, ...) {

## *[CB] Non-food Balance ----

FAOSTAT_load_raw_data("CB") # Old FBS-nonfood -2013
FAOSTAT_load_raw_data("CB", .Envir = Curr_Envir) # Old FBS-nonfood -2013

CB %>% distinct(element, element_code, unit)
# Keep population (old)
Expand Down
19 changes: 4 additions & 15 deletions R/xfaostat_L101_RawDataPreProc5_TCL.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ module_xfaostat_L101_RawDataPreProc5_TCL <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----
Curr_Envir <- environment()

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)


if(Process_Raw_FAO_Data == FALSE) {
Expand All @@ -48,24 +47,14 @@ module_xfaostat_L101_RawDataPreProc5_TCL <- function(command, ...) {

} else {


# FAOSTAT_RDS <- c("TCL")
#
# DIR_PREBUILT_FAOSTAT <- "data/PREBUILT_FAOSTAT"
#
# lapply(FAOSTAT_RDS, function(d){
# assertthat::assert_that(file.exists(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))))
# assign(d, readRDS(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))),
# envir = parent.env(environment()))
# })
#
# TCL %>% filter(year >= min(FAOSTAT_Hist_Year_TCL)) %>% spread(year, value) -> TCL_wide
# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)


QCL_area_code <- QCL_area_code_map %>% distinct(area_code) %>% pull()

# *[TCL] Gross trade ----
FAOSTAT_load_raw_data("TCL") # Gross trade
FAOSTAT_load_raw_data("TCL", .Envir = Curr_Envir) # Gross trade

TCL %>% distinct(element, element_code, unit)
TCL %>% distinct(item, item_code)
Expand Down
31 changes: 16 additions & 15 deletions R/xfaostat_L101_RawDataPreProc6_TM.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)
Curr_Envir <- environment()


if(Process_Raw_FAO_Data == FALSE) {
Expand All @@ -47,17 +45,8 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) {

} else {


# FAOSTAT_RDS <- c("TM_bilateral_wide")
#
# DIR_PREBUILT_FAOSTAT <- "data"
#
# lapply(FAOSTAT_RDS, function(d){
# assertthat::assert_that(file.exists(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))))
# assign(d, readRDS(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))),
# envir = parent.env(environment()))
# })

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)


# Get area code ----
Expand All @@ -66,7 +55,7 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) {

# *[TM] Bilateral trade ----
#*FAO has better quality bilateral data since 1992, covering most SUA items
FAOSTAT_load_raw_data("TM") # Bilateral trade
FAOSTAT_load_raw_data("TM", .Envir = Curr_Envir) # Bilateral trade

TM %>%
# Only keep quantities for elements with a unit of tonnes
Expand Down Expand Up @@ -143,3 +132,15 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) {
stop("Unknown command")
}
}

# FAOSTAT_RDS <- c("TM_bilateral_wide")
#
# DIR_PREBUILT_FAOSTAT <- "data"
#
# lapply(FAOSTAT_RDS, function(d){
# assertthat::assert_that(file.exists(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))))
# assign(d, readRDS(file.path(DIR_PREBUILT_FAOSTAT, paste0(d, ".rds"))),
# envir = parent.env(environment()))
# })


11 changes: 6 additions & 5 deletions R/xfaostat_L101_RawDataPreProc7_FO.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ module_xfaostat_L101_RawDataPreProc7_FO <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----


get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)
Curr_Envir <- environment()



Expand All @@ -46,7 +43,11 @@ module_xfaostat_L101_RawDataPreProc7_FO <- function(command, ...) {


} else {
FAOSTAT_load_raw_data(DATASETCODE = "FO", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT)

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)

FAOSTAT_load_raw_data(DATASETCODE = "FO", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir)

# Only keep roundwood
FO %>% filter(year %in% FAOSTAT_Hist_Year,
Expand Down
9 changes: 4 additions & 5 deletions R/xfaostat_L101_RawDataPreProc8_RL_RFN.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ module_xfaostat_L101_RawDataPreProc8_RL_RFN <- function(command, ...) {

all_data <- list(...)[[1]]

# Load required inputs ----

get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)

Curr_Envir <- environment()


if(Process_Raw_FAO_Data == FALSE) {
Expand All @@ -50,8 +47,10 @@ module_xfaostat_L101_RawDataPreProc8_RL_RFN <- function(command, ...) {

} else {

# Load required inputs ----
get_data_list(all_data, MODULE_INPUTS, strip_attributes = TRUE)

FAOSTAT_load_raw_data(DATASETCODE = "RL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT)
FAOSTAT_load_raw_data(DATASETCODE = "RL", DATA_FOLDER = DIR_RAW_DATA_FAOSTAT, .Envir = Curr_Envir)

RL %>%
filter(year %in% FAOSTAT_Hist_Year,
Expand Down
12 changes: 8 additions & 4 deletions R/xfaostat_L102_ProductionArea.R
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,13 @@ module_xfaostat_L102_ProductionArea <- function(command, ...) {
FBS %>% distinct(element, element_code, unit)

# Get fish items through mapping

Curr_Envir <- environment()
checkitem <-
FF_join_checkmap(c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), "item_code", "item") %>%
FF_join_checkmap(DFs = c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"),
COL_by = "item_code",
COL_rename = "item",
.ENVIR = Curr_Envir
) %>%
mutate(match = if_else(QCL_COMM_AN_PRIMARY_item == FAO_an_items_PRODSTAT_item, T, F))


Expand Down Expand Up @@ -467,13 +471,13 @@ module_xfaostat_L102_ProductionArea <- function(command, ...) {
# P.S. Check primary product mapping ----

# checkitem <-
# FF_join_checkmap(c("QCL_COMM_CROP_PRIMARY", "FAO_ag_items_PRODSTAT"), "item_code", "item") %>%
# FF_join_checkmap(c("QCL_COMM_CROP_PRIMARY", "FAO_ag_items_PRODSTAT"), "item_code", "item",.ENVIR = Curr_Envir) %>%
# mutate(match = if_else(QCL_COMM_CROP_PRIMARY_item == FAO_ag_items_PRODSTAT_item , T, F))
# checkitem %>% filter(is.na(match)|match == F)
# # 160 primary items (matching here) + 15/16 fodder crops
#
# checkitem <-
# FF_join_checkmap(c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), "item_code", "item") %>%
# FF_join_checkmap(c("QCL_COMM_AN_PRIMARY", "FAO_an_items_PRODSTAT"), "item_code", "item",.ENVIR = Curr_Envir) %>%
# mutate(match = if_else(QCL_COMM_AN_PRIMARY_item == FAO_an_items_PRODSTAT_item, T, F))
# checkitem %>% filter(is.na(match)|match == F)
# # Snails, not sea production came from SCL
Expand Down
4 changes: 2 additions & 2 deletions R/xfaostat_L103_ProducerPrices.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ module_xfaostat_L103_ProducerPrices <- function(command, ...) {

#FF_check_count_plot(QCL_PRIMARY) # 205 items
#FF_check_count_plot(PP) # PP %>% distinct(area)

Curr_Envir <- environment()
# check items
FF_join_checkmap(c("QCL_PRIMARY", "PP"), "item_code", "item") -> A
FF_join_checkmap(c("QCL_PRIMARY", "PP"), "item_code", "item", .ENVIR = Curr_Envir) -> A
A %>% filter(!is.na(QCL_PRIMARY_item)) %>% filter(is.na(PP_item)) -> PP_NO_DATA_ITEM
# 20 items out of 205 were missing; mostly animal offals, fats or skins

Expand Down
6 changes: 4 additions & 2 deletions R/xfaostat_L104_ProductionAreaAddFodder.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ module_xfaostat_L104_ProductionAreaAddFodder <- function(command, ...) {
"QCL_area_code_map")->
QCL_FODDERCROP


Curr_Envir <- environment()
# 160 primary items +16 fodder crops
FF_join_checkmap(c("QCL_FODDERCROP", "FAO_ag_items_PRODSTAT"), "item_code", "item") %>%
FF_join_checkmap(DFs = c("QCL_FODDERCROP", "FAO_ag_items_PRODSTAT"),
COL_by = "item_code",
COL_rename = "item", .ENVIR = Curr_Envir) %>%
mutate(match = if_else(QCL_FODDERCROP_item == FAO_ag_items_PRODSTAT_item , T, F))-> checkitem
checkitem %>% filter(match == T)
# 15 fodder crops + 1 pumpkin removed in mapping
Expand Down
Loading

0 comments on commit ca841a4

Please sign in to comment.