Skip to content

Commit

Permalink
Merge pull request #43 from tscheypidi/hardcore
Browse files Browse the repository at this point in the history
adjusted package to mrcommons split
  • Loading branch information
pascal-sauer authored May 13, 2024
2 parents 34d63f0 + ae54138 commit f207a73
Show file tree
Hide file tree
Showing 42 changed files with 196 additions and 198 deletions.
6 changes: 3 additions & 3 deletions .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ValidationKey: '29200821'
ValidationKey: '29386880'
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
Expand Down
4 changes: 2 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: '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
Expand Down
15 changes: 8 additions & 7 deletions DESCRIPTION
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.47.1
Date: 2024-05-08
Version: 1.48.0
Date: 2024-05-13
Authors@R: c(
person("Kristine", "Karstens", , "[email protected]", role = c("aut", "cre")),
person("Jan Philipp", "Dietrich", , "[email protected]", role = "aut"),
Expand All @@ -25,10 +25,11 @@ BugReports: https://github.com/pik-piam/mrmagpie/issues
Depends:
madrat (>= 2.8.0),
magclass (>= 3.17),
mrcommons (>= 1.37.0),
mrland (>= 0.51.0),
mrcommons (>= 1.41.0),
mrlandcore (>= 1.0.0),
mrland (>= 0.59.0),
mrsoil (>= 2.0.0),
mrwater (>= 1.11.0),
mrwater (>= 1.13.0),
R (>= 3.5.0)
Imports:
abind,
Expand All @@ -37,9 +38,9 @@ Imports:
dplyr,
ggplot2,
lpjclass,
luplot (>= 3.56.0),
luplot (>= 3.64.0),
magpiesets,
mstools,
mstools (>= 0.6.0),
ncdf4,
pbapply,
raster,
Expand Down
15 changes: 8 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import(magclass)
import(magpiesets)
import(mrcommons)
import(mrland)
import(mrlandcore)
import(mrsoil)
import(mrwater)
import(mstools)
Expand Down Expand Up @@ -71,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)
Expand Down
15 changes: 8 additions & 7 deletions R/calcAfforestationMask.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@
#' }
#'
#' @importFrom magclass as.magpie
#' @importFrom mstools toolGetMappingCoord2Country toolCoord2Isocell

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 <- 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 <- toolCoord2Isocell(x, cells = cells)
}

weight <- calcOutput("LandArea", aggregate = FALSE, cells = cells)
Expand Down
4 changes: 2 additions & 2 deletions R/calcAgeClassDistribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ".")
Expand Down
1 change: 1 addition & 0 deletions R/calcAreaEquippedForIrrigation.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#' calcOutput("AreaEquippedForIrrigation", source = "LUH2v2", cellular = TRUE, aggregate = FALSE)
#' }
#' @importFrom magpiesets findset
#' @importFrom mstools toolCoord2Isocell
#'
#' @export

Expand Down
6 changes: 3 additions & 3 deletions R/calcAvlLandSi.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
#'

Expand Down Expand Up @@ -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))
}
6 changes: 3 additions & 3 deletions R/calcDegradationYieldReduction.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <- mrcommons::toolGetMappingCoord2Country()
coordMapping <- toolGetMappingCoord2Country()
cellnames <- paste(coordMapping$coords, coordMapping$iso, sep = ".")
x <- new.magpie(cells_and_regions = cellnames,
years = seq(1995, 2150, 5),
Expand All @@ -24,7 +24,7 @@ calcDegradationYieldReduction <- function(cells = "lpjcell") {
fill = 0)

if (cells == "magpiecell") {
x <- mrcommons::toolCoord2Isocell(x, cells = cells)
x <- toolCoord2Isocell(x, cells = cells)
}

return(list(x = x,
Expand Down
3 changes: 2 additions & 1 deletion R/calcEFRRockstroem.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion R/calcEFRSmakthin.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion R/calcEnvmtlFlow.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 11 additions & 17 deletions R/calcGrasslandBiomass.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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")

Expand All @@ -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",
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions R/calcIrrigation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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") {
Expand Down
Loading

0 comments on commit f207a73

Please sign in to comment.