Skip to content

Commit

Permalink
weight bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
caviddhen committed Jul 4, 2024
1 parent 0a34d01 commit 47f8335
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '11825352'
ValidationKey: '11944800'
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 CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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.59.4
version: 0.60.0
date-released: '2024-07-04'
abstract: The package provides land related data via the madrat framework.
authors:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: mrland
Title: MadRaT land data package
Version: 0.59.4
Version: 0.60.0
Date: 2024-07-04
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre")),
Expand Down
2 changes: 1 addition & 1 deletion R/calcGTAPTrade.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ calcGTAPTrade <- function(subtype = NULL, bilateral = FALSE) {

out <- toolAggregate(out, rel = sectorMapping,
from = "gtap", to = "magpie", dim = 3,
weight = w[getItems(out, dim = 1), getYears(out), ],
weight = w[getItems(out, dim = 1), getYears(out), ] + 10^-10,
partrel = TRUE)


Expand Down
104 changes: 53 additions & 51 deletions R/calcProcessing_shares.R
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
#' @importFrom magclass setNames getNames

calcProcessing_shares<-function(){
massbalance<-calcOutput("FAOmassbalance",aggregate = F)

ksd<-findset("ksd")
kpr<-findset("kpr")

kprocessing<-findset("processing20")
mb_reduced<-dimSums(massbalance[,,"dm"],dim=c(3.3))
kmb<-paste("X",kpr,sep="")

production_estimated<-dimSums(mb_reduced[,,"production_estimated"][,,ksd],dim=c(3.2))
convmatrix<-add_dimension(x = production_estimated,dim = 3.2,add = "primary",nm = kmb)

convmatrix[,,]<-0


out<-list(x=convmatrix,weight=convmatrix)

calcshare<-function(from,to){
tmp <- dimSums(mb_reduced[,,from][,,kpr],dim = c(3.2))
tmpweight<-dimSums(tmp,dim=3.1)
tmp<-tmp/tmpweight
out$x[,,to]<-setNames(tmp,paste0("X",getNames(tmp)))
out$weight[,,to]<-setNames(dimSums(tmpweight,dim=3.1),paste0("X",getNames(dimSums(tmpweight,dim=3.1))))
calcProcessing_shares <- function() { # nolint
massbalance <- calcOutput("FAOmassbalance", aggregate = FALSE)

ksd <- findset("ksd")
kpr <- findset("kpr")

mbReduced <- dimSums(massbalance[, , "dm"], dim = c(3.3))
kmb <- paste("X", kpr, sep = "")

productionEstimated <- dimSums(mbReduced[, , "production_estimated"][, , ksd], dim = c(3.2))
convmatrix <- add_dimension(x = productionEstimated, dim = 3.2, add = "primary", nm = kmb)

convmatrix[, , ] <- 0


out <- list(x = convmatrix, weight = convmatrix)

calcshare <- function(from, to) {
tmp <- dimSums(mbReduced[, , from][, , kpr], dim = c(3.2))
tmpweight <- dimSums(tmp, dim = 3.1)
tmp <- tmp / tmpweight
out$x[, , to] <- setNames(tmp, paste0("X", getNames(tmp)))
out$weight[, , to] <- setNames(dimSums(tmpweight, dim = 3.1), paste0("X", getNames(dimSums(tmpweight, dim = 3.1))))
return(out)
}
out<-calcshare(c("alcohol1","alcohol2","alcohol3", "alcohol4"),c("alcohol"))
out<-calcshare(c("brewers_grain1","distillers_grain1"),c("distillers_grain"))
out<-calcshare(c("brans1"),c("brans"))
out<-calcshare(c("branoil1","oil1","oil2"),c("oils"))
out<-calcshare(c("ethanol1"),c("ethanol"))
out<-calcshare(c("molasses1"),c("molasses"))
out<-calcshare(c("sugar1"),c("sugar"))
out<-calcshare(c("oilcakes1"),c("oilcakes"))
out$x[,,"scp"][,,"Xbegr"] <- 0.5
out$x[,,"scp"][,,"Xsugr_cane"] <- 0.5
out$x[,,"scp"][,,"Xfoddr"] <- 0
out$x[,,"fibres"][,,"Xcottn_pro"] <- 1
out$weight[,,"scp"][,,"Xbegr"] <-1
out$weight[,,"scp"][,,"Xsugr_cane"] <- 1
out$weight[,,"scp"][,,"Xfoddr"] <- 1
out$weight[,,"fibres"][,,"Xcottn_pro"]<-1
out$x[is.na(out$x)]<-0
out$weight[is.na(out$weight)]<-0
getNames(out$x, dim=2)<-substring(getNames(out$x, dim=2),2)
getNames(out$weight, dim=2)<-substring(getNames(out$weight, dim=2),2)
#add years beyond 2010
out <- calcshare(c("alcohol1", "alcohol2", "alcohol3", "alcohol4"), c("alcohol"))
out <- calcshare(c("brewers_grain1", "distillers_grain1"), c("distillers_grain"))
out <- calcshare(c("brans1"), c("brans"))
out <- calcshare(c("branoil1", "oil1", "oil2"), c("oils"))
out <- calcshare(c("ethanol1"), c("ethanol"))
out <- calcshare(c("molasses1"), c("molasses"))
out <- calcshare(c("sugar1"), c("sugar"))
out <- calcshare(c("oilcakes1"), c("oilcakes"))

out$x[, , "scp"][, , "Xbegr"] <- 0.5
out$x[, , "scp"][, , "Xsugr_cane"] <- 0.5
out$x[, , "scp"][, , "Xfoddr"] <- 0
out$x[, , "fibres"][, , "Xcottn_pro"] <- 1
out$weight[, , "scp"][, , "Xbegr"] <- 1
out$weight[, , "scp"][, , "Xsugr_cane"] <- 1
out$weight[, , "scp"][, , "Xfoddr"] <- 1
out$weight[, , "fibres"][, , "Xcottn_pro"] <- 1

out$x[is.na(out$x)] <- 0
out$weight[is.na(out$weight)] <- 0

getNames(out$x, dim = 2) <- substring(getNames(out$x, dim = 2), 2)
getNames(out$weight, dim = 2) <- substring(getNames(out$weight, dim = 2), 2)

# add years beyond 2010
out$x <- toolHoldConstantBeyondEnd(out$x)
out$weight <- toolHoldConstantBeyondEnd(out$weight)

return(list(x=out$x,weight=out$weight,unit="share",description="Share of processed products coming from different primary products"))

out$weight <- out$weight + 10^-10

return(list(x = out$x, weight = out$weight, unit = "share",
description = "Share of processed products coming from different primary products"))
}
1 change: 1 addition & 0 deletions R/calcTradeSelfSuff.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ calcTradeSelfSuff <- function() {

out <- toolHoldConstantBeyondEnd(selfSuff)
weight <- toolHoldConstantBeyondEnd(weight)
weight <- weight + 10^-10

return(list(x = out,
weight = weight,
Expand Down
2 changes: 1 addition & 1 deletion R/calcTradeTariff.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ calcTradeTariff<- function(gtap_version = "GTAP9", type_tariff = "total", bilate
missing <- setdiff(kTrade, getNames(weight))
weight <- add_columns(weight, dim = 3.1, addnm = missing)
weight[, , missing] <- 1
weight <- weight[, , setdiff(getNames(weight), kTrade), invert = TRUE]
weight <- weight[, , setdiff(getNames(weight), kTrade), invert = TRUE] + 10^-10


description <- paste0(type_tariff, "trade tariff")
Expand Down
4 changes: 0 additions & 4 deletions R/fullMAGPIE.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ fullMAGPIE <- function(rev = numeric_version("0.1"), dev = "") {
aggregate = superregion)
calcOutput("TradeTariff", round = 4, file = "f21_trade_tariff.cs3",
aggregate = superregion)
calcOutput("TradeTariff", type_tariff = "export", round = 4, file = "f21_trade_tariff_export.cs3",
aggregate = superregion)
calcOutput("TradeTariff", type_tariff = "import", round = 4, file = "f21_trade_tariff_import.cs3",
aggregate = superregion)
calcOutput("TradeMargin", bilateral = TRUE, round = 4, file = "f21_trade_margin_bilat.cs5",
aggregate = TRUE)
calcOutput("TradeTariff", bilateral = TRUE, round = 4, file = "f21_trade_tariff_bilat.cs5",
Expand Down
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.59.4**
R package **mrland**, version **0.60.0**

[![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 (2024). _mrland: MadRaT land data package_. doi:10.5281/zenodo.3822083 <https://doi.org/10.5281/zenodo.3822083>, R package version 0.59.4, <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 (2024). _mrland: MadRaT land data package_. doi: 10.5281/zenodo.3822083 (URL: https://doi.org/10.5281/zenodo.3822083), R package version 0.60.0, <URL: 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},
year = {2024},
note = {R package version 0.59.4},
url = {https://github.com/pik-piam/mrland},
note = {R package version 0.60.0},
doi = {10.5281/zenodo.3822083},
url = {https://github.com/pik-piam/mrland},
}
```

0 comments on commit 47f8335

Please sign in to comment.