Skip to content

Commit

Permalink
Added Global Carbon Budget to validations
Browse files Browse the repository at this point in the history
  • Loading branch information
mscrawford committed Sep 9, 2024
1 parent ef89c16 commit 05a2cf6
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '51771052'
ValidationKey: '51935000'
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: 'mrvalidation: madrat data preparation for validation purposes'
version: 2.59.4
date-released: '2024-08-23'
version: 2.60.0
date-released: '2024-09-09'
abstract: Package contains routines to prepare data for validation exercises.
authors:
- family-names: Bodirsky
Expand Down
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrvalidation
Title: madrat data preparation for validation purposes
Version: 2.59.4
Date: 2024-08-23
Version: 2.60.0
Date: 2024-09-09
Authors@R: c(
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")),
person("Stephen", "Wirth", role = "aut"),
Expand Down Expand Up @@ -55,7 +55,8 @@ Imports:
stringr,
tidyr,
utils,
withr
withr,
rvest
Suggests:
covr
Encoding: UTF-8
Expand Down
52 changes: 52 additions & 0 deletions R/calcValidGlobalCarbonBudget.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' @title ValidGlobalCarbonBudget
#' @description validation for total and cumulative land emissions from the Global Carbon Budget, including
#' all bookkeeping models
#' @author Michael Crawford
#'
#' @param cumulative cumulative from y2000
#'
#' @return a MAgPIE object
#'
#' @examples
#' \dontrun{
#' calcOutput("ValidGlobalCarbonBudget")
#' }

calcValidGlobalCarbonBudget <- function(cumulative = FALSE) {

allOut <- readSource("GlobalCarbonBudget")

allOut <- add_dimension(allOut, dim = 3.1, add = "scenario", nm = "historical")

if (cumulative) {
allOut[, "y1995", ] <- 0
allOut <- magclass::as.magpie(apply(allOut, c(1, 3), cumsum))

# convert from Mt CO2 per year to Gt C02 per year
allOut <- allOut * 10e-4

reportingNames <- magclass::getNames(allOut, dim = 3)
reportingNames <- stringr::str_replace(
reportingNames,
"Emissions\\|CO2\\|Land(\\||$)",
"Emissions|CO2|Land|Cumulative\\1"
)
magclass::getNames(allOut, dim = 3) <- reportingNames
}

# append units
reportingNames <- magclass::getNames(allOut, dim = 3)
if (cumulative) {
reportingNames <- paste0(reportingNames, " (Gt CO2)")
} else {
reportingNames <- paste0(reportingNames, " (Mt CO2/yr)")
}
magclass::getNames(allOut, dim = 3) <- reportingNames

return(list(
x = allOut,
weight = NULL,
unit = "Mt or Gt (if cumulative) CO2 per year",
description = "Gross emissions, indirect emissions, and net land CO2 flux from GCB"
))
}
61 changes: 61 additions & 0 deletions R/downloadGlobalCarbonBudget.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#' @title downloadGlobalCarbonBudget
#' @description download the most current Global Carbon Budget dataset
#' @author Michael Crawford
#'
#' @return Metadata from the Global Carbon Budget 2023 dataset
#'
#' @examples
#' \dontrun{
#' downloadSource("GlobalCarbonBudget")
#' }
#'
#' @importFrom dplyr %>%

downloadGlobalCarbonBudget <- function() {

url <- "https://globalcarbonbudgetdata.org/latest-data.html"

downloadLink <- rvest::read_html(url) %>%
rvest::html_nodes("a") %>%
rvest::html_attr("href") %>%
stringr::str_subset("Global_Carbon_Budget_.*\\.xlsx")

if (length(downloadLink) == 0) {
stop("No download link found on the page.")
}
downloadLink <- downloadLink[1]

# Build the full download URL
baseUrl <- sub("/[^/]*$", "/", url)
downloadUrl <- ifelse(stringr::str_detect(downloadLink, "^http"), downloadLink, paste0(baseUrl, downloadLink))

outputFile <- "GCB.xlsx"

tryCatch(
{
download.file(downloadUrl, destfile = outputFile, mode = "wb")
},
error = function(e) {
stop("Failed to download the file: ", e$message)
}
)

# nolint start
# The reference section for the Global Carbon Budget is hard-coded for 2023
return(list(
url = "https://essd.copernicus.org/articles/15/5301/2023/",
doi = "10.5194/essd-15-5301-2023",
title = "Global Carbon Budget 2023",
unit = "Gt C per year",
author = list(
person("Pierre", "Friedlingstein"), person("Michael", "O Sullivan"), person("Matthew W.", "Jones"),
person("Robbie M.", "Andrew"), person("Dorothee C. E.", "Bakker"), person("Judith", "Hauck"),
person("Peter", "Landschuetzer"), person("Corinne", "Le Quere"), person("Ingrid T.", "Luijkx")
),
release_date = "2023-12-05",
description = "This dataset provides a comprehensive overview of the global carbon budget, including anthropogenic CO2 emissions and their redistribution among the atmosphere, ocean, and terrestrial biosphere. The data covers the period from 1750 to 2023, with a focus on detailed analysis for the year 2022 and projections for 2023.",
license = "Creative Commons Attribution 4.0 License",
reference = "Friedlingstein, P., O Sullivan, M., Jones, M. W., Andrew, R. M., Bakker, D. C. E., Hauck, J., Landschuetzer, P., Le Quere, C., Luijkx, I. T., et al. (2023). Global Carbon Budget 2023. Earth System Science Data, 15, 5301-5369. doi:10.5194/essd-15-5301-2023"
))
# nolint end
}
4 changes: 4 additions & 0 deletions R/fullVALIDATION.R
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ fullVALIDATION <- function(rev = 0.1) {
file = valfile, append = TRUE, try = TRUE) # ready
calcOutput(type = "ValidEmissionsPeatland", aggregate = FALSE,
file = valfile, append = TRUE, try = TRUE) # ready
calcOutput(type = "ValidGlobalCarbonBudget", aggregate = FALSE,
file = valfile, append = TRUE, try = TRUE) # ready
calcOutput(type = "ValidGlobalCarbonBudget", aggregate = FALSE, cumulative = TRUE,
file = valfile, append = TRUE, try = TRUE) # ready

# Yield
calcOutput(type = "ValidYield", datasource = "FAO", aggregate = "REG+GLO", file = valfile, append = TRUE, try = TRUE)
Expand Down
88 changes: 88 additions & 0 deletions R/readGlobalCarbonBudget.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#' @title readGlobalCarbonBudget
#' @description read the Global Carbon Budget, selecting the models GCB, BLUE, H&C2023, OSCAR and their sub-components
#' Net, Deforestation, Forest regrowth, Other transitions, Wood harvest and other forest management
#'
#' @author Michael Crawford
#'
#' @return a magpie object in Mt CO2 per year
#'
#' @examples
#' \dontrun{
#' readSource("GlobalCarbonBudget")
#' }
#' @importFrom dplyr %>%
#' @importFrom rlang .data

readGlobalCarbonBudget <- function() {

# -----------------------------------------------------------------------------------------------------------------
# Emissions from Land-use Change
eluc <- suppressMessages(readxl::read_excel("GCB.xlsx", sheet = "Land-Use Change Emissions", skip = 36))

desiredColumns <- c(
"Emissions|CO2|Land|+|Land-use Change",
"Emissions|CO2|Land|Land-use Change|+|Deforestation", # includes shifting cultivation
"Emissions|CO2|Land|Land-use Change|+|Regrowth",
"Emissions|CO2|Land|Land-use Change|+|Other land conversion", # incongruent definitions
"Emissions|CO2|Land|Land-use Change|+|Timber"
)

yearData <- eluc %>%
dplyr::select(.data$Year) %>%
dplyr::slice(-1)

elucOut <- NULL
modelNames <- c("GCB", "BLUE", "H&C2023", "OSCAR")
for (model in modelNames) {
startCol <- which(names(eluc) == model)
selectedCols <- startCol:(startCol + length(desiredColumns) - 1)

modelData <- eluc %>%
dplyr::select(dplyr::all_of(selectedCols)) %>%
dplyr::slice(-1)

names(modelData) <- desiredColumns

modelData <- dplyr::bind_cols(yearData, modelData) %>%
dplyr::mutate(dplyr::across(dplyr::everything(), as.numeric))

modelOut <- magclass::as.magpie(modelData)
modelOut <- magclass::add_dimension(modelOut, dim = 3.1, add = "model", nm = model)

elucOut <- magclass::mbind(elucOut, modelOut)
}

# -----------------------------------------------------------------------------------------------------------------
# Indirect emissions from climate change
sland <- suppressMessages(readxl::read_excel("GCB.xlsx", sheet = "Terrestrial Sink", skip = 27)) %>%
select(.data$Year, .data$GCB) %>%
rename(`Emissions|CO2|Land|+|Indirect` = .data$GCB) %>%
mutate(`Emissions|CO2|Land|+|Indirect` = .data$`Emissions|CO2|Land|+|Indirect` * -1) # as negative emissions

slandOut <- magclass::as.magpie(sland)
slandOut <- magclass::add_dimension(slandOut, dim = 3.1, add = "model", nm = "GCB")

# -----------------------------------------------------------------------------------------------------------------
# Net land flux
gcbEluc <- elucOut[, , "GCB"][, , "Emissions|CO2|Land|+|Land-use Change"]
gcbSland <- slandOut[, , "GCB"][, , "Emissions|CO2|Land|+|Indirect"]

gcbNetLandFlux <- gcbEluc
gcbNetLandFlux[, , ] <- 0
gcbNetLandFlux[, , ] <- gcbEluc + gcbSland
magclass::getNames(gcbNetLandFlux, dim = 2) <- "Emissions|CO2|Land"

# -----------------------------------------------------------------------------------------------------------------
# Combine output
allOut <- magclass::mbind(gcbNetLandFlux, slandOut, elucOut)

# select MAgPIE years
years <- magclass::getYears(allOut, as.integer = TRUE)
years <- years[years >= 1995]
allOut <- allOut[, years, ]

# convert from Gt C per year to Mt CO2 per year
allOut <- allOut * (44 / 12) * 1e3

return(allOut)
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# madrat data preparation for validation purposes

R package **mrvalidation**, version **2.59.4**
R package **mrvalidation**, version **2.60.0**

[![CRAN status](https://www.r-pkg.org/badges/version/mrvalidation)](https://cran.r-project.org/package=mrvalidation) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4317826.svg)](https://doi.org/10.5281/zenodo.4317826) [![R build status](https://github.com/pik-piam/mrvalidation/workflows/check/badge.svg)](https://github.com/pik-piam/mrvalidation/actions) [![codecov](https://codecov.io/gh/pik-piam/mrvalidation/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrvalidation) [![r-universe](https://pik-piam.r-universe.dev/badges/mrvalidation)](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 **mrvalidation** in publications use:

Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.59.4, <https://github.com/pik-piam/mrvalidation>.
Bodirsky B, Wirth S, Karstens K, Humpenoeder F, Stevanovic M, Mishra A, Biewald A, Weindl I, Beier F, Chen D, Crawford M, Leip D, Molina Bacca E, Kreidenweis U, W. Yalew A, von Jeetze P, Wang X, Dietrich J, Alves M (2024). _mrvalidation: madrat data preparation for validation purposes_. doi:10.5281/zenodo.4317826 <https://doi.org/10.5281/zenodo.4317826>, R package version 2.60.0, <https://github.com/pik-piam/mrvalidation>.

A BibTeX entry for LaTeX users is

Expand All @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is
title = {mrvalidation: madrat data preparation for validation purposes},
author = {Benjamin Leon Bodirsky and Stephen Wirth and Kristine Karstens and Florian Humpenoeder and Mishko Stevanovic and Abhijeet Mishra and Anne Biewald and Isabelle Weindl and Felicitas Beier and David Chen and Michael Crawford and Debbora Leip and Edna {Molina Bacca} and Ulrich Kreidenweis and Amsalu {W. Yalew} and Patrick {von Jeetze} and Xiaoxi Wang and Jan Philipp Dietrich and Marcos Alves},
year = {2024},
note = {R package version 2.59.4},
note = {R package version 2.60.0},
url = {https://github.com/pik-piam/mrvalidation},
doi = {10.5281/zenodo.4317826},
}
Expand Down
26 changes: 26 additions & 0 deletions man/calcValidGlobalCarbonBudget.Rd

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

23 changes: 23 additions & 0 deletions man/downloadGlobalCarbonBudget.Rd

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

23 changes: 23 additions & 0 deletions man/readGlobalCarbonBudget.Rd

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

0 comments on commit 05a2cf6

Please sign in to comment.