diff --git a/.buildlibrary b/.buildlibrary index d6f084c5..8c195066 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '26521208' +ValidationKey: '26627440' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index fcd7136b..7d564a18 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -1,5 +1,3 @@ -# Run CI for R using https://eddelbuettel.github.io/r-ci/ - name: check on: @@ -8,11 +6,6 @@ on: pull_request: branches: [main, master] -env: - USE_BSPM: "true" - _R_CHECK_FORCE_SUGGESTS_: "false" - NO_BINARY_INSTALL_R_PACKAGES: 'c("madrat", "magclass", "citation", "gms", "goxygen", "GDPuc", "roxygen2")' - jobs: check: runs-on: ubuntu-latest @@ -20,80 +13,37 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Bootstrap - run: | - sudo chown runner -R . - sudo locale-gen en_US.UTF-8 - sudo add-apt-repository -y ppa:ubuntugis/ppa - curl -OLs https://eddelbuettel.github.io/r-ci/run.sh - chmod 0755 run.sh - ./run.sh bootstrap - rm -f bspm_*.tar.gz - - - name: Enable r-universe repo, modify bspm integration - run: | - # install packages from https://pik-piam.r-universe.dev and CRAN - echo ' - options(repos = c(universe = "https://pik-piam.r-universe.dev", - CRAN = "https://cloud.r-project.org")) - ' >> .Rprofile - cat .Rprofile - # modify bspm integration to never install binary builds of PIK CRAN packages - sudo sed -i '/bspm::enable()/d' /etc/R/Rprofile.site - # need double % because of printf, %s is replaced with "$NO_BINARY_INSTALL_R_PACKAGES" (see "env:" above) - printf ' - local({ - expr <- quote({ - if (!is.null(repos)) { - noBinaryInstallRPackages <- %s - pkgs <- c(bspm::install_sys(pkgs[!pkgs %%in%% noBinaryInstallRPackages]), - pkgs[pkgs %%in%% noBinaryInstallRPackages]) - } - type <- "source" - }) - trace(utils::install.packages, expr, print = FALSE) - }) - ' "$NO_BINARY_INSTALL_R_PACKAGES" | sudo tee --append /etc/R/Rprofile.site >/dev/null - cat /etc/R/Rprofile.site - - - name: Set up Pandoc - uses: r-lib/actions/setup-pandoc@v2 + - uses: r-lib/actions/setup-pandoc@v2 - - name: Set up Python 3.9 - uses: actions/setup-python@v4 + - uses: r-lib/actions/setup-r@v2 with: - python-version: 3.9 + use-public-rspm: true + extra-repositories: "https://rse.pik-potsdam.de/r/packages" - - name: Cache R libraries - if: ${{ !env.ACT }} # skip when running locally via nektos/act - uses: pat-s/always-upload-cache@v3 + - uses: r-lib/actions/setup-r-dependencies@v2 with: - path: /usr/local/lib/R/ - key: 3-${{ runner.os }}-usr-local-lib-R-${{ hashFiles('DESCRIPTION') }} - restore-keys: | - 3-${{ runner.os }}-usr-local-lib-R- - - - name: Restore R library permissions - run: | - sudo chmod 2777 /usr/local/lib/R /usr/local/lib/R/site-library - - - name: Install dependencies - run: | - ./run.sh install_aptget libhdf5-dev libharfbuzz-dev libfribidi-dev - ./run.sh install_all - ./run.sh install_r_binary covr rstudioapi - ./run.sh install_r lucode2 + extra-packages: | + any::lucode2 + any::covr + any::madrat + any::magclass + any::citation + any::gms + any::goxygen + any::GDPuc + # piam packages also available on CRAN (madrat, magclass, citation, + # gms, goxygen, GDPuc) will usually have an outdated binary version + # available; by using extra-packages we get the newest version + + - uses: actions/setup-python@v4 + with: + python-version: 3.9 - name: Install python dependencies if applicable run: | [ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true [ -f requirements.txt ] && pip install -r requirements.txt || true - - name: Remove bspm integration # to get rid of error when running install.packages - run: | - sudo sed -i '/ trace(utils::install.packages, expr, print = FALSE)/d' /etc/R/Rprofile.site - cat /etc/R/Rprofile.site - - name: Verify validation key shell: Rscript {0} run: lucode2:::validkey(stopIfInvalid = TRUE) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9c3b0698..5d2e4ca9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,7 @@ repos: - id: mixed-line-ending - repo: https://github.com/lorenzwalthert/precommit - rev: v0.3.2.9013 + rev: v0.3.2.9019 hooks: - id: parsable-R - id: deps-in-desc diff --git a/CITATION.cff b/CITATION.cff index 5a192cc4..e038ad53 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: 'mrmagpie: madrat based MAgPIE Input Data Library' -version: 1.35.7 -date-released: '2023-07-06' +version: 1.36.0 +date-released: '2023-08-10' abstract: Provides functions for MAgPIE country and cellular input data generation. authors: - family-names: Karstens diff --git a/DESCRIPTION b/DESCRIPTION index 772bfc02..6bfa881f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", , "karstens@pik-potsdam.de", role = c("aut", "cre")), person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = "aut"), diff --git a/R/calcPeatland2.R b/R/calcPeatland2.R new file mode 100644 index 00000000..5340a3d6 --- /dev/null +++ b/R/calcPeatland2.R @@ -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)) +} diff --git a/R/convertGPD2022.R b/R/convertGPD2022.R new file mode 100644 index 00000000..925eaeff --- /dev/null +++ b/R/convertGPD2022.R @@ -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) +} diff --git a/R/correctGPM2.R b/R/correctGPM2.R new file mode 100644 index 00000000..49bfd8c8 --- /dev/null +++ b/R/correctGPM2.R @@ -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) +} diff --git a/R/fullCELLULARMAGPIE.R b/R/fullCELLULARMAGPIE.R index d84388f1..3c01c200 100644 --- a/R/fullCELLULARMAGPIE.R +++ b/R/fullCELLULARMAGPIE.R @@ -337,6 +337,9 @@ fullCELLULARMAGPIE <- function(rev = 0.1, dev = "", calcOutput("Peatland", subtype = "intact", aggregate = "cluster", round = 6, file = paste0("f58_peatland_intact_", ctype, ".mz")) + calcOutput("Peatland2", aggregate = FALSE, round = 6, file = "f58_peatland_area_0.5.mz") + calcOutput("Peatland2", aggregate = "cluster", round = 6, + file = paste0("f58_peatland_area_", ctype, ".mz")) # 59 som calcOutput("SOMinitialsiationPools", aggregate = "cluster", round = 6, diff --git a/R/readGPD2022.R b/R/readGPD2022.R new file mode 100644 index 00000000..f704eabb --- /dev/null +++ b/R/readGPD2022.R @@ -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) +} diff --git a/R/readGPM2.R b/R/readGPM2.R new file mode 100644 index 00000000..854fabcd --- /dev/null +++ b/R/readGPM2.R @@ -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) +} diff --git a/README.md b/README.md index 4fcbf5e1..b86ed15d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat based MAgPIE Input Data Library -R package **mrmagpie**, version **1.35.7** +R package **mrmagpie**, version **1.36.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrmagpie)](https://cran.r-project.org/package=mrmagpie) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4319612.svg)](https://doi.org/10.5281/zenodo.4319612) [![R build status](https://github.com/pik-piam/mrmagpie/workflows/check/badge.svg)](https://github.com/pik-piam/mrmagpie/actions) [![codecov](https://codecov.io/gh/pik-piam/mrmagpie/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrmagpie) [![r-universe](https://pik-piam.r-universe.dev/badges/mrmagpie)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Kristine Karstens . +Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, v. Jeetze P, Mishra A, Humpenoeder F, Führlich P (2023). _mrmagpie: madrat based MAgPIE Input Data Library_. doi:10.5281/zenodo.4319612 , R package version 1.36.0, . A BibTeX entry for LaTeX users is @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrmagpie: madrat based MAgPIE Input Data Library}, author = {Kristine Karstens and Jan Philipp Dietrich and David Chen and Michael Windisch and Marcos Alves and Felicitas Beier and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Führlich}, year = {2023}, - note = {R package version 1.35.7}, + note = {R package version 1.36.0}, doi = {10.5281/zenodo.4319612}, url = {https://github.com/pik-piam/mrmagpie}, } diff --git a/man/calcPeatland2.Rd b/man/calcPeatland2.Rd new file mode 100644 index 00000000..bfbdfa22 --- /dev/null +++ b/man/calcPeatland2.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcPeatland2.R +\name{calcPeatland2} +\alias{calcPeatland2} +\title{calcPeatland2} +\usage{ +calcPeatland2() +} +\value{ +magpie object in cellular resolution +} +\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. +} +\examples{ +\dontrun{ +calcOutput("Peatland2", aggregate = FALSE) +} + +} +\author{ +Florian Humpenoeder +} diff --git a/man/convertGPD2022.Rd b/man/convertGPD2022.Rd new file mode 100644 index 00000000..21f1138d --- /dev/null +++ b/man/convertGPD2022.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/convertGPD2022.R +\name{convertGPD2022} +\alias{convertGPD2022} +\title{convertGPD2022} +\usage{ +convertGPD2022(x) +} +\arguments{ +\item{x}{magpie object provided by the read function} +} +\value{ +List of magpie objects with results on iso level, weight, unit and description. +} +\description{ +convert GPD2022 +} +\examples{ +\dontrun{ +readSource("GPD2022", convert = TRUE) +} + +} +\author{ +Florian Humpenoeder +} diff --git a/man/correctGPM2.Rd b/man/correctGPM2.Rd new file mode 100644 index 00000000..e5b671a2 --- /dev/null +++ b/man/correctGPM2.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/correctGPM2.R +\name{correctGPM2} +\alias{correctGPM2} +\title{correctGPM2} +\usage{ +correctGPM2(x) +} +\arguments{ +\item{x}{magpie object provided by the read function} +} +\value{ +List of magpie objects with results on cellular level, weight, unit and description. +} +\description{ +correct peatland area +} +\examples{ + +\dontrun{ + readSource("GPM2", convert="onlycorrect") +} +} +\author{ +Florian Humpenoeder +} diff --git a/man/readGPD2022.Rd b/man/readGPD2022.Rd new file mode 100644 index 00000000..9aec24f8 --- /dev/null +++ b/man/readGPD2022.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readGPD2022.R +\name{readGPD2022} +\alias{readGPD2022} +\title{readGPD2022} +\usage{ +readGPD2022() +} +\value{ +List of magpie objects with results on cellular level, weight, unit and description. +} +\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)", +} +\examples{ +\dontrun{ +readSource("x", convert = "onlycorrect") +} +} +\author{ +Florian Humpenoeder +} diff --git a/man/readGPM2.Rd b/man/readGPM2.Rd new file mode 100644 index 00000000..96c8e360 --- /dev/null +++ b/man/readGPM2.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/readGPM2.R +\name{readGPM2} +\alias{readGPM2} +\title{readGPM2} +\usage{ +readGPM2() +} +\value{ +List of magpie objects with results on cellular level, weight, unit and description. +} +\description{ +read peatland area from GPM2 +} +\examples{ +\dontrun{ +readSource("GPM2", convert = "onlycorrect") +} +} +\author{ +Florian Humpenoeder +}