diff --git a/.buildlibrary b/.buildlibrary index 54acb1c..a1d4028 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '51771052' +ValidationKey: '51935000' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package .* was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 9b683fe..42ac3f3 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: '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 diff --git a/DESCRIPTION b/DESCRIPTION index 1c6f2e6..d0c3452 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "bodirsky@pik-potsdam.de", role = c("aut", "cre")), person("Stephen", "Wirth", role = "aut"), @@ -55,7 +55,8 @@ Imports: stringr, tidyr, utils, - withr + withr, + rvest Suggests: covr Encoding: UTF-8 diff --git a/R/calcValidGlobalCarbonBudget.R b/R/calcValidGlobalCarbonBudget.R new file mode 100644 index 0000000..b76f0fd --- /dev/null +++ b/R/calcValidGlobalCarbonBudget.R @@ -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" + )) +} diff --git a/R/downloadGlobalCarbonBudget.R b/R/downloadGlobalCarbonBudget.R new file mode 100644 index 0000000..a221ce2 --- /dev/null +++ b/R/downloadGlobalCarbonBudget.R @@ -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 +} diff --git a/R/fullVALIDATION.R b/R/fullVALIDATION.R index 2a97a65..b50a2cc 100644 --- a/R/fullVALIDATION.R +++ b/R/fullVALIDATION.R @@ -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) diff --git a/R/readGlobalCarbonBudget.R b/R/readGlobalCarbonBudget.R new file mode 100644 index 0000000..d60a98e --- /dev/null +++ b/R/readGlobalCarbonBudget.R @@ -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) +} diff --git a/README.md b/README.md index d0b8c88..148a38c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -39,7 +39,7 @@ In case of questions / problems please contact Benjamin Leon Bodirsky , R package version 2.59.4, . +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 , R package version 2.60.0, . A BibTeX entry for LaTeX users is @@ -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}, } diff --git a/man/calcValidGlobalCarbonBudget.Rd b/man/calcValidGlobalCarbonBudget.Rd new file mode 100644 index 0000000..6503f22 --- /dev/null +++ b/man/calcValidGlobalCarbonBudget.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcValidGlobalCarbonBudget.R +\name{calcValidGlobalCarbonBudget} +\alias{calcValidGlobalCarbonBudget} +\title{ValidGlobalCarbonBudget} +\usage{ +calcValidGlobalCarbonBudget(cumulative = FALSE) +} +\arguments{ +\item{cumulative}{cumulative from y2000} +} +\value{ +a MAgPIE object +} +\description{ +validation for total and cumulative land emissions from the Global Carbon Budget, including +all bookkeeping models +} +\examples{ +\dontrun{ +calcOutput("ValidGlobalCarbonBudget") +} +} +\author{ +Michael Crawford +} diff --git a/man/downloadGlobalCarbonBudget.Rd b/man/downloadGlobalCarbonBudget.Rd new file mode 100644 index 0000000..0f5bf02 --- /dev/null +++ b/man/downloadGlobalCarbonBudget.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/downloadGlobalCarbonBudget.R +\name{downloadGlobalCarbonBudget} +\alias{downloadGlobalCarbonBudget} +\title{downloadGlobalCarbonBudget} +\usage{ +downloadGlobalCarbonBudget() +} +\value{ +Metadata from the Global Carbon Budget 2023 dataset +} +\description{ +download the most current Global Carbon Budget dataset +} +\examples{ +\dontrun{ +downloadSource("GlobalCarbonBudget") +} + +} +\author{ +Michael Crawford +} diff --git a/man/readGlobalCarbonBudget.Rd b/man/readGlobalCarbonBudget.Rd new file mode 100644 index 0000000..fcac57e --- /dev/null +++ b/man/readGlobalCarbonBudget.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readGlobalCarbonBudget.R +\name{readGlobalCarbonBudget} +\alias{readGlobalCarbonBudget} +\title{readGlobalCarbonBudget} +\usage{ +readGlobalCarbonBudget() +} +\value{ +a magpie object in Mt CO2 per year +} +\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 +} +\examples{ +\dontrun{ +readSource("GlobalCarbonBudget") +} +} +\author{ +Michael Crawford +}