Skip to content

Commit

Permalink
Add a new export data of capital stock for GCAM KLEAM
Browse files Browse the repository at this point in the history
  • Loading branch information
realxinzhao committed Apr 14, 2024
1 parent cdafac1 commit e24a6b8
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 13 deletions.
42 changes: 41 additions & 1 deletion R/xfaostat_L101_RawDataPreProc2_PP_PD_OA.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) {
c(FAOSTAT_FILE = "aglu/FAO/FAOSTAT/Prices_E_All_Data_Normalized",
FAOSTAT_FILE = "aglu/FAO/FAOSTAT/Deflators_E_All_Data_Normalized",
FAOSTAT_FILE = "aglu/FAO/FAOSTAT/Population_E_All_Data_Normalized",
FAOSTAT_FILE = "aglu/FAO/FAOSTAT/Investment_CapitalStock_E_All_Data_Normalized",
FILE = "aglu/FAO/FAOSTAT/Other_supplementary/GDP_deflator_Taiwan",
"QCL_area_code_map")


MODULE_OUTPUTS <-
c("PP_wide", # Producer prices
"PD", # GDP deflator
"OA") # Population
"OA", # Population
"CS") # Capital stock

if(command == driver.DECLARE_INPUTS) {
return(MODULE_INPUTS)
Expand All @@ -52,6 +54,7 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) {
PP_wide <- extract_prebuilt_data("PP_wide")
PD <- extract_prebuilt_data("PD")
OA <- extract_prebuilt_data("OA")
CS <- extract_prebuilt_data("CS")

} else {

Expand Down Expand Up @@ -216,6 +219,43 @@ module_xfaostat_L101_RawDataPreProc2_PP_PD_OA <- function(command, ...) {
OA

verify_identical_prebuilt(OA)

# *[CS]: Capital stock ----

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

CS %>% distinct(element, element_code)
CS %>% distinct(item, item_code)
# check area mapping
CS %>% filter(area_code < 400) %>%
distinct(area, area_code) %>% full_join(QCL_area_code_map, by = c("area_code")) -> A


CS %>% filter(area_code %in% QCL_area_code,
# only keep regions with production
element_code == 6184 # Value US$, 2015 prices
) %>%
select(area_code,
area,
item_code,
item,
element_code,
element,
year,
value,
unit) %>%
rm_accent("item", "area") -> CS1

### output OA ----
CS1 %>%
add_title("FAO capiral stock") %>%
add_units("2015 USD") %>%
add_comments("Preprocessed FAO CS") %>%
add_precursors("aglu/FAO/FAOSTAT/Investment_CapitalStock_E_All_Data_(Normalized)") ->
CS

verify_identical_prebuilt(CS)

}

return_data(MODULE_OUTPUTS)
Expand Down
47 changes: 39 additions & 8 deletions R/xfaostat_L999_CSVExport.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
module_xfaostat_L999_CSVExport <- function(command, ...) {

MODULE_INPUTS <-
c("PD",
c("CS",
"PD",
"Bal_new_all",
"FBSH_CB_wide",
"QCL_PROD",
Expand All @@ -44,6 +45,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {
CSV = "GCAMFAOSTAT_AnimalStock",
CSV = "GCAMFAOSTAT_ProdPrice",
CSV = "FAO_GDP_Deflators",
CSV = "GCAMFAOSTAT_CapitalStock",
CSV = "GCAMFAOSTAT_MacroNutrientRate",
CSV = "GCAMFAOSTAT_ForProdTrade",
CSV = "GCAMFAOSTAT_ForExpPrice",
Expand Down Expand Up @@ -297,7 +299,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {

rm(QCL_CROP_PRIMARY, QCL_FODDERCROP, QCL_PROD)

#**AnimalStocks ----
## *AnimalStocks ----
# Laying is not needed for now; live animal stocks and milk animals are used for water demand calculation in GCAM
# 1171 "Animals live nes" and 1083 "Pigeons, other birds" are not available

Expand Down Expand Up @@ -333,7 +335,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {
)

# Producer prices in quantity and value ----
# **Producer Price ----
## *Producer Price ----

QCL_PRIMARY_PROD_PV %>%
filter(year >= MIN_HIST_PP_YEAR) %>%
Expand Down Expand Up @@ -361,7 +363,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {
rm(QCL_PRIMARY_PROD_PV)


###** GDP deflators ----
## *GDP deflators ----

FAO_GDP_Deflators <-
PD %>% FAOSTAT_AREA_RM_NONEXIST(RM_AREA_CODE = NULL) %>%
Expand Down Expand Up @@ -426,7 +428,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {

# Forestry data output ----

#*ForProdTrade ----
## *ForProdTrade ----
For_Balance %>%
filter(element != "Demand") %>%
spread(year, value) %>%
Expand All @@ -450,7 +452,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {
out_dir = DIR_OUTPUT_CSV
)

#*ForExpPrice ----
## *ForExpPrice ----
FO_RoundwoodProducts_Export_Q_V %>%
spread(year, value) %>%
add_title("GCAMFAOSTAT_ForExpPrice") %>%
Expand All @@ -476,7 +478,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {

# Land Cover data output ----

#*LandCover ----
## *LandCover ----
RL_LandCover %>%
spread(year, value) %>%
add_title("GCAMFAOSTAT_LandCover") %>%
Expand All @@ -502,7 +504,7 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {

# Fertilizer data output----

#* N Fertilizer ----
## *N Fertilizer ----
RFN_ProdDemand %>%
spread(year, value) %>%
add_title("GCAMFAOSTAT_NFertilizer") %>%
Expand Down Expand Up @@ -538,6 +540,35 @@ module_xfaostat_L999_CSVExport <- function(command, ...) {
)


# Macroeconomics output----

## *Capital stock ----
CS %>%
FAO_AREA_DISAGGREGATE_HIST_DISSOLUTION_ALL(SUDAN2012_MERGE = T) %>%
spread(year, value) %>%
add_title("GCAMFAOSTAT_CapitalStock") %>%
add_units("2015 USD") %>%
add_comments("gcamfaostat Export CSV") %>%
add_precursors("CS",
"xfaostat_L199_GCAMFAOSTAT_CSV") ->
GCAMFAOSTAT_CapitalStock

add_to_output_meta(.df = GCAMFAOSTAT_NFertilizer, "GCAMFAOSTAT_CapitalStock")

output_csv_data(
gcam_dataset = GCAMFAOSTAT_CapitalStock,
out_filename = "GCAMFAOSTAT_CapitalStock",
col_type_nonyear = "iiciccc",
title = "FAO macroeconomic metrics of capital shocks by country_year",
unit = "tonnes N",
description = "Data is preprocessed and generated by gcamfaostat",
code = "CS",
out_dir = DIR_OUTPUT_CSV
)




}

xfaostat_L199_GCAMFAOSTAT_CSV %>%
Expand Down
3 changes: 2 additions & 1 deletion R/xfaostat_helper_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ gcamfaostat_metadata <- function(.DIR_RAW_DATA_FAOSTAT = DIR_RAW_DATA_FAOSTAT,
"RFN", # Fertilizer by nutrient
"RL", # Land Use
"FO", # Forest production and trade
"OA" # Population
"OA", # Population
"CS" # Capital stock
)

if (OnlyReturnDatasetCodeRequired == T) {
Expand Down
26 changes: 26 additions & 0 deletions data-raw/examples of adding CS data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@




# Capital Stock
FF_download_FAOSTAT(DATASETCODE = "CS")

# Modity gcamfaostat_metadata to include "CS" in FAO_dataset_code_required

gcamfaostat_metadata(OnlyReturnDatasetCodeRequired = T)

# Run
gcamfaostat_metadata()

# adding preprocessing to module_xfaostat_L101_RawDataPreProc2_PP_PD_OA

## FAOSTAT_FILE = "aglu/FAO/FAOSTAT/Investment_CapitalStock_E_All_Data_(Normalized)" to MODULE_INPUTS
## CS related processing


# adding export to module_xfaostat_L999_CSVExport

## "CS" to MODULE_INPUTS
## "GCAMFAOSTAT_CapitalStock" to MODULE_OUTPUTS

## add CS to prebuilt data in generate_package_data
7 changes: 4 additions & 3 deletions data-raw/generate_package_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ prebuilt_data_names <- c(
"QCL_area_code_map"), # Country code

# outputs of module_xfaostat_L101_RawDataPreProc2_PP_PD_OA
c("PP_wide", # Producer prices
"PD", # GDP deflator
"OA"), # Population
c("PP_wide", # Producer prices
"PD", # GDP deflator
"OA", # Population
"CS"), # Capital stock

# outputs of module_xfaostat_L101_RawDataPreProc3_SCL_FBS
c("SCL_wide", # Supply utilization accounting
Expand Down
Binary file modified data/PREBUILT_DATA.rda
Binary file not shown.
Loading

0 comments on commit e24a6b8

Please sign in to comment.