Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix warning: The naming option = 'indicator.scenario' is deprecated... #51

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '13020150'
ValidationKey: '13049295'
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 CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'mrland: MadRaT land data package'
version: 0.65.0
date-released: '2024-11-04'
version: 0.65.1
date-released: '2024-11-18'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
Expand Down
7 changes: 2 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrland
Title: MadRaT land data package
Version: 0.65.0
Date: 2024-11-04
Version: 0.65.1
Date: 2024-11-18
Authors@R: c(
person("Jan Philipp", "Dietrich", , "[email protected]", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand Down Expand Up @@ -54,11 +54,8 @@ Imports:
Suggests:
covr,
ggplot2,
knitr,
rmarkdown,
testthat
VignetteBuilder:
knitr
Encoding: UTF-8
LazyData: no
RoxygenNote: 7.3.2
129 changes: 66 additions & 63 deletions R/readS4Nproject_input.R
Original file line number Diff line number Diff line change
@@ -1,90 +1,93 @@
#' @title readS4Nproject_input
#' @description reads in total bioenergy (1st gen, 2nd gen and residues) demand and co2 prices from IMAGE model for Sim4Nexus project
#'
#' readS4Nproject_input
#'
#' reads in total bioenergy (1st gen, 2nd gen and residues)
#' demand and co2 prices from IMAGE model for Sim4Nexus project
#'
#' @param subtype IMAGE input to be read in: co2prices or bioenergy
#'
#' @return magpie object at country-level resolution
#'
#' @author Felicitas Beier
#' @seealso \code{\link{readSource}}
#' @examples
#'
#' \dontrun{ a <- readSource("S4Nproject_input", convert="onlycorrect", aggregate=FALSE)
#' \dontrun{
#' a <- readSource("S4Nproject_input", convert = "onlycorrect", aggregate = FALSE)
#' }

readS4Nproject_input <- function(subtype="co2prices") {

readS4Nproject_input <- function(subtype = "co2prices") { # nolint: object_name_linter.
# IMAGE-iso mapping file
regionmapping <- read.csv("image_isocodes.csv")
regionmapping <- data.frame(iso=regionmapping$ISO.code,Region=regionmapping$IMAGE.region.name)
regionmapping <- as.data.frame(as.matrix(regionmapping),stringsAsFactors=F)
regionmapping <- data.frame(iso = regionmapping$ISO.code, Region = regionmapping$IMAGE.region.name)
regionmapping <- as.data.frame(as.matrix(regionmapping), stringsAsFactors = FALSE)

# match IMAGE countries with all iso countries
iso <- getISOlist(type = "all")
mapping <- data.frame(iso=iso, stringsAsFactors=F)
regionmapping <- merge(mapping, regionmapping, "iso", all.x=TRUE)
mapping <- data.frame(iso = iso, stringsAsFactors = FALSE)
regionmapping <- merge(mapping, regionmapping, "iso", all.x = TRUE)
# missing matches:
regionmapping$Region[regionmapping$iso=="SRB"] <- "Eastern Europe"
regionmapping$Region[regionmapping$iso=="SSD"] <- "Eastern Africa"
regionmapping$Region[regionmapping$iso=="PSE"] <- "Middle East"
regionmapping$Region[regionmapping$iso=="SXM"] <- "Rest Centr America"
regionmapping$Region[regionmapping$iso=="ALA"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="ATA"] <- "Oceania"
regionmapping$Region[regionmapping$iso=="BES"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="BLM"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="CUW"] <- "Rest Centr America"
regionmapping$Region[regionmapping$iso=="GGY"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="GRL"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="IMN"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="JEY"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso=="MAF"] <- "Rest Centr America"
regionmapping$Region[regionmapping$iso=="MNE"] <- "Eastern Europe"
regionmapping$Region[regionmapping$iso == "SRB"] <- "Eastern Europe"
regionmapping$Region[regionmapping$iso == "SSD"] <- "Eastern Africa"
regionmapping$Region[regionmapping$iso == "PSE"] <- "Middle East"
regionmapping$Region[regionmapping$iso == "SXM"] <- "Rest Centr America"
regionmapping$Region[regionmapping$iso == "ALA"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "ATA"] <- "Oceania"
regionmapping$Region[regionmapping$iso == "BES"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "BLM"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "CUW"] <- "Rest Centr America"
regionmapping$Region[regionmapping$iso == "GGY"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "GRL"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "IMN"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "JEY"] <- "OECD Europe"
regionmapping$Region[regionmapping$iso == "MAF"] <- "Rest Centr America"
regionmapping$Region[regionmapping$iso == "MNE"] <- "Eastern Europe"

# read in data
if (subtype=="co2prices") {
data <- read.csv("IMAGE_data_26112020_co2price.csv", stringsAsFactors=F)
} else if (subtype=="bioenergy") {
data <- read.csv("IMAGE_data_26112020_bioenergy.csv", stringsAsFactors=F)
if (subtype == "co2prices") {
data <- read.csv("IMAGE_data_26112020_co2price.csv", stringsAsFactors = FALSE)
} else if (subtype == "bioenergy") {
data <- read.csv("IMAGE_data_26112020_bioenergy.csv", stringsAsFactors = FALSE)
} else {
stop("Specify subtype to be read in: co2prices or bioenergy (total bioenergy demand)")
}

# region name correction
data$Region[data$Region=="Russia+"] <- "Russia +"
data$Region[data$Region=="China+"] <- "China +"
data$Region[data$Region=="Rest C. America"] <- "Rest Centr America"
data$Region[data$Region=="Rest S. America"] <- "Rest South America"
data$Region[data$Region=="Rest S. Asia"] <- "Rest South Asia"
data$Region[data$Region=="Rest S. Africa"] <- "Rest Southern Africa"
data$Region[data$Region == "Russia+"] <- "Russia +"
data$Region[data$Region == "China+"] <- "China +"
data$Region[data$Region == "Rest C. America"] <- "Rest Centr America"
data$Region[data$Region == "Rest S. America"] <- "Rest South America"
data$Region[data$Region == "Rest S. Asia"] <- "Rest South Asia"
data$Region[data$Region == "Rest S. Africa"] <- "Rest Southern Africa"

data <- merge(data, regionmapping, "Region")
data <- data.frame(scenario=data$Scenario,year=data$Year,value=data$Value,iso=data$iso, stringsAsFactors=F)
data$year <- paste0("y",data$year)
data <- data.frame(scenario = data$Scenario,
year = data$Year,
value = data$Value,
iso = data$iso,
stringsAsFactors = FALSE)
data$year <- paste0("y", data$year)
x <- collapseNames(as.magpie(data))
if (subtype=="bioenergy"){

if (subtype == "bioenergy") {
# Disaggregate regional bioenergy to country values using population weight
# country population in SSP2
pop <- calcOutput("Population",
naming = "indicator.scenario",
extension2150 = "none",
aggregate = FALSE,
years = paste0("y", c(2005, seq(2010, 2100, by = 10))),
round = 6)
pop <- collapseNames(pop[,,"SSP2"])
pop <- collapseNames(calcOutput("Population",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop "collapseNames" entirely :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that would lead to different output:

> waldo::compare(a,b)
`names(dimnames([email protected]))`: "iso" "year" "scenario.variable"
`names(dimnames([email protected]))`: "iso" "year" "scenario"         

`dimnames([email protected])$scenario.variable` is a character vector ('SSP2.pop_SSP2', 'SSP2_SPA2_26I_D.pop_SSP2')
`dimnames([email protected])$scenario.variable` is absent

`dimnames([email protected])$scenario` is absent
`dimnames([email protected])$scenario` is a character vector ('SSP2', 'SSP2_SPA2_26I_D')

comment(old) vs comment(new)
- " origin: readSource(type = \"S4Nproject_input\", subtype = \"bioenergy\", convert = \"onlycorrect\") (madrat 3.15.3 | mrland 0.65.1)"
+ " origin: readSource(type = \"S4Nproject_input\", subtype = \"bioenergy\", convert = \"onlycorrect\") (madrat 3.15.3 | mrland 0.65.0)"
- " creation date: Mon Nov 18 16:29:58 2024"
+ " creation date: Mon Nov 18 14:49:35 2024"

`names(dimnames(old))`: "iso" "year" "scenario.variable"
`names(dimnames(new))`: "iso" "year" "scenario"         

`dimnames(old)$scenario.variable` is a character vector ('SSP2.pop_SSP2', 'SSP2_SPA2_26I_D.pop_SSP2')
`dimnames(new)$scenario.variable` is absent

`dimnames(old)$scenario` is absent
`dimnames(new)$scenario` is a character vector ('SSP2', 'SSP2_SPA2_26I_D')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run the comparison with scenario = "SSP2"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see... collapseNames deletes the data name, if only 1 data dimension exists.

scenario = "SSP2",
extension2150 = "none",
aggregate = FALSE,
years = paste0("y", c(2005, seq(2010, 2100, by = 10))),
round = 6))

# regional population
pop_reg <- toolAggregate(pop, rel=regionmapping, weight=NULL)
pop_reg <- as.data.frame(pop_reg)
pop_reg <- data.frame(Region=pop_reg$Region, year=as.character(pop_reg$Year), Value=pop_reg$Value, stringsAsFactors=F)
pop_reg <- merge(pop_reg, regionmapping, "Region")
pop_reg <- collapseNames(as.magpie(data.frame(iso=pop_reg$iso, year=pop_reg$year, Value=pop_reg$Value)))
popReg <- toolAggregate(pop, rel = regionmapping, weight = NULL)
popReg <- as.data.frame(popReg)
popReg <- data.frame(Region = popReg$Region,
year = as.character(popReg$Year),
Value = popReg$Value,
stringsAsFactors = FALSE)
popReg <- merge(popReg, regionmapping, "Region")
popReg <- collapseNames(as.magpie(data.frame(iso = popReg$iso, year = popReg$year, Value = popReg$Value)))

# country population share
pop_shr <- pop/pop_reg

# bioenergy demand weighted by population
x <- x * pop/pop_reg
x <- x * pop / popReg
Comment on lines +79 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment, but can't one simply do x <- toolAggregate(x, rel = regionmapping, from = "Region", to = "iso", weight = pop) to disaggregate x?
(I didn't test this!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, but I didn't want to change more than necessary (to fix the warning + linter), so I don't need to test more than necessary 😉

}

return(x)
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MadRaT land data package

R package **mrland**, version **0.65.0**
R package **mrland**, version **0.65.1**

[![CRAN status](https://www.r-pkg.org/badges/version/mrland)](https://cran.r-project.org/package=mrland) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.3822083.svg)](https://doi.org/10.5281/zenodo.3822083) [![R build status](https://github.com/pik-piam/mrland/workflows/check/badge.svg)](https://github.com/pik-piam/mrland/actions) [![codecov](https://codecov.io/gh/pik-piam/mrland/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrland) [![r-universe](https://pik-piam.r-universe.dev/badges/mrland)](https://pik-piam.r-universe.dev/builds)

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Jan Philipp Dietrich <dietrich@pi

To cite package **mrland** in publications use:

Dietrich J, Mishra A, Weindl I, Bodirsky B, Wang X, Baumstark L, Kreidenweis U, Klein D, Steinmetz N, Chen D, Humpenoeder F, von Jeetze P, Wirth S, Beier F, Hoetten D, Sauer P, Tommey J (2024). _mrland: MadRaT land data package_. doi: 10.5281/zenodo.3822083 (URL: https://doi.org/10.5281/zenodo.3822083), R package version 0.65.0, <URL: https://github.com/pik-piam/mrland>.
Dietrich J, Mishra A, Weindl I, Bodirsky B, Wang X, Baumstark L, Kreidenweis U, Klein D, Steinmetz N, Chen D, Humpenoeder F, von Jeetze P, Wirth S, Beier F, Hoetten D, Sauer P, Tommey J (2024). _mrland: MadRaT land data package_. doi:10.5281/zenodo.3822083 <https://doi.org/10.5281/zenodo.3822083>, R package version 0.65.1, <https://github.com/pik-piam/mrland>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,8 +48,8 @@ A BibTeX entry for LaTeX users is
title = {mrland: MadRaT land data package},
author = {Jan Philipp Dietrich and Abhijeet Mishra and Isabelle Weindl and Benjamin Leon Bodirsky and Xiaoxi Wang and Lavinia Baumstark and Ulrich Kreidenweis and David Klein and Nele Steinmetz and David Chen and Florian Humpenoeder and Patrick {von Jeetze} and Stephen Wirth and Felicitas Beier and David Hoetten and Pascal Sauer and Jake Tommey},
year = {2024},
note = {R package version 0.65.0},
doi = {10.5281/zenodo.3822083},
note = {R package version 0.65.1},
url = {https://github.com/pik-piam/mrland},
doi = {10.5281/zenodo.3822083},
}
```
7 changes: 4 additions & 3 deletions man/readS4Nproject_input.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.