-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
getRegions -> getItems & fixed various lintr warnings
- Loading branch information
1 parent
09ecf33
commit 4c7df84
Showing
11 changed files
with
394 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: mrcommons | ||
Type: Package | ||
Title: MadRat commons Input Data Library | ||
Version: 1.7.13 | ||
Date: 2022-03-01 | ||
Version: 1.7.14 | ||
Date: 2022-03-02 | ||
Authors@R: c(person("Benjamin Leon", "Bodirsky", email = "[email protected]", role = "aut"), | ||
person("Kristine", "Karstens", role = "aut"), | ||
person("Lavinia", "Baumstark", role = "aut"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
convertCEDS <- function(x,subtype) { | ||
convertCEDS <- function(x, subtype) { | ||
|
||
# fill all missing countries with 0 | ||
x[is.na(x)] <- 0 | ||
|
||
x1 <- x["srb (kosovo)",,] | ||
getRegions(x1) <- c("srb") | ||
x["srb",,] <- x["srb",,] + x1 | ||
x <- x[c("srb (kosovo)"),,,invert=TRUE] | ||
# Steve Smith 11.3.2016 on CEDS_Review_3-10-16.zip: there is a huge bug for fugitive emissions in zmb, and eth | ||
# I believe just past 2010. so do something to correct that (just keep those emissions constant from 2010 forward | ||
x1 <- x["srb (kosovo)", , ] | ||
getItems(x1, dim = 1) <- "srb" | ||
x["srb", , ] <- x["srb", , ] + x1 | ||
x <- x[c("srb (kosovo)"), , , invert = TRUE] | ||
|
||
# Steve Smith 11.3.2016 on CEDS_Review_3-10-16.zip: there is a huge bug for fugitive emissions in zmb, and eth | ||
# I believe just past 2010. so do something to correct that (just keep those emissions constant from 2010 forward | ||
# for that sector in those two countries). | ||
|
||
# checked: all zero expept for NMVOC. But for NMVOC emission after 2010 do not show significant deviations from before 2010 | ||
|
||
|
||
# checked: all zero expept for NMVOC. But for NMVOC emission after 2010 do not | ||
# show significant deviations from before 2010 | ||
|
||
# rename global to glo | ||
getRegions(x) <- gsub("global","glo", getRegions(x)) | ||
getRegions(x) <- toupper(getRegions(x)) | ||
|
||
# delete ANT and SCG from raw data because their successors are already included in the data | ||
#x <- x[c("ANT","SCG"),,invert=TRUE] | ||
getItems(x, dim = 1) <- gsub("global", "glo", getItems(x, dim = 1)) | ||
getItems(x, dim = 1) <- toupper(getItems(x, dim = 1)) | ||
|
||
# most shipping and aviation data is global only (except 1A3dii_Domestic-navigation regional). We want to distribute it evenly across all countries. | ||
# most shipping and aviation data is global only (except 1A3dii_Domestic-navigation | ||
# regional). We want to distribute it evenly across all countries. | ||
# Therefore, save global data because it will be removed by toolCountryfill | ||
|
||
# 1A3dii_Domestic-navigation regional (global value is zero ) | ||
# 1A3di_International-shipping global (no regional values exist) | ||
# 1A3ai_International-aviation global (no regional values exist) | ||
# 1A3aii_Domestic-aviation global (no regional values exist) | ||
|
||
var_glob <- c("1A3di_International-shipping", | ||
varGlob <- c("1A3di_International-shipping", | ||
"1A3ai_International-aviation", | ||
"1A3aii_Domestic-aviation") | ||
x_glo <- x["GLO",,var_glob] | ||
|
||
# remove global values. Note: the sector 2A1_Cement-production has a global sum that is indentical to the sum over regions | ||
x <- x["GLO",,invert=TRUE] | ||
xGLO <- x["GLO", , varGlob] | ||
|
||
# remove global values. Note: the sector 2A1_Cement-production has a global | ||
# sum that is indentical to the sum over regions | ||
x <- x["GLO", , invert = TRUE] | ||
# fills missing ISO countires and remove unknown ISO countires | ||
x <- toolCountryFill(x,fill=0) | ||
x <- toolCountryFill(x, fill = 0) | ||
|
||
# Create weight 1 for xGLO | ||
w <- new.magpie(getItems(x, dim = 1), getItems(x, dim = 2), getItems(xGLO, dim = 3), fill = 1) | ||
|
||
# Create weight 1 for x_glo | ||
w <- new.magpie(getRegions(x),getYears(x),getNames(x_glo),fill=1) | ||
|
||
# Create mapping of each country to GLO | ||
mapping <- matrix(c(getRegions(x),rep("GLO",length(getRegions(x)))),length(getRegions(x))) | ||
mapping <- data.frame(from = getItems(x, dim = 1), to = "GLO") | ||
|
||
# Spread global shipping and aviation data evenly across countries and save it to regions of x | ||
x[,,var_glob] <- toolAggregate(x_glo,mapping,weight=w) | ||
x[, , varGlob] <- toolAggregate(xGLO, mapping, weight = w) | ||
|
||
return(x) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,75 @@ | ||
#' @title convertCEDS2021 | ||
#' | ||
#' | ||
#' @description converts emission data from the CEDS database | ||
#' @param x magpie object from source function | ||
#' @return MAgPIE object | ||
#' @author Benjamin Leon Bodirsky, David Klein | ||
|
||
convertCEDS2021 <- function(x) { | ||
|
||
# fill all missing countries with 0 | ||
x[is.na(x)] <- 0 | ||
|
||
# change unit to Mt | ||
x=x/1000 | ||
|
||
x1 <- x["srb (kosovo)",,] | ||
getRegions(x1) <- c("srb") | ||
x["srb",,] <- x["srb",,] + x1 | ||
x <- x[c("srb (kosovo)"),,,invert=TRUE] | ||
getRegions(x) <- gsub("global","glo", getRegions(x)) | ||
getRegions(x) <- toupper(getRegions(x)) | ||
map2 <- c(BC_ktC="bc_c", | ||
CO_ktCO="co", | ||
CH4_ktCH4="ch4", | ||
N2O_ktN2O="n2o_n", | ||
NH3_ktNH3="nh3_n", | ||
NOx_ktNO2="no2_n", | ||
NMVOC_ktNMVOC="nmvoc", | ||
OC_ktC="oc_c", | ||
SO2_ktSO2="so2", | ||
CO2_ktCO2="co2_c" | ||
x <- x / 1000 | ||
|
||
x1 <- x["srb (kosovo)", , ] | ||
getItems(x1, dim = 1) <- "srb" | ||
x["srb", , ] <- x["srb", , ] + x1 | ||
x <- x[c("srb (kosovo)"), , , invert = TRUE] | ||
|
||
getItems(x, dim = 1) <- gsub("global", "glo", getItems(x, dim = 1)) | ||
getItems(x, dim = 1) <- toupper(getItems(x, dim = 1)) | ||
|
||
map2 <- c(BC_ktC = "bc_c", | ||
CO_ktCO = "co", | ||
CH4_ktCH4 = "ch4", | ||
N2O_ktN2O = "n2o_n", | ||
NH3_ktNH3 = "nh3_n", | ||
NOx_ktNO2 = "no2_n", | ||
NMVOC_ktNMVOC = "nmvoc", | ||
OC_ktC = "oc_c", | ||
SO2_ktSO2 = "so2", | ||
CO2_ktCO2 = "co2_c" | ||
) | ||
getNames(x,dim=2) <- map2[getNames(x,dim=2)] | ||
|
||
x[,,"n2o_n"]=x[,,"n2o_n"]/44*28 | ||
x[,,"nh3_n"]=x[,,"nh3_n"]/17*14 | ||
x[,,"no2_n"]=x[,,"no2_n"]/46*14 | ||
x[,,"co2_c"]=x[,,"co2_c"]/44*12 | ||
|
||
|
||
|
||
|
||
# most shipping and aviation data is global only (except 1A3dii_Domestic-navigation regional). We want to distribute it evenly across all countries. | ||
getNames(x, dim = 2) <- map2[getNames(x, dim = 2)] | ||
|
||
x[, , "n2o_n"] <- x[, , "n2o_n"] / 44 * 28 | ||
x[, , "nh3_n"] <- x[, , "nh3_n"] / 17 * 14 | ||
x[, , "no2_n"] <- x[, , "no2_n"] / 46 * 14 | ||
x[, , "co2_c"] <- x[, , "co2_c"] / 44 * 12 | ||
|
||
|
||
|
||
|
||
# most shipping and aviation data is global only (except 1A3dii_Domestic-navigation | ||
# regional). We want to distribute it evenly across all countries. | ||
# Therefore, save global data because it will be removed by toolCountryfill | ||
|
||
# 1A3dii_Domestic-navigation regional (global value is zero ) | ||
# 1A3di_International-shipping global (no regional values exist) | ||
# 1A3ai_International-aviation global (no regional values exist) | ||
# 1A3aii_Domestic-aviation global (no regional values exist) | ||
var_glob <- c("1A3di_International-shipping", | ||
|
||
varGlob <- c("1A3di_International-shipping", | ||
"1A3ai_International-aviation", | ||
"1A3aii_Domestic-aviation") | ||
x_glo <- x["GLO",,var_glob] | ||
|
||
# remove global values. Note: the sector 2A1_Cement-production has a global sum that is indentical to the sum over regions | ||
x <- x["GLO",,invert=TRUE] | ||
xGLO <- x["GLO", , varGlob] | ||
|
||
# remove global values. Note: the sector 2A1_Cement-production has a global | ||
# sum that is indentical to the sum over regions | ||
x <- x["GLO", , invert = TRUE] | ||
# fills missing ISO countires and remove unknown ISO countires | ||
x <- toolCountryFill(x,fill=0) | ||
# Create weight 1 for x_glo | ||
w <- new.magpie(getRegions(x),getYears(x),getNames(x_glo),fill=1) | ||
x <- toolCountryFill(x, fill = 0) | ||
|
||
# Create weight 1 for xGLO | ||
w <- new.magpie(getItems(x, dim = 1), getItems(x, dim = 2), getItems(xGLO, dim = 3), fill = 1) | ||
|
||
# Create mapping of each country to GLO | ||
mapping <- matrix(c(getRegions(x),rep("GLO",length(getRegions(x)))),length(getRegions(x))) | ||
mapping <- data.frame(from = getItems(x, dim = 1), to = "GLO") | ||
|
||
# Spread global shipping and aviation data evenly across countries and save it to regions of x | ||
x[,,var_glob] <- toolAggregate(x_glo,mapping,weight=w) | ||
x[, , varGlob] <- toolAggregate(xGLO, mapping, weight = w) | ||
|
||
return(x) | ||
} | ||
} |
Oops, something went wrong.