Skip to content

Commit

Permalink
Merge pull request #55 from mscrawford/master
Browse files Browse the repository at this point in the history
bugfix getReportGridEmissions
  • Loading branch information
mscrawford authored Mar 3, 2024
2 parents 2328f3b + 712b33e commit c70f13a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '238214844'
ValidationKey: '238270755'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
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: 'magpie4: MAgPIE outputs R package for MAgPIE version 4.x'
version: 1.204.2
date-released: '2024-02-29'
version: 1.204.3
date-released: '2024-03-03'
abstract: Common output routines for extracting results from the MAgPIE framework
(versions 4.x).
authors:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: magpie4
Title: MAgPIE outputs R package for MAgPIE version 4.x
Version: 1.204.2
Date: 2024-02-29
Version: 1.204.3
Date: 2024-03-03
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"),
Expand Down
54 changes: 26 additions & 28 deletions R/getReportGridEmissions.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,83 +23,81 @@
#'

getReportGridEmissions <- function(magpieOutputDir, reportOutputDir = NULL, scenario = NULL) {

# -----------------------------------------------------------------------------------------------------------------
# Helper functions

.formatReport <- function(x, name) {
getSets(x)[c("d1.1", "d1.2")] <- c("iso", "cell")
getSets(x)[c("d1.1", "d1.2", "d1.3")] <- c("x", "y", "iso")
getSets(x, fulldim = FALSE)[3] <- "variable"

currentNames <- getNames(x)
getNames(x) <- paste0(name, "|", currentNames)

return(x)
}

.saveReport <- function(x, file, comment = NULL) {
if (!is.null(reportOutputDir) && !is.null(scenario)) {
write.magpie(x,
file_name = file.path(reportOutputDir, paste0(scenario, "-", file, ".mz")),
comment = comment)

# write.magpie(x,
# file_name = file.path(reportOutputDir, paste0(scenario, "-", file, ".nc")),
# comment = comment)
file_name = file.path(reportOutputDir, paste0(scenario, "-", file, ".mz")),
comment = comment)
write.magpie(x,
file_name = file.path(reportOutputDir, paste0(scenario, "-", file, ".nc")),
comment = comment)
}
}

gdxPath <- file.path(magpieOutputDir, "fulldata.gdx")

# -----------------------------------------------------------------------------------------------------------------
# Nitrogen emissions

nitrogenEmissionsNames <- c("N2O-N", "NH3-N", "NO2-N", "NO3-N")

# Cropland
cropland <- reportNitrogenBudgetCropland(gdxPath, grid = TRUE, dir = magpieOutputDir, include_emissions = TRUE)
cropland <- cropland[, , nitrogenEmissionsNames]
cropland <- .formatReport(cropland, name = "Cropland")

# Pasture lands
pasture <- reportNitrogenBudgetPasture(gdxPath, grid = TRUE, dir = magpieOutputDir, include_emissions = TRUE)
pasture <- pasture[, , nitrogenEmissionsNames]
pasture <- .formatReport(pasture, name = "Pasture")

# Manure
manure <- reportGridManureExcretion(gdxPath, dir = magpieOutputDir)

manureNames <- data.frame(
currentNames = c("Manure|Manure In Confinements|Losses|N2O-N|Direct",
"Manure|Manure In Confinements|Losses|NH3-N",
"Manure|Manure In Confinements|Losses|NO2-N",
"Manure|Manure In Confinements|Losses|NO3-N"),
newNames = nitrogenEmissionsNames
)

manure <- manure[, , manureNames$currentNames]
getNames(manure) <- manureNames$newNames

manure <- .formatReport(manure, name = "Manure")

# # Non-agricultural land
# nonagland <- reportNitrogenBudgetNonagland(gdxPath, grid = TRUE, dir = magpieOutputDir)
# nonagland <- nonagland[, , nitrogenEmissionsNames]
# nonagland <- .formatReport(nonagland, name = "Non-agricultural land")

# Combine
nitrogen <- mbind(cropland, pasture, manure)
.saveReport(nitrogen, file = "nitrogenEmissionsGridded", comment = "Mt N/yr")



# -----------------------------------------------------------------------------------------------------------------
# CO2 emissions

# -----------------------------------------------------------------------------------------------------------------
# CH4 emissions

return(list(
"Nitrogen" = nitrogen
))

}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MAgPIE outputs R package for MAgPIE version 4.x

R package **magpie4**, version **1.204.2**
R package **magpie4**, version **1.204.3**

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

Expand Down Expand Up @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky <bodirsky@

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

Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 <https://doi.org/10.5281/zenodo.1158582>, R package version 1.204.2, <https://github.com/pik-piam/magpie4>.
Bodirsky B, Humpenoeder F, Dietrich J, Stevanovic M, Weindl I, Karstens K, Wang X, Mishra A, Beier F, Breier J, Yalew A, Chen D, Biewald A, Wirth S, von Jeetze P, Leip D, Crawford M, Alves M (2024). _magpie4: MAgPIE outputs R package for MAgPIE version 4.x_. doi:10.5281/zenodo.1158582 <https://doi.org/10.5281/zenodo.1158582>, R package version 1.204.3, <https://github.com/pik-piam/magpie4>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {magpie4: MAgPIE outputs R package for MAgPIE version 4.x},
author = {Benjamin Leon Bodirsky and Florian Humpenoeder and Jan Philipp Dietrich and Miodrag Stevanovic and Isabelle Weindl and Kristine Karstens and Xiaoxi Wang and Abhijeet Mishra and Felicitas Beier and Jannes Breier and Amsalu Woldie Yalew and David Chen and Anne Biewald and Stephen Wirth and Patrick {von Jeetze} and Debbora Leip and Michael Crawford and Marcos Alves},
year = {2024},
note = {R package version 1.204.2},
note = {R package version 1.204.3},
doi = {10.5281/zenodo.1158582},
url = {https://github.com/pik-piam/magpie4},
}
Expand Down

0 comments on commit c70f13a

Please sign in to comment.