Skip to content

Commit

Permalink
Accurate report template for bs and hs.
Browse files Browse the repository at this point in the history
  • Loading branch information
robinhasse committed May 30, 2024
1 parent 47afa03 commit 2b1251a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ message: If you use this software, please cite it using the metadata from this f
type: software
title: 'reportbrick: Reporting package for BRICK'
version: 0.4.1
date-released: '2024-05-28'
date-released: '2024-05-30'
abstract: This package contains BRICK-specific routines to report model results. The
main functionality is to generate a mif-file from a given BRICK model run folder.
authors:
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: reportbrick
Title: Reporting package for BRICK
Version: 0.4.1
Date: 2024-05-28
Date: 2024-05-30
Authors@R: c(
person("Robin", "Hasse", , "[email protected]",
role = c("aut", "cre"),
Expand Down
27 changes: 10 additions & 17 deletions R/convGDX2MIF.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#'
#' @importFrom magclass mbind add_dimension write.report getSets<-
#' @importFrom utils capture.output
#' @importFrom gamstransfer Container
#' @export

convGDX2MIF <- function(gdx,
Expand All @@ -34,16 +35,10 @@ convGDX2MIF <- function(gdx,
}

brickSets <- readBrickSets(tmpl)
setElements <- .findInconsistenSetElements(brickSets, gdx)
if (!is.null(setElements[["surplus"]])) {
warning("The following set elements are mapped in the reporting template ",
"but not found in the gdx:\n ",
paste(capture.output(setElements[["surplus"]]), collapse = "\n "))
}
if (!is.null(setElements[["missing"]])) {
stop("The following set elements are found in the gdx but not mapped in ",
"the reporting template:\n ",
paste(capture.output(setElements[["missing"]]), collapse = "\n "))
inconsistencies <- .findInconsistenSetElements(brickSets, gdx)
if (!is.null(inconsistencies)) {
stop("The reporting template is not consistent with the gdx:\n ",
paste(capture.output(inconsistencies), collapse = "\n "))
}

# central object containing all output data
Expand Down Expand Up @@ -100,21 +95,19 @@ convGDX2MIF <- function(gdx,
#' @returns named list of missing and surplus sets elements

.findInconsistenSetElements <- function(brickSets, gdx) {
out <- do.call(rbind, lapply(names(brickSets), function(s) {
elementsGdx <- as.character(readGdxSymbol(gdx, s, asMagpie = FALSE)[[1]])
m <- Container$new(gdx)
setsGdx <- setNames(m$getSymbols(names(brickSets)), names(brickSets))
do.call(rbind, lapply(names(brickSets), function(s) {
elementsGdx <- as.character(setsGdx[[s]]$records[[1]])
elementsMap <- names(brickSets[[s]][["elements"]])
inconsistencies <- list(missing = setdiff(elementsGdx, elementsMap),
surplus = setdiff(elementsMap, elementsGdx))
do.call(rbind, lapply(names(inconsistencies), function(i) {
if (length(inconsistencies[[i]]) > 0) {
data.frame(set = s, inconsistency = i, element = inconsistencies[[i]])
data.frame(set = s, element = inconsistencies[[i]], inconsistency = i)
} else {
NULL
}
}))
}))
if (is.null(out)) {
return(NULL)
}
split(out[c("set", "element")], out[["inconsistency"]])
}
26 changes: 21 additions & 5 deletions R/readBrickSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,33 @@ readBrickSets <- function(tmpl) {
}
message("Read reporting template: ", file)

# duplicate aliases
# duplicate aliases and childs
brickSetsExplicit <- list()
for (dimName in names(brickSets)) {
dim <- brickSets[dimName]
aliases <- dim[[1]][["alias"]]
dim[[1]][["alias"]] <- NULL
aliasDims <- rep(dim, length(aliases))
names(aliasDims) <- aliases
brickSetsExplicit <- c(brickSetsExplicit, c(as.list(dim), aliasDims))
childs <- dim[[1]][["child"]]
dim[[1]][c("alias", "child")] <- NULL
aliasDims <- stats::setNames(rep(dim, length(aliases)), aliases)
childDims <- list()
for (childName in names(childs)) {
childDef <- childs[[childName]]
childDims[[childName]] <- list()
for (key in names(childDef)) {
if (!key %in% names(dim[[1]])) {
stop("Problem with reporting template ", file,
" in dimension '", dimName, "': ",
"The child '", childName, "' uses the unkown key '", key, "'.")
}
childDims[[childName]][[key]] <- dim[[1]][[key]][childDef[[key]]]
}
}
brickSetsExplicit <- c(brickSetsExplicit, c(as.list(dim),
aliasDims,
childDims))
}


attr(brickSetsExplicit, "file") <- file
return(brickSetsExplicit)
}
Expand Down
10 changes: 8 additions & 2 deletions inst/extdata/sectoral/brickSets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

## Building shell ====
bsr:
alias: bs
elements:
low: Low efficiency
med: Medium efficiency
Expand All @@ -12,11 +11,14 @@ bsr:
subsets:
all: [low, med, high]
all0: [low, med, high, "0"]
child:
bs:
elements: [low, med, high]
subsets: all


## Heating system ====
hsr:
alias: hs
elements:
biom: Biomass heater
dihe: District heating
Expand All @@ -30,6 +32,10 @@ hsr:
all: [biom, dihe, ehp1, reel, gabo, libo, sobo]
all0: [biom, dihe, ehp1, reel, gabo, libo, sobo, "0"]
elec: [ehp1, reel]
child:
hs:
elements: [biom, dihe, ehp1, reel, gabo, libo, sobo]
subsets: [all, elec]


## Energy carrier ====
Expand Down
5 changes: 4 additions & 1 deletion inst/extdata/sectoral/brickSets_ignoreShell_onlyRes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
## Building shell ====
# Only low for state and `0` for renovation
bsr:
alias: bs
elements:
low: Low efficiency
"0": No change
subsets:
all: low
all0: [low, "0"]
child:
bs:
elements: low
subsets: all


## Building type ====
Expand Down

0 comments on commit 2b1251a

Please sign in to comment.