-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
342 additions
and
80 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
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: mrmagpie | ||
Title: madrat based MAgPIE Input Data Library | ||
Version: 1.35.7 | ||
Date: 2023-07-06 | ||
Version: 1.36.0 | ||
Date: 2023-08-10 | ||
Authors@R: c( | ||
person("Kristine", "Karstens", , "[email protected]", role = c("aut", "cre")), | ||
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"), | ||
|
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,40 @@ | ||
#' @title calcPeatland2 | ||
#' @description This function calculates degraded and intact peatland area at cell level. | ||
#' The function takes degraded and intact peatland area from the Global Peatland Database 2022 (GPD2022) at the | ||
#' national level and downscales the peatland area to grid cell level using gridded peatland area | ||
#' from the Global Peatland Map 2.0 (GPM2) | ||
#' The data has been provided by Alexandra Barthelmes. | ||
#' | ||
#' @return magpie object in cellular resolution | ||
#' @author Florian Humpenoeder | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' calcOutput("Peatland2", aggregate = FALSE) | ||
#' } | ||
#' | ||
#' @importFrom madrat toolAggregate | ||
|
||
calcPeatland2 <- function() { | ||
# Country-level data on intact and degraded peatland from Global Peatland Database for 2022 (GPD2022) | ||
gpd2022 <- readSource("GPD2022", convert = TRUE) | ||
|
||
# Global Peatland Map 2.0; peatland location and extent; but no information on status peatlands (intact / degraded) | ||
gpm2 <- readSource("GPM2", convert = "onlycorrect") | ||
|
||
# Dissag. GPD2022 from country to cell with GPM2 as weight | ||
map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE) | ||
outCell <- toolAggregate(x = mrcommons::toolIso2CellCountries(gpd2022, cells = "lpjcell"), rel = map, | ||
weight = gpm2, dim = 1, from = "iso", to = "coords") | ||
names(dimnames(outCell)) <- c("coords", "t", "d3") | ||
dimnames(outCell) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = getNames(outCell)) | ||
|
||
description <- "Intact and degraded peatland area (Mha) by land-use type, based GPD 2022 and GPM2.0" | ||
|
||
return(list( | ||
x = outCell, | ||
weight = NULL, | ||
unit = "Mha", | ||
description = description, | ||
isocountries = FALSE)) | ||
} |
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,57 @@ | ||
#' @title convertGPD2022 | ||
#' @description convert GPD2022 | ||
#' @param x magpie object provided by the read function | ||
#' @return List of magpie objects with results on iso level, weight, unit and description. | ||
#' @author Florian Humpenoeder | ||
#' @examples | ||
#' \dontrun{ | ||
#' readSource("GPD2022", convert = TRUE) | ||
#' } | ||
#' | ||
convertGPD2022 <- function(x) { | ||
# convert to Mha | ||
x <- x / 1000 | ||
|
||
# create output object for filling | ||
out <- new.magpie(getCells(x), NULL, c("intact", "crop", "past", "forestry", "peatExtract"), fill = 0) | ||
|
||
## Merge deeply drained (DD) and shallow drained (SD) peatlands for composite agriculture variables | ||
ag <- setNames(dimSums(x[, , c("agricultureDD", "agricultureSD")], dim = 3), "agriculture") | ||
# use croplandDD and grasslandDD as weight | ||
w <- x[, , c("croplandDD", "grasslandDD")] | ||
# assume 0.5 as weight in case of no information | ||
w[dimSums(w, dim = 3) == 0] <- 0.5 | ||
# names | ||
getNames(w) <- c("crop", "past") | ||
# create the mapping | ||
from <- c("agriculture", | ||
"agriculture") | ||
to <- c("crop", | ||
"past") | ||
map <- data.frame(from, to) | ||
# do the disaggregation into cropland and grassland | ||
ag2 <- toolAggregate(ag, map, from = "from", to = "to", dim = 3, weight = w) | ||
# add the disaggregated categories to the existing categories | ||
out[, , "crop"] <- setNames(x[, , "croplandDD", ], "crop") + ag2[, , "crop"] | ||
out[, , "past"] <- setNames(x[, , "grasslandDD", ], "past") + ag2[, , "past"] | ||
|
||
## Merge deeply drained (DD) and shallow drained (SD) peatlands used for forestry | ||
out[, , "forestry"] <- dimSums(x[, , c("forestryDD", "forestrySD")], dim = 3) | ||
|
||
## peatExtract | ||
out[, , "peatExtract"] <- x[, , "peatExtract"] | ||
|
||
## calculate intact peatland area | ||
out[, , "intact"] <- collapseNames(x[, , c("peatlandAreaTotal")]) - dimSums(out, dim = 3) | ||
|
||
# dimnames | ||
names(dimnames(out)) <- c("iso", "t", "d3") | ||
|
||
# area cannot be smaller than 0 | ||
out[out < 0] <- 0 | ||
|
||
# fill missing countries | ||
out <- toolCountryFill(out, fill = 0) | ||
|
||
return(out) | ||
} |
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,17 @@ | ||
#' @title correctGPM2 | ||
#' @description correct peatland area | ||
#' @param x magpie object provided by the read function | ||
#' @return List of magpie objects with results on cellular level, weight, unit and description. | ||
#' @author Florian Humpenoeder | ||
#' @examples | ||
#' | ||
#' \dontrun{ | ||
#' readSource("GPM2", convert="onlycorrect") | ||
#' } | ||
|
||
correctGPM2 <- function(x) { | ||
|
||
x[is.na(x)] <- 0 | ||
|
||
return(x) | ||
} |
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,30 @@ | ||
#' @title readGPD2022 | ||
#' @description read x | ||
#' Data from the Global Peatland Database provided by Alexandra Barthelmes. | ||
#' The original xls file has been clean-up manually (country names). Turkey had | ||
#' two identical entries in the original xls file. | ||
#' Sources: | ||
#' "Inventory Reports and National Communications UNFCC 2014", | ||
#' "soil and peatland science", | ||
#' "European Mires Book" , | ||
#' "own estimates (incl. GIS data)", | ||
#' @return List of magpie objects with results on cellular level, weight, unit and description. | ||
#' @author Florian Humpenoeder | ||
#' @examples | ||
#' \dontrun{ | ||
#' readSource("x", convert = "onlycorrect") | ||
#' } | ||
#' @importFrom readxl read_xls | ||
|
||
readGPD2022 <- function() { | ||
# read-in xls file Global Peatland Database | ||
x <- read_xls("GPD2022.xls") | ||
x <- x[x$ISO3 != "NA", ] | ||
x <- x[, names(x)[-2]] | ||
|
||
# convert to magclass object | ||
x <- as.magpie(x, spatial = 1) | ||
x[is.na(x)] <- 0 | ||
|
||
return(x) | ||
} |
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,34 @@ | ||
#' @title readGPM2 | ||
#' @description read peatland area from GPM2 | ||
#' @return List of magpie objects with results on cellular level, weight, unit and description. | ||
#' @author Florian Humpenoeder | ||
#' @examples | ||
#' \dontrun{ | ||
#' readSource("GPM2", convert = "onlycorrect") | ||
#' } | ||
#' @importFrom magclass as.magpie | ||
|
||
readGPM2 <- function() { | ||
# read-in file | ||
r <- terra::rast("peatMAY22_1x1_mw_RUS30.tif") | ||
|
||
# 0.5 deg raster object | ||
r05 <- terra::rast(res = 0.5) | ||
|
||
# project r to 0.5 deg raster | ||
r2 <- terra::project(r, r05, method = "sum") | ||
|
||
# get cell area | ||
a <- terra::cellSize(r2[[1]], unit = "ha", mask = TRUE) * 1e-6 | ||
|
||
# get spatial mapping | ||
map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE) | ||
|
||
# transform raster to magpie object | ||
x <- as.magpie(terra::extract(a, map[c("lon", "lat")])[, -1], spatial = 1) | ||
|
||
# set dimension names | ||
dimnames(x) <- list("coords" = map$coords, "t" = NULL, "d3" = NULL) | ||
|
||
return(x) | ||
} |
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
Oops, something went wrong.