From 8c5842c8899045beff737e789e43a85b12a91892 Mon Sep 17 00:00:00 2001 From: Jan Philipp Dietrich Date: Thu, 25 Apr 2024 12:05:11 +0200 Subject: [PATCH 1/4] adjust package to mrcommons split --- DESCRIPTION | 6 +-- NAMESPACE | 14 +++---- R/calcAfforestationMask.R | 14 +++---- R/calcAgeClassDistribution.R | 4 +- R/calcAreaEquippedForIrrigation.R | 1 + R/calcAvlLandSi.R | 6 +-- R/calcDegradationYieldReduction.R | 4 +- R/calcEFRRockstroem.R | 3 +- R/calcEFRSmakthin.R | 3 +- R/calcEnvmtlFlow.R | 3 +- R/calcGrasslandBiomass.R | 28 ++++++------- R/calcIrrigation.R | 13 ++++--- R/calcLabourProdImpact.R | 43 ++++++++++---------- R/calcLabourProdImpactEmu.R | 7 ++-- R/calcPeatland.R | 65 ++++++++++++++++--------------- R/calcPeatland2.R | 17 ++++---- R/calcPotentialForestArea.R | 2 +- R/calcSoilCharacteristics.R | 2 +- R/calcTopsoilCarbon.R | 2 +- R/correctAvlLandSi.R | 9 ++--- R/correctBendingTheCurve.R | 9 ++--- R/correctGFAD.R | 2 +- R/correctSoilClassification.R | 9 ++--- R/correctTransportDistance.R | 9 ++--- R/readGFAD.R | 3 +- R/readGPM2.R | 2 +- R/readGridPopIsimip.R | 2 +- R/readLabourProdImpactEmu.R | 38 +++++++++--------- R/readLeifeld2018.R | 2 +- R/readMAPSPAM.R | 2 +- R/readMehta2022.R | 2 +- 31 files changed, 160 insertions(+), 166 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9b583bcfe..8056abb57 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,10 +25,10 @@ BugReports: https://github.com/pik-piam/mrmagpie/issues Depends: madrat (>= 2.8.0), magclass (>= 3.17), - mrcommons (>= 1.37.0), + mrcommons (>= 1.40.7), mrland (>= 0.51.0), mrsoil (>= 2.0.0), - mrwater (>= 1.11.0), + mrwater (>= 1.12.0), R (>= 3.5.0) Imports: abind, @@ -40,7 +40,7 @@ Imports: luplot (>= 3.56.0), luscale, magpiesets, - mstools, + mstools (>= 0.5.1), ncdf4, pbapply, raster, diff --git a/NAMESPACE b/NAMESPACE index 82a30e146..484128c8b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -72,15 +72,15 @@ importFrom(magclass,where) importFrom(magclass,wrap) importFrom(magpiesets,addLocation) importFrom(magpiesets,findset) -importFrom(mrcommons,toolCell2isoCell) -importFrom(mrcommons,toolCoord2Isocell) -importFrom(mrcommons,toolCoord2Isocoord) -importFrom(mrcommons,toolGetMappingCoord2Country) -importFrom(mrcommons,toolHarmonize2Baseline) -importFrom(mrcommons,toolIso2CellCountries) -importFrom(mrcommons,toolLPJmLVersion) importFrom(mrland,spatial_header) +importFrom(mrlandcore,toolLPJmLVersion) +importFrom(mstools,toolCell2isoCell) +importFrom(mstools,toolCoord2Isocell) +importFrom(mstools,toolCoord2Isocoord) +importFrom(mstools,toolGetMappingCoord2Country) +importFrom(mstools,toolHarmonize2Baseline) importFrom(mstools,toolHoldConstant) +importFrom(mstools,toolIso2CellCountries) importFrom(ncdf4,nc_open) importFrom(ncdf4,ncvar_get) importFrom(pbapply,pbapply) diff --git a/R/calcAfforestationMask.R b/R/calcAfforestationMask.R index 4eced82f7..1904654fc 100644 --- a/R/calcAfforestationMask.R +++ b/R/calcAfforestationMask.R @@ -16,26 +16,26 @@ calcAfforestationMask <- function(subtype, cells = "lpjcell") { if (subtype == "unrestricted") { r <- terra::rast(res = 0.5, vals = 1) - } else if (subtype == "noboreal") { #Exclude boreal regions > 50deg N + } else if (subtype == "noboreal") { # Exclude boreal regions > 50deg N r <- terra::rast(res = 0.5, vals = 1) lat <- terra::init(r, "y") |> terra::mask(r) - r <- terra::mask(r, lat>50, maskvalue=TRUE, updatevalue=0) - } else if (subtype == "onlytropical") { #only tropical areas between 20deg S and 20deg N + r <- terra::mask(r, lat > 50, maskvalue = TRUE, updatevalue = 0) + } else if (subtype == "onlytropical") { # only tropical areas between 20deg S and 20deg N r <- terra::rast(res = 0.5, vals = 1) lat <- terra::init(r, "y") |> terra::mask(r) - r <- terra::mask(r, lat > 20, maskvalue=TRUE, updatevalue=0) - r <- terra::mask(r, lat < -20, maskvalue=TRUE, updatevalue=0) + r <- terra::mask(r, lat > 20, maskvalue = TRUE, updatevalue = 0) + r <- terra::mask(r, lat < -20, maskvalue = TRUE, updatevalue = 0) } # get spatial mapping - map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE) + map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) # transform raster to magpie object x <- as.magpie(terra::extract(r, map[c("lon", "lat")])[, -1], spatial = 1) dimnames(x) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = NULL) if (cells == "magpiecell") { - x <- mrcommons::toolCoord2Isocell(x, cells = cells) + x <- mstools::toolCoord2Isocell(x, cells = cells) } weight <- calcOutput("LandArea", aggregate = FALSE, cells = cells) diff --git a/R/calcAgeClassDistribution.R b/R/calcAgeClassDistribution.R index 49ac0259d..fe1c69911 100644 --- a/R/calcAgeClassDistribution.R +++ b/R/calcAgeClassDistribution.R @@ -12,16 +12,16 @@ #' } #' #' @importFrom magclass where +#' @importFrom mstools toolCoord2Isocell calcAgeClassDistribution <- function(cells = "lpjcell") { - # Cell fraction from poulter data set poulterDataset <- readSource("GFAD", convert = "onlycorrect") # Calculate cellarea mapping <- toolGetMappingCoord2Country() cb <- toolGetMapping("LPJ_CellBelongingsToCountries.csv", - type = "cell", where = "mrcommons") + type = "cell", where = "mrcommons") cellArea <- (111e3 * 0.5) * (111e3 * 0.5) * cos(cb$lat / 180 * pi) cellArea <- as.magpie(cellArea, spatial = 1) getItems(cellArea, dim = 1, raw = TRUE) <- paste(mapping$coords, mapping$iso, sep = ".") diff --git a/R/calcAreaEquippedForIrrigation.R b/R/calcAreaEquippedForIrrigation.R index 9d45bc2ec..7d3b93250 100644 --- a/R/calcAreaEquippedForIrrigation.R +++ b/R/calcAreaEquippedForIrrigation.R @@ -22,6 +22,7 @@ #' calcOutput("AreaEquippedForIrrigation", source = "LUH2v2", cellular = TRUE, aggregate = FALSE) #' } #' @importFrom magpiesets findset +#' @importFrom mstools toolCoord2Isocell #' #' @export diff --git a/R/calcAvlLandSi.R b/R/calcAvlLandSi.R index a3961aa6c..7242c09af 100644 --- a/R/calcAvlLandSi.R +++ b/R/calcAvlLandSi.R @@ -13,7 +13,7 @@ #' #' @importFrom madrat readSource calcOutput #' @importFrom magclass dimSums getCells getYears getNames mbind collapseDim -#' @importFrom mrcommons toolCoord2Isocell toolGetMappingCoord2Country +#' @importFrom mstools toolCoord2Isocell toolGetMappingCoord2Country #' @importFrom magpiesets addLocation #' @@ -69,7 +69,7 @@ calcAvlLandSi <- function(cells = "lpjcell") { weight = NULL, unit = "Mha", description = paste0("si and nsi0 areas based on Ramankutty", - "suitability information and LUH area", - "information from initialization year"), + "suitability information and LUH area", + "information from initialization year"), isocountries = FALSE)) } diff --git a/R/calcDegradationYieldReduction.R b/R/calcDegradationYieldReduction.R index 76f2e5a92..abb6dbab2 100644 --- a/R/calcDegradationYieldReduction.R +++ b/R/calcDegradationYieldReduction.R @@ -15,7 +15,7 @@ calcDegradationYieldReduction <- function(cells = "lpjcell") { # create a dummy data set, which is later used to define yield impacts of land degradation - coordMapping <- mrcommons::toolGetMappingCoord2Country() + coordMapping <- mstools::toolGetMappingCoord2Country() cellnames <- paste(coordMapping$coords, coordMapping$iso, sep = ".") x <- new.magpie(cells_and_regions = cellnames, years = seq(1995, 2150, 5), @@ -24,7 +24,7 @@ calcDegradationYieldReduction <- function(cells = "lpjcell") { fill = 0) if (cells == "magpiecell") { - x <- mrcommons::toolCoord2Isocell(x, cells = cells) + x <- mstools::toolCoord2Isocell(x, cells = cells) } return(list(x = x, diff --git a/R/calcEFRRockstroem.R b/R/calcEFRRockstroem.R index 106c33a8a..0ef981649 100644 --- a/R/calcEFRRockstroem.R +++ b/R/calcEFRRockstroem.R @@ -13,7 +13,8 @@ #' @import magclass #' @import madrat #' @importFrom stats quantile -#' @importFrom mrcommons toolHarmonize2Baseline toolLPJmLVersion +#' @importFrom mstools toolHarmonize2Baseline +#' @importFrom mrlandcore toolLPJmLVersion #' #' @return magpie object in cellular resolution #' @author Felicitas Beier, Jens Heinke diff --git a/R/calcEFRSmakthin.R b/R/calcEFRSmakthin.R index b1dd083fd..90200e8b7 100644 --- a/R/calcEFRSmakthin.R +++ b/R/calcEFRSmakthin.R @@ -22,7 +22,8 @@ #' @import magclass #' @import madrat #' @importFrom stats quantile -#' @importFrom mrcommons toolHarmonize2Baseline toolLPJmLVersion +#' @importFrom mstools toolHarmonize2Baseline +#' @importFrom mrlandcore toolLPJmLVersion #' #' @return magpie object in cellular resolution #' @author Felicitas Beier, Abhijeet Mishra diff --git a/R/calcEnvmtlFlow.R b/R/calcEnvmtlFlow.R index c1677e756..4159eaaac 100644 --- a/R/calcEnvmtlFlow.R +++ b/R/calcEnvmtlFlow.R @@ -11,7 +11,8 @@ #' @import magclass #' @import madrat #' @importFrom stats quantile -#' @importFrom mrcommons toolHarmonize2Baseline toolLPJmLVersion +#' @importFrom mstools toolHarmonize2Baseline +#' @importFrom mrlandcore toolLPJmLVersion #' #' @return magpie object in cellular resolution #' @author Felicitas Beier diff --git a/R/calcGrasslandBiomass.R b/R/calcGrasslandBiomass.R index 10de31af3..2dde5757c 100644 --- a/R/calcGrasslandBiomass.R +++ b/R/calcGrasslandBiomass.R @@ -1,5 +1,6 @@ #' @title calcGrasslandBiomass -#' @description Calculates pasture biomass demand for the historical period split between rangelands and managed pastures. +#' @description Calculates pasture biomass demand for the historical period split +#' between rangelands andmanaged pastures. #' @param cells "magpiecell" for 59199 cells or "lpjcell" for 67420 cells #' @return Regional biomass demand #' @author Marcos Alves @@ -9,11 +10,10 @@ #' \dontrun{ #' calcOutput("GrasslandBiomass") #' } -#' @importFrom mrcommons toolCoord2Isocell +#' @importFrom mstools toolCoord2Isocell #' @importFrom magpiesets findset calcGrasslandBiomass <- function(cells = "lpjcell") { - # select years magYearsPast <- findset("past") @@ -33,17 +33,11 @@ calcGrasslandBiomass <- function(cells = "lpjcell") { # "other land" that is assumed to be used for grazing in those three countries. grasslLand["IND", , "pastr"] <- grasslLand["IND", , "pastr"] + - setNames(dimSums(land["IND", , c("primother", "secdother")], - dim = 3), - "pastr") + setNames(dimSums(land["IND", , c("primother", "secdother")], dim = 3), "pastr") grasslLand["BGD", , "pastr"] <- grasslLand["BGD", , "pastr"] + - setNames(dimSums(land["BGD", , c("primother", "secdother")], - dim = 3), - "pastr") + setNames(dimSums(land["BGD", , c("primother", "secdother")], dim = 3), "pastr") grasslLand["PAK", , "pastr"] <- grasslLand["PAK", , "pastr"] + - setNames(dimSums(land["PAK", , c("primother", "secdother")], - dim = 3), - "pastr") + setNames(dimSums(land["PAK", , c("primother", "secdother")], dim = 3), "pastr") grassYld <- calcOutput("GrasslandsYields", lpjml = "lpjml5p2_pasture", @@ -66,7 +60,7 @@ calcGrasslandBiomass <- function(cells = "lpjcell") { livestock <- setNames(readSource("GLW3", subtype = "Aw"), "liv_numb") if (cells == "magpiecell") { - livestock <- toolCoord2Isocell(livestock) + livestock <- toolCoord2Isocell(livestock) } livestock[livestock < 1] <- 0 @@ -80,11 +74,11 @@ calcGrasslandBiomass <- function(cells = "lpjcell") { livstSplit <- livestock * potBioMassShare livstSplit <- collapseNames(livstSplit) if (cells == "lpjcell") { - livstSplitCtry <- dimSums(livstSplit, dim = c("x", "y")) + livstSplitCtry <- dimSums(livstSplit, dim = c("x", "y")) } else if (cells == "magpiecell") { - livstSplitCtry <- dimSums(livstSplit, dim = 1.2) + livstSplitCtry <- dimSums(livstSplit, dim = 1.2) } else { - stop("Please select cells argument in calcGrasslandBiomass") + stop("Please select cells argument in calcGrasslandBiomass") } livstShareCtry <- livstSplitCtry[, , "pastr"] / dimSums(livstSplitCtry, dim = 3) livstShareCtry[is.nan(livstShareCtry) | is.infinite(livstShareCtry)] <- 0 @@ -96,7 +90,7 @@ calcGrasslandBiomass <- function(cells = "lpjcell") { # with the production of grass in pastures and rangelands in a country. That can be # derived by the fact that the feedbaskets assume the same feed ingredients shares # within a country. - commonCtrs <- intersect(getItems(biomass, dim = 1), + commonCtrs <- intersect(getItems(biomass, dim = 1), unique(getItems(livstShareCtry, dim = 1))) biomassSplit <- biomass[commonCtrs, , ] * livstShareCtry[commonCtrs, , ] biomassSplit <- toolCountryFill(biomassSplit, fill = 0) diff --git a/R/calcIrrigation.R b/R/calcIrrigation.R index d603f91ae..532d1c6b7 100644 --- a/R/calcIrrigation.R +++ b/R/calcIrrigation.R @@ -18,7 +18,8 @@ #' } #' #' @importFrom magpiesets findset -#' @importFrom mrcommons toolLPJmLVersion toolGetMappingCoord2Country toolCoord2Isocell +#' @importFrom mrlandcore toolLPJmLVersion +#' @importFrom mstools toolGetMappingCoord2Country toolCoord2Isocell #' @importFrom madrat toolGetMapping calcOutput toolAggregate #' @importFrom magclass dimSums getItems getSets collapseNames #' @importFrom withr local_options @@ -59,16 +60,16 @@ calcIrrigation <- function(lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de", crop # Clustering weight: totalCropland <- dimSums(calcOutput("Croparea", sectoral = "kcr", physical = TRUE, - cellular = TRUE, cells = "lpjcell", - years = "y1995", round = 6, - irrigation = TRUE, aggregate = FALSE), - dim = 3.2) + cellular = TRUE, cells = "lpjcell", + years = "y1995", round = 6, + irrigation = TRUE, aggregate = FALSE), + dim = 3.2) map <- toolGetMappingCoord2Country() getItems(totalCropland, dim = 1, raw = TRUE) <- paste(map$coords, map$iso, sep = ".") getSets(totalCropland) <- c("x", "y", "iso", "year", "irrigation") weightCropArea <- collapseNames(totalCropland[, , "irrigated"]) + - rainfedweight * collapseNames(totalCropland[, , "rainfed"]) + rainfedweight * collapseNames(totalCropland[, , "rainfed"]) # Reduce to 59199 cells if (cells == "magpiecell") { diff --git a/R/calcLabourProdImpact.R b/R/calcLabourProdImpact.R index ac5be7051..2c084a515 100644 --- a/R/calcLabourProdImpact.R +++ b/R/calcLabourProdImpact.R @@ -4,61 +4,60 @@ #' @param subtype data source comes from #' @param cellular cellular is true #' @param cells "magpiecell" or "lpjcell" -#' @return List of magpie objects with results on 0.5deg grid level, weights based on production value, unit (ratio) and description. +#' @return List of magpie objects with results on 0.5deg grid level, weights +#' based on production value, unit (ratio) and description. #' @author David Chen #' @importFrom magpiesets findset #' @importFrom mstools toolHoldConstant #' @importFrom magclass collapseNames -calcLabourProdImpact <-function(timestep = "5year", subtype="Orlov", cellular=TRUE, cells = "lpjcell"){ +calcLabourProdImpact <- function(timestep = "5year", subtype = "Orlov", cellular = TRUE, cells = "lpjcell") { - if (subtype=="Orlov"){ + if (subtype == "Orlov") { - out <- readSource("LabourProdImpactOrlov", subtype="IPSL-CM5A-LR_rcp85_wbgtod_hothaps_400W.nc", convert=FALSE) + out <- readSource("LabourProdImpactOrlov", subtype = "IPSL-CM5A-LR_rcp85_wbgtod_hothaps_400W.nc", convert = FALSE) out <- toolTimeSpline(out) - tran <- readSource("LabourProdImpactOrlov", subtype="IPSL-CM5A-LR_rcp60_wbgtod_hothaps_400W.nc", convert=FALSE) + tran <- readSource("LabourProdImpactOrlov", subtype = "IPSL-CM5A-LR_rcp60_wbgtod_hothaps_400W.nc", convert = FALSE) tran <- toolTimeSpline(tran) - out[,c(2006:2020),] <- tran[,c(2006:2020),] + out[, c(2006:2020), ] <- tran[, c(2006:2020), ] - if(timestep == "5year"){ - #add future - out <- out[,seq(1985,2095,5),] + if (timestep == "5year") { + # add future + out <- out[, seq(1985, 2095, 5), ] - past <- new.magpie(cells_and_regions = getCells(out), years=seq(1965,1980,5), names=getNames(out),fill=1) - past[,seq(1965,1980,5),] <- setYears(out[,1985,],NULL) - out <- mbind(past,out) + past <- new.magpie(cells_and_regions = getCells(out), years = seq(1965, 1980, 5), names = getNames(out), fill = 1) + past[, seq(1965, 1980, 5), ] <- setYears(out[, 1985, ], NULL) + out <- mbind(past, out) out <- toolHoldConstantBeyondEnd(out) out <- collapseNames(out) - } - - else if(timestep=="yearly"){ + } else if (timestep == "yearly") { - past <- new.magpie(cells_and_regions = getCells(out), years=c(1965:1980), names=getNames(out),fill=1) - past[,c(1965:1980),] <- setYears(out[,1981,],NULL) - out <- mbind(past,out) + past <- new.magpie(cells_and_regions = getCells(out), years = c(1965:1980), names = getNames(out), fill = 1) + past[, c(1965:1980), ] <- setYears(out[, 1981, ], NULL) + out <- mbind(past, out) out <- toolHoldConstantBeyondEnd(out) out <- collapseNames(out) } } - #weight <- calcOutput("ValueProduction",aggregate=F)[,2010,] - #no weight yet because doesn't work in old preprocessing + + # no weight yet because doesn't work in old preprocessing if (subtype != "Orlov") { stop("Not a Valid Subtype") } if (cells == "magpiecell") { - x <- mrcommons::toolCoord2Isocell(x, cells = cells) + x <- mstools::toolCoord2Isocell(x, cells = cells) } return(list(x = out, weight = NULL, unit = "Percentage of total labour productivity", - isocountries = (!cellular & (nregions(out)!=1)), + isocountries = (!cellular & (nregions(out) != 1)), description = "Labour productivity impacts as percentage of full labour prod 1")) } diff --git a/R/calcLabourProdImpactEmu.R b/R/calcLabourProdImpactEmu.R index 9e32e3df4..fb8e4d206 100644 --- a/R/calcLabourProdImpactEmu.R +++ b/R/calcLabourProdImpactEmu.R @@ -55,14 +55,15 @@ calcLabourProdImpactEmu <- function(timestep = "5year", cellular = TRUE, avlCropAreaWeight[avlCropAreaWeight == 0] <- 10^-10 if (cells == "magpiecell") { - out <- mrcommons::toolCoord2Isocell(out, cells = cells, fillMissing = 1) + out <- mstools::toolCoord2Isocell(out, cells = cells, fillMissing = 1) } return(list(x = out, weight = avlCropAreaWeight, unit = "Percentage of total labour productivity (1)", - description = "Remaining labour productivity as percentage of full labour productivity after accounting for - climate change impacts on labour productivity", + description = "Remaining labour productivity as percentage of full labour + productivity after accounting for climate change impacts on + labour productivity", isocountries = FALSE)) } diff --git a/R/calcPeatland.R b/R/calcPeatland.R index f02a74e00..1162046d6 100644 --- a/R/calcPeatland.R +++ b/R/calcPeatland.R @@ -1,7 +1,10 @@ #' @title calcPeatland #' @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 (GPD) at the national level and downscales the peatland area to grid cell level using gridded potential peatland area. -#' The GPD has been provided by Alexandra Barthelmes. The potential peatland area has been provided by Leifeld_2018 (DOI 10.1038/s41467-018-03406-6). +#' The function takes degraded and intact peatland area from the Global Peatland Database +#' (GPD) at the national level and downscales the peatland area to grid cell level using +#' gridded potential peatland area. +#' The GPD has been provided by Alexandra Barthelmes. The potential peatland area has been +#' provided by Leifeld_2018 (DOI 10.1038/s41467-018-03406-6). #' @param subtype degraded (default) or intact #' @param cells "magpiecell" or "lpjcell" #' @@ -15,50 +18,48 @@ #' #' @importFrom magclass clean_magpie #' @importFrom madrat toolGetMapping toolAggregate -#' @importFrom mrcommons toolIso2CellCountries +#' @importFrom mstools toolIso2CellCountries -calcPeatland <-function(subtype = "degraded", cells = "lpjcell"){ +calcPeatland <- function(subtype = "degraded", cells = "lpjcell") { - GPD <- readSource("GPD", convert = TRUE) + gpd <- readSource("GPD", convert = TRUE) potPeatArea <- readSource("Leifeld2018", convert = "onlycorrect") # Total and drained peatland area - PeatAreaTotal <- collapseNames(GPD[, , "PeatAreaTotal"]) - names(dimnames(PeatAreaTotal)) <- c("iso", "t", "d3") - PeatAreaDrained <- collapseNames(GPD[, , "PeatAreaDrained"]) - names(dimnames(PeatAreaDrained)) <- c("iso", "t", "d3") + peatAreaTotal <- collapseNames(gpd[, , "PeatAreaTotal"]) + names(dimnames(peatAreaTotal)) <- c("iso", "t", "d3") + peatAreaDrained <- collapseNames(gpd[, , "PeatAreaDrained"]) + names(dimnames(peatAreaDrained)) <- c("iso", "t", "d3") - #Dissag. from country to cell - #CountryToCell <- toolGetMapping("CountryToCellMapping.csv", type = "cell") + # Dissag. from country to cell map <- toolGetMappingCoord2Country(pretty = TRUE) - PeatAreaTotal <- toolAggregate(x=toolIso2CellCountries(PeatAreaTotal, cells = "lpjcell"), rel = map, weight = potPeatArea, dim = 1, from = "iso", to = "coords") - names(dimnames(PeatAreaTotal)) <- c("coords","t","d3") - PeatAreaDrained <- toolAggregate(x=toolIso2CellCountries(PeatAreaDrained, cells = "lpjcell"), rel = map, weight = potPeatArea, dim = 1, from = "iso", to = "coords") - names(dimnames(PeatAreaDrained)) <- c("coords", "t", "d3") + peatAreaTotal <- toolAggregate(x = toolIso2CellCountries(peatAreaTotal, cells = "lpjcell"), rel = map, + weight = potPeatArea, dim = 1, from = "iso", to = "coords") + names(dimnames(peatAreaTotal)) <- c("coords", "t", "d3") + peatAreaDrained <- toolAggregate(x = toolIso2CellCountries(peatAreaDrained, cells = "lpjcell"), rel = map, + weight = potPeatArea, dim = 1, from = "iso", to = "coords") + names(dimnames(peatAreaDrained)) <- c("coords", "t", "d3") # potPeatArea is the upper limit of peatland area in a cell; this will reduce the peatland area of GPD! - PeatAreaTotal[PeatAreaTotal > potPeatArea] <- potPeatArea[PeatAreaTotal > potPeatArea] - PeatAreaDrained[PeatAreaDrained > potPeatArea] <- potPeatArea[PeatAreaDrained > potPeatArea] + peatAreaTotal[peatAreaTotal > potPeatArea] <- potPeatArea[peatAreaTotal > potPeatArea] + peatAreaDrained[peatAreaDrained > potPeatArea] <- potPeatArea[peatAreaDrained > potPeatArea] - # check; GPD is somewhat higher because we used potPeatArea as upper limit! - # dimSums(GPD,dim=1) - # dimSums(PeatAreaTotal,dim=1) - # dimSums(PeatAreaDrained,dim=1) + dimnames(peatAreaTotal) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = NULL) + dimnames(peatAreaDrained) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = NULL) + peatAreaIntact <- peatAreaTotal - peatAreaDrained - dimnames(PeatAreaTotal) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = NULL) - dimnames(PeatAreaDrained) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = NULL) - PeatAreaIntact <- PeatAreaTotal - PeatAreaDrained - - if(subtype=="degraded") { - description <- "Degraded peatland area (Mha) in 0.5 degree resolution based on Humpenoeder et al 2020 (DOI 10.1088/1748-9326/abae2a)" - x <- PeatAreaDrained - } else if(subtype == "intact") { - description <- "Intact peatland area (Mha) in 0.5 degree resolution based on Humpenoeder et al 2020 (DOI 10.1088/1748-9326/abae2a)" - x <- PeatAreaIntact + if (subtype == "degraded") { + description <- "Degraded peatland area (Mha) in 0.5 degree resolution based on Humpenoeder + et al 2020 (DOI 10.1088/1748-9326/abae2a)" + x <- peatAreaDrained + } else if (subtype == "intact") { + description <- "Intact peatland area (Mha) in 0.5 degree resolution based on Humpenoeder + et al 2020 (DOI 10.1088/1748-9326/abae2a)" + x <- peatAreaIntact } if (cells == "magpiecell") { - x <- mrcommons::toolCoord2Isocell(x, cells = cells) + x <- mstools::toolCoord2Isocell(x, cells = cells) } return(list(x = x, diff --git a/R/calcPeatland2.R b/R/calcPeatland2.R index 263989526..0831bd74e 100644 --- a/R/calcPeatland2.R +++ b/R/calcPeatland2.R @@ -23,22 +23,21 @@ calcPeatland2 <- function(cells = "magpiecell") { 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, + map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) + outCell <- toolAggregate(x = mstools::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)) if (cells == "magpiecell") { - outCell <- mrcommons::toolCoord2Isocell(outCell) + outCell <- mstools::toolCoord2Isocell(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)) + return(list(x = outCell, + weight = NULL, + unit = "Mha", + description = description, + isocountries = FALSE)) } diff --git a/R/calcPotentialForestArea.R b/R/calcPotentialForestArea.R index 65772598a..01285d5c6 100644 --- a/R/calcPotentialForestArea.R +++ b/R/calcPotentialForestArea.R @@ -23,7 +23,7 @@ #' #' @importFrom madrat readSource calcOutput toolCountryFill #' @importFrom magclass dimSums getYears -#' @importFrom mrcommons toolCoord2Isocell +#' @importFrom mstools toolCoord2Isocell calcPotentialForestArea <- function(refData = "lpj", countryLevel = FALSE, cells = "lpjcell", lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de"), diff --git a/R/calcSoilCharacteristics.R b/R/calcSoilCharacteristics.R index fb4ece533..636764c4c 100644 --- a/R/calcSoilCharacteristics.R +++ b/R/calcSoilCharacteristics.R @@ -23,7 +23,7 @@ calcSoilCharacteristics <- function() { # THIS FUNCTION NEEDS A MAJOR REWRITE # Creating aggregation weights for the physical variables # #Future implementation - # map <- mrcommons:::toolGetMappingCoord2Country() + # map <- mstools:::toolGetMappingCoord2Country() # x <- new.magpie(cells_and_regions=map$coords) # getSets(x)[c(1,2)] <- c("x", "y") # x[,,1] <- 1 diff --git a/R/calcTopsoilCarbon.R b/R/calcTopsoilCarbon.R index b06a4920b..fd9efae90 100644 --- a/R/calcTopsoilCarbon.R +++ b/R/calcTopsoilCarbon.R @@ -15,7 +15,7 @@ #' } #' #' @importFrom magpiesets findset -#' @importFrom mrcommons toolCoord2Isocell +#' @importFrom mstools toolCoord2Isocell calcTopsoilCarbon <- function(cells = "lpjcell", lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de", crop = "ggcmi_phase3_nchecks_9ca735cb"), diff --git a/R/correctAvlLandSi.R b/R/correctAvlLandSi.R index 1f73515fa..51568c327 100644 --- a/R/correctAvlLandSi.R +++ b/R/correctAvlLandSi.R @@ -6,17 +6,16 @@ #' @seealso #' \code{\link{readAvlLandSi}} #' @examples -#' #' \dontrun{ -#' readSource("AvlLandSi", convert="onlycorrect") +#' readSource("AvlLandSi", convert = "onlycorrect") #' } #' #' @importFrom madrat toolConditionalReplace -#' @importFrom mrcommons toolCell2isoCell +#' @importFrom mstools toolCell2isoCell -correctAvlLandSi <- function(x){ +correctAvlLandSi <- function(x) { - x <- toolConditionalReplace(x, conditions = c("is.na()","<0"), replaceby = 0) + x <- toolConditionalReplace(x, conditions = c("is.na()", "<0"), replaceby = 0) x <- toolCell2isoCell(x) return(x) diff --git a/R/correctBendingTheCurve.R b/R/correctBendingTheCurve.R index dfe345e94..3e6967708 100644 --- a/R/correctBendingTheCurve.R +++ b/R/correctBendingTheCurve.R @@ -4,18 +4,17 @@ #' @return List of magpie objects with results on cellular level, weight, unit and description. #' @author Patrick v. Jeetze, Michael Windisch #' @examples -#' #' \dontrun{ -#' readSource("BendingTheCurve", subtype="rr_layer", convert="onlycorrect") +#' readSource("BendingTheCurve", subtype = "rr_layer", convert = "onlycorrect") #' } #' #' @importFrom madrat toolConditionalReplace -#' @importFrom mrcommons toolCell2isoCell +#' @importFrom mstools toolCell2isoCell -correctBendingTheCurve <- function(x){ +correctBendingTheCurve <- function(x) { - x <- toolConditionalReplace(x, conditions = c("is.na()","<0"), replaceby = 0) + x <- toolConditionalReplace(x, conditions = c("is.na()", "<0"), replaceby = 0) return(x) } diff --git a/R/correctGFAD.R b/R/correctGFAD.R index 350aa01ef..67d8c7068 100644 --- a/R/correctGFAD.R +++ b/R/correctGFAD.R @@ -12,7 +12,7 @@ #' } #' #' @importFrom madrat toolConditionalReplace -#' @importFrom mrcommons toolCoord2Isocoord +#' @importFrom mstools toolCoord2Isocoord correctGFAD <- function(x) { diff --git a/R/correctSoilClassification.R b/R/correctSoilClassification.R index 28a48160d..9509d624c 100644 --- a/R/correctSoilClassification.R +++ b/R/correctSoilClassification.R @@ -6,18 +6,17 @@ #' @seealso #' \code{\link{readSoilClassification}}, #' @examples -#' #' \dontrun{ -#' readSource("SoilClassification", subtype="HWSD.soil", convert="onlycorrect") +#' readSource("SoilClassification", subtype = "HWSD.soil", convert = "onlycorrect") #' } #' #' @import magclass #' @importFrom madrat toolConditionalReplace -#' @importFrom mrcommons toolCell2isoCell +#' @importFrom mstools toolCell2isoCell -correctSoilClassification <- function(x){ +correctSoilClassification <- function(x) { - x <- toolConditionalReplace(x, conditions = c("<1",">13", "!is.integer()"), replaceby = 0) + x <- toolConditionalReplace(x, conditions = c("<1", ">13", "!is.integer()"), replaceby = 0) x <- toolCell2isoCell(x) return(x) diff --git a/R/correctTransportDistance.R b/R/correctTransportDistance.R index f39d9ce47..cd34bd8fa 100644 --- a/R/correctTransportDistance.R +++ b/R/correctTransportDistance.R @@ -6,17 +6,16 @@ #' @seealso #' \code{\link{readTransportDistance}} #' @examples -#' #' \dontrun{ -#' readSource("TransportDistance", convert="onlycorrect") +#' readSource("TransportDistance", convert = "onlycorrect") #' } #' #' @importFrom madrat toolConditionalReplace -#' @importFrom mrcommons toolCell2isoCell +#' @importFrom mstools toolCell2isoCell -correctTransportDistance <- function(x){ +correctTransportDistance <- function(x) { - x <- toolConditionalReplace(x, conditions = c("is.na()","<0"), replaceby = 0) + x <- toolConditionalReplace(x, conditions = c("is.na()", "<0"), replaceby = 0) x <- toolCell2isoCell(x) return(x) diff --git a/R/readGFAD.R b/R/readGFAD.R index 13389781d..f9368f7b6 100644 --- a/R/readGFAD.R +++ b/R/readGFAD.R @@ -9,14 +9,13 @@ #' } #' @importFrom raster brick subset as.matrix t #' @importFrom ncdf4 nc_open ncvar_get -#' @importFrom mrcommons toolGetMappingCoord2Country +#' @importFrom mstools toolGetMappingCoord2Country #' @importFrom magclass clean_magpie add_dimension setYears getSets #' @export readGFAD <- function() { poulterData <- "GFAD_V1-1.nc" - gfad <- nc_open(poulterData) mapping <- toolGetMappingCoord2Country() diff --git a/R/readGPM2.R b/R/readGPM2.R index 002ff2205..eb30e8c73 100644 --- a/R/readGPM2.R +++ b/R/readGPM2.R @@ -33,7 +33,7 @@ readGPM2 <- function() { r3 <- terra::aggregate(a, fact = 48, fun = "sum", na.rm = TRUE) # get spatial mapping - map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE) + map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) # transform raster to magpie object x <- as.magpie(terra::extract(r3, map[c("lon", "lat")])[, -1], spatial = 1) diff --git a/R/readGridPopIsimip.R b/R/readGridPopIsimip.R index dfabc7d1b..4d8020b53 100644 --- a/R/readGridPopIsimip.R +++ b/R/readGridPopIsimip.R @@ -12,7 +12,7 @@ #' @import magclass #' @importFrom raster extent brick #' @importFrom magpiesets findset -#' @importFrom mrcommons toolGetMappingCoord2Country +#' @importFrom mstools toolGetMappingCoord2Country #' #' @export diff --git a/R/readLabourProdImpactEmu.R b/R/readLabourProdImpactEmu.R index d24d473ec..59cfe95f1 100644 --- a/R/readLabourProdImpactEmu.R +++ b/R/readLabourProdImpactEmu.R @@ -5,7 +5,7 @@ #' @author Michael Windisch, Florian Humpenöder, Felicitas Beier #' @seealso \code{\link{readSource}} #' @importFrom magclass new.magpie read.magpie mbind -#' @importFrom mrcommons toolCoord2Isocell +#' @importFrom mstools toolCoord2Isocell readLabourProdImpactEmu <- function() { @@ -14,24 +14,24 @@ readLabourProdImpactEmu <- function() { for (exp in (c("CTL_rcp585", "CTL_rcp119", "FRST", "CROP", "HARV", "IRR"))) { for (int in c("300", "400")) { for (sta in c("ensmean", "ensstd")) { - for (fct in c("ISO", "HOTHAPS")) { - tmp <- read.magpie(paste0(exp, "/", exp, "_", sta, "_laborprod_wby_grdays_", fct, "_", int, "_0.5.nc")) - # reduce to 67420 cells and fill missings with 0 - inter <- new.magpie(cells_and_regions = mapping$coords, - years = getItems(tmp, dim = 2), - names = getItems(tmp, dim = 3), - fill = 0) - tmp <- tmp[intersect(getItems(inter, dim = 1), getItems(tmp, dim = 1)), , ] - inter[intersect(getItems(inter, dim = 1), getItems(tmp, dim = 1)), , ] <- tmp - inter <- toolCoord2Isocoord(inter) - # clean up object dimensions - getYears(inter) <- seq(from = 1995, to = 2095, by = 1) - inter <- collapseNames(inter) - getNames(inter) <- paste0(exp, ".", fct, ".", int, "W", ".", sta) - getSets(inter) <- c("x", "y", "iso", "year", "data") - # bind to previous inputs - x <- mbind(x, inter) - } + for (fct in c("ISO", "HOTHAPS")) { + tmp <- read.magpie(paste0(exp, "/", exp, "_", sta, "_laborprod_wby_grdays_", fct, "_", int, "_0.5.nc")) + # reduce to 67420 cells and fill missings with 0 + inter <- new.magpie(cells_and_regions = mapping$coords, + years = getItems(tmp, dim = 2), + names = getItems(tmp, dim = 3), + fill = 0) + tmp <- tmp[intersect(getItems(inter, dim = 1), getItems(tmp, dim = 1)), , ] + inter[intersect(getItems(inter, dim = 1), getItems(tmp, dim = 1)), , ] <- tmp + inter <- toolCoord2Isocoord(inter) + # clean up object dimensions + getYears(inter) <- seq(from = 1995, to = 2095, by = 1) + inter <- collapseNames(inter) + getNames(inter) <- paste0(exp, ".", fct, ".", int, "W", ".", sta) + getSets(inter) <- c("x", "y", "iso", "year", "data") + # bind to previous inputs + x <- mbind(x, inter) + } } } } diff --git a/R/readLeifeld2018.R b/R/readLeifeld2018.R index b0f403ed1..f9ede257f 100644 --- a/R/readLeifeld2018.R +++ b/R/readLeifeld2018.R @@ -18,7 +18,7 @@ readLeifeld2018 <- function() { a <- terra::cellSize(rp2[[1]], unit = "ha", mask = TRUE) a <- a * 1e-6 # get spatial mapping - map <- mrcommons::toolGetMappingCoord2Country(pretty = TRUE) + map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) # transform raster to magpie object x <- as.magpie(terra::extract(a, map[c("lon", "lat")])[, -1], spatial = 1) # set dimension names diff --git a/R/readMAPSPAM.R b/R/readMAPSPAM.R index e02fe2490..e51545e54 100644 --- a/R/readMAPSPAM.R +++ b/R/readMAPSPAM.R @@ -5,7 +5,7 @@ #' @param subtype It can be either "harvested" or "physical" area #' @importFrom terra rast values crds aggregate #' @importFrom luscale speed_aggregate -#' @importFrom mrcommons toolGetMappingCoord2Country +#' @importFrom mstools toolGetMappingCoord2Country #' @importFrom magpiesets findset #' @importFrom magclass new.magpie #' @seealso [readSource()] diff --git a/R/readMehta2022.R b/R/readMehta2022.R index b825ba2a1..f8e247a70 100644 --- a/R/readMehta2022.R +++ b/R/readMehta2022.R @@ -9,7 +9,7 @@ #' } #' @importFrom terra aggregate project rast global #' @importFrom magclass as.magpie -#' @importFrom mrcommons toolGetMappingCoord2Country +#' @importFrom mstools toolGetMappingCoord2Country readMehta2022 <- function() { From 34b5961ba1527d936ca05523e2721fa635f4b7ee Mon Sep 17 00:00:00 2001 From: Jan Philipp Dietrich Date: Thu, 25 Apr 2024 12:23:33 +0200 Subject: [PATCH 2/4] - adjusted package to mrcommons split - fixed various linter warnings --- .buildlibrary | 6 +++--- CITATION.cff | 2 +- DESCRIPTION | 3 ++- NAMESPACE | 1 + R/imports.R | 2 +- README.md | 6 +++--- man/calcGrasslandBiomass.Rd | 3 ++- man/calcLabourProdImpact.Rd | 3 ++- man/calcPeatland.Rd | 7 +++++-- man/correctAvlLandSi.Rd | 3 +-- man/correctBendingTheCurve.Rd | 3 +-- man/correctSoilClassification.Rd | 3 +-- man/correctTransportDistance.Rd | 3 +-- 13 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 6fecfb6bd..9844faf2a 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,8 +1,8 @@ -ValidationKey: '28963480' +ValidationKey: '29161860' AutocreateReadme: yes AcceptedWarnings: -- 'Warning: package ''.*'' was built under R version' -- 'Warning: namespace ''.*'' is not available and has been replaced' +- 'Warning: package .* was built under R version' +- 'Warning: namespace .* is not available and has been replaced' AcceptedNotes: - unable to verify current time - includes the non-default packages diff --git a/CITATION.cff b/CITATION.cff index 0d14d0f74..b377ae13e 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -2,7 +2,7 @@ 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.46.0 +version: 1.47.0 date-released: '2024-04-25' abstract: Provides functions for MAgPIE country and cellular input data generation. authors: diff --git a/DESCRIPTION b/DESCRIPTION index d5a63acfa..3ca810964 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: mrmagpie Title: madrat based MAgPIE Input Data Library -Version: 1.46.0 +Version: 1.47.0 Date: 2024-04-25 Authors@R: c( person("Kristine", "Karstens", , "karstens@pik-potsdam.de", role = c("aut", "cre")), @@ -26,6 +26,7 @@ Depends: madrat (>= 2.8.0), magclass (>= 3.17), mrcommons (>= 1.40.7), + mrlandcore, mrland (>= 0.51.0), mrsoil (>= 2.0.0), mrwater (>= 1.12.0), diff --git a/NAMESPACE b/NAMESPACE index 484128c8b..ed82a4e2e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ import(magclass) import(magpiesets) import(mrcommons) import(mrland) +import(mrlandcore) import(mrsoil) import(mrwater) import(mstools) diff --git a/R/imports.R b/R/imports.R index 54954d121..1c672b1a5 100644 --- a/R/imports.R +++ b/R/imports.R @@ -1,4 +1,4 @@ # Generated by lucode2: do not edit by hand -#' @import madrat magclass mrcommons mrland mrsoil mrwater +#' @import madrat magclass mrcommons mrlandcore mrland mrsoil mrwater NULL diff --git a/README.md b/README.md index 72dc2b9f4..97ab070bc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat based MAgPIE Input Data Library -R package **mrmagpie**, version **1.46.0** +R package **mrmagpie**, version **1.47.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 , R package version 1.46.0, . +Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Köberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2024). _mrmagpie: madrat based MAgPIE Input Data Library_. doi: 10.5281/zenodo.4319612 (URL: https://doi.org/10.5281/zenodo.4319612), R package version 1.47.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 Alexandre Köberle and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Sauer}, year = {2024}, - note = {R package version 1.46.0}, + note = {R package version 1.47.0}, doi = {10.5281/zenodo.4319612}, url = {https://github.com/pik-piam/mrmagpie}, } diff --git a/man/calcGrasslandBiomass.Rd b/man/calcGrasslandBiomass.Rd index c5b28f184..5b82ed2c2 100644 --- a/man/calcGrasslandBiomass.Rd +++ b/man/calcGrasslandBiomass.Rd @@ -13,7 +13,8 @@ calcGrasslandBiomass(cells = "lpjcell") Regional biomass demand } \description{ -Calculates pasture biomass demand for the historical period split between rangelands and managed pastures. +Calculates pasture biomass demand for the historical period split +between rangelands andmanaged pastures. } \examples{ \dontrun{ diff --git a/man/calcLabourProdImpact.Rd b/man/calcLabourProdImpact.Rd index a6ae5a038..50f22add3 100644 --- a/man/calcLabourProdImpact.Rd +++ b/man/calcLabourProdImpact.Rd @@ -21,7 +21,8 @@ calcLabourProdImpact( \item{cells}{"magpiecell" or "lpjcell"} } \value{ -List of magpie objects with results on 0.5deg grid level, weights based on production value, unit (ratio) and description. +List of magpie objects with results on 0.5deg grid level, weights +based on production value, unit (ratio) and description. } \description{ Labour productivity impacts diff --git a/man/calcPeatland.Rd b/man/calcPeatland.Rd index 4d0aae750..3bce84aab 100644 --- a/man/calcPeatland.Rd +++ b/man/calcPeatland.Rd @@ -16,8 +16,11 @@ 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 (GPD) at the national level and downscales the peatland area to grid cell level using gridded potential peatland area. -The GPD has been provided by Alexandra Barthelmes. The potential peatland area has been provided by Leifeld_2018 (DOI 10.1038/s41467-018-03406-6). +The function takes degraded and intact peatland area from the Global Peatland Database +(GPD) at the national level and downscales the peatland area to grid cell level using +gridded potential peatland area. +The GPD has been provided by Alexandra Barthelmes. The potential peatland area has been +provided by Leifeld_2018 (DOI 10.1038/s41467-018-03406-6). } \examples{ \dontrun{ diff --git a/man/correctAvlLandSi.Rd b/man/correctAvlLandSi.Rd index 2ac811cca..ad552f6e9 100644 --- a/man/correctAvlLandSi.Rd +++ b/man/correctAvlLandSi.Rd @@ -16,9 +16,8 @@ List of magpie objects with results on cellular level, weight, unit and descript Read Available Land Si } \examples{ - \dontrun{ - readSource("AvlLandSi", convert="onlycorrect") +readSource("AvlLandSi", convert = "onlycorrect") } } diff --git a/man/correctBendingTheCurve.Rd b/man/correctBendingTheCurve.Rd index 5fbc8d478..a8d63fa17 100644 --- a/man/correctBendingTheCurve.Rd +++ b/man/correctBendingTheCurve.Rd @@ -16,9 +16,8 @@ List of magpie objects with results on cellular level, weight, unit and descript Read bending the curve data } \examples{ - \dontrun{ - readSource("BendingTheCurve", subtype="rr_layer", convert="onlycorrect") +readSource("BendingTheCurve", subtype = "rr_layer", convert = "onlycorrect") } } diff --git a/man/correctSoilClassification.Rd b/man/correctSoilClassification.Rd index 9824c15b7..b8c6df877 100644 --- a/man/correctSoilClassification.Rd +++ b/man/correctSoilClassification.Rd @@ -16,9 +16,8 @@ List of magpie objects with results on cellular level, weight, unit and descript Correct soil classification } \examples{ - \dontrun{ - readSource("SoilClassification", subtype="HWSD.soil", convert="onlycorrect") +readSource("SoilClassification", subtype = "HWSD.soil", convert = "onlycorrect") } } diff --git a/man/correctTransportDistance.Rd b/man/correctTransportDistance.Rd index 08200f105..25c28eb50 100644 --- a/man/correctTransportDistance.Rd +++ b/man/correctTransportDistance.Rd @@ -16,9 +16,8 @@ List of magpie objects with results on cellular level, weight, unit and descript Read transport distance file } \examples{ - \dontrun{ - readSource("TransportDistance", convert="onlycorrect") +readSource("TransportDistance", convert = "onlycorrect") } } From 6c6da72dd8c7cec83694809d05607fd547c8974b Mon Sep 17 00:00:00 2001 From: Pascal Sauer Date: Mon, 13 May 2024 15:36:28 +0200 Subject: [PATCH 3/4] importFrom mstools --- DESCRIPTION | 12 ++++++------ R/calcAfforestationMask.R | 5 +++-- R/calcDegradationYieldReduction.R | 6 +++--- R/calcLabourProdImpact.R | 5 ++--- R/calcLabourProdImpactEmu.R | 4 ++-- R/calcPeatland.R | 4 ++-- R/calcPeatland2.R | 8 ++++---- R/calcSoilCharacteristics.R | 3 ++- R/readGPM2.R | 4 ++-- R/readLeifeld2018.R | 4 ++-- 10 files changed, 28 insertions(+), 27 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index d60c2ae58..c1594719a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,11 +25,11 @@ BugReports: https://github.com/pik-piam/mrmagpie/issues Depends: madrat (>= 2.8.0), magclass (>= 3.17), - mrcommons (>= 1.40.7), - mrlandcore, - mrland (>= 0.51.0), + mrcommons (>= 1.41.0), + mrlandcore (>= 1.0.0), + mrland (>= 0.59.0), mrsoil (>= 2.0.0), - mrwater (>= 1.12.0), + mrwater (>= 1.13.0), R (>= 3.5.0) Imports: abind, @@ -38,9 +38,9 @@ Imports: dplyr, ggplot2, lpjclass, - luplot (>= 3.56.0), + luplot (>= 3.64.0), magpiesets, - mstools (>= 0.5.1), + mstools (>= 0.6.0), ncdf4, pbapply, raster, diff --git a/R/calcAfforestationMask.R b/R/calcAfforestationMask.R index 1904654fc..920416074 100644 --- a/R/calcAfforestationMask.R +++ b/R/calcAfforestationMask.R @@ -11,6 +11,7 @@ #' } #' #' @importFrom magclass as.magpie +#' @importFrom mstools toolGetMappingCoord2Country toolCoord2Isocell calcAfforestationMask <- function(subtype, cells = "lpjcell") { @@ -29,13 +30,13 @@ calcAfforestationMask <- function(subtype, cells = "lpjcell") { # get spatial mapping - map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) + map <- toolGetMappingCoord2Country(pretty = TRUE) # transform raster to magpie object x <- as.magpie(terra::extract(r, map[c("lon", "lat")])[, -1], spatial = 1) dimnames(x) <- list("x.y.iso" = paste(map$coords, map$iso, sep = "."), "t" = NULL, "d3" = NULL) if (cells == "magpiecell") { - x <- mstools::toolCoord2Isocell(x, cells = cells) + x <- toolCoord2Isocell(x, cells = cells) } weight <- calcOutput("LandArea", aggregate = FALSE, cells = cells) diff --git a/R/calcDegradationYieldReduction.R b/R/calcDegradationYieldReduction.R index abb6dbab2..4580bd659 100644 --- a/R/calcDegradationYieldReduction.R +++ b/R/calcDegradationYieldReduction.R @@ -11,11 +11,11 @@ #' calcOutput("DegradationYieldReduction", aggregate = FALSE) #' } #' @importFrom magpiesets findset -#' +#' @importFrom mstools toolGetMappingCoord2Country toolCoord2Isocell calcDegradationYieldReduction <- function(cells = "lpjcell") { # create a dummy data set, which is later used to define yield impacts of land degradation - coordMapping <- mstools::toolGetMappingCoord2Country() + coordMapping <- toolGetMappingCoord2Country() cellnames <- paste(coordMapping$coords, coordMapping$iso, sep = ".") x <- new.magpie(cells_and_regions = cellnames, years = seq(1995, 2150, 5), @@ -24,7 +24,7 @@ calcDegradationYieldReduction <- function(cells = "lpjcell") { fill = 0) if (cells == "magpiecell") { - x <- mstools::toolCoord2Isocell(x, cells = cells) + x <- toolCoord2Isocell(x, cells = cells) } return(list(x = x, diff --git a/R/calcLabourProdImpact.R b/R/calcLabourProdImpact.R index 2c084a515..d4bd392bb 100644 --- a/R/calcLabourProdImpact.R +++ b/R/calcLabourProdImpact.R @@ -10,8 +10,7 @@ #' @importFrom magpiesets findset #' @importFrom mstools toolHoldConstant #' @importFrom magclass collapseNames - - +#' @importFrom mstools toolCoord2Isocell calcLabourProdImpact <- function(timestep = "5year", subtype = "Orlov", cellular = TRUE, cells = "lpjcell") { if (subtype == "Orlov") { @@ -52,7 +51,7 @@ calcLabourProdImpact <- function(timestep = "5year", subtype = "Orlov", cellular } if (cells == "magpiecell") { - x <- mstools::toolCoord2Isocell(x, cells = cells) + x <- toolCoord2Isocell(x, cells = cells) } return(list(x = out, diff --git a/R/calcLabourProdImpactEmu.R b/R/calcLabourProdImpactEmu.R index fb8e4d206..922cf8aa9 100644 --- a/R/calcLabourProdImpactEmu.R +++ b/R/calcLabourProdImpactEmu.R @@ -8,7 +8,7 @@ #' @return List of magpie object of gridded (0.5) labour productivity as percentage of full labour prod 1 #' @author Michael Windisch, Florian Humpenöder #' @importFrom magclass dimSums mbind - +#' @importFrom mstools toolCoord2Isocell calcLabourProdImpactEmu <- function(timestep = "5year", cellular = TRUE, subtype = "impact", cells = "lpjcell") { @@ -55,7 +55,7 @@ calcLabourProdImpactEmu <- function(timestep = "5year", cellular = TRUE, avlCropAreaWeight[avlCropAreaWeight == 0] <- 10^-10 if (cells == "magpiecell") { - out <- mstools::toolCoord2Isocell(out, cells = cells, fillMissing = 1) + out <- toolCoord2Isocell(out, cells = cells, fillMissing = 1) } return(list(x = out, diff --git a/R/calcPeatland.R b/R/calcPeatland.R index 1162046d6..44e867375 100644 --- a/R/calcPeatland.R +++ b/R/calcPeatland.R @@ -18,7 +18,7 @@ #' #' @importFrom magclass clean_magpie #' @importFrom madrat toolGetMapping toolAggregate -#' @importFrom mstools toolIso2CellCountries +#' @importFrom mstools toolIso2CellCountries toolCoord2Isocell calcPeatland <- function(subtype = "degraded", cells = "lpjcell") { @@ -59,7 +59,7 @@ calcPeatland <- function(subtype = "degraded", cells = "lpjcell") { } if (cells == "magpiecell") { - x <- mstools::toolCoord2Isocell(x, cells = cells) + x <- toolCoord2Isocell(x, cells = cells) } return(list(x = x, diff --git a/R/calcPeatland2.R b/R/calcPeatland2.R index 0831bd74e..17261e2b9 100644 --- a/R/calcPeatland2.R +++ b/R/calcPeatland2.R @@ -14,7 +14,7 @@ #' } #' #' @importFrom madrat toolAggregate - +#' @importFrom mstools toolGetMappingCoord2Country toolIso2CellCountries toolCoord2Isocell calcPeatland2 <- function(cells = "magpiecell") { # Country-level data on intact and degraded peatland from Global Peatland Database for 2022 (GPD2022) gpd2022 <- readSource("GPD2022", convert = TRUE) @@ -23,14 +23,14 @@ calcPeatland2 <- function(cells = "magpiecell") { gpm2 <- readSource("GPM2", convert = "onlycorrect") # Dissag. GPD2022 from country to cell with GPM2 as weight - map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) - outCell <- toolAggregate(x = mstools::toolIso2CellCountries(gpd2022, cells = "lpjcell"), rel = map, + map <- toolGetMappingCoord2Country(pretty = TRUE) + outCell <- toolAggregate(x = 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)) if (cells == "magpiecell") { - outCell <- mstools::toolCoord2Isocell(outCell) + outCell <- toolCoord2Isocell(outCell) } description <- "Intact and degraded peatland area (Mha) by land-use type, based GPD 2022 and GPM2.0" diff --git a/R/calcSoilCharacteristics.R b/R/calcSoilCharacteristics.R index 636764c4c..0feed4f42 100644 --- a/R/calcSoilCharacteristics.R +++ b/R/calcSoilCharacteristics.R @@ -14,6 +14,7 @@ #' @importFrom raster area #' @importFrom dplyr left_join #' @importFrom raster crs<- +#' @importFrom mstools toolGetMappingCoord2Country #' calcSoilCharacteristics <- function() { @@ -23,7 +24,7 @@ calcSoilCharacteristics <- function() { # THIS FUNCTION NEEDS A MAJOR REWRITE # Creating aggregation weights for the physical variables # #Future implementation - # map <- mstools:::toolGetMappingCoord2Country() + # map <- toolGetMappingCoord2Country() # x <- new.magpie(cells_and_regions=map$coords) # getSets(x)[c(1,2)] <- c("x", "y") # x[,,1] <- 1 diff --git a/R/readGPM2.R b/R/readGPM2.R index 19d9129a2..f24a44777 100644 --- a/R/readGPM2.R +++ b/R/readGPM2.R @@ -7,7 +7,7 @@ #' readSource("GPM2", convert = "onlycorrect") #' } #' @importFrom magclass as.magpie - +#' @importFrom mstools toolGetMappingCoord2Country readGPM2 <- function() { previousOptions <- terra::terraOptions(print = FALSE) terra::terraOptions(tempdir = withr::local_tempdir(tmpdir = getConfig("tmpfolder")), @@ -34,7 +34,7 @@ readGPM2 <- function() { r3 <- terra::aggregate(a, fact = 48, fun = "sum", na.rm = TRUE) # get spatial mapping - map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) + map <- toolGetMappingCoord2Country(pretty = TRUE) # transform raster to magpie object x <- as.magpie(terra::extract(r3, map[c("lon", "lat")])[, -1], spatial = 1) diff --git a/R/readLeifeld2018.R b/R/readLeifeld2018.R index c4658993f..d6337aa3c 100644 --- a/R/readLeifeld2018.R +++ b/R/readLeifeld2018.R @@ -7,7 +7,7 @@ #' readSource("Leifeld2018", convert = "onlycorrect") #' } #' @importFrom magclass as.magpie - +#' @importFrom mstools toolGetMappingCoord2Country readLeifeld2018 <- function() { # projection is +proj=igh x <- terra::rast("Degradation_raster_homolosine_hires_rev4.tif") @@ -18,7 +18,7 @@ readLeifeld2018 <- function() { cellArea <- terra::cellSize(rp2[[1]], unit = "ha", mask = FALSE) * 1e-6 cellArea <- terra::mask(cellArea, rp2[[1]]) # get spatial mapping - map <- mstools::toolGetMappingCoord2Country(pretty = TRUE) + map <- toolGetMappingCoord2Country(pretty = TRUE) # transform raster to magpie object x <- as.magpie(terra::extract(cellArea, map[c("lon", "lat")])[, -1], spatial = 1) # set dimension names From ae5413854ee9ea4d6c08faddf27732fbd251a150 Mon Sep 17 00:00:00 2001 From: Pascal Sauer Date: Mon, 13 May 2024 16:06:30 +0200 Subject: [PATCH 4/4] build --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- README.md | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 1199447ec..4621c606b 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '' +ValidationKey: '29386880' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package .* was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 6a2a5b4da..78d9e34a1 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.47.1 -date-released: '2024-05-08' +version: 1.48.0 +date-released: '2024-05-13' abstract: Provides functions for MAgPIE country and cellular input data generation. authors: - family-names: Karstens diff --git a/DESCRIPTION b/DESCRIPTION index c1594719a..afa833810 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Type: Package Package: mrmagpie Title: madrat based MAgPIE Input Data Library -Version: 1.47.1.9001 -Date: 2024-05-08 +Version: 1.48.0 +Date: 2024-05-13 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/README.md b/README.md index 43205c7d3..d0c3ff389 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # madrat based MAgPIE Input Data Library -R package **mrmagpie**, version **1.47.1.9001** +R package **mrmagpie**, version **1.48.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 , R package version 1.47.1.9001, . +Karstens K, Dietrich J, Chen D, Windisch M, Alves M, Beier F, Köberle A, v. Jeetze P, Mishra A, Humpenoeder F, Sauer P (2024). _mrmagpie: madrat based MAgPIE Input Data Library_. doi:10.5281/zenodo.4319612 , R package version 1.48.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 Alexandre Köberle and Patrick {v. Jeetze} and Abhijeet Mishra and Florian Humpenoeder and Pascal Sauer}, year = {2024}, - note = {R package version 1.47.1.9001}, + note = {R package version 1.48.0}, url = {https://github.com/pik-piam/mrmagpie}, doi = {10.5281/zenodo.4319612}, }