Skip to content

Commit

Permalink
getRegions -> getItems & fixed various lintr warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tscheypidi committed Mar 2, 2022
1 parent 09ecf33 commit 4c7df84
Show file tree
Hide file tree
Showing 11 changed files with 394 additions and 377 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '32636076'
ValidationKey: '32656842'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
4 changes: 2 additions & 2 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "mrcommons: MadRat commons Input Data Library",
"version": "1.7.13",
"version": "1.7.14",
"description": "<p>Provides useful functions and a common structure to all the input data required to run models like MAgPIE\n and REMIND of model input data.<\/p>",
"creators": [
{
Expand Down Expand Up @@ -85,7 +85,7 @@
"name": "Soergel, Bjoern"
},
{
"name": "Führlich, Pascal"
"name": "Führlich, Pascal"
},
{
"name": "Dietrich, Jan Philipp"
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
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"),
Expand Down
2 changes: 1 addition & 1 deletion R/convertACCMIP.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' }
convertACCMIP <- function(x) {
map <- toolGetMapping(name = "CountryToCellMapping.rds", where = "mrcommons")
getRegions(x) <- map$cell
getItems(x, dim = 1, raw = TRUE) <- map$cell
y <- toolAggregate(x, map, from = 1, to = 3, dim = 1)
y <- toolCountryFill(y, fill = NA)
return(y)
Expand Down
66 changes: 33 additions & 33 deletions R/convertCEDS.R
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)
}
}
104 changes: 53 additions & 51 deletions R/convertCEDS2021.R
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)
}
}
Loading

0 comments on commit 4c7df84

Please sign in to comment.