Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted package to mrcommons split #45

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
ValidationKey: '11333779'
ValidationKey: '11715040'
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
- 'Warning: namespace ''.*'' is not available and has been replaced'
AcceptedNotes: unable to verify current time
AcceptedNotes:
- unable to verify current time
- Depends: includes the non-default packages
AutocreateReadme: yes
allowLinterWarnings: no
enforceVersionUpdate: no
6 changes: 4 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: 'mrland: MadRaT land data package'
version: 0.57.1
date-released: '2024-05-06'
version: 0.59.0
date-released: '2024-05-13'
abstract: The package provides land related data via the madrat framework.
authors:
- family-names: Dietrich
Expand Down Expand Up @@ -37,6 +37,8 @@ authors:
given-names: Felicitas
- family-names: Hoetten
given-names: David
- family-names: Sauer
given-names: Pascal
license: LGPL-3.0
repository-code: https://github.com/pik-piam/mrland
doi: 10.5281/zenodo.3822083
Expand Down
22 changes: 12 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: mrland
Title: MadRaT land data package
Version: 0.57.1
Date: 2024-05-06
Version: 0.59.0
Date: 2024-05-13
Authors@R: c(
person("Jan Philipp", "Dietrich", , "dietrich@pik-potsdam.de", role = c("aut", "cre")),
person("Abhijeet", "Mishra", role = "aut"),
Expand All @@ -18,7 +18,8 @@ Authors@R: c(
person("Patrick", "von Jeetze", role = "aut"),
person("Stephen", "Wirth", role = "aut"),
person("Felicitas", "Beier", role = "aut"),
person("David", "Hoetten", role = "aut")
person("David", "Hoetten", role = "aut"),
person("Pascal", "Sauer", role = "aut")
)
Description: The package provides land related data via the madrat
framework.
Expand All @@ -27,24 +28,25 @@ URL: https://github.com/pik-piam/mrland,
https://doi.org/10.5281/zenodo.3822083
BugReports: https://github.com/pik-piam/mrland/issues
Depends:
R (>= 2.10.0),
madrat (>= 1.30),
magclass (>= 6.13.1),
mrdrivers (>= 1.0.0),
mrcommons (>= 1.37.0),
mrfactors
mrcommons (>= 1.41.0),
mrfactors (>= 0.4.0),
mrfaocore (>= 1.0.0),
mrlandcore (>= 1.0.0),
R (>= 2.10.0)
Imports:
GDPuc,
SPEI,
countrycode,
data.table,
dplyr,
GDPuc,
magpiesets,
mstools,
mstools (>= 0.6.0),
ncdf4,
raster,
readxl,
reshape2,
SPEI,
stringr,
terra,
withr
Expand Down
19 changes: 11 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import(countrycode)
import(madrat)
import(magclass)
import(mrcommons)
import(mrdrivers)
import(mrfactors)
import(mrfaocore)
import(mrlandcore)
import(readxl)
import(utils)
importFrom(GDPuc,convertGDP)
Expand Down Expand Up @@ -87,14 +88,16 @@ importFrom(magclass,unwrap)
importFrom(magclass,where)
importFrom(magpiesets,addLocation)
importFrom(magpiesets,findset)
importFrom(mrcommons,toolAggregateCell2Country)
importFrom(mrcommons,toolCoord2Isocell)
importFrom(mrcommons,toolGetMappingCoord2Country)
importFrom(mrcommons,toolHarmonize2Baseline)
importFrom(mrcommons,toolHoldConstantBeyondEnd)
importFrom(mrcommons,toolLPJmLVersion)
importFrom(mrcommons,toolSmooth)
importFrom(mrlandcore,toolLPJmLVersion)
importFrom(mstools,toolAggregateCell2Country)
importFrom(mstools,toolCell2isoCell)
importFrom(mstools,toolCoord2Isocell)
importFrom(mstools,toolCountryFillBilateral)
importFrom(mstools,toolGetMappingCoord2Country)
importFrom(mstools,toolHarmonize2Baseline)
importFrom(mstools,toolHoldConstant)
importFrom(mstools,toolHoldConstantBeyondEnd)
importFrom(mstools,toolSmooth)
importFrom(ncdf4,nc_open)
importFrom(raster,aggregate)
importFrom(raster,brick)
Expand Down
39 changes: 19 additions & 20 deletions R/SOMexogenous.R
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
#' @title calcSOMexogenous
#' @description
#' @description
#' Uses an exogenous trajectory of Soil organic matter loss nitrogen release
#' @return List of magpie objects with results on country level, weight on country level, unit and description.
#'
#'
#' @author Benjamin Leon Bodirsky
#' @seealso \code{\link{calcOutput}}
#' @examples
#'
#' \dontrun{
#' \dontrun{
#' calcOutput("SOMexogenous")
#' }
#' @export
#' @importFrom mrcommons toolHoldConstantBeyondEnd
#' @importFrom mstools toolHoldConstantBeyondEnd
#' @importFrom magpiesets findset

calcSOMexogenous <- function(){
input <- calcOutput("SOMlossN",cellular=FALSE,aggregate = F)
calcSOMexogenous <- function() {

input <- calcOutput("SOMlossN", cellular = FALSE, aggregate = FALSE)
constant <- toolHoldConstantBeyondEnd(collapseNames(input))
getNames(constant)="constant"
fadeout_2050 = constant
fadeout_2050 = convergence(origin = constant,aim = 0,start_year = "y2020",end_year = "y2050", type="linear")
getNames(fadeout_2050)="fadeout_2050"
x <- mbind(constant,fadeout_2050)
x <- x[,findset("time"),]
return(list(x=x,
weight=NULL,
unit="Tg Nr/yr",
description="Nr release through soil organic matter loss")
getNames(constant) <- "constant"
fadeout2050 <- constant
fadeout2050 <- convergence(origin = constant, aim = 0, start_year = "y2020", end_year = "y2050", type = "linear")
getNames(fadeout2050) <- "fadeout_2050"
x <- mbind(constant, fadeout2050)

x <- x[, findset("time"), ]

return(list(x = x,
weight = NULL,
unit = "Tg Nr/yr",
description = "Nr release through soil organic matter loss")
)
}
2 changes: 1 addition & 1 deletion R/calcAvlCropland.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#' @importFrom madrat readSource calcOutput
#' @importFrom magclass dimSums getCells getYears getNames mbind collapseDim as.magpie
#' @importFrom magpiesets addLocation
#' @importFrom mrcommons toolCoord2Isocell toolAggregateCell2Country
#' @importFrom mstools toolCoord2Isocell toolAggregateCell2Country
#'

calcAvlCropland <- function(marginal_land = "magpie", cell_upper_bound = 0.9, country_level = FALSE, # nolint
Expand Down
9 changes: 4 additions & 5 deletions R/calcBHIFL.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#'
#' @importFrom magpiesets findset addLocation
#' @importFrom magclass collapseDim
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'

calcBHIFL <- function(cells = "lpjcell", nclasses = "seven") {

# Land area (in Mha):
iniLU <- calcOutput("LanduseInitialisation",
cellular = TRUE, cells = cells,
Expand Down Expand Up @@ -86,7 +85,7 @@ calcBHIFL <- function(cells = "lpjcell", nclasses = "seven") {

# Conservation potential after 2020
consvPot <- landArea - dimSums(wdpaBase[, "y2020", ], dim = 3) -
setCells(urbanLand[, "y2020", "SSP2"], getCells(wdpaBase))
setCells(urbanLand[, "y2020", "SSP2"], getCells(wdpaBase))
consvPot <- toolConditionalReplace(consvPot, "<0", 0)

# Where conservation priority area is larger than conservation potential
Expand All @@ -100,7 +99,7 @@ calcBHIFL <- function(cells = "lpjcell", nclasses = "seven") {
if (nclasses == "seven") {
# calulate share of respective natveg classes
natvegShr <- iniLU[, , c("primforest", "secdforest", "other")] /
dimSums(iniLU[, , c("primforest", "secdforest", "other")], dim = 3)
dimSums(iniLU[, , c("primforest", "secdforest", "other")], dim = 3)
natvegShr <- toolConditionalReplace(natvegShr, "is.na()", 0)

# magpie object containing all land classes and their conservation land shares
Expand All @@ -113,7 +112,7 @@ calcBHIFL <- function(cells = "lpjcell", nclasses = "seven") {
} else if (nclasses == "nine") {
# calulate share of respective natveg classes
natvegShr <- iniLU[, , c("primforest", "secdforest", "primother", "secdother")] /
dimSums(iniLU[, , c("primforest", "secdforest", "primother", "secdother")], dim = 3)
dimSums(iniLU[, , c("primforest", "secdforest", "primother", "secdother")], dim = 3)
natvegShr <- toolConditionalReplace(natvegShr, "is.na()", 0)

# magpie object containing all land classes and their conservation land shares
Expand Down
9 changes: 4 additions & 5 deletions R/calcBrooks2005OldConservationPrios.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#'
#' @importFrom magpiesets findset addLocation
#' @importFrom magclass collapseDim
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'

calcBrooks2005OldConservationPrios <- function(cells = "lpjcell", nclasses = "seven") {

# Land area (in Mha):
iniLU <- calcOutput("LanduseInitialisation",
cellular = TRUE, cells = cells,
Expand Down Expand Up @@ -99,7 +98,7 @@ calcBrooks2005OldConservationPrios <- function(cells = "lpjcell", nclasses = "se

# Conservation potential after 2020
consvPot <- landArea - dimSums(wdpaBase[, "y2020", ], dim = 3) -
setCells(urbanLand[, "y2020", "SSP2"], getCells(wdpaBase))
setCells(urbanLand[, "y2020", "SSP2"], getCells(wdpaBase))
consvPot <- toolConditionalReplace(consvPot, "<0", 0)

# Where conservation priority area is larger than conservation potential
Expand All @@ -113,7 +112,7 @@ calcBrooks2005OldConservationPrios <- function(cells = "lpjcell", nclasses = "se
if (nclasses == "seven") {
# calulate share of respective natveg classes
natvegShr <- iniLU[, , c("primforest", "secdforest", "other")] /
dimSums(iniLU[, , c("primforest", "secdforest", "other")], dim = 3)
dimSums(iniLU[, , c("primforest", "secdforest", "other")], dim = 3)
natvegShr <- toolConditionalReplace(natvegShr, "is.na()", 0)

# magpie object containing all land classes and their conservation land shares
Expand All @@ -126,7 +125,7 @@ calcBrooks2005OldConservationPrios <- function(cells = "lpjcell", nclasses = "se
} else if (nclasses == "nine") {
# calulate share of respective natveg classes
natvegShr <- iniLU[, , c("primforest", "secdforest", "primother", "secdother")] /
dimSums(iniLU[, , c("primforest", "secdforest", "primother", "secdother")], dim = 3)
dimSums(iniLU[, , c("primforest", "secdforest", "primother", "secdother")], dim = 3)
natvegShr <- toolConditionalReplace(natvegShr, "is.na()", 0)

# magpie object containing all land classes and their conservation land shares
Expand Down
9 changes: 4 additions & 5 deletions R/calcConservationPriorities.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
#'
#' @importFrom magpiesets findset addLocation
#' @importFrom magclass collapseDim
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'

calcConservationPriorities <- function(consvBaseYear = "y1750", cells = "lpjcell", nclasses = "seven") {

# ===============================
# Get conservation templates
# ===============================
Expand Down Expand Up @@ -270,8 +269,8 @@ calcConservationPriorities <- function(consvBaseYear = "y1750", cells = "lpjcell
weight = NULL,
unit = "Mha",
description = paste0("Land conservation priority targets in each land type. ",
"Land use in conservation priority areas is based on the ",
"reference year ", consvBaseYear,
ifelse(gsub("y", "", consvBaseYear) <= 1800, " (pre-industrial)", "")),
"Land use in conservation priority areas is based on the ",
"reference year ", consvBaseYear,
ifelse(gsub("y", "", consvBaseYear) <= 1800, " (pre-industrial)", "")),
isocountries = FALSE))
}
8 changes: 3 additions & 5 deletions R/calcCriticalConnectivityAreas.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
#' calcOutput("calcCriticalConnectivityAreas", aggregate = FALSE)
#' }
#'
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'
calcCriticalConnectivityAreas <- function(maginput = TRUE, nclasses = "seven",
cells = "lpjcell", mask = "KBA_GSN") {
if (mask == "KBA") {
cca <- readSource("Brennan2022", subtype = "KBA_masked", convert = "onlycorrect")
cca <- readSource("Brennan2022", subtype = "KBA_masked", convert = "onlycorrect")
} else if (mask == "KBA_GSN") {
cca <- readSource("Brennan2022", subtype = "KBA_GSN_masked", convert = "onlycorrect")
cca <- readSource("Brennan2022", subtype = "KBA_GSN_masked", convert = "onlycorrect")
} else {
stop("Option specified for argument 'mask' does not exist.")
}
Expand Down Expand Up @@ -68,7 +68,6 @@ calcCriticalConnectivityAreas <- function(maginput = TRUE, nclasses = "seven",
cca[, , "CCA"] <- cca[, , "CCA"] * landMismatch[, , "CCA"]

if (nclasses %in% c("seven", "nine")) {

# differentiate primary and secondary forest based on LUH2v2 data
totForestLUH <- dimSums(luh2v2[, , c("primf", "secdf")], dim = 3) # nolint
primforestShr <- luh2v2[, , "primf"] / setNames(totForestLUH + 1e-10, NULL)
Expand All @@ -94,7 +93,6 @@ calcCriticalConnectivityAreas <- function(maginput = TRUE, nclasses = "seven",
}

if (nclasses == "nine") {

# separate pasture into pasture and rangeland
totGrassLUH <- dimSums(luh2v2[, , c("pastr", "range")], dim = 3) # nolint
pastShr <- luh2v2[, , "pastr"] / setNames(totGrassLUH + 1e-10, NULL)
Expand Down
2 changes: 1 addition & 1 deletion R/calcCroplandTreecover.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' calcOutput("CroplandTreecover", aggregate = FALSE)
#' }
#'
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'
calcCroplandTreecover <- function(maginput = TRUE, cells = "magpiecell") {
treecover <- readSource("Copernicus", subtype = "CroplandTreecover", convert = "onlycorrect")
Expand Down
2 changes: 1 addition & 1 deletion R/calcGlobalSafetyNet.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' calcOutput("calcGlobalSafetyNet", aggregate = FALSE)
#' }
#'
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'
calcGlobalSafetyNet <- function(maginput = TRUE, nclasses = "seven", cells = "lpjcell") {
gsn <- mbind(
Expand Down
13 changes: 6 additions & 7 deletions R/calcHalfEarth.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
#'
#' @importFrom magpiesets findset addLocation
#' @importFrom magclass collapseDim
#' @importFrom mrcommons toolCoord2Isocell
#' @importFrom mstools toolCoord2Isocell
#'

calcHalfEarth <- function(cells = "lpjcell", nclasses = "seven") {

# Land area (in Mha):
iniLU <- calcOutput("LanduseInitialisation",
cellular = TRUE, cells = cells,
Expand All @@ -46,8 +45,8 @@ calcHalfEarth <- function(cells = "lpjcell", nclasses = "seven") {

# get WDPA baseline data (1995 - 2020)
wdpaBase <- calcOutput("ProtectedAreaBaseline",
aggregate = FALSE, cells = cells,
nclasses = nclasses, magpie_input = TRUE)
aggregate = FALSE, cells = cells,
nclasses = nclasses, magpie_input = TRUE)

# make sure that current WDPA protected areas
# are not part of conservation priority targets
Expand All @@ -60,7 +59,7 @@ calcHalfEarth <- function(cells = "lpjcell", nclasses = "seven") {

# Conservation potential after 2020
consvPot <- landArea - dimSums(wdpaBase[, "y2020", ], dim = 3) -
setCells(urbanLand[, "y2020", "SSP2"], getCells(wdpaBase))
setCells(urbanLand[, "y2020", "SSP2"], getCells(wdpaBase))
consvPot <- toolConditionalReplace(consvPot, "<0", 0)

# Where conservation priority area is larger than conservation potential
Expand All @@ -74,7 +73,7 @@ calcHalfEarth <- function(cells = "lpjcell", nclasses = "seven") {
if (nclasses == "seven") {
# calulate share of respective natveg classes
natvegShr <- iniLU[, , c("primforest", "secdforest", "other")] /
dimSums(iniLU[, , c("primforest", "secdforest", "other")], dim = 3)
dimSums(iniLU[, , c("primforest", "secdforest", "other")], dim = 3)
natvegShr <- toolConditionalReplace(natvegShr, "is.na()", 0)

# magpie object containing all land classes and their conservation land shares
Expand All @@ -87,7 +86,7 @@ calcHalfEarth <- function(cells = "lpjcell", nclasses = "seven") {
} else if (nclasses == "nine") {
# calulate share of respective natveg classes
natvegShr <- iniLU[, , c("primforest", "secdforest", "primother", "secdother")] /
dimSums(iniLU[, , c("primforest", "secdforest", "primother", "secdother")], dim = 3)
dimSums(iniLU[, , c("primforest", "secdforest", "primother", "secdother")], dim = 3)
natvegShr <- toolConditionalReplace(natvegShr, "is.na()", 0)

# magpie object containing all land classes and their conservation land shares
Expand Down
Loading