-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Global Carbon Budget to validations
- Loading branch information
1 parent
ef89c16
commit 05a2cf6
Showing
11 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
|
@@ -55,7 +55,8 @@ Imports: | |
stringr, | ||
tidyr, | ||
utils, | ||
withr | ||
withr, | ||
rvest | ||
Suggests: | ||
covr | ||
Encoding: UTF-8 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.