Skip to content

Commit

Permalink
update mrtutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
David Chen committed Mar 7, 2022
1 parent dadf8a7 commit 12deccc
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '514404'
ValidationKey: '533624'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
Expand Down
2 changes: 1 addition & 1 deletion .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "mrtutorial: Tutorial Package For Madrat Package Library",
"version": "0.2.7",
"version": "0.2.8",
"description": "<p>Example package of mr- world libraries, in conjunction with MAgPIE MADRat tutorial. For more information please see: https://github.com/magpiemodel/tutorials/tree/master/madrat .<\/p>",
"creators": [
{
Expand Down
9 changes: 6 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: mrtutorial
Type: Package
Title: Tutorial Package For Madrat Package Library
Date: 2022-03-01
Version: 0.2.7
Date: 2022-03-07
Version: 0.2.8
Authors@R:
person("David Meng-Chuen", "Chen", email = "[email protected]", role = c("aut","cre"))
Description: Example package of mr- world libraries, in conjunction with MAgPIE MADRat tutorial. For more information please see: https://github.com/magpiemodel/tutorials/tree/master/madrat .
Expand All @@ -20,6 +20,9 @@ Imports:
countrycode,
lucode2,
reshape2,
magpiesets
magpiesets,
dplyr,
tidyr,
rlang
Suggests:
covr
6 changes: 5 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ importFrom(WDI,WDI)
importFrom(WDI,WDIcache)
importFrom(WDI,WDIsearch)
importFrom(countrycode,countrycode)
importFrom(dplyr,"%>%")
importFrom(dplyr,filter)
importFrom(dplyr,select)
importFrom(lucode2,path)
importFrom(madrat,calcOutput)
importFrom(madrat,getConfig)
Expand All @@ -27,5 +30,6 @@ importFrom(magclass,getRegions)
importFrom(magclass,getSets)
importFrom(magclass,getYears)
importFrom(magpiesets,findset)
importFrom(reshape2,melt)
importFrom(rlang,.data)
importFrom(tidyr,pivot_longer)
importFrom(utils,read.csv)
6 changes: 5 additions & 1 deletion R/calcAgGDP.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
calcAgGDP <- function(){

gdp <- readSource("TutorialWDI", subtype="NY.GDP.MKTP.CD")

ag_gdp <- readSource("TutorialWDI", subtype="NV.AGR.TOTL.CD")

x <- ag_gdp/gdp
Expand All @@ -24,9 +25,12 @@ calcAgGDP <- function(){
## some division by 0 happening
x[is.na(x)] <- 0

#population weight
weight <- readSource("TutorialWDI", subtype = "SP.POP.TOTL")

return(list(
x=x,
weight=NULL,
weight=weight,
unit="share",
description="Ag GDP as percentage of total GDP"))
}
66 changes: 24 additions & 42 deletions R/convertTutorialWDI.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,28 @@
#' @importFrom madrat toolCountryFill


convertTutorialWDI<-function(x,subtype){
WDI_data <- WDI::WDI_data

# changing scale of indicators
if (subtype %in% c("SP.POP.TOTL",
"NY.GDP.MKTP.PP.KD",
"NV.AGR.TOTL.CD",
"NY.GDP.MKTP.PP.CD",
"NY.GDP.MKTP.CD",
"NY.GDP.MKTP.KD",
"NY.GDP.MKTP.KN")) {
x <- x/1000000

#Kosovo added to Serbia
x["RS",,] <- dimSums(x[c("RS","XK"),,],dim=1,na.rm=T)
}else if (subtype %in% WDI_data$series[,"indicator"]){

vcat("Warning: Kosovo left out of conversion and has differing population values from FAO", verbosity=2)
x <- x
}else {
stop("subtype does not exist in the dataset!")
}
y <- x

## Channel Islands add to JEY
JG <- "JEY"
names(JG) <- "JG"
getCells(y)<-countrycode::countrycode(getCells(y),"iso2c","iso3c", custom_match = JG)
y<-y[!is.na(getCells(y)),,]
y<-clean_magpie(y)

y<-y["ANT",,,invert=TRUE]

y<-toolCountryFill(y,fill = 0)
y[is.na(y)]<-0
y <- y[,sort(getYears(y)),]

#remove years which only contain 0s as entries
y <- y[,!apply(y,2,function(x) return(all(x==0))),]

y<-y[,sort(getYears(y)),]
return(y)
convertTutorialWDI<-function(x, subtype){

# change scale of absolute indicators, MAgPIE runs in Million scale for Pop and GDP drivers
if (subtype %in% c("NY.GDP.MKTP.CD",
"SP.POP.TOTL",
"NV.AGR.TOTL.CD")){

out <- x/1e6 } else

if (subtype == "SL.AGR.EMPL.ZS"){
# don't change for percentage of ag employment

out <- x } else{

stop("subtype does not exist in the dataset!")}

# toolCountryFill fills in missing countries - 249 required for aggregation

x<-toolCountryFill(out,fill = 0)

x[is.na(x)] <- 0 #remove NAs


return(x)
}
33 changes: 33 additions & 0 deletions R/fullTUTORIALVALIDATION.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#' fullTUTORIALVALIDATION
#'
#' Function that produces the complete validation data set used for evaluation of MAgPIE outputs
#'
#' @param rev data revision which should be used as input (positive numeric).
#' @author Jan Philipp Dietrich, Kristine Karstens
#' @seealso
#' \code{\link{readSource}}, \code{\link{getCalculations}}, \code{\link{calcOutput}}
#' @examples
#' \dontrun{
#' retrieveData("VALIDATION")
#' }
#' @importFrom madrat getConfig calcOutput
#' @importFrom lucode2 path

fullTUTORIALVALIDATION <- function(rev = 0.1) {

# all validation data regional aggregations happens here

# exitsting "validation.mif" file is loaded if all functions are set to append=TRUE
# Please put the validation.mif into your output folder.
# Use retrieveData("VALIDATION") to create a .tgz

file.copy(lucode2::path(paste0(getConfig("mainfolder"), "/output/"), "validation.mif"),
lucode2::path(getConfig("outputfolder"), "validation.mif"))

valfile <- "validation.mif"

# Add your validation scripts here
calcOutput(type = "ValidPopulation", datasource = "WDI", aggregate = "REG+GLO",
file = valfile, append = TRUE, na_warning = FALSE, try = TRUE)

}
34 changes: 21 additions & 13 deletions R/readTutorialWDI.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,40 @@
#'
#'
#' @param subtype Type of WDI data that should be read. Please use the
#' worldbank indicator abbreviation. Available types are e.g.: \itemize{ \item
#' \code{SP.POP.TOTL}: Population, total \item \code{NY.GDP.MKTP.PP.KD}: GDP,
#' PPP (constant 2011 international Dollar) \item \code{NY.GDP.MKTP.PP.CD}:
#' GDP, PPP (current international Dollar) \item \code{NY.GDP.MKTP.CD}: GDP MER
#' (current USDollar) \item \code{NY.GDP.MKTP.KD}: GDP MER (constant 2005
#' USDollar) \item \code{NY.GDP.MKTP.KN}: GDP LCU (constant LCU)
#' \item \code{SP.URB.TOTL.IN.ZS}: Urban population (peccentage of total)
#' \item \code{ NV.AGR.TOTL.CD}: GDP (current USD)}
#' worldbank indicator abbreviation. Available types are e.g.: \itemize{
#' \item \code{SP.POP.TOTL}: Population, total
#' \item \code{NY.GDP.MKTP.PP.CD}: GDP, PPP (current international Dollar)
#' \item \code{SL.AGR.EMPL.ZS}: Employment in agriculture as % of total employment
#' \item \code{NV.AGR.TOTL.CD}: Agricultural GDP (current international Dollar) }
#' @return magpie object of the WDI data
#' @author David Chen
#' @seealso \code{\link{readSource}} \code{\link{downloadTutorialWDI}}
#' @examples
#'
#' \dontrun{ a <- readSource(type="WDI",subtype="SP.POP.TOTL")
#' }
#' @importFrom reshape2 melt
#' @importFrom dplyr select filter %>%
#' @importFrom tidyr pivot_longer
#' @importFrom magclass as.magpie
#' @importFrom rlang .data

readTutorialWDI<-function(subtype){

load("WDI.rda")
wdi$country <- NULL
wdi <- as.magpie(melt(wdi,id.vars = c("iso2c","year")),
spatial=1,temporal=2,tidy=TRUE,replacement =".")

#convert the iso2c to iso3c using madrat tool
wdi$iso2c <- toolCountry2isocode(wdi$iso2c)

wdi <- wdi %>%
dplyr::select(!.data$country) %>%
filter(!is.na(.data$iso2c)) %>%
pivot_longer(cols=3:6) %>%
as.magpie(spatial = 1, temporal = 2, replacement =".")

wdi <- wdi[,,subtype]

getNames(wdi) <- gsub("\\.", "_", getNames(wdi)) ## Replace . with _
getNames(wdi) <- gsub("\\.", "_", getNames(wdi))
# Replacement of . with _ in dimension is also done automatically in as.magpie()

return(wdi)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tutorial Package For Madrat Package Library

R package **mrtutorial**, version **0.2.7**
R package **mrtutorial**, version **0.2.8**

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

Expand Down Expand Up @@ -38,7 +38,7 @@ In case of questions / problems please contact David Meng-Chuen Chen <david.chen

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

Chen D (2022). _mrtutorial: Tutorial Package For Madrat Package Library_. R package version 0.2.7.
Chen D (2022). _mrtutorial: Tutorial Package For Madrat Package Library_. R package version 0.2.8.

A BibTeX entry for LaTeX users is

Expand All @@ -47,6 +47,6 @@ A BibTeX entry for LaTeX users is
title = {mrtutorial: Tutorial Package For Madrat Package Library},
author = {David Meng-Chuen Chen},
year = {2022},
note = {R package version 0.2.7},
note = {R package version 0.2.8},
}
```
25 changes: 25 additions & 0 deletions man/fullTUTORIALVALIDATION.Rd

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

13 changes: 5 additions & 8 deletions man/readTutorialWDI.Rd

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

0 comments on commit 12deccc

Please sign in to comment.