From c19ba35913cd165f5500623bcb2b5263e2633622 Mon Sep 17 00:00:00 2001 From: robinhasse Date: Thu, 10 Oct 2024 08:45:36 +0200 Subject: [PATCH] Handle missing periods from report functions --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- NAMESPACE | 2 ++ R/convGDX2MIF.R | 10 +++++----- R/extendPeriods.R | 19 +++++++++++++++++++ README.md | 6 +++--- man/extendPeriods.Rd | 24 ++++++++++++++++++++++++ 8 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 R/extendPeriods.R create mode 100644 man/extendPeriods.Rd diff --git a/.buildlibrary b/.buildlibrary index e7d04a0..743d18b 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '1400280' +ValidationKey: '1420426' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 929eb37..d77cc1a 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -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: 'reportbrick: Reporting package for BRICK' -version: 0.7.0 -date-released: '2024-10-08' +version: 0.7.1 +date-released: '2024-10-10' 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: diff --git a/DESCRIPTION b/DESCRIPTION index 3b67807..785bf34 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: reportbrick Title: Reporting package for BRICK -Version: 0.7.0 -Date: 2024-10-08 +Version: 0.7.1 +Date: 2024-10-10 Authors@R: c( person("Robin", "Hasse", , "robin.hasse@pik-potsdam.de", role = c("aut", "cre"), diff --git a/NAMESPACE b/NAMESPACE index c6c5f02..72a5abc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -34,9 +34,11 @@ importFrom(magclass,dimSums) importFrom(magclass,getItems) importFrom(magclass,getNames) importFrom(magclass,getSets) +importFrom(magclass,getYears) importFrom(magclass,mbind) importFrom(magclass,mselect) importFrom(magclass,ndim) +importFrom(magclass,new.magpie) importFrom(magclass,setNames) importFrom(magclass,write.report) importFrom(piamutils,getSystemFile) diff --git a/R/convGDX2MIF.R b/R/convGDX2MIF.R index 9caa182..cd017ca 100644 --- a/R/convGDX2MIF.R +++ b/R/convGDX2MIF.R @@ -50,23 +50,23 @@ convGDX2MIF <- function(gdx, ## Stock ==== message("running reportBuildingStock ...") - output <- mbind(output, reportBuildingStock(gdx, brickSets, silent = silent)[, t, ]) + output <- mbind(output, extendPeriods(reportBuildingStock(gdx, brickSets, silent = silent), t)) ## Construction ==== message("running reportConstruction ...") - output <- mbind(output, reportConstruction(gdx, brickSets, silent = silent)[, t, ]) + output <- mbind(output, extendPeriods(reportConstruction(gdx, brickSets, silent = silent), t)) ## Renovation ==== message("running reportRenovation ...") - output <- mbind(output, reportRenovation(gdx, brickSets, silent = silent)[, t, ]) + output <- mbind(output, extendPeriods(reportRenovation(gdx, brickSets, silent = silent), t)) ## Demolition ==== message("running reportDemolition ...") - output <- mbind(output, reportDemolition(gdx, brickSets, silent = silent)[, t, ]) + output <- mbind(output, extendPeriods(reportDemolition(gdx, brickSets, silent = silent), t)) ## Energy ==== message("running reportEnergy ...") - output <- mbind(output, reportEnergy(gdx, brickSets, silent = silent)[, t, ]) + output <- mbind(output, extendPeriods(reportEnergy(gdx, brickSets, silent = silent), t)) # FINISH --------------------------------------------------------------------- diff --git a/R/extendPeriods.R b/R/extendPeriods.R new file mode 100644 index 0000000..e9a1182 --- /dev/null +++ b/R/extendPeriods.R @@ -0,0 +1,19 @@ +#' Crop MagPIE object to given periods and fill missing periods +#' +#' @param x MagPIE object +#' @param periods numeric vector with target periods +#' @param fill value to fill missing period values +#' @returns MagPIE object with given periods in temporal dimension +#' +#' @author Robin Hasse +#' +#' @importFrom magclass getYears new.magpie getItems getNames + +extendPeriods <- function(x, periods, fill = NA) { + r <- getItems(x, dim = 1) + n <- getNames(x) + t <- intersect(getYears(x, as.integer = TRUE), periods) + out <- new.magpie(cells_and_regions = r, years = periods, names = n, fill = fill) + out[r, t, ] <- x[r, t, ] + return(out) +} diff --git a/README.md b/README.md index cb2ffd2..f1b4721 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Reporting package for BRICK -R package **reportbrick**, version **0.7.0** +R package **reportbrick**, version **0.7.1** [![CRAN status](https://www.r-pkg.org/badges/version/reportbrick)](https://cran.r-project.org/package=reportbrick) [![R build status](https://github.com/pik-piam/reportbrick/workflows/check/badge.svg)](https://github.com/pik-piam/reportbrick/actions) [![codecov](https://codecov.io/gh/pik-piam/reportbrick/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/reportbrick) [![r-universe](https://pik-piam.r-universe.dev/badges/reportbrick)](https://pik-piam.r-universe.dev/builds) @@ -38,7 +38,7 @@ In case of questions / problems please contact Robin Hasse . +Hasse R, Rosemann R (2024). _reportbrick: Reporting package for BRICK_. R package version 0.7.1, . A BibTeX entry for LaTeX users is @@ -47,7 +47,7 @@ A BibTeX entry for LaTeX users is title = {reportbrick: Reporting package for BRICK}, author = {Robin Hasse and Ricarda Rosemann}, year = {2024}, - note = {R package version 0.7.0}, + note = {R package version 0.7.1}, url = {https://github.com/pik-piam/reportbrick}, } ``` diff --git a/man/extendPeriods.Rd b/man/extendPeriods.Rd new file mode 100644 index 0000000..e53a909 --- /dev/null +++ b/man/extendPeriods.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/extendPeriods.R +\name{extendPeriods} +\alias{extendPeriods} +\title{Crop MagPIE object to given periods and fill missing periods} +\usage{ +extendPeriods(x, periods, fill = NA) +} +\arguments{ +\item{x}{MagPIE object} + +\item{periods}{numeric vector with target periods} + +\item{fill}{value to fill missing period values} +} +\value{ +MagPIE object with given periods in temporal dimension +} +\description{ +Crop MagPIE object to given periods and fill missing periods +} +\author{ +Robin Hasse +}