From 6afca49ac5e0b8fa91da90a450eabf48681fb39b Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Wed, 4 Mar 2026 15:57:19 +0100 Subject: [PATCH 01/36] update croparea --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/calcCroparea.R | 294 ++++++++++++-------------------------- R/calcCropareaFAOLUH.R | 224 +++++++++++++++++++++++++++++ README.md | 8 +- man/calcCroparea.Rd | 21 ++- man/calcCropareaFAOLUH.Rd | 44 ++++++ man/downloadGAEZv4.Rd | 2 +- 9 files changed, 383 insertions(+), 220 deletions(-) create mode 100644 R/calcCropareaFAOLUH.R create mode 100644 man/calcCropareaFAOLUH.Rd diff --git a/.buildlibrary b/.buildlibrary index 4a54f2f..746f1fa 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3375900' +ValidationKey: '34036064516' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index ef70907..395c9e3 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.5 -date-released: '2026-01-07' +version: 1.6.5.9001 +date-released: '2026-03-04' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index de3977d..039716c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.5 -Date: 2026-01-07 +Version: 1.6.5.9001 +Date: 2026-03-04 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 4f3fe37..cb29954 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -1,223 +1,119 @@ #' @title calcCroparea -#' @description Returns harvested areas of individual crops from FAOSTAT. -#' Total harvested areas can be lower or higher than arable -#' land because of multicropping or fallow land. -#' Rice areas are distributed to flooded LUH areas. Additional FAOSTAT -#' rice areas are distributed based on country shares. +#' @description returns croparea #' -#' @param sectoral "area_harvested" returns croparea aggregated to FAO products, -#' "ProductionItem" unaggregated ProdSTAT items, -#' "FoodBalanceItem" Food Balance Sheet categories, -#' "kcr" MAgPIE items, and "lpj" LPJmL items -#' @param physical if TRUE the sum over all crops agrees with the cropland area per country -#' @param cellular if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. -#' Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) -#' are mapped to MAgpIE crop types using "FAO2LUH2MAG_croptypes" and doing -#' an intermediate step via harvested areas of FAO weight area within a -#' specific LUH crop type to divide into MAgPIE crop types. -#' @param irrigation If true: cellular areas are returned separated -#' into irrigated and rainfed (see setup in calcLUH3) +#' @param sectoral "kcr" MAgPIE items, and "lpj" LPJmL items +#' @param physical if TRUE the sum over all crops agrees with the cropland area per country +#' @param cellular if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. +#' Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) +#' are mapped to MAgpIE crop types using "FAO2LUH2MAG_croptypes" and doing +#' an intermediate step via harvested areas of FAO weight area within a +#' specific LUH crop type to divide into MAgPIE crop types. +#' @param irrigation If true: cellular areas are returned separated +#' into irrigated and rainfed (see setup in calcLUH3) +#' @param selectyears Vector of years to be returned +#' @param datasource Croparea data source (LandInG or FAOLUH). +#' Datasource FAOLUH is deprecated and is only kept for +#' backwards compatibility. #' #' @return areas of individual crops from FAOSTAT and weight #' -#' @author Ulrich Kreidenweis, Kristine Karstens, Felicitas Beier +#' @author Felicitas Beier #' -#' @importFrom utils read.csv -#' @importFrom magclass setNames getCells collapseDim getItems -#' @importFrom magpiesets findset addLocation -#' @importFrom madrat toolAggregate toolGetMapping -#' @importFrom mrfaocore toolExtrapolateFodder toolFAOcombine -#' @importFrom utils person -#' @importFrom withr local_options +#' @importFrom magpiesets findset -calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, irrigation = FALSE) { +calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, + irrigation = FALSE, selectyears = "past", datasource = "LandInG") { - local_options(magclass_sizeLimit = 1e+10) - - if (!cellular) { - - if (irrigation) stop("Irrigation levels for country based data not yet implemented!") - - ################################# - ### Croparea on country level ### - ################################# - - if (!is.null(sectoral) && !(sectoral == "lpj")) { - - cropPrim <- readSource("FAO_online", "CropLive2010")[, , "area_harvested"] - # use linear_interpolate - fodder <- readSource("FAO", "Fodder")[, , "area_harvested"] - fodder <- toolExtrapolateFodder(fodder, endyear = max(getYears(cropPrim, as.integer = TRUE))) - data <- toolFAOcombine(cropPrim, fodder) / 10^6 # convert to Mha - - if (sectoral %in% c("FoodBalanceItem", "kcr")) { - - aggregation <- toolGetMapping("FAOitems_online_2010update.csv", type = "sectoral", - where = "mrfaocore") - remove <- setdiff(getNames(data, dim = 1), aggregation$post2010_ProductionItem) - data <- data[, , remove, invert = TRUE] - data <- toolAggregate(data, rel = aggregation, from = "post2010_ProductionItem", - to = ifelse(sectoral == "kcr", "k", sectoral), - dim = 3.1, partrel = TRUE) - - if (sectoral == "kcr") { - # add bioenergy with 0 values - data <- add_columns(x = data, addnm = c("betr", "begr"), dim = 3.1) - data[, , c("betr", "begr")] <- 0 - - # remove all non kcr items - kcr <- findset("kcr") - remove <- setdiff(getItems(data, dim = 3.1), kcr) - - if (length(remove) > 0) { - remainArea <- mean(dimSums(data[, , "area_harvested"][, , remove], dim = 1) / - dimSums(dimSums(data[, , "area_harvested"], dim = 3), dim = 1)) - if (remainArea > 0.02) vcat(1, "Aggregation created a 'remaining' category. The area harvested is", - round(remainArea, digits = 3) * 100, "% of total \n") - vcat(2, paste0("Data for the following items removed: ", remove)) - data <- data[, , kcr] - } - } - - } else if (sectoral != "ProductionItem") { - stop("Sectoral aggregation not supported") - } - - } else if (sectoral == "lpj") { - - magCroparea <- calcOutput("Croparea", sectoral = "kcr", physical = physical, - cellular = FALSE, irrigation = FALSE, aggregate = FALSE) - - mag2lpj <- toolGetMapping(type = "sectoral", name = "MAgPIE_LPJmL.csv", - where = "mappingfolder") - mag2lpj <- mag2lpj[!(mag2lpj$MAgPIE == "pasture"), ] - - lpjCroparea <- toolAggregate(magCroparea, rel = mag2lpj, from = "MAgPIE", to = "LPJmL", dim = 3.1) - data <- lpjCroparea + if (selectyears == "past") { + selectyears <- findset("past") + } - } else { - stop("Sectoral aggregation not supported") - } + if (datasource == "LandInG") { + # read in croparea + croparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = physical, + cellular = TRUE, irrigation = irrigation, + selectyears = selectyears, aggregate = FALSE) + + # description of data to be returned + description <- paste0(ifelse(physical, "physical ", "harvested "), + "croparea from LandInG data set") + + # correction to match LUH cropland area + physCroparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, + cellular = TRUE, irrigation = FALSE, + selectyears = selectyears, aggregate = FALSE) + fallowLand <- calcOutput("FallowLand", cellular = TRUE, aggregate = FALSE) + luh3Croparea <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, + cellular = TRUE, yrs = selectyears, aggregate = FALSE) + + mismatch <- dimSums(physCroparea, dim = 3) - luh3Croparea + ratio <- luh3Croparea / physCroparea + + + + # fallow land + crop area = LUH cropland + # do this at cellular, then aggregate + + # check whether physical croparea matches LUH total land area + # LUH3 landarea must >= physical crop area + + # Check for landmass mismatch: Total physical croparea should be smaller than landmass + # Total physical croparea + physTotal <- dimSums(physCroparea, dim = 3) + # Total land area according to LUH3 in iniyear (constant over time) + landarea <- dimSums(luh3Croparea, dim = 3) + + if (any(round(landarea - physTotal, digits = 6) < 0)) { + # Note: Due to mismatches in the land masks used in LandInG + # and LUH, croparea may exceed total landarea. + stop("This should no longer be the case after the LandInG update. + Please check where mismatch is coming from starting from calcCropareaAdjusted.") + vcat(verbosity = 0, + paste0("In calcCropareaAdjusted: There is a mismatch in the landmask underlying + the ", dataset, " croparea dataset and LUH. + Croparea is cut here to fit into the landmass as reported by LUH, + but a more generic solution should be found to make the data consistent!")) + + ratio <- ifelse(landarea - physTotal < 0, + landarea / physTotal, + 1) + # Scale down crop-specific croparea by mismatch area + phys <- phys * ratio - # use the share of the single crops to calculate their "physical" area - if (physical) { - # 6620 = (6620|Arable land and Permanent crops or 6620|Cropland) - cropland <- setNames(collapseNames(calcOutput("FAOLand", - aggregate = FALSE)[, , "6620", pmatch = TRUE]), "crop") - harvestedShare <- data / dimSums(data, dim = 3.1) - commonyears <- intersect(getYears(cropland), getYears(harvestedShare)) - data <- collapseNames(cropland[, commonyears, ] * harvestedShare[, commonyears, ]) } - data[is.na(data)] <- 0 - - } else { - ################################## - ### Croparea on cellular level ### - ################################## - - if (sectoral == "kcr") { - # LUH related data input on cell level - luhWeights <- calcOutput("LUH2MAgPIE", share = "MAGofLUH", - missing = "fill", rice = "non_flooded", aggregate = FALSE) - - luhCroptypes <- c("c3ann", "c4ann", "c3per", "c4per", "c3nfx") - - luhCroparea <- calcOutput("LUH3", landuseTypes = "LUH3", aggregate = FALSE, - irrigation = irrigation, cellular = TRUE)[, , luhCroptypes] - commonYears <- intersect(getYears(luhWeights), getYears(luhCroparea)) - luhWeights <- luhWeights[, commonYears, ] - luhCroparea <- luhCroparea[, commonYears, ] - commonCountries <- intersect(getItems(luhWeights, dim = "ISO"), getItems(luhCroparea, dim = "iso")) - - # corrected rice area (in Mha) - ricearea <- calcOutput("Ricearea", cellular = TRUE, - share = FALSE, aggregate = FALSE) - - commonYears <- intersect(getYears(ricearea), getYears(luhCroparea)) - luhWeights <- luhWeights[, commonYears, ] - luhCroparea <- luhCroparea[, commonYears, ] - ricearea <- ricearea[, commonYears, ] - - # irrigation - if (irrigation == TRUE) { - # for check - luhCropareaTotal <- dimSums(luhCroparea, dim = 3) - - # calculate irrigation share for rice area correction - irrigShr <- new.magpie(cells_and_regions = getCells(luhCroparea), - years = getYears(luhCroparea), - names = getNames(luhCroparea), fill = NA) - luhCroparea <- add_columns(luhCroparea, addnm = "total", dim = 3.2, fill = NA) - luhCroparea[, , "total"] <- dimSums(luhCroparea[, , c("rainfed", "irrigated")], dim = 3.2) - - irrigShr[, , "irrigated"] <- collapseNames(ifelse(luhCroparea[, , "total"] > 0, - luhCroparea[, , "irrigated"] / luhCroparea[, , "total"], 0)) - irrigShr[, , "rainfed"] <- 1 - collapseNames(irrigShr[, , "irrigated"]) - - # flooded rice areas - floodedRice <- collapseNames(ricearea[, , "flooded"] * irrigShr[, , "c3ann"]) - - luhCroparea <- luhCroparea[, , "total", invert = TRUE] - - } else { - # for check - luhCropareaTotal <- dimSums(luhCroparea, dim = 3) - - # flooded rice areas - floodedRice <- collapseNames(ricearea[, , "flooded"]) - - } - - # temporarily exclude flooded rice for distribution of other crops and aerobic rice areas - luhCroparea[, , "c3ann"] <- luhCroparea[, , "c3ann"] - floodedRice - - # correction of LUH cropareas with FAO country shares - luh2mag <- luhCroparea * luhWeights[commonCountries, , ] - magCroparea <- dimSums(luh2mag, dim = 3.1) - - # total rice area correction - magCroparea[, , "rice_pro"] <- magCroparea[, , "rice_pro"] + floodedRice - - # check sums - if (any(round(abs(dimSums(magCroparea, dim = 3) - luhCropareaTotal), digits = 6) > 1e-6)) { - stop("Sums after rice correction in calcCroparea don't match!") - } - - data <- collapseNames(magCroparea) - - } else if (sectoral == "lpj") { - - magCroparea <- calcOutput("Croparea", sectoral = "kcr", physical = physical, - cellular = TRUE, irrigation = irrigation, aggregate = FALSE) - mag2lpj <- toolGetMapping(type = "sectoral", name = "MAgPIE_LPJmL.csv", - where = "mappingfolder") - mag2lpj <- mag2lpj[!(mag2lpj$MAgPIE == "pasture"), ] - lpjCroparea <- toolAggregate(magCroparea, rel = mag2lpj, from = "MAgPIE", to = "LPJmL", dim = "MAG") - data <- lpjCroparea - - } else { - stop("Not possible (for now) for the given item set (sectoral)!") + # Aggregation to iso-level + if (!cellular) { + # aggregate to countries + croparea <- dimSums(croparea, dim = c("x", "y")) + # fill missing countries with 0 + croparea <- toolConditionalReplace(x = toolCountryFill(croparea), + conditions = "is.na()", replaceby = 0) } - if (!physical) { + } else if (datasource == "FAOLUH") { - multiCropping <- calcOutput("Multicropping", aggregate = FALSE) - commonCountries <- intersect(getItems(multiCropping, dim = "ISO"), getItems(data, dim = "iso")) - data <- data * multiCropping[commonCountries, getYears(data), ] - } - } + ### For backwards compatibility only ### + # This chunk can be deleted when croparea update is completed. - data <- collapseNames(data) + # read in croparea + croparea <- calcOutput("CropareaFAOLUH", sectoral = sectoral, physical = physical, + cellular = cellular, irrigation = irrigation, + aggregate = FALSE) + croparea <- croparea[, selectyears, ] - # not more precision than 1 ha needed. very small areas can make problems in some weighting scripts - data <- round(data, 6) + # description of data to be returned + description <- paste0(ifelse(physical, "physical ", "harvested "), + "croparea from LUH and FAOSTAT") + } else { + stop("Selected data source in calcCroparea unknown.") + } - return(list(x = data, + return(list(x = croparea, weight = NULL, unit = "million ha", - description = "harvested crop areas from FAOSTAT", + description = description, isocountries = !cellular)) } diff --git a/R/calcCropareaFAOLUH.R b/R/calcCropareaFAOLUH.R new file mode 100644 index 0000000..63d3ef8 --- /dev/null +++ b/R/calcCropareaFAOLUH.R @@ -0,0 +1,224 @@ +#' @title calcCropareaFAOLUH +#' @description Returns croparea from LUH (for cellular resolution) and +#' FAOSTAT (for country-level resolution). +#' Total harvested areas can be lower or higher than arable +#' land because of multicropping or fallow land. +#' Rice areas are distributed to flooded LUH areas. Additional FAOSTAT +#' rice areas are distributed based on country shares. +#' +#' @param sectoral "area_harvested" returns croparea aggregated to FAO products, +#' "ProductionItem" unaggregated ProdSTAT items, +#' "FoodBalanceItem" Food Balance Sheet categories, +#' "kcr" MAgPIE items, and "lpj" LPJmL items +#' @param physical if TRUE the sum over all crops agrees with the cropland area per country +#' @param cellular if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. +#' Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) +#' are mapped to MAgpIE crop types using "FAO2LUH2MAG_croptypes" and doing +#' an intermediate step via harvested areas of FAO weight area within a +#' specific LUH crop type to divide into MAgPIE crop types. +#' @param irrigation If true: cellular areas are returned separated +#' into irrigated and rainfed (see setup in calcLUH3) +#' +#' @return areas of individual crops from FAOSTAT and weight +#' +#' @author Ulrich Kreidenweis, Kristine Karstens, Felicitas Beier +#' +#' @importFrom utils read.csv +#' @importFrom magclass setNames getCells collapseDim getItems +#' @importFrom magpiesets findset addLocation +#' @importFrom madrat toolAggregate toolGetMapping +#' @importFrom mrfaocore toolExtrapolateFodder toolFAOcombine +#' @importFrom utils person +#' @importFrom withr local_options + +calcCropareaFAOLUH <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, irrigation = FALSE) { + + local_options(magclass_sizeLimit = 1e+10) + + if (!cellular) { + + if (irrigation) stop("Irrigation levels for country based data not yet implemented!") + + ################################# + ### Croparea on country level ### + ################################# + + if (!is.null(sectoral) && !(sectoral == "lpj")) { + + cropPrim <- readSource("FAO_online", "CropLive2010")[, , "area_harvested"] + # use linear_interpolate + fodder <- readSource("FAO", "Fodder")[, , "area_harvested"] + fodder <- toolExtrapolateFodder(fodder, endyear = max(getYears(cropPrim, as.integer = TRUE))) + data <- toolFAOcombine(cropPrim, fodder) / 10^6 # convert to Mha + + if (sectoral %in% c("FoodBalanceItem", "kcr")) { + + aggregation <- toolGetMapping("FAOitems_online_2010update.csv", type = "sectoral", + where = "mrfaocore") + remove <- setdiff(getNames(data, dim = 1), aggregation$post2010_ProductionItem) + data <- data[, , remove, invert = TRUE] + data <- toolAggregate(data, rel = aggregation, from = "post2010_ProductionItem", + to = ifelse(sectoral == "kcr", "k", sectoral), + dim = 3.1, partrel = TRUE) + + if (sectoral == "kcr") { + # add bioenergy with 0 values + data <- add_columns(x = data, addnm = c("betr", "begr"), dim = 3.1) + data[, , c("betr", "begr")] <- 0 + + # remove all non kcr items + kcr <- findset("kcr") + remove <- setdiff(getItems(data, dim = 3.1), kcr) + + if (length(remove) > 0) { + remainArea <- mean(dimSums(data[, , "area_harvested"][, , remove], dim = 1) / + dimSums(dimSums(data[, , "area_harvested"], dim = 3), dim = 1)) + if (remainArea > 0.02) vcat(1, "Aggregation created a 'remaining' category. The area harvested is", + round(remainArea, digits = 3) * 100, "% of total \n") + vcat(2, paste0("Data for the following items removed: ", remove)) + data <- data[, , kcr] + } + } + + } else if (sectoral != "ProductionItem") { + stop("Sectoral aggregation not supported") + } + + } else if (sectoral == "lpj") { + + magCroparea <- calcOutput("Croparea", sectoral = "kcr", physical = physical, + cellular = FALSE, irrigation = FALSE, aggregate = FALSE) + + mag2lpj <- toolGetMapping(type = "sectoral", name = "MAgPIE_LPJmL.csv", + where = "mappingfolder") + mag2lpj <- mag2lpj[!(mag2lpj$MAgPIE == "pasture"), ] + + lpjCroparea <- toolAggregate(magCroparea, rel = mag2lpj, from = "MAgPIE", to = "LPJmL", dim = 3.1) + data <- lpjCroparea + + } else { + stop("Sectoral aggregation not supported") + } + + # use the share of the single crops to calculate their "physical" area + if (physical) { + # 6620 = (6620|Arable land and Permanent crops or 6620|Cropland) + cropland <- setNames(collapseNames(calcOutput("FAOLand", + aggregate = FALSE)[, , "6620", pmatch = TRUE]), "crop") + harvestedShare <- data / dimSums(data, dim = 3.1) + commonyears <- intersect(getYears(cropland), getYears(harvestedShare)) + data <- collapseNames(cropland[, commonyears, ] * harvestedShare[, commonyears, ]) + } + + data[is.na(data)] <- 0 + + } else { + ################################## + ### Croparea on cellular level ### + ################################## + + if (sectoral == "kcr") { + # LUH related data input on cell level + luhWeights <- calcOutput("LUH2MAgPIE", share = "MAGofLUH", + missing = "fill", rice = "non_flooded", aggregate = FALSE) + + luhCroptypes <- c("c3ann", "c4ann", "c3per", "c4per", "c3nfx") + + luhCroparea <- calcOutput("LUH3", landuseTypes = "LUH3", aggregate = FALSE, + irrigation = irrigation, cellular = TRUE)[, , luhCroptypes] + + commonYears <- intersect(getYears(luhWeights), getYears(luhCroparea)) + luhWeights <- luhWeights[, commonYears, ] + luhCroparea <- luhCroparea[, commonYears, ] + + commonCountries <- intersect(getItems(luhWeights, dim = "ISO"), getItems(luhCroparea, dim = "iso")) + + # corrected rice area (in Mha) + ricearea <- calcOutput("Ricearea", cellular = TRUE, + share = FALSE, aggregate = FALSE) + + commonYears <- intersect(getYears(ricearea), getYears(luhCroparea)) + luhWeights <- luhWeights[, commonYears, ] + luhCroparea <- luhCroparea[, commonYears, ] + ricearea <- ricearea[, commonYears, ] + + # irrigation + if (irrigation == TRUE) { + # for check + luhCropareaTotal <- dimSums(luhCroparea, dim = 3) + + # calculate irrigation share for rice area correction + irrigShr <- new.magpie(cells_and_regions = getCells(luhCroparea), + years = getYears(luhCroparea), + names = getNames(luhCroparea), fill = NA) + luhCroparea <- add_columns(luhCroparea, addnm = "total", dim = 3.2, fill = NA) + luhCroparea[, , "total"] <- dimSums(luhCroparea[, , c("rainfed", "irrigated")], dim = 3.2) + + irrigShr[, , "irrigated"] <- collapseNames(ifelse(luhCroparea[, , "total"] > 0, + luhCroparea[, , "irrigated"] / luhCroparea[, , "total"], 0)) + irrigShr[, , "rainfed"] <- 1 - collapseNames(irrigShr[, , "irrigated"]) + + # flooded rice areas + floodedRice <- collapseNames(ricearea[, , "flooded"] * irrigShr[, , "c3ann"]) + + luhCroparea <- luhCroparea[, , "total", invert = TRUE] + + } else { + # for check + luhCropareaTotal <- dimSums(luhCroparea, dim = 3) + + # flooded rice areas + floodedRice <- collapseNames(ricearea[, , "flooded"]) + + } + + # temporarily exclude flooded rice for distribution of other crops and aerobic rice areas + luhCroparea[, , "c3ann"] <- luhCroparea[, , "c3ann"] - floodedRice + + # correction of LUH cropareas with FAO country shares + luh2mag <- luhCroparea * luhWeights[commonCountries, , ] + magCroparea <- dimSums(luh2mag, dim = 3.1) + + # total rice area correction + magCroparea[, , "rice_pro"] <- magCroparea[, , "rice_pro"] + floodedRice + + # check sums + if (any(round(abs(dimSums(magCroparea, dim = 3) - luhCropareaTotal), digits = 6) > 1e-6)) { + stop("Sums after rice correction in calcCroparea don't match!") + } + + data <- collapseNames(magCroparea) + + } else if (sectoral == "lpj") { + + magCroparea <- calcOutput("Croparea", sectoral = "kcr", physical = physical, + cellular = TRUE, irrigation = irrigation, aggregate = FALSE) + mag2lpj <- toolGetMapping(type = "sectoral", name = "MAgPIE_LPJmL.csv", + where = "mappingfolder") + mag2lpj <- mag2lpj[!(mag2lpj$MAgPIE == "pasture"), ] + lpjCroparea <- toolAggregate(magCroparea, rel = mag2lpj, from = "MAgPIE", to = "LPJmL", dim = "MAG") + data <- lpjCroparea + + } else { + stop("Not possible (for now) for the given item set (sectoral)!") + } + + if (!physical) { + + multiCropping <- calcOutput("Multicropping", aggregate = FALSE) + commonCountries <- intersect(getItems(multiCropping, dim = "ISO"), getItems(data, dim = "iso")) + data <- data * multiCropping[commonCountries, getYears(data), ] + } + } + + data <- collapseNames(data) + + # not more precision than 1 ha needed. very small areas can make problems in some weighting scripts + data <- round(data, 6) + + return(list(x = data, + weight = NULL, + unit = "million ha", + description = "croparea", + isocountries = !cellular)) +} diff --git a/README.md b/README.md index e5aad14..3bcde3a 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.5** +R package **mrlandcore**, version **1.6.5.9001** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.5.9001, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-01-07}, + date = {2026-03-04}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.5}, + note = {Version: 1.6.5.9001}, } ``` diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index f4ce18b..08de33f 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -8,14 +8,13 @@ calcCroparea( sectoral = "kcr", physical = TRUE, cellular = FALSE, - irrigation = FALSE + irrigation = FALSE, + selectyears = "past", + datasource = "LandInG" ) } \arguments{ -\item{sectoral}{"area_harvested" returns croparea aggregated to FAO products, -"ProductionItem" unaggregated ProdSTAT items, -"FoodBalanceItem" Food Balance Sheet categories, -"kcr" MAgPIE items, and "lpj" LPJmL items} +\item{sectoral}{"kcr" MAgPIE items, and "lpj" LPJmL items} \item{physical}{if TRUE the sum over all crops agrees with the cropland area per country} @@ -27,17 +26,17 @@ specific LUH crop type to divide into MAgPIE crop types.} \item{irrigation}{If true: cellular areas are returned separated into irrigated and rainfed (see setup in calcLUH3)} + +\item{selectyears}{Vector of years to be returned} + +\item{datasource}{Croparea data source (LandInG or FAOLUH)} } \value{ areas of individual crops from FAOSTAT and weight } \description{ -Returns harvested areas of individual crops from FAOSTAT. - Total harvested areas can be lower or higher than arable - land because of multicropping or fallow land. - Rice areas are distributed to flooded LUH areas. Additional FAOSTAT - rice areas are distributed based on country shares. +returns croparea } \author{ -Ulrich Kreidenweis, Kristine Karstens, Felicitas Beier +Felicitas Beier } diff --git a/man/calcCropareaFAOLUH.Rd b/man/calcCropareaFAOLUH.Rd new file mode 100644 index 0000000..de53ff9 --- /dev/null +++ b/man/calcCropareaFAOLUH.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcCropareaFAOLUH.R +\name{calcCropareaFAOLUH} +\alias{calcCropareaFAOLUH} +\title{calcCropareaFAOLUH} +\usage{ +calcCropareaFAOLUH( + sectoral = "kcr", + physical = TRUE, + cellular = FALSE, + irrigation = FALSE +) +} +\arguments{ +\item{sectoral}{"area_harvested" returns croparea aggregated to FAO products, +"ProductionItem" unaggregated ProdSTAT items, +"FoodBalanceItem" Food Balance Sheet categories, +"kcr" MAgPIE items, and "lpj" LPJmL items} + +\item{physical}{if TRUE the sum over all crops agrees with the cropland area per country} + +\item{cellular}{if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. +Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) +are mapped to MAgpIE crop types using "FAO2LUH2MAG_croptypes" and doing +an intermediate step via harvested areas of FAO weight area within a +specific LUH crop type to divide into MAgPIE crop types.} + +\item{irrigation}{If true: cellular areas are returned separated +into irrigated and rainfed (see setup in calcLUH3)} +} +\value{ +areas of individual crops from FAOSTAT and weight +} +\description{ +Returns croparea from LUH (for cellular resolution) and + FAOSTAT (for country-level resolution). + Total harvested areas can be lower or higher than arable + land because of multicropping or fallow land. + Rice areas are distributed to flooded LUH areas. Additional FAOSTAT + rice areas are distributed based on country shares. +} +\author{ +Ulrich Kreidenweis, Kristine Karstens, Felicitas Beier +} diff --git a/man/downloadGAEZv4.Rd b/man/downloadGAEZv4.Rd index d662195..8a43228 100644 --- a/man/downloadGAEZv4.Rd +++ b/man/downloadGAEZv4.Rd @@ -8,7 +8,7 @@ downloadGAEZv4(subtype = NULL) } \arguments{ \item{subtype}{Name of file to be downloaded -Available are for example: +Available subtypes are for example: "mci_CRUTS32_Hist_0010.tif" (irrigated multiple cropping suitability for years 2000-2010), "mcr_CRUTS32_Hist_0010.tif" (rainfed multiple cropping suitability for years 2000-2010)} } From a1c57d176f2406cf987c3415528c9c27ab91a8c9 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Wed, 4 Mar 2026 17:49:27 +0100 Subject: [PATCH 02/36] Croparea function bugfixes --- R/calcCroparea.R | 70 ++++++++++++++------------------------ R/calcCropareaLandInG.R | 12 +------ man/calcCroparea.Rd | 6 ++-- man/calcCropareaLandInG.Rd | 3 -- 4 files changed, 31 insertions(+), 60 deletions(-) diff --git a/R/calcCroparea.R b/R/calcCroparea.R index cb29954..708fc68 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -17,7 +17,7 @@ #' #' @return areas of individual crops from FAOSTAT and weight #' -#' @author Felicitas Beier +#' @author Felicitas Beier, Benjamin Leon Bodirsky #' #' @importFrom magpiesets findset @@ -39,50 +39,32 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, "croparea from LandInG data set") # correction to match LUH cropland area - physCroparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, - cellular = TRUE, irrigation = FALSE, - selectyears = selectyears, aggregate = FALSE) - fallowLand <- calcOutput("FallowLand", cellular = TRUE, aggregate = FALSE) - luh3Croparea <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, - cellular = TRUE, yrs = selectyears, aggregate = FALSE) - - mismatch <- dimSums(physCroparea, dim = 3) - luh3Croparea - ratio <- luh3Croparea / physCroparea - - - - # fallow land + crop area = LUH cropland - # do this at cellular, then aggregate - - # check whether physical croparea matches LUH total land area - # LUH3 landarea must >= physical crop area - - # Check for landmass mismatch: Total physical croparea should be smaller than landmass - # Total physical croparea - physTotal <- dimSums(physCroparea, dim = 3) - # Total land area according to LUH3 in iniyear (constant over time) - landarea <- dimSums(luh3Croparea, dim = 3) - - if (any(round(landarea - physTotal, digits = 6) < 0)) { - # Note: Due to mismatches in the land masks used in LandInG - # and LUH, croparea may exceed total landarea. - stop("This should no longer be the case after the LandInG update. - Please check where mismatch is coming from starting from calcCropareaAdjusted.") - vcat(verbosity = 0, - paste0("In calcCropareaAdjusted: There is a mismatch in the landmask underlying - the ", dataset, " croparea dataset and LUH. - Croparea is cut here to fit into the landmass as reported by LUH, - but a more generic solution should be found to make the data consistent!")) - - ratio <- ifelse(landarea - physTotal < 0, - landarea / physTotal, - 1) - # Scale down crop-specific croparea by mismatch area - phys <- phys * ratio - + physCroparea <- dimSums(calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, + cellular = TRUE, irrigation = FALSE, + selectyears = selectyears, aggregate = FALSE), dim = "crop") + fallowLand <- calcOutput("FallowLand", cellular = TRUE, aggregate = FALSE)[, selectyears, ] + luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, + cellular = TRUE, yrs = selectyears, aggregate = FALSE) + luh3Croparea <- collapseNames(luh3[, , "crop"]) + luh3Total <- dimSums(luh3, dim = "landuse") + + #### BENNI: Please correct the correction! + correctionFactor <- ifelse((physCroparea + fallowLand) > 0 & luh3Croparea > 0, + luh3Croparea / (physCroparea + fallowLand), + 1) + + # apply correction to croparea from above + croparea <- croparea * correctionFactor + + # Sanity check (physical croparea + fallow land should not exceed total land area in LUH3) + if (any((physCroparea + fallowLand) - luh3Total > 0)) { + stop("Croparea is larger than total land area in LUH3.") + } + # Sanity check (physical croparea + fallow land should not exceed cropland area in LUH3) + if (any((physCroparea + fallowLand) - luh3Croparea > 0)) { + stop("Croparea + fallow land is larger than cropland area in LUH3. + This should no longer be the case after the correction.") } - - # Aggregation to iso-level if (!cellular) { diff --git a/R/calcCropareaLandInG.R b/R/calcCropareaLandInG.R index a6fa3ec..1b379ad 100644 --- a/R/calcCropareaLandInG.R +++ b/R/calcCropareaLandInG.R @@ -10,7 +10,6 @@ #' agrees with the physical cropland of readLandInG(subtype = physical) #' @param cellular if TRUE: calculates cellular crop area for all magpie croptypes. #' Option FALSE is not (yet) available. -#' @param cells Switch between "magpiecell" (59199) and "lpjcell" (67420) #' @param irrigation If true: cellular areas are returned separated #' into irrigated and rainfed #' @param selectyears extract certain years from the data @@ -27,7 +26,7 @@ #' @importFrom mstools toolHoldConstant #' calcCropareaLandInG <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, - cells = "magpiecell", irrigation = FALSE, selectyears = "all", + irrigation = FALSE, selectyears = "all", lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de", crop = "ggcmi_phase3_nchecks_bft_e511ac58"), climatetype = "GSWP3-W5E5:historical") { @@ -280,15 +279,6 @@ calcCropareaLandInG <- function(sectoral = "kcr", physical = TRUE, cellular = FA # fill missing countries with 0 cropArea <- toolConditionalReplace(x = toolCountryFill(cropArea), conditions = "is.na()", replaceby = 0) - } else { - if (cells == "magpiecell") { - cropArea <- toolCoord2Isocell(cropArea) - } else if (cells == "lpjcell") { - # this is already the format of cropArea - } else { - stop("This value for the cell parameter is not supported, - choose between \"magpiecell\" and \"lpjcell\"") - } } cropAreaList[[y]] <- cropArea } diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index 08de33f..4ab26d4 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -29,7 +29,9 @@ into irrigated and rainfed (see setup in calcLUH3)} \item{selectyears}{Vector of years to be returned} -\item{datasource}{Croparea data source (LandInG or FAOLUH)} +\item{datasource}{Croparea data source (LandInG or FAOLUH). +Datasource FAOLUH is deprecated and is only kept for +backwards compatibility.} } \value{ areas of individual crops from FAOSTAT and weight @@ -38,5 +40,5 @@ areas of individual crops from FAOSTAT and weight returns croparea } \author{ -Felicitas Beier +Felicitas Beier, Benjamin Leon Bodirsky } diff --git a/man/calcCropareaLandInG.Rd b/man/calcCropareaLandInG.Rd index a4da1e5..b98690e 100644 --- a/man/calcCropareaLandInG.Rd +++ b/man/calcCropareaLandInG.Rd @@ -8,7 +8,6 @@ calcCropareaLandInG( sectoral = "kcr", physical = TRUE, cellular = FALSE, - cells = "magpiecell", irrigation = FALSE, selectyears = "all", lpjml = c(natveg = "LPJmL4_for_MAgPIE_44ac93de", crop = @@ -25,8 +24,6 @@ agrees with the physical cropland of readLandInG(subtype = physical)} \item{cellular}{if TRUE: calculates cellular crop area for all magpie croptypes. Option FALSE is not (yet) available.} -\item{cells}{Switch between "magpiecell" (59199) and "lpjcell" (67420)} - \item{irrigation}{If true: cellular areas are returned separated into irrigated and rainfed} From 819fcba79d5ff3bb4d485f34e714d0d48026e9df Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Thu, 12 Mar 2026 15:35:31 +0100 Subject: [PATCH 03/36] bugfixing the scaling of LandInG to LUH3 cropland and included fallow argument to optionally return fallow land as element in crop set --- .Rbuildignore | 2 + .buildlibrary | 3 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/calcCroparea.R | 116 +++++++++++++++++++++++++++++++++++++------- README.md | 8 +-- man/calcCroparea.Rd | 3 ++ 7 files changed, 114 insertions(+), 26 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 8888bbe..dcdf56c 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,5 @@ ^.lintr$ ^tests/.lintr$ ^codecov\.yml$ +^\.claude$ +^_pkgdown\.yml$ diff --git a/.buildlibrary b/.buildlibrary index 746f1fa..54f5641 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '34036064516' +ValidationKey: '34049357048' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' @@ -8,3 +8,4 @@ allowLinterWarnings: no enforceVersionUpdate: no AutocreateCITATION: yes skipCoverage: no +UsePkgDown: no diff --git a/CITATION.cff b/CITATION.cff index 395c9e3..27cda0d 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.5.9001 -date-released: '2026-03-04' +version: 1.6.5.9002 +date-released: '2026-03-12' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index 039716c..849e0f2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.5.9001 -Date: 2026-03-04 +Version: 1.6.5.9002 +Date: 2026-03-12 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 708fc68..5b1b267 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -3,6 +3,7 @@ #' #' @param sectoral "kcr" MAgPIE items, and "lpj" LPJmL items #' @param physical if TRUE the sum over all crops agrees with the cropland area per country +#' @param fallow if TRUE fallow land is returned as element in crop set #' @param cellular if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. #' Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) #' are mapped to MAgpIE crop types using "FAO2LUH2MAG_croptypes" and doing @@ -21,7 +22,7 @@ #' #' @importFrom magpiesets findset -calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, +calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cellular = FALSE, irrigation = FALSE, selectyears = "past", datasource = "LandInG") { if (selectyears == "past") { @@ -38,32 +39,99 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, description <- paste0(ifelse(physical, "physical ", "harvested "), "croparea from LandInG data set") - # correction to match LUH cropland area + ############################################################ + ########## Correction to match LUH cropland area ########## + ############################################################ + # Note: Correction necessary until LandInG data set is updated to LUH3 physCroparea <- dimSums(calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, cellular = TRUE, irrigation = FALSE, selectyears = selectyears, aggregate = FALSE), dim = "crop") fallowLand <- calcOutput("FallowLand", cellular = TRUE, aggregate = FALSE)[, selectyears, ] luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, cellular = TRUE, yrs = selectyears, aggregate = FALSE) - luh3Croparea <- collapseNames(luh3[, , "crop"]) + luh3Cropland <- collapseNames(luh3[, , "crop"]) + landingCropland <- physCroparea + fallowLand luh3Total <- dimSums(luh3, dim = "landuse") - #### BENNI: Please correct the correction! - correctionFactor <- ifelse((physCroparea + fallowLand) > 0 & luh3Croparea > 0, - luh3Croparea / (physCroparea + fallowLand), - 1) + # cell-specific scaling factor from LandInG cropland to LUH3 cropland + scalingFactor <- luh3Cropland / landingCropland - # apply correction to croparea from above - croparea <- croparea * correctionFactor + # fallback option for grid cells without LandInG cropland, but LUH3 cropland + fallbackCells <- landingCropland == 0 & luh3Cropland > 0 + zeroCells <- landingCropland == 0 & luh3Cropland == 0 - # Sanity check (physical croparea + fallow land should not exceed total land area in LUH3) - if (any((physCroparea + fallowLand) - luh3Total > 0)) { - stop("Croparea is larger than total land area in LUH3.") + # country and global aggregates + physCropareaISO <- dimSums(physCroparea, dim = c("x", "y")) + fallowLandISO <- dimSums(fallowLand, dim = c("x", "y")) + landingCroplandISO <- dimSums(landingCropland, dim = c("x", "y")) + cropareaISO <- dimSums(croparea, dim = c("x", "y")) + + physCropareaGLO <- dimSums(physCroparea, dim = 1) + fallowLandGLO <- dimSums(fallowLand, dim = 1) + landingCroplandGLO <- dimSums(landingCropland, dim = 1) + cropareaGLO <- dimSums(croparea, dim = 1) + + # country and global shares + fallowShareCountry <- fallowLandISO / landingCroplandISO + physShareCountry <- physCropareaISO / landingCroplandISO + cropareaToPhysRatioCountry <- cropareaISO / physCropareaISO + + fallowShareGLO <- fallowLandGLO / landingCroplandGLO + physShareGLO <- physCropareaGLO / landingCroplandGLO + cropareaToPhysRatioGLO <- cropareaGLO / physCropareaGLO + + fallowShareCountry[is.nan(fallowShareCountry)] <- fallowShareGLO + physShareCountry[is.nan(physShareCountry)] <- physShareGLO + cropareaToPhysRatioCountry[is.nan(cropareaToPhysRatioCountry)] <- cropareaToPhysRatioGLO + + # expand to grid level + fallowShareCountryGrid <- magpie_expand(fallowShareCountry, ref = fallowLand) + physShareCountryGrid <- magpie_expand(physShareCountry, ref = fallowLand) + cropareaToPhysRatioCountryGrid <- magpie_expand(cropareaToPhysRatioCountry, ref = croparea) + + # Apply scaling factor to croparea and fallow land + cropareaCalibrated <- croparea * scalingFactor + fallowLandCalibrated <- fallowLand * scalingFactor + + # replace NaNs from 0/0 by zero + cropareaCalibrated[is.nan(cropareaCalibrated)] <- 0 + fallowLandCalibrated[is.nan(fallowLandCalibrated)] <- 0 + + # Overwrite where fallback option needed + physCropareaFallback <- luh3Cropland * physShareCountryGrid + fallowLandFallback <- luh3Cropland * fallowShareCountryGrid + cropareaFallback <- physCropareaFallback * cropareaToPhysRatioCountryGrid + + fallowLandCalibrated[fallbackCells] <- fallowLandFallback[fallbackCells] + cropareaCalibrated[fallbackCells] <- cropareaFallback[fallbackCells] + + # ensure exact zero where both LUH3 and LandInG are zero + fallowLandCalibrated[zeroCells] <- 0 + cropareaCalibrated[zeroCells] <- 0 + + # return calibrated croparea with calibrated fallow land + fallowLandCalibrated <- setNames(object = fallowLandCalibrated, "fallow") + getSets(fallowLandCalibrated) <- c("x", "y", "iso", "year", "crop") + + ### Check physical cropland matching ### + if (physical) { + # Sanity check (physical croparea + fallow land should not exceed total land area in LUH3) + if (any((dimSums(cropareaCalibrated, dim = "crop") + fallowLandCalibrated) - luh3Total > 0)) { + stop("Croparea is larger than total land area in LUH3.") + } + # Sanity check (physical croparea + fallow land should match total cropland in LUH3 after calibration) + if (any(round(dimSums(cropareaCalibrated, dim = "crop") + fallowLandCalibrated - luh3Cropland, 6) != 0)) { + stop("Calibrated physical croparea + fallow land does not match LUH3 cropland.") + } } - # Sanity check (physical croparea + fallow land should not exceed cropland area in LUH3) - if (any((physCroparea + fallowLand) - luh3Croparea > 0)) { - stop("Croparea + fallow land is larger than cropland area in LUH3. - This should no longer be the case after the correction.") + + # return croparea (and optionally fallow land) + if (fallow) { + croparea <- mbind(cropareaCalibrated, fallowLandCalibrated) + description <- paste0(description, " including fallow land.") + } else { + croparea <- cropareaCalibrated + description <- paste0(description, " excluding fallow land.") } # Aggregation to iso-level @@ -76,7 +144,6 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, } } else if (datasource == "FAOLUH") { - ### For backwards compatibility only ### # This chunk can be deleted when croparea update is completed. @@ -89,6 +156,21 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, cellular = FALSE, # description of data to be returned description <- paste0(ifelse(physical, "physical ", "harvested "), "croparea from LUH and FAOSTAT") + + # return croparea (and optionally fallow land) + if (fallow) { + # LUH3/FAO doesn't provide distinction of fallow land + fallowLand <- new.magpie(cells_and_regions = getItems(croparea, dim = 1), + years = getItems(croparea, dim = 2), + names = "fallow", + sets = getSets(croparea)[["d3.1"]], + fill = 0) + croparea <- mbind(croparea, fallowLand) + description <- paste0(description, " including fallow land.") + } else { + description <- paste0(description, " excluding fallow land.") + } + } else { stop("Selected data source in calcCroparea unknown.") } diff --git a/README.md b/README.md index 3bcde3a..ec4f1a8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.5.9001** +R package **mrlandcore**, version **1.6.5.9002** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.5.9002, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-03-04}, + date = {2026-03-12}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.5.9001}, + note = {Version: 1.6.5.9002}, } ``` diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index 4ab26d4..e5e35fa 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -7,6 +7,7 @@ calcCroparea( sectoral = "kcr", physical = TRUE, + fallow = FALSE, cellular = FALSE, irrigation = FALSE, selectyears = "past", @@ -18,6 +19,8 @@ calcCroparea( \item{physical}{if TRUE the sum over all crops agrees with the cropland area per country} +\item{fallow}{if TRUE fallow land is returned as element in crop set} + \item{cellular}{if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) are mapped to MAgpIE crop types using "FAO2LUH2MAG_croptypes" and doing From 27f2324fd9001b030fcb77d02663bd48da3455d5 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 13 Mar 2026 09:28:06 +0100 Subject: [PATCH 04/36] changed default selectyears from past to all --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- R/calcCroparea.R | 2 +- README.md | 8 ++++---- man/calcCroparea.Rd | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 54f5641..3ac301d 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '34049357048' +ValidationKey: '34051036575' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 27cda0d..e54888f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.5.9002 -date-released: '2026-03-12' +version: 1.6.5.9003 +date-released: '2026-03-13' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index 849e0f2..2e86437 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.5.9002 -Date: 2026-03-12 +Version: 1.6.5.9003 +Date: 2026-03-13 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 5b1b267..37bbe1d 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -23,7 +23,7 @@ #' @importFrom magpiesets findset calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cellular = FALSE, - irrigation = FALSE, selectyears = "past", datasource = "LandInG") { + irrigation = FALSE, selectyears = "all", datasource = "LandInG") { if (selectyears == "past") { selectyears <- findset("past") diff --git a/README.md b/README.md index ec4f1a8..81a7d88 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.5.9002** +R package **mrlandcore**, version **1.6.5.9003** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.5.9003, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-03-12}, + date = {2026-03-13}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.5.9002}, + note = {Version: 1.6.5.9003}, } ``` diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index e5e35fa..700887e 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -10,7 +10,7 @@ calcCroparea( fallow = FALSE, cellular = FALSE, irrigation = FALSE, - selectyears = "past", + selectyears = "all", datasource = "LandInG" ) } From e3db0b1960b2e74920d3b0ac0f14db73ea6eb9db Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 13 Mar 2026 10:50:54 +0100 Subject: [PATCH 05/36] removed selectyears argument and instead introduced intersect --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcCroparea.R | 46 ++++++++++++++++++++++++++------------------- README.md | 6 +++--- man/calcCroparea.Rd | 3 --- 6 files changed, 33 insertions(+), 28 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 3ac301d..0d0a69d 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '34051036575' +ValidationKey: '34051057100' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index e54888f..ac906b5 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,7 +4,7 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.5.9003 +version: 1.6.5.9004 date-released: '2026-03-13' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 2e86437..d28dfdc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.5.9003 +Version: 1.6.5.9004 Date: 2026-03-13 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 37bbe1d..2078f88 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -11,7 +11,6 @@ #' specific LUH crop type to divide into MAgPIE crop types. #' @param irrigation If true: cellular areas are returned separated #' into irrigated and rainfed (see setup in calcLUH3) -#' @param selectyears Vector of years to be returned #' @param datasource Croparea data source (LandInG or FAOLUH). #' Datasource FAOLUH is deprecated and is only kept for #' backwards compatibility. @@ -22,18 +21,24 @@ #' #' @importFrom magpiesets findset -calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cellular = FALSE, - irrigation = FALSE, selectyears = "all", datasource = "LandInG") { - - if (selectyears == "past") { - selectyears <- findset("past") - } +calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, + cellular = FALSE, + irrigation = FALSE, datasource = "LandInG") { if (datasource == "LandInG") { # read in croparea croparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = physical, cellular = TRUE, irrigation = irrigation, - selectyears = selectyears, aggregate = FALSE) + aggregate = FALSE) + + # for correction: read LUH3 cropland area + # Note: Can be removed when LandInG update complete + luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, + cellular = TRUE, aggregate = FALSE) + + selectyears <- intersect(getItems(croparea, dim = 2), getItems(luh3, dim = 2)) + croparea <- croparea[, selectyears, ] + luh3 <- luh3[, selectyears, ] # description of data to be returned description <- paste0(ifelse(physical, "physical ", "harvested "), @@ -45,13 +50,13 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cell # Note: Correction necessary until LandInG data set is updated to LUH3 physCroparea <- dimSums(calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, cellular = TRUE, irrigation = FALSE, - selectyears = selectyears, aggregate = FALSE), dim = "crop") - fallowLand <- calcOutput("FallowLand", cellular = TRUE, aggregate = FALSE)[, selectyears, ] - luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, - cellular = TRUE, yrs = selectyears, aggregate = FALSE) - luh3Cropland <- collapseNames(luh3[, , "crop"]) + aggregate = FALSE), dim = "crop")[, selectyears, ] + fallowLand <- calcOutput("FallowLand", cellular = TRUE, + aggregate = FALSE)[, selectyears, ] + + luh3Cropland <- collapseNames(luh3[, , "crop"]) landingCropland <- physCroparea + fallowLand - luh3Total <- dimSums(luh3, dim = "landuse") + luh3Total <- dimSums(luh3, dim = "landuse") # cell-specific scaling factor from LandInG cropland to LUH3 cropland scalingFactor <- luh3Cropland / landingCropland @@ -94,8 +99,12 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cell fallowLandCalibrated <- fallowLand * scalingFactor # replace NaNs from 0/0 by zero - cropareaCalibrated[is.nan(cropareaCalibrated)] <- 0 - fallowLandCalibrated[is.nan(fallowLandCalibrated)] <- 0 + if (any(is.nan(cropareaCalibrated))) { + cropareaCalibrated[is.nan(cropareaCalibrated)] <- 0 + } + if (any(is.nan(fallowLandCalibrated))) { + fallowLandCalibrated[is.nan(fallowLandCalibrated)] <- 0 + } # Overwrite where fallback option needed physCropareaFallback <- luh3Cropland * physShareCountryGrid @@ -127,10 +136,10 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cell # return croparea (and optionally fallow land) if (fallow) { - croparea <- mbind(cropareaCalibrated, fallowLandCalibrated) + croparea <- mbind(cropareaCalibrated, fallowLandCalibrated) description <- paste0(description, " including fallow land.") } else { - croparea <- cropareaCalibrated + croparea <- cropareaCalibrated description <- paste0(description, " excluding fallow land.") } @@ -151,7 +160,6 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cell croparea <- calcOutput("CropareaFAOLUH", sectoral = sectoral, physical = physical, cellular = cellular, irrigation = irrigation, aggregate = FALSE) - croparea <- croparea[, selectyears, ] # description of data to be returned description <- paste0(ifelse(physical, "physical ", "harvested "), diff --git a/README.md b/README.md index 81a7d88..e6bb5fd 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.5.9003** +R package **mrlandcore**, version **1.6.5.9004** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.5.9004, . A BibTeX entry for LaTeX users is @@ -51,6 +51,6 @@ A BibTeX entry for LaTeX users is date = {2026-03-13}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.5.9003}, + note = {Version: 1.6.5.9004}, } ``` diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index 700887e..efff2b4 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -10,7 +10,6 @@ calcCroparea( fallow = FALSE, cellular = FALSE, irrigation = FALSE, - selectyears = "all", datasource = "LandInG" ) } @@ -30,8 +29,6 @@ specific LUH crop type to divide into MAgPIE crop types.} \item{irrigation}{If true: cellular areas are returned separated into irrigated and rainfed (see setup in calcLUH3)} -\item{selectyears}{Vector of years to be returned} - \item{datasource}{Croparea data source (LandInG or FAOLUH). Datasource FAOLUH is deprecated and is only kept for backwards compatibility.} From 070b0bf29a889c7e3027fc6a2caf54cb33ecf1f6 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 13 Mar 2026 11:32:45 +0100 Subject: [PATCH 06/36] extended years to 2020 --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcCroparea.R | 5 +++++ README.md | 6 +++--- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 0d0a69d..763ff13 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '34051057100' +ValidationKey: '34051077625' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index ac906b5..e4fa3f1 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,7 +4,7 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.5.9004 +version: 1.6.5.9005 date-released: '2026-03-13' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: diff --git a/DESCRIPTION b/DESCRIPTION index d28dfdc..635dfa2 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.5.9004 +Version: 1.6.5.9005 Date: 2026-03-13 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 2078f88..b4dc1cf 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -143,6 +143,11 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, description <- paste0(description, " excluding fallow land.") } + # Extend data until 2020 + if (!any(grepl("y2020", getItems(croparea, dim = 2)))) { + croparea <- toolHoldConstant(croparea, years = c(getItems(croparea, dim = 2), "y2020")) + } + # Aggregation to iso-level if (!cellular) { # aggregate to countries diff --git a/README.md b/README.md index e6bb5fd..bdc2984 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.5.9004** +R package **mrlandcore**, version **1.6.5.9005** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.5.9005, . A BibTeX entry for LaTeX users is @@ -51,6 +51,6 @@ A BibTeX entry for LaTeX users is date = {2026-03-13}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.5.9004}, + note = {Version: 1.6.5.9005}, } ``` From 900929d310b127202354947c8acc4e229470a2c2 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Fri, 13 Mar 2026 16:38:08 +0100 Subject: [PATCH 07/36] .. --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcCroparea.R | 55 ++++++++++++++++++++++++++--------------- R/calcCropareaLandInG.R | 2 +- README.md | 6 ++--- man/calcCroparea.Rd | 3 ++- mrlandcore.Rproj | 1 + 8 files changed, 45 insertions(+), 28 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 763ff13..50d959a 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '34051077625' +ValidationKey: '3407150' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index e4fa3f1..9c02e80 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,7 +4,7 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.5.9005 +version: 1.6.6 date-released: '2026-03-13' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 635dfa2..d6b5873 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.5.9005 +Version: 1.6.6 Date: 2026-03-13 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index b4dc1cf..eed4898 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -2,7 +2,8 @@ #' @description returns croparea #' #' @param sectoral "kcr" MAgPIE items, and "lpj" LPJmL items -#' @param physical if TRUE the sum over all crops agrees with the cropland area per country +#' @param physical if TRUE it returns the physical area, with cropareas of multicropping systems being +#' scaled to match physical areas, if FALSE the area harvested is returned #' @param fallow if TRUE fallow land is returned as element in crop set #' @param cellular if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. #' Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) @@ -31,14 +32,18 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cellular = TRUE, irrigation = irrigation, aggregate = FALSE) + selectyears <- getYears(croparea, as.integer = TRUE) + + # Extend data until 2020 + if (!any(grepl("y2020", getItems(croparea, dim = 2)))) { + selectyears <- c(selectyears, (tail(selectyears, 1) + 1):2020) + croparea <- toolHoldConstant(croparea, years = selectyears) + } + # for correction: read LUH3 cropland area # Note: Can be removed when LandInG update complete luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, - cellular = TRUE, aggregate = FALSE) - - selectyears <- intersect(getItems(croparea, dim = 2), getItems(luh3, dim = 2)) - croparea <- croparea[, selectyears, ] - luh3 <- luh3[, selectyears, ] + cellular = TRUE, aggregate = FALSE, yrs = selectyears) # description of data to be returned description <- paste0(ifelse(physical, "physical ", "harvested "), @@ -50,9 +55,19 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, # Note: Correction necessary until LandInG data set is updated to LUH3 physCroparea <- dimSums(calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, cellular = TRUE, irrigation = FALSE, - aggregate = FALSE), dim = "crop")[, selectyears, ] - fallowLand <- calcOutput("FallowLand", cellular = TRUE, - aggregate = FALSE)[, selectyears, ] + aggregate = FALSE), dim = "crop") + fallowLand <- calcOutput("FallowLandInG", cellular = TRUE, + aggregate = FALSE) + # Extend data until 2020 + if (!any(grepl("y2020", getItems(physCroparea, dim = 2)))) { + physCroparea <- toolHoldConstant(physCroparea, years = selectyears) + } + if (!any(grepl("y2020", getItems(fallowLand, dim = 2)))) { + fallowLand <- toolHoldConstant(fallowLand, years = selectyears) + } + + physCroparea <- physCroparea[, selectyears, ] + fallowLand <- fallowLand[, selectyears, ] luh3Cropland <- collapseNames(luh3[, , "crop"]) landingCropland <- physCroparea + fallowLand @@ -81,13 +96,18 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, physShareCountry <- physCropareaISO / landingCroplandISO cropareaToPhysRatioCountry <- cropareaISO / physCropareaISO - fallowShareGLO <- fallowLandGLO / landingCroplandGLO - physShareGLO <- physCropareaGLO / landingCroplandGLO - cropareaToPhysRatioGLO <- cropareaGLO / physCropareaGLO + fallowShareGLO <- fallowShareCountry * NA + physShareGLO <- fallowShareCountry * NA + cropareaToPhysRatioGLO <- cropareaToPhysRatioCountry * NA - fallowShareCountry[is.nan(fallowShareCountry)] <- fallowShareGLO - physShareCountry[is.nan(physShareCountry)] <- physShareGLO - cropareaToPhysRatioCountry[is.nan(cropareaToPhysRatioCountry)] <- cropareaToPhysRatioGLO + fallowShareGLO[, , ] <- fallowLandGLO / landingCroplandGLO + physShareGLO[, , ] <- physCropareaGLO / landingCroplandGLO + cropareaToPhysRatioGLO[, , ] <- cropareaGLO / physCropareaGLO + + fallowShareCountry[is.nan(fallowShareCountry)] <- fallowShareGLO[is.nan(fallowShareCountry)] + physShareCountry[is.nan(physShareCountry)] <- physShareGLO[is.nan(physShareCountry)] + cropareaToPhysRatioCountry[is.nan(cropareaToPhysRatioCountry)] <- + cropareaToPhysRatioGLO[is.nan(cropareaToPhysRatioCountry)] # expand to grid level fallowShareCountryGrid <- magpie_expand(fallowShareCountry, ref = fallowLand) @@ -143,11 +163,6 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, description <- paste0(description, " excluding fallow land.") } - # Extend data until 2020 - if (!any(grepl("y2020", getItems(croparea, dim = 2)))) { - croparea <- toolHoldConstant(croparea, years = c(getItems(croparea, dim = 2), "y2020")) - } - # Aggregation to iso-level if (!cellular) { # aggregate to countries diff --git a/R/calcCropareaLandInG.R b/R/calcCropareaLandInG.R index 1b379ad..b7669e8 100644 --- a/R/calcCropareaLandInG.R +++ b/R/calcCropareaLandInG.R @@ -49,7 +49,7 @@ calcCropareaLandInG <- function(sectoral = "kcr", physical = TRUE, cellular = FA ### Calculations ### # read in fallow land (for check below) - fallow <- calcOutput("FallowLand", aggregate = FALSE) + fallow <- calcOutput("FallowLandInG", aggregate = FALSE) # year selection if (any(selectyears == "all")) { diff --git a/README.md b/README.md index bdc2984..12b3862 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.5.9005** +R package **mrlandcore**, version **1.6.6** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.6, . A BibTeX entry for LaTeX users is @@ -51,6 +51,6 @@ A BibTeX entry for LaTeX users is date = {2026-03-13}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.5.9005}, + note = {Version: 1.6.6}, } ``` diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index efff2b4..a5a05bd 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -16,7 +16,8 @@ calcCroparea( \arguments{ \item{sectoral}{"kcr" MAgPIE items, and "lpj" LPJmL items} -\item{physical}{if TRUE the sum over all crops agrees with the cropland area per country} +\item{physical}{if TRUE it returns the physical area, with cropareas of multicropping systems being +scaled to match physical areas, if FALSE the area harvested is returned} \item{fallow}{if TRUE fallow land is returned as element in crop set} diff --git a/mrlandcore.Rproj b/mrlandcore.Rproj index 497f8bf..312dce9 100644 --- a/mrlandcore.Rproj +++ b/mrlandcore.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 5ee65684-ef1b-4101-b673-ba397a37d7bd RestoreWorkspace: Default SaveWorkspace: Default From 80af56ec722d1333ab4770043d4f0e143cbda4b9 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Fri, 13 Mar 2026 17:07:32 +0100 Subject: [PATCH 08/36] .. --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcCropareaLandInG.R | 2 +- R/calcFallow.R | 35 ++++++++++++++++++++ R/{calcFallowLand.R => calcFallowLandInG.R} | 6 ++-- README.md | 6 ++-- man/calcFallowLand.Rd | 36 --------------------- 8 files changed, 45 insertions(+), 46 deletions(-) create mode 100644 R/calcFallow.R rename R/{calcFallowLand.R => calcFallowLandInG.R} (93%) delete mode 100644 man/calcFallowLand.Rd diff --git a/.buildlibrary b/.buildlibrary index 50d959a..2e3a99a 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3407150' +ValidationKey: '3427675' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 9c02e80..c9ed5ee 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,7 +4,7 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.6 +version: 1.6.7 date-released: '2026-03-13' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: diff --git a/DESCRIPTION b/DESCRIPTION index d6b5873..2e52c6c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.6 +Version: 1.6.7 Date: 2026-03-13 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/calcCropareaLandInG.R b/R/calcCropareaLandInG.R index b7669e8..02a7231 100644 --- a/R/calcCropareaLandInG.R +++ b/R/calcCropareaLandInG.R @@ -49,7 +49,7 @@ calcCropareaLandInG <- function(sectoral = "kcr", physical = TRUE, cellular = FA ### Calculations ### # read in fallow land (for check below) - fallow <- calcOutput("FallowLandInG", aggregate = FALSE) + fallow <- calcOutput("FallowLandInG", aggregate = FALSE, cellular = TRUE) # year selection if (any(selectyears == "all")) { diff --git a/R/calcFallow.R b/R/calcFallow.R new file mode 100644 index 0000000..90397a3 --- /dev/null +++ b/R/calcFallow.R @@ -0,0 +1,35 @@ +#' @title calcFallow +#' @description +#' Returns fallow land of our default datasource. +#' The estimates are scaled to match our Land classes in the Croparea function. +#' Due to multiple cropping, harvested cropland area can be greater than non-fallow land area +#' and even greater than physical cropland area. +#' @param cellular TRUE for cellular outputs. +#' @param physical if TRUE it returns the physical area, with fallow of multicropping systems being +#' scaled to match physical areas of the rotation, if FALSE the area harvested +#' being fallow is returned +#' @return MAgPIE object containing fallow land in Mha +#' @author David Hoetten, Felicitas Beier +#' @seealso +#' \code{\link{readLandInG}} +#' @examples +#' \dontrun{ +#' calcOutput("Fallow") +#' } +#' @importFrom magclass dimSums mbind +#' @importFrom madrat toolConditionalReplace +#' +calcFallow <- function(cellular = FALSE, physical = TRUE) { + + fallow <- calcOutput("Croparea", cellular = cellular, + physical = physical, fallow = TRUE, + irrigation = FALSE, aggregate = FALSE) + fallow <- fallow[, , "fallow"] + + return(list(x = fallow, + weight = NULL, + description = "Fallow land", + unit = "Mha", + isocountries = FALSE)) + +} diff --git a/R/calcFallowLand.R b/R/calcFallowLandInG.R similarity index 93% rename from R/calcFallowLand.R rename to R/calcFallowLandInG.R index 168460e..5db3258 100644 --- a/R/calcFallowLand.R +++ b/R/calcFallowLandInG.R @@ -1,4 +1,4 @@ -#' @title calcFallowLand +#' @title calcFallowLandInG #' @description #' Calculates fallow land on grid cell level, #' based on physical cropland extend and harvested area output @@ -16,12 +16,12 @@ #' \code{\link{readLandInG}} #' @examples #' \dontrun{ -#' calcOutput("FallowLand") +#' calcOutput("FallowLandInG") #' } #' @importFrom magclass dimSums mbind #' @importFrom madrat toolConditionalReplace #' -calcFallowLand <- function(cellular = TRUE) { +calcFallowLandInG <- function(cellular = TRUE) { harvestedArea <- readSource("LandInG", subtype = "harvestedArea") diff --git a/README.md b/README.md index 12b3862..493f00c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.6** +R package **mrlandcore**, version **1.6.7** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.6.7, . A BibTeX entry for LaTeX users is @@ -51,6 +51,6 @@ A BibTeX entry for LaTeX users is date = {2026-03-13}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.6}, + note = {Version: 1.6.7}, } ``` diff --git a/man/calcFallowLand.Rd b/man/calcFallowLand.Rd deleted file mode 100644 index 55d73de..0000000 --- a/man/calcFallowLand.Rd +++ /dev/null @@ -1,36 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcFallowLand.R -\name{calcFallowLand} -\alias{calcFallowLand} -\title{calcFallowLand} -\usage{ -calcFallowLand(cellular = TRUE) -} -\arguments{ -\item{cellular}{TRUE for cellular outputs.} -} -\value{ -MAgPIE object containing fallow land in Mha -} -\description{ -Calculates fallow land on grid cell level, -based on physical cropland extend and harvested area output -of LandInG data. -The formula -"fallow land are = max( physical cropland area - harvested cropland area, 0)" -is used. -Due to multiple cropping, harvested cropland area can be greater than non-fallow land area -and even greater than physical cropland area. -Thus, the results can only be considered a rough estimate of fallow land area. -} -\examples{ -\dontrun{ -calcOutput("FallowLand") -} -} -\seealso{ -\code{\link{readLandInG}} -} -\author{ -David Hoetten, Felicitas Beier -} From 3989b347baa6582cb56458ba724395fb650c233c Mon Sep 17 00:00:00 2001 From: bodirsky Date: Wed, 18 Mar 2026 17:57:22 +0100 Subject: [PATCH 09/36] .. --- .buildlibrary | 2 +- CITATION.cff | 4 +- DESCRIPTION | 4 +- R/calcCroparea.R | 223 +++++++++++++++++++++-------------------------- README.md | 8 +- 5 files changed, 108 insertions(+), 133 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 2e3a99a..425b716 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3427675' +ValidationKey: '3490100' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index c9ed5ee..e65ba89 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.6.7 -date-released: '2026-03-13' +version: 1.7.0 +date-released: '2026-03-18' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index 2e52c6c..0d8ca34 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.6.7 -Date: 2026-03-13 +Version: 1.7.0 +Date: 2026-03-18 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index eed4898..c33378b 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -27,152 +27,127 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, irrigation = FALSE, datasource = "LandInG") { if (datasource == "LandInG") { - # read in croparea - croparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = physical, - cellular = TRUE, irrigation = irrigation, - aggregate = FALSE) + # description of data to be returned + description <- paste0(ifelse(physical, "physical ", "harvested "), + "croparea from LandInG data set") + if (irrigation && cellular && fallow) { + # read in croparea + croparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, + cellular = TRUE, irrigation = TRUE, + aggregate = FALSE) + selectyears <- getYears(croparea, as.integer = TRUE) + # Extend data until 2020 + if (!any(grepl("y2020", getItems(croparea, dim = 2)))) { + selectyears <- c(selectyears, (tail(selectyears, 1) + 1):2020) + croparea <- toolHoldConstant(croparea, years = selectyears) + } - selectyears <- getYears(croparea, as.integer = TRUE) + if (!physical) { + # calculate crop and irrigation-specific multicropping factor + cropareaMulti <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = FALSE, + cellular = TRUE, irrigation = TRUE, + aggregate = FALSE) - # Extend data until 2020 - if (!any(grepl("y2020", getItems(croparea, dim = 2)))) { - selectyears <- c(selectyears, (tail(selectyears, 1) + 1):2020) - croparea <- toolHoldConstant(croparea, years = selectyears) - } + if (!any(grepl("y2020", getItems(cropareaMulti, dim = 2)))) { + cropareaMulti <- toolHoldConstant(cropareaMulti, years = selectyears) + } - # for correction: read LUH3 cropland area - # Note: Can be removed when LandInG update complete - luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, - cellular = TRUE, aggregate = FALSE, yrs = selectyears) + multiFactor <- cropareaMulti / croparea + multiFactor <- ifelse(test = is.nan(multiFactor), 1, multiFactor) + multiFactor <- ifelse(test = multiFactor > 2, 2, multiFactor) + multiFactor <- add_columns(x = multiFactor, addnm = "fallow", + dim = "crop", fill = 1) - # description of data to be returned - description <- paste0(ifelse(physical, "physical ", "harvested "), - "croparea from LandInG data set") + } - ############################################################ - ########## Correction to match LUH cropland area ########## - ############################################################ - # Note: Correction necessary until LandInG data set is updated to LUH3 - physCroparea <- dimSums(calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, - cellular = TRUE, irrigation = FALSE, - aggregate = FALSE), dim = "crop") - fallowLand <- calcOutput("FallowLandInG", cellular = TRUE, - aggregate = FALSE) - # Extend data until 2020 - if (!any(grepl("y2020", getItems(physCroparea, dim = 2)))) { - physCroparea <- toolHoldConstant(physCroparea, years = selectyears) - } - if (!any(grepl("y2020", getItems(fallowLand, dim = 2)))) { - fallowLand <- toolHoldConstant(fallowLand, years = selectyears) - } + fallowLand <- calcOutput("FallowLandInG", cellular = TRUE, + aggregate = FALSE) - physCroparea <- physCroparea[, selectyears, ] - fallowLand <- fallowLand[, selectyears, ] - - luh3Cropland <- collapseNames(luh3[, , "crop"]) - landingCropland <- physCroparea + fallowLand - luh3Total <- dimSums(luh3, dim = "landuse") - - # cell-specific scaling factor from LandInG cropland to LUH3 cropland - scalingFactor <- luh3Cropland / landingCropland - - # fallback option for grid cells without LandInG cropland, but LUH3 cropland - fallbackCells <- landingCropland == 0 & luh3Cropland > 0 - zeroCells <- landingCropland == 0 & luh3Cropland == 0 - - # country and global aggregates - physCropareaISO <- dimSums(physCroparea, dim = c("x", "y")) - fallowLandISO <- dimSums(fallowLand, dim = c("x", "y")) - landingCroplandISO <- dimSums(landingCropland, dim = c("x", "y")) - cropareaISO <- dimSums(croparea, dim = c("x", "y")) - - physCropareaGLO <- dimSums(physCroparea, dim = 1) - fallowLandGLO <- dimSums(fallowLand, dim = 1) - landingCroplandGLO <- dimSums(landingCropland, dim = 1) - cropareaGLO <- dimSums(croparea, dim = 1) - - # country and global shares - fallowShareCountry <- fallowLandISO / landingCroplandISO - physShareCountry <- physCropareaISO / landingCroplandISO - cropareaToPhysRatioCountry <- cropareaISO / physCropareaISO - - fallowShareGLO <- fallowShareCountry * NA - physShareGLO <- fallowShareCountry * NA - cropareaToPhysRatioGLO <- cropareaToPhysRatioCountry * NA - - fallowShareGLO[, , ] <- fallowLandGLO / landingCroplandGLO - physShareGLO[, , ] <- physCropareaGLO / landingCroplandGLO - cropareaToPhysRatioGLO[, , ] <- cropareaGLO / physCropareaGLO - - fallowShareCountry[is.nan(fallowShareCountry)] <- fallowShareGLO[is.nan(fallowShareCountry)] - physShareCountry[is.nan(physShareCountry)] <- physShareGLO[is.nan(physShareCountry)] - cropareaToPhysRatioCountry[is.nan(cropareaToPhysRatioCountry)] <- - cropareaToPhysRatioGLO[is.nan(cropareaToPhysRatioCountry)] - - # expand to grid level - fallowShareCountryGrid <- magpie_expand(fallowShareCountry, ref = fallowLand) - physShareCountryGrid <- magpie_expand(physShareCountry, ref = fallowLand) - cropareaToPhysRatioCountryGrid <- magpie_expand(cropareaToPhysRatioCountry, ref = croparea) - - # Apply scaling factor to croparea and fallow land - cropareaCalibrated <- croparea * scalingFactor - fallowLandCalibrated <- fallowLand * scalingFactor - - # replace NaNs from 0/0 by zero - if (any(is.nan(cropareaCalibrated))) { - cropareaCalibrated[is.nan(cropareaCalibrated)] <- 0 - } - if (any(is.nan(fallowLandCalibrated))) { - fallowLandCalibrated[is.nan(fallowLandCalibrated)] <- 0 - } + if (!any(grepl("y2020", getItems(fallowLand, dim = 2)))) { + fallowLand <- toolHoldConstant(fallowLand, years = selectyears) + } - # Overwrite where fallback option needed - physCropareaFallback <- luh3Cropland * physShareCountryGrid - fallowLandFallback <- luh3Cropland * fallowShareCountryGrid - cropareaFallback <- physCropareaFallback * cropareaToPhysRatioCountryGrid + ### Integrate fallow into physical land + # implicit assumption: fallow is the same in physical and harvested area, + # as multicropping with fallow is not multicropping - fallowLandCalibrated[fallbackCells] <- fallowLandFallback[fallbackCells] - cropareaCalibrated[fallbackCells] <- cropareaFallback[fallbackCells] + fallowLand <- setNames(fallowLand[, selectyears, ], "fallow") - # ensure exact zero where both LUH3 and LandInG are zero - fallowLandCalibrated[zeroCells] <- 0 - cropareaCalibrated[zeroCells] <- 0 + totalCroparea <- dimSums(croparea, dim = "crop") + irrigationShare <- (totalCroparea[, , c("rainfed", "irrigated")] / + dimSums(totalCroparea, dim = "irrigation")) + irrigationShare <- ifelse(is.nan(irrigationShare), 0, irrigationShare) + fallowLandIrrigated <- irrigationShare * fallowLand - # return calibrated croparea with calibrated fallow land - fallowLandCalibrated <- setNames(object = fallowLandCalibrated, "fallow") - getSets(fallowLandCalibrated) <- c("x", "y", "iso", "year", "crop") + cropareaWithFallow <- mbind(croparea, fallowLandIrrigated) - ### Check physical cropland matching ### - if (physical) { - # Sanity check (physical croparea + fallow land should not exceed total land area in LUH3) - if (any((dimSums(cropareaCalibrated, dim = "crop") + fallowLandCalibrated) - luh3Total > 0)) { - stop("Croparea is larger than total land area in LUH3.") - } + ### Correction to match LUH cropland area + # Note: Correction necessary to make LandInG match LUH3 land classes + + # calculate area shares + cropareaShareGrid <- cropareaWithFallow / dimSums(cropareaWithFallow, dim = c("irrigation", "crop")) + cropareaShareIso <- dimSums(cropareaWithFallow, dim = c("x", "y")) / + dimSums(cropareaWithFallow, dim = c("x", "y", "irrigation", "crop")) + cropareaShareGlo <- dimSums(cropareaWithFallow, dim = 1) / + dimSums(cropareaWithFallow, dim = c("x", "y", "iso", "irrigation", "crop")) + cropareaShareIso <- ifelse(is.nan(cropareaShareIso), cropareaShareGlo, cropareaShareIso) + cropareaShareGrid <- ifelse(is.nan(cropareaShareGrid), cropareaShareIso, cropareaShareGrid) + + # for correction: read LUH3 cropland area + luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, + cellular = TRUE, aggregate = FALSE, yrs = selectyears) + + luh3physCropland <- collapseNames(luh3[, , "crop"]) + + # resclae + cropareaCalibrated <- cropareaShareGrid * luh3physCropland + + ### Check physical cropland matching ### # Sanity check (physical croparea + fallow land should match total cropland in LUH3 after calibration) - if (any(round(dimSums(cropareaCalibrated, dim = "crop") + fallowLandCalibrated - luh3Cropland, 6) != 0)) { + if (any(round(dimSums(cropareaCalibrated, dim = c("crop", "irrigation")) - luh3physCropland, 6) != 0)) { stop("Calibrated physical croparea + fallow land does not match LUH3 cropland.") } - } - # return croparea (and optionally fallow land) - if (fallow) { - croparea <- mbind(cropareaCalibrated, fallowLandCalibrated) + if (!physical) { + cropareaCalibrated <- cropareaCalibrated * multiFactor + } + + croparea <- cropareaCalibrated description <- paste0(description, " including fallow land.") + } else { - croparea <- cropareaCalibrated - description <- paste0(description, " excluding fallow land.") + # To speed up, aggregation is done with incursively. + croparea <- calcOutput("Croparea", aggregate = FALSE, sectoral = sectoral, + physical = physical, fallow = TRUE, + cellular = TRUE, irrigation = TRUE, + datasource = "LandInG") + # Aggregation to iso-level and wrt irrigation + if (!cellular) { + # aggregate to countries + croparea <- dimSums(croparea, dim = c("x", "y")) + # fill missing countries with 0 + croparea <- toolConditionalReplace(x = toolCountryFill(croparea), + conditions = "is.na()", replaceby = 0) + } + if (!irrigation) { + # aggregate to countries + croparea <- dimSums(croparea, dim = c("irrigation")) + } } - # Aggregation to iso-level - if (!cellular) { - # aggregate to countries - croparea <- dimSums(croparea, dim = c("x", "y")) - # fill missing countries with 0 - croparea <- toolConditionalReplace(x = toolCountryFill(croparea), - conditions = "is.na()", replaceby = 0) + # return croparea (and optionally fallow land) + if (!fallow) { + croparea <- croparea[, , "fallow", invert = TRUE] + description <- paste0(description, " excluding fallow land.") + } else { + description <- paste0(description, " including fallow land.") } } else if (datasource == "FAOLUH") { + + if (fallow) { + stop("fallow not implemented for FAOLUH") + } ### For backwards compatibility only ### # This chunk can be deleted when croparea update is completed. diff --git a/README.md b/README.md index 493f00c..84490c1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.6.7** +R package **mrlandcore**, version **1.7.0** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.0, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-03-13}, + date = {2026-03-18}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.6.7}, + note = {Version: 1.7.0}, } ``` From a76b0d12b56087ff2d72fd76611a1e147bb25940 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Wed, 18 Mar 2026 20:33:44 +0100 Subject: [PATCH 10/36] switch to LanduseIni instead of LUH3 --- .buildlibrary | 2 +- CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcCroparea.R | 20 ++++++++++---------- README.md | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 425b716..2b88e1d 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3490100' +ValidationKey: '3510630' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index e65ba89..f73f22f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,7 +4,7 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.0 +version: 1.7.1 date-released: '2026-03-18' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: diff --git a/DESCRIPTION b/DESCRIPTION index 0d8ca34..ce28336 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.0 +Version: 1.7.1 Date: 2026-03-18 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index c33378b..4905d2f 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -81,8 +81,7 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cropareaWithFallow <- mbind(croparea, fallowLandIrrigated) - ### Correction to match LUH cropland area - # Note: Correction necessary to make LandInG match LUH3 land classes + ### Correction to match LanduseInitialisation cropland area # calculate area shares cropareaShareGrid <- cropareaWithFallow / dimSums(cropareaWithFallow, dim = c("irrigation", "crop")) @@ -93,19 +92,20 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, cropareaShareIso <- ifelse(is.nan(cropareaShareIso), cropareaShareGlo, cropareaShareIso) cropareaShareGrid <- ifelse(is.nan(cropareaShareGrid), cropareaShareIso, cropareaShareGrid) - # for correction: read LUH3 cropland area - luh3 <- calcOutput("LUH3", landuseTypes = "magpie", irrigation = FALSE, - cellular = TRUE, aggregate = FALSE, yrs = selectyears) + # for correction: read LanduseInitialisation cropland area + landuseIni <- calcOutput(type = "LanduseInitialisation", aggregate = FALSE, + cellular = TRUE, selectyears = selectyears) - luh3physCropland <- collapseNames(luh3[, , "crop"]) + landuseIniCrop <- collapseNames(landuseIni[, , "crop"]) # resclae - cropareaCalibrated <- cropareaShareGrid * luh3physCropland + cropareaCalibrated <- cropareaShareGrid * landuseIniCrop ### Check physical cropland matching ### - # Sanity check (physical croparea + fallow land should match total cropland in LUH3 after calibration) - if (any(round(dimSums(cropareaCalibrated, dim = c("crop", "irrigation")) - luh3physCropland, 6) != 0)) { - stop("Calibrated physical croparea + fallow land does not match LUH3 cropland.") + # Sanity check (physical croparea + fallow land should match total cropland + # in LanduseInitialisation after calibration) + if (any(round(dimSums(cropareaCalibrated, dim = c("crop", "irrigation")) - landuseIniCrop, 6) != 0)) { + stop("Calibrated physical croparea + fallow land does not match LanduseInitialisation cropland.") } if (!physical) { diff --git a/README.md b/README.md index 84490c1..2e5f4c3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.0** +R package **mrlandcore**, version **1.7.1** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.1, . A BibTeX entry for LaTeX users is @@ -51,6 +51,6 @@ A BibTeX entry for LaTeX users is date = {2026-03-18}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.0}, + note = {Version: 1.7.1}, } ``` From cd2b2207270b2ab7c5923f5cac16a2031686ee8f Mon Sep 17 00:00:00 2001 From: bodirsky Date: Thu, 9 Apr 2026 14:54:02 +0200 Subject: [PATCH 11/36] .. --- .buildlibrary | 2 +- CITATION.cff | 4 +-- DESCRIPTION | 4 +-- R/calcCroparea.R | 2 +- R/calcCropareaFAOLUH.R | 6 ++-- R/calcMulticropping.R | 60 +++++++++------------------------------- README.md | 8 +++--- man/calcMulticropping.Rd | 21 ++++---------- 8 files changed, 31 insertions(+), 76 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 2b88e1d..29be589 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3510630' +ValidationKey: '3532364' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index f73f22f..5c05758 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.1 -date-released: '2026-03-18' +version: 1.7.2 +date-released: '2026-03-25' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index ce28336..d73454d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.1 -Date: 2026-03-18 +Version: 1.7.2 +Date: 2026-03-25 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 4905d2f..97c2d8e 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -54,7 +54,7 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, multiFactor <- cropareaMulti / croparea multiFactor <- ifelse(test = is.nan(multiFactor), 1, multiFactor) - multiFactor <- ifelse(test = multiFactor > 2, 2, multiFactor) + multiFactor <- ifelse(test = multiFactor > 3, 3, multiFactor) multiFactor <- add_columns(x = multiFactor, addnm = "fallow", dim = "crop", fill = 1) diff --git a/R/calcCropareaFAOLUH.R b/R/calcCropareaFAOLUH.R index 63d3ef8..01487f7 100644 --- a/R/calcCropareaFAOLUH.R +++ b/R/calcCropareaFAOLUH.R @@ -86,7 +86,7 @@ calcCropareaFAOLUH <- function(sectoral = "kcr", physical = TRUE, cellular = FAL } else if (sectoral == "lpj") { - magCroparea <- calcOutput("Croparea", sectoral = "kcr", physical = physical, + magCroparea <- calcOutput("CropareaFAOLUH", sectoral = "kcr", physical = physical, cellular = FALSE, irrigation = FALSE, aggregate = FALSE) mag2lpj <- toolGetMapping(type = "sectoral", name = "MAgPIE_LPJmL.csv", @@ -191,7 +191,7 @@ calcCropareaFAOLUH <- function(sectoral = "kcr", physical = TRUE, cellular = FAL } else if (sectoral == "lpj") { - magCroparea <- calcOutput("Croparea", sectoral = "kcr", physical = physical, + magCroparea <- calcOutput("CropareaFAOLUH", sectoral = "kcr", physical = physical, cellular = TRUE, irrigation = irrigation, aggregate = FALSE) mag2lpj <- toolGetMapping(type = "sectoral", name = "MAgPIE_LPJmL.csv", where = "mappingfolder") @@ -205,7 +205,7 @@ calcCropareaFAOLUH <- function(sectoral = "kcr", physical = TRUE, cellular = FAL if (!physical) { - multiCropping <- calcOutput("Multicropping", aggregate = FALSE) + multiCropping <- calcOutput("MulticroppingOld", aggregate = FALSE) commonCountries <- intersect(getItems(multiCropping, dim = "ISO"), getItems(data, dim = "iso")) data <- data * multiCropping[commonCountries, getYears(data), ] } diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index a2576ad..24d09b6 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -1,68 +1,34 @@ #' @title calcMulticropping -#' @description calculates a multiple cropping factor based on area harvested, -#' physical cropland area (and optionally fallow land). +#' @description Multicropping factor, telling how many harvests are produced +#' on one unit of phyiscal area. Fallow lands are not counted in, so the factor +#' is always larger than 1. #' -#' @param extend_future if TRUE -#' @param factortype CI: cropping intensity factor calculated as ratio of -#' harvested to physical area where values above one -#' indicate multicropping, below one fallow land (default) -#' MC: multiple cropping factor indicating areas that are -#' harvested more than once in one year calculated taking -#' fallow land into account explicitly: -#' harvestedArea / (physicalArea - fallowLand) #' @return List of magpie objects with results on country level, weight on country level, unit and description. -#' @author Benjamin Leon Bodirsky, David Chen, Felicitas Beier +#' @author Benjamin Leon Bodirsky #' @seealso -#' [calcFAOLand()], #' [calcCroparea()] #' @examples #' \dontrun{ #' calcOutput("Multicropping") #' } #' -calcMulticropping <- function(extend_future = FALSE, factortype = "CI") { # nolint +calcMulticropping <- function() { # nolint - # physical cropland area ("6620|Cropland") - phys <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6620", pmatch = TRUE]) - # harvested area + phys <- collapseNames(dimSums(calcOutput("Croparea", physical = TRUE, + aggregate = FALSE, sectoral = "kcr"), + dim = 3.1)) harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, aggregate = FALSE, sectoral = "kcr"), dim = 3.1)) + out <- harv / phys - # match year dimension - phys <- phys[, intersect(getYears(phys), getYears(harv)), ] - harv <- harv[, intersect(getYears(phys), getYears(harv)), ] - - if (factortype == "CI") { - # Cropping intensity (>1: mulitple cropping dominates; <1: fallow land dominates) - out <- harv / phys - description <- "cropping intensity factor with values above one indicating multicropping, below one fallow land" - - } else if (factortype == "MC") { - # fallow land ("6640|Land with temporary fallow") - fallow <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6640", pmatch = TRUE]) - - # match year dimension - fallow <- fallow[, intersect(getYears(fallow), getYears(harv)), ] - - # Multiple cropping factor accounting for land that is left fallow - out <- harv / (phys - fallow) - description <- "multiple cropping factor explicitly accounting for fallow land" - - } else { - stop("Please select calculation method via the type argument in calcMulticropping") - } - - out[is.na(out)] <- 0 - out[out == Inf] <- 0 + out[is.na(out)] <- 1 - if (extend_future == TRUE) { - out <- toolHoldConstantBeyondEnd(out) - phys <- toolHoldConstantBeyondEnd(phys) - } + out <- toolHoldConstantBeyondEnd(out) + phys <- toolHoldConstantBeyondEnd(phys) return(list(x = out, weight = phys, unit = "ratio", - description = description)) + description = "Ratio of area harvested to phyiscal area, excluding fallow land")) } diff --git a/README.md b/README.md index 2e5f4c3..75152ff 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.1** +R package **mrlandcore**, version **1.7.2** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.2, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-03-18}, + date = {2026-03-25}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.1}, + note = {Version: 1.7.2}, } ``` diff --git a/man/calcMulticropping.Rd b/man/calcMulticropping.Rd index 6d33ccb..22419b0 100644 --- a/man/calcMulticropping.Rd +++ b/man/calcMulticropping.Rd @@ -4,25 +4,15 @@ \alias{calcMulticropping} \title{calcMulticropping} \usage{ -calcMulticropping(extend_future = FALSE, factortype = "CI") -} -\arguments{ -\item{extend_future}{if TRUE} - -\item{factortype}{CI: cropping intensity factor calculated as ratio of - harvested to physical area where values above one - indicate multicropping, below one fallow land (default) -MC: multiple cropping factor indicating areas that are - harvested more than once in one year calculated taking - fallow land into account explicitly: - harvestedArea / (physicalArea - fallowLand)} +calcMulticropping() } \value{ List of magpie objects with results on country level, weight on country level, unit and description. } \description{ -calculates a multiple cropping factor based on area harvested, - physical cropland area (and optionally fallow land). +Multicropping factor, telling how many harvests are produced +on one unit of phyiscal area. Fallow lands are not counted in, so the factor +is always larger than 1. } \examples{ \dontrun{ @@ -31,9 +21,8 @@ calcOutput("Multicropping") } \seealso{ -[calcFAOLand()], [calcCroparea()] } \author{ -Benjamin Leon Bodirsky, David Chen, Felicitas Beier +Benjamin Leon Bodirsky } From 5a9971e152154394e0ab8f4479fb8945f94f1864 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Thu, 9 Apr 2026 15:35:46 +0200 Subject: [PATCH 12/36] .. --- R/calcMulticroppingOld.R | 70 +++++++++++++++++++++++++++++++++++++ man/calcFallow.Rd | 35 +++++++++++++++++++ man/calcFallowLandInG.Rd | 36 +++++++++++++++++++ man/calcMulticroppingOld.Rd | 41 ++++++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 R/calcMulticroppingOld.R create mode 100644 man/calcFallow.Rd create mode 100644 man/calcFallowLandInG.Rd create mode 100644 man/calcMulticroppingOld.Rd diff --git a/R/calcMulticroppingOld.R b/R/calcMulticroppingOld.R new file mode 100644 index 0000000..6cbc998 --- /dev/null +++ b/R/calcMulticroppingOld.R @@ -0,0 +1,70 @@ +#' @title calcMulticroppingOld +#' @description calculates a multiple cropping factor based on area harvested, +#' physical cropland area (and optionally fallow land). This function +#' is an outdated version of calcMulticropping that is kept for +#' Legacy reasons only for calcCropareaFAOLUH +#' +#' @param extend_future if TRUE +#' @param factortype CI: cropping intensity factor calculated as ratio of +#' harvested to physical area where values above one +#' indicate multicropping, below one fallow land (default) +#' MC: multiple cropping factor indicating areas that are +#' harvested more than once in one year calculated taking +#' fallow land into account explicitly: +#' harvestedArea / (physicalArea - fallowLand) +#' @return List of magpie objects with results on country level, weight on country level, unit and description. +#' @author Benjamin Leon Bodirsky, David Chen, Felicitas Beier +#' @seealso +#' [calcFAOLand()], +#' [calcCroparea()] +#' @examples +#' \dontrun{ +#' calcOutput("calcMulticroppingOld") +#' } +#' +calcMulticroppingOld <- function(extend_future = FALSE, factortype = "CI") { # nolint + + # physical cropland area ("6620|Cropland") + phys <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6620", pmatch = TRUE]) + # harvested area + harv <- collapseNames(dimSums(calcOutput("CropareaFAOLUH", physical = FALSE, + aggregate = FALSE, sectoral = "kcr"), + dim = 3.1)) + + # match year dimension + phys <- phys[, intersect(getYears(phys), getYears(harv)), ] + harv <- harv[, intersect(getYears(phys), getYears(harv)), ] + + if (factortype == "CI") { + # Cropping intensity (>1: mulitple cropping dominates; <1: fallow land dominates) + out <- harv / phys + description <- "cropping intensity factor with values above one indicating multicropping, below one fallow land" + + } else if (factortype == "MC") { + # fallow land ("6640|Land with temporary fallow") + fallow <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6640", pmatch = TRUE]) + + # match year dimension + fallow <- fallow[, intersect(getYears(fallow), getYears(harv)), ] + + # Multiple cropping factor accounting for land that is left fallow + out <- harv / (phys - fallow) + description <- "multiple cropping factor explicitly accounting for fallow land" + + } else { + stop("Please select calculation method via the type argument in calcMulticropping") + } + + out[is.na(out)] <- 0 + out[out == Inf] <- 0 + + if (extend_future == TRUE) { + out <- toolHoldConstantBeyondEnd(out) + phys <- toolHoldConstantBeyondEnd(phys) + } + + return(list(x = out, + weight = phys, + unit = "ratio", + description = description)) +} diff --git a/man/calcFallow.Rd b/man/calcFallow.Rd new file mode 100644 index 0000000..39ed012 --- /dev/null +++ b/man/calcFallow.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcFallow.R +\name{calcFallow} +\alias{calcFallow} +\title{calcFallow} +\usage{ +calcFallow(cellular = FALSE, physical = TRUE) +} +\arguments{ +\item{cellular}{TRUE for cellular outputs.} + +\item{physical}{if TRUE it returns the physical area, with fallow of multicropping systems being +scaled to match physical areas of the rotation, if FALSE the area harvested +being fallow is returned} +} +\value{ +MAgPIE object containing fallow land in Mha +} +\description{ +Returns fallow land of our default datasource. +The estimates are scaled to match our Land classes in the Croparea function. +Due to multiple cropping, harvested cropland area can be greater than non-fallow land area +and even greater than physical cropland area. +} +\examples{ +\dontrun{ +calcOutput("Fallow") +} +} +\seealso{ +\code{\link{readLandInG}} +} +\author{ +David Hoetten, Felicitas Beier +} diff --git a/man/calcFallowLandInG.Rd b/man/calcFallowLandInG.Rd new file mode 100644 index 0000000..fccdeb0 --- /dev/null +++ b/man/calcFallowLandInG.Rd @@ -0,0 +1,36 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcFallowLandInG.R +\name{calcFallowLandInG} +\alias{calcFallowLandInG} +\title{calcFallowLandInG} +\usage{ +calcFallowLandInG(cellular = TRUE) +} +\arguments{ +\item{cellular}{TRUE for cellular outputs.} +} +\value{ +MAgPIE object containing fallow land in Mha +} +\description{ +Calculates fallow land on grid cell level, +based on physical cropland extend and harvested area output +of LandInG data. +The formula +"fallow land are = max( physical cropland area - harvested cropland area, 0)" +is used. +Due to multiple cropping, harvested cropland area can be greater than non-fallow land area +and even greater than physical cropland area. +Thus, the results can only be considered a rough estimate of fallow land area. +} +\examples{ +\dontrun{ +calcOutput("FallowLandInG") +} +} +\seealso{ +\code{\link{readLandInG}} +} +\author{ +David Hoetten, Felicitas Beier +} diff --git a/man/calcMulticroppingOld.Rd b/man/calcMulticroppingOld.Rd new file mode 100644 index 0000000..98af158 --- /dev/null +++ b/man/calcMulticroppingOld.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcMulticroppingOld.R +\name{calcMulticroppingOld} +\alias{calcMulticroppingOld} +\title{calcMulticroppingOld} +\usage{ +calcMulticroppingOld(extend_future = FALSE, factortype = "CI") +} +\arguments{ +\item{extend_future}{if TRUE} + +\item{factortype}{CI: cropping intensity factor calculated as ratio of + harvested to physical area where values above one + indicate multicropping, below one fallow land (default) +MC: multiple cropping factor indicating areas that are + harvested more than once in one year calculated taking + fallow land into account explicitly: + harvestedArea / (physicalArea - fallowLand)} +} +\value{ +List of magpie objects with results on country level, weight on country level, unit and description. +} +\description{ +calculates a multiple cropping factor based on area harvested, + physical cropland area (and optionally fallow land). This function + is an outdated version of calcMulticropping that is kept for + Legacy reasons only for calcCropareaFAOLUH +} +\examples{ +\dontrun{ +calcOutput("calcMulticroppingOld") +} + +} +\seealso{ +[calcFAOLand()], +[calcCroparea()] +} +\author{ +Benjamin Leon Bodirsky, David Chen, Felicitas Beier +} From 0cb77c76389e02d6a1d8455aa1d1e0856ddd4457 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Sat, 11 Apr 2026 07:31:59 +0200 Subject: [PATCH 13/36] ... --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- README.md | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 2868dfd..00ffa36 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3492310' +ValidationKey: '3514734' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index a61eb25..d6f95b4 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.0 -date-released: '2026-03-31' +version: 1.7.1 +date-released: '2026-04-11' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index 68e60ee..d212481 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.0 -Date: 2026-03-31 +Version: 1.7.1 +Date: 2026-04-11 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/README.md b/README.md index 54aeee5..728c48c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.0** +R package **mrlandcore**, version **1.7.1** [![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.1, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-03-31}, + date = {2026-04-11}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.0}, + note = {Version: 1.7.1}, } ``` From 2cfcec0474e01116ca03505dbbd2db07972fdb9c Mon Sep 17 00:00:00 2001 From: bodirsky Date: Wed, 6 May 2026 14:38:35 +0200 Subject: [PATCH 14/36] .. --- R/calcLUH2MAgPIE.R | 3 ++- R/calcMulticropping.R | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/R/calcLUH2MAgPIE.R b/R/calcLUH2MAgPIE.R index b7c96f8..a263d65 100644 --- a/R/calcLUH2MAgPIE.R +++ b/R/calcLUH2MAgPIE.R @@ -1,5 +1,5 @@ #' @title calcLUH2MAgPIE -#' @description Calculates the real aggregation of LUH croptypes to MAgPIE croptypes +#' @description Calculates the relation matrix for LUH croptype and MAgPIE croptype areas #' out of LUH2FAO and FAO2MAgPIE mappings #' #' @param share total (for total numbers), LUHofMAG (for share of LUH within kcr types), @@ -48,6 +48,7 @@ calcLUH2MAgPIE <- function(share = "total", bioenergy = "ignore", warning("No missing data for total numbers assumed.") } + # BLB: Shouldnt this rather use FAOharmonized? cropPrim <- readSource("FAO_online", "CropLive2010")[, , "area_harvested"] # use linear_interpolate fodder <- readSource("FAO", "Fodder")[, , "area_harvested"] diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index 24d09b6..15418da 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -3,6 +3,7 @@ #' on one unit of phyiscal area. Fallow lands are not counted in, so the factor #' is always larger than 1. #' +#' @param irrigation report irrigated and non-irrigated areas separately #' @return List of magpie objects with results on country level, weight on country level, unit and description. #' @author Benjamin Leon Bodirsky #' @seealso @@ -12,13 +13,13 @@ #' calcOutput("Multicropping") #' } #' -calcMulticropping <- function() { # nolint +calcMulticropping <- function(irrigation = TRUE, cellular = FALSE) { # nolint - phys <- collapseNames(dimSums(calcOutput("Croparea", physical = TRUE, - aggregate = FALSE, sectoral = "kcr"), + phys <- collapseNames(dimSums(calcOutput("Croparea", physical = TRUE, cellular = cellular, + aggregate = FALSE, irrigation = irrigation), dim = 3.1)) - harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, - aggregate = FALSE, sectoral = "kcr"), + harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, cellular = cellular, + aggregate = FALSE, irrigation = irrigation), dim = 3.1)) out <- harv / phys From 8338aec5bd41b5b4b47ff20d4c93a436a63e223b Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 29 May 2026 16:03:11 +0200 Subject: [PATCH 15/36] fixes in calcMulticropping --- .Rbuildignore | 1 + .buildlibrary | 2 +- .github/workflows/check.yaml | 31 ++++++++----------------------- .github/workflows/pkgdown.yaml | 20 +++++++++----------- .gitignore | 1 + .pre-commit-config.yaml | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 6 +++--- R/calcMulticropping.R | 12 ++++++------ README.md | 14 +++++++------- man/calcLUH2MAgPIE.Rd | 2 +- man/calcMulticropping.Rd | 9 ++++++--- man/mrlandcore-package.Rd | 1 + 13 files changed, 47 insertions(+), 58 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index dcdf56c..a87edbd 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -11,3 +11,4 @@ ^codecov\.yml$ ^\.claude$ ^_pkgdown\.yml$ +^\.positai$ diff --git a/.buildlibrary b/.buildlibrary index 00ffa36..57beb97 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3514734' +ValidationKey: '35414858602' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index e7ef0c4..a685741 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -11,37 +11,22 @@ jobs: check: runs-on: ubuntu-latest if: github.event.pull_request.draft == false + container: + image: ghcr.io/pik-piam/ci-image:latest steps: - uses: actions/checkout@v4 - - uses: r-lib/actions/setup-pandoc@v2 - - - uses: r-lib/actions/setup-r@v2 - with: - use-public-rspm: true - extra-repositories: "https://rse.pik-potsdam.de/r/packages" - - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: | - lucode2 - covr - madrat - magclass - citation - gms - goxygen - 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 + - name: Install package and dependencies + shell: Rscript {0} + run: | + options(repos = c(pikpiam = 'https://pik-piam.r-universe.dev', + CRAN = Sys.getenv('RSPM'))) + pak::pak() - name: Run pre-commit checks shell: bash run: | - python -m pip install pre-commit - python -m pip freeze --local pre-commit run --show-diff-on-failure --color=always --all-files - name: Verify validation key diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 4152dcd..ec2d3cb 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -18,20 +18,18 @@ jobs: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} permissions: contents: write + container: + image: ghcr.io/pik-piam/ci-image:latest steps: - uses: actions/checkout@v4 - - uses: r-lib/actions/setup-pandoc@v2 - - - uses: r-lib/actions/setup-r@v2 - with: - use-public-rspm: true - extra-repositories: "https://rse.pik-potsdam.de/r/packages" - - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - extra-packages: any::pkgdown, local::. - needs: website + - name: Install + shell: Rscript {0} + run: | + options(repos = c(pikpiam = 'https://pik-piam.r-universe.dev', + CRAN = Sys.getenv('RSPM'))) + pak::pak(".") + pak::pkg_install("tidyverse/tidytemplate") - name: Build site run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) diff --git a/.gitignore b/.gitignore index 3dc02f4..2224bc8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .RData .Ruserdata .Rprofile +.positai diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3edf90a..b1ab7b4 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: 3b70240796cdccbe1474b0176560281aaded97e6 # frozen: v0.4.3.9003 + rev: v0.4.3.9021 hooks: - id: parsable-R - id: deps-in-desc diff --git a/CITATION.cff b/CITATION.cff index d6f95b4..9535093 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.1 -date-released: '2026-04-11' +version: 1.7.1.9001 +date-released: '2026-05-29' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index d212481..b21f2bc 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.1 -Date: 2026-04-11 +Version: 1.7.1.9001 +Date: 2026-05-29 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), @@ -49,5 +49,5 @@ Imports: Suggests: testthat Encoding: UTF-8 -RoxygenNote: 7.3.3 Config/Needs/website: tidyverse/tidytemplate +Config/roxygen2/version: 8.0.0 diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index 15418da..b40fbd7 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -1,11 +1,11 @@ #' @title calcMulticropping #' @description Multicropping factor, telling how many harvests are produced -#' on one unit of phyiscal area. Fallow lands are not counted in, so the factor +#' on one unit of physical area. Fallow lands are not counted in, so the factor #' is always larger than 1. #' #' @param irrigation report irrigated and non-irrigated areas separately #' @return List of magpie objects with results on country level, weight on country level, unit and description. -#' @author Benjamin Leon Bodirsky +#' @author Benjamin Leon Bodirsky, Felicitas Beier #' @seealso #' [calcCroparea()] #' @examples @@ -13,15 +13,15 @@ #' calcOutput("Multicropping") #' } #' -calcMulticropping <- function(irrigation = TRUE, cellular = FALSE) { # nolint +calcMulticropping <- function(irrigation = TRUE) { # nolint - phys <- collapseNames(dimSums(calcOutput("Croparea", physical = TRUE, cellular = cellular, + phys <- collapseNames(dimSums(calcOutput("Croparea", physical = TRUE, cellular = TRUE, aggregate = FALSE, irrigation = irrigation), dim = 3.1)) - harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, cellular = cellular, + harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, cellular = TRUE, aggregate = FALSE, irrigation = irrigation), dim = 3.1)) - out <- harv / phys + out <- ifelse(phys > 0, harv / phys, NA) out[is.na(out)] <- 1 diff --git a/README.md b/README.md index 728c48c..945e5d6 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.1** +R package **mrlandcore**, version **1.7.1.9001** -[![CRAN status](https://www.r-pkg.org/badges/version/mrlandcore)](https://cran.r-project.org/package=mrlandcore) [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) + [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) ## Purpose and Functionality @@ -21,13 +21,13 @@ The additional repository can be made available permanently by adding the line a After that the most recent version of the package can be installed using `install.packages`: -```r +```r install.packages("mrlandcore") ``` Package updates can be installed using `update.packages` (make sure that the additional repository has been added before running that command): -```r +```r update.packages() ``` @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.1.9001, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-04-11}, + date = {2026-05-29}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.1}, + note = {Version: 1.7.1.9001}, } ``` diff --git a/man/calcLUH2MAgPIE.Rd b/man/calcLUH2MAgPIE.Rd index c28732a..ca3db11 100644 --- a/man/calcLUH2MAgPIE.Rd +++ b/man/calcLUH2MAgPIE.Rd @@ -27,7 +27,7 @@ MAGofLUH (for share of kcr within LUH types)} List of magpie objects with results on country level, weight on country level, unit and description } \description{ -Calculates the real aggregation of LUH croptypes to MAgPIE croptypes +Calculates the relation matrix for LUH croptype and MAgPIE croptype areas out of LUH2FAO and FAO2MAgPIE mappings } \examples{ diff --git a/man/calcMulticropping.Rd b/man/calcMulticropping.Rd index 22419b0..6afe625 100644 --- a/man/calcMulticropping.Rd +++ b/man/calcMulticropping.Rd @@ -4,14 +4,17 @@ \alias{calcMulticropping} \title{calcMulticropping} \usage{ -calcMulticropping() +calcMulticropping(irrigation = TRUE) +} +\arguments{ +\item{irrigation}{report irrigated and non-irrigated areas separately} } \value{ List of magpie objects with results on country level, weight on country level, unit and description. } \description{ Multicropping factor, telling how many harvests are produced -on one unit of phyiscal area. Fallow lands are not counted in, so the factor +on one unit of physical area. Fallow lands are not counted in, so the factor is always larger than 1. } \examples{ @@ -24,5 +27,5 @@ calcOutput("Multicropping") [calcCroparea()] } \author{ -Benjamin Leon Bodirsky +Benjamin Leon Bodirsky, Felicitas Beier } diff --git a/man/mrlandcore-package.Rd b/man/mrlandcore-package.Rd index c905415..89d1a7a 100644 --- a/man/mrlandcore-package.Rd +++ b/man/mrlandcore-package.Rd @@ -20,6 +20,7 @@ Useful links: Authors: \itemize{ + \item Felicitas Beier \email{beier@pik-potsdam.de} \item Kristine Karstens \item Marcos Alves \item Jan Philipp Dietrich From e7d87df0ceff2e9ca3185a773f1476fb7ea46551 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 29 May 2026 17:12:27 +0200 Subject: [PATCH 16/36] include water dimension in multicropping factor and corrected typos in calcCroparea --- R/calcCroparea.R | 12 ++++++++---- R/calcMulticropping.R | 10 ++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 97c2d8e..d047f1c 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -30,6 +30,10 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, # description of data to be returned description <- paste0(ifelse(physical, "physical ", "harvested "), "croparea from LandInG data set") + + # The following calculations are based on cellular, irrigation and fallow + # dimensions being included. The selection of what is returned happens + # further down by a recursive function call if (irrigation && cellular && fallow) { # read in croparea croparea <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = TRUE, @@ -98,7 +102,7 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, landuseIniCrop <- collapseNames(landuseIni[, , "crop"]) - # resclae + # rescale cropareaCalibrated <- cropareaShareGrid * landuseIniCrop ### Check physical cropland matching ### @@ -116,7 +120,7 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, description <- paste0(description, " including fallow land.") } else { - # To speed up, aggregation is done with incursively. + # To speed up, aggregation is done with recursively croparea <- calcOutput("Croparea", aggregate = FALSE, sectoral = sectoral, physical = physical, fallow = TRUE, cellular = TRUE, irrigation = TRUE, @@ -130,8 +134,8 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, conditions = "is.na()", replaceby = 0) } if (!irrigation) { - # aggregate to countries - croparea <- dimSums(croparea, dim = c("irrigation")) + # aggregate irrigation dimension + croparea <- dimSums(croparea, dim = "irrigation") } } diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index b40fbd7..d5b307a 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -15,12 +15,10 @@ #' calcMulticropping <- function(irrigation = TRUE) { # nolint - phys <- collapseNames(dimSums(calcOutput("Croparea", physical = TRUE, cellular = TRUE, - aggregate = FALSE, irrigation = irrigation), - dim = 3.1)) - harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, cellular = TRUE, - aggregate = FALSE, irrigation = irrigation), - dim = 3.1)) + phys <- collapseNames(calcOutput("Croparea", physical = TRUE, cellular = TRUE, + aggregate = FALSE, irrigation = irrigation)) + harv <- collapseNames(calcOutput("Croparea", physical = FALSE, cellular = TRUE, + aggregate = FALSE, irrigation = irrigation)) out <- ifelse(phys > 0, harv / phys, NA) out[is.na(out)] <- 1 From 482493b0f1d96289315da66b63ce5fca3a6d42d8 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Tue, 2 Jun 2026 18:21:19 +0200 Subject: [PATCH 17/36] remove nolint that is not needed anymore --- R/calcMulticropping.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index d5b307a..7b7a4eb 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -13,7 +13,7 @@ #' calcOutput("Multicropping") #' } #' -calcMulticropping <- function(irrigation = TRUE) { # nolint +calcMulticropping <- function(irrigation = TRUE) { phys <- collapseNames(calcOutput("Croparea", physical = TRUE, cellular = TRUE, aggregate = FALSE, irrigation = irrigation)) From 3384b6a1c0f43f2820b704d0fbb2973aa2c31aab Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Wed, 3 Jun 2026 12:04:30 +0200 Subject: [PATCH 18/36] bugfix in return statement of calcMulticropping --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- R/calcMulticropping.R | 7 ++++--- README.md | 8 ++++---- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 57beb97..2dfc810 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '35414858602' +ValidationKey: '35423474214' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 9535093..1eaed7f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.1.9001 -date-released: '2026-05-29' +version: 1.7.1.9002 +date-released: '2026-06-03' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index b21f2bc..f771d8e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.1.9001 -Date: 2026-05-29 +Version: 1.7.1.9002 +Date: 2026-06-03 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index 7b7a4eb..0b540c0 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -16,9 +16,9 @@ calcMulticropping <- function(irrigation = TRUE) { phys <- collapseNames(calcOutput("Croparea", physical = TRUE, cellular = TRUE, - aggregate = FALSE, irrigation = irrigation)) + aggregate = FALSE, irrigation = irrigation)) harv <- collapseNames(calcOutput("Croparea", physical = FALSE, cellular = TRUE, - aggregate = FALSE, irrigation = irrigation)) + aggregate = FALSE, irrigation = irrigation)) out <- ifelse(phys > 0, harv / phys, NA) out[is.na(out)] <- 1 @@ -29,5 +29,6 @@ calcMulticropping <- function(irrigation = TRUE) { return(list(x = out, weight = phys, unit = "ratio", - description = "Ratio of area harvested to phyiscal area, excluding fallow land")) + description = "Ratio of area harvested to phyiscal area, excluding fallow land", + isocountries = FALSE)) } diff --git a/README.md b/README.md index 945e5d6..552fa78 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.1.9001** +R package **mrlandcore**, version **1.7.1.9002** [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.1.9002, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-05-29}, + date = {2026-06-03}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.1.9001}, + note = {Version: 1.7.1.9002}, } ``` From d9ab1b993eb14248ede5d6efa73b7f446b67d22e Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Thu, 4 Jun 2026 09:57:12 +0200 Subject: [PATCH 19/36] handling of aggregation with zeros in calcMulticropping --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- R/calcMulticropping.R | 1 + README.md | 8 ++++---- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 2dfc810..bc80a45 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '35423474214' +ValidationKey: '35425213824' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 1eaed7f..edfa7fa 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.1.9002 -date-released: '2026-06-03' +version: 1.7.1.9003 +date-released: '2026-06-04' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index f771d8e..cb82f45 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.1.9002 -Date: 2026-06-03 +Version: 1.7.1.9003 +Date: 2026-06-04 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index 0b540c0..1f973d0 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -28,6 +28,7 @@ calcMulticropping <- function(irrigation = TRUE) { return(list(x = out, weight = phys, + aggregationArguments = list(zeroWeight = "fix"), unit = "ratio", description = "Ratio of area harvested to phyiscal area, excluding fallow land", isocountries = FALSE)) diff --git a/README.md b/README.md index 552fa78..d333c92 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.1.9002** +R package **mrlandcore**, version **1.7.1.9003** [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.1.9003, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-06-03}, + date = {2026-06-04}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.1.9002}, + note = {Version: 1.7.1.9003}, } ``` From 2a30987438abdc2999e6705b273241e058759eae Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 5 Jun 2026 11:29:45 +0200 Subject: [PATCH 20/36] hotfix for aggregation issue in calcMulticropping --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- R/calcMulticropping.R | 3 +-- README.md | 8 ++++---- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index bc80a45..4ea4d2d 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '35425213824' +ValidationKey: '35426953436' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index edfa7fa..e216a3f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.7.1.9003 -date-released: '2026-06-04' +version: 1.7.1.9004 +date-released: '2026-06-05' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index cb82f45..60c8101 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.1.9003 -Date: 2026-06-04 +Version: 1.7.1.9004 +Date: 2026-06-05 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index 1f973d0..cd2b56e 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -24,11 +24,10 @@ calcMulticropping <- function(irrigation = TRUE) { out[is.na(out)] <- 1 out <- toolHoldConstantBeyondEnd(out) - phys <- toolHoldConstantBeyondEnd(phys) + phys <- toolHoldConstantBeyondEnd(phys) + 10^-30 return(list(x = out, weight = phys, - aggregationArguments = list(zeroWeight = "fix"), unit = "ratio", description = "Ratio of area harvested to phyiscal area, excluding fallow land", isocountries = FALSE)) diff --git a/README.md b/README.md index d333c92..028ff35 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.7.1.9003** +R package **mrlandcore**, version **1.7.1.9004** [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.7.1.9004, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-06-04}, + date = {2026-06-05}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.7.1.9003}, + note = {Version: 1.7.1.9004}, } ``` From 115f7126bd1bcf8706b5343a4d46cab6cda651e5 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Thu, 11 Jun 2026 16:10:20 +0200 Subject: [PATCH 21/36] revert hotfix --- DESCRIPTION | 2 +- R/calcMulticropping.R | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 60c8101..7098367 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.7.1.9004 +Version: 1.7.1.9005 Date: 2026-06-05 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index cd2b56e..1f973d0 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -24,10 +24,11 @@ calcMulticropping <- function(irrigation = TRUE) { out[is.na(out)] <- 1 out <- toolHoldConstantBeyondEnd(out) - phys <- toolHoldConstantBeyondEnd(phys) + 10^-30 + phys <- toolHoldConstantBeyondEnd(phys) return(list(x = out, weight = phys, + aggregationArguments = list(zeroWeight = "fix"), unit = "ratio", description = "Ratio of area harvested to phyiscal area, excluding fallow land", isocountries = FALSE)) From 2c147b7aa28746cdde42b4e10b538617b2daee4a Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 10 Jul 2026 11:03:21 +0200 Subject: [PATCH 22/36] reintroduce hotfix until Pascal fixed technical issue --- R/calcMulticropping.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index 1f973d0..cd2b56e 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -24,11 +24,10 @@ calcMulticropping <- function(irrigation = TRUE) { out[is.na(out)] <- 1 out <- toolHoldConstantBeyondEnd(out) - phys <- toolHoldConstantBeyondEnd(phys) + phys <- toolHoldConstantBeyondEnd(phys) + 10^-30 return(list(x = out, weight = phys, - aggregationArguments = list(zeroWeight = "fix"), unit = "ratio", description = "Ratio of area harvested to phyiscal area, excluding fallow land", isocountries = FALSE)) From 6884a909ec31990f7acb3b0be4df813deb886ac6 Mon Sep 17 00:00:00 2001 From: Pascal Sauer <156898545+pascal-sauer@users.noreply.github.com> Date: Thu, 16 Jul 2026 12:37:23 +0200 Subject: [PATCH 23/36] Revert "reintroduce hotfix until Pascal fixed technical issue" This reverts commit 2c147b7aa28746cdde42b4e10b538617b2daee4a. --- R/calcMulticropping.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/calcMulticropping.R b/R/calcMulticropping.R index cd2b56e..1f973d0 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -24,10 +24,11 @@ calcMulticropping <- function(irrigation = TRUE) { out[is.na(out)] <- 1 out <- toolHoldConstantBeyondEnd(out) - phys <- toolHoldConstantBeyondEnd(phys) + 10^-30 + phys <- toolHoldConstantBeyondEnd(phys) return(list(x = out, weight = phys, + aggregationArguments = list(zeroWeight = "fix"), unit = "ratio", description = "Ratio of area harvested to phyiscal area, excluding fallow land", isocountries = FALSE)) From 84c22d3f42afeabe79f39dd8cef3349d5282ad1b Mon Sep 17 00:00:00 2001 From: Pascal Sauer <156898545+pascal-sauer@users.noreply.github.com> Date: Thu, 16 Jul 2026 12:38:25 +0200 Subject: [PATCH 24/36] require madrat with fixed toolFixWeight --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 88a4ea6..aa181d3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,7 +26,7 @@ Description: Fundamental MAgPIE preprocessing functions including License: LGPL-3 URL: https://github.com/pik-piam/mrlandcore Depends: - madrat (>= 2.20.9), + madrat (>= 3.37.4), magclass (>= 3.17), mrdownscale (>= 0.43.0), mrdrivers (>= 1.0.0), From 87128ac98bebbdc0072601c464fbe7029de29f4d Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Thu, 6 Aug 2026 12:15:49 +0200 Subject: [PATCH 25/36] deleted old calcMulticroppingOld function --- .buildlibrary | 2 +- .github/workflows/pkgdown.yaml | 1 - CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcMulticroppingOld.R | 70 ---------------------------------- README.md | 2 +- man/calcMulticroppingOld.Rd | 41 -------------------- 7 files changed, 4 insertions(+), 116 deletions(-) delete mode 100644 R/calcMulticroppingOld.R delete mode 100644 man/calcMulticroppingOld.Rd diff --git a/.buildlibrary b/.buildlibrary index 7cad537..7e1ff8c 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3714840' +ValidationKey: '3720780' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 5fb9ac4..59b443b 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -28,7 +28,6 @@ jobs: run: | options(repos = c(pikpiam = 'https://pik-piam.r-universe.dev', CRAN = Sys.getenv('RSPM'))) - pak::pak(".", dependencies = 'all') pak::pkg_install("tidyverse/tidytemplate") diff --git a/CITATION.cff b/CITATION.cff index ecbf454..caf1159 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -5,7 +5,7 @@ title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation version: 1.8.0 -date-released: '2026-07-04' +date-released: '2026-08-06' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index aa181d3..d0a3264 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation Version: 1.8.0 -Date: 2026-07-04 +Date: 2026-08-06 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcMulticroppingOld.R b/R/calcMulticroppingOld.R deleted file mode 100644 index 6cbc998..0000000 --- a/R/calcMulticroppingOld.R +++ /dev/null @@ -1,70 +0,0 @@ -#' @title calcMulticroppingOld -#' @description calculates a multiple cropping factor based on area harvested, -#' physical cropland area (and optionally fallow land). This function -#' is an outdated version of calcMulticropping that is kept for -#' Legacy reasons only for calcCropareaFAOLUH -#' -#' @param extend_future if TRUE -#' @param factortype CI: cropping intensity factor calculated as ratio of -#' harvested to physical area where values above one -#' indicate multicropping, below one fallow land (default) -#' MC: multiple cropping factor indicating areas that are -#' harvested more than once in one year calculated taking -#' fallow land into account explicitly: -#' harvestedArea / (physicalArea - fallowLand) -#' @return List of magpie objects with results on country level, weight on country level, unit and description. -#' @author Benjamin Leon Bodirsky, David Chen, Felicitas Beier -#' @seealso -#' [calcFAOLand()], -#' [calcCroparea()] -#' @examples -#' \dontrun{ -#' calcOutput("calcMulticroppingOld") -#' } -#' -calcMulticroppingOld <- function(extend_future = FALSE, factortype = "CI") { # nolint - - # physical cropland area ("6620|Cropland") - phys <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6620", pmatch = TRUE]) - # harvested area - harv <- collapseNames(dimSums(calcOutput("CropareaFAOLUH", physical = FALSE, - aggregate = FALSE, sectoral = "kcr"), - dim = 3.1)) - - # match year dimension - phys <- phys[, intersect(getYears(phys), getYears(harv)), ] - harv <- harv[, intersect(getYears(phys), getYears(harv)), ] - - if (factortype == "CI") { - # Cropping intensity (>1: mulitple cropping dominates; <1: fallow land dominates) - out <- harv / phys - description <- "cropping intensity factor with values above one indicating multicropping, below one fallow land" - - } else if (factortype == "MC") { - # fallow land ("6640|Land with temporary fallow") - fallow <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6640", pmatch = TRUE]) - - # match year dimension - fallow <- fallow[, intersect(getYears(fallow), getYears(harv)), ] - - # Multiple cropping factor accounting for land that is left fallow - out <- harv / (phys - fallow) - description <- "multiple cropping factor explicitly accounting for fallow land" - - } else { - stop("Please select calculation method via the type argument in calcMulticropping") - } - - out[is.na(out)] <- 0 - out[out == Inf] <- 0 - - if (extend_future == TRUE) { - out <- toolHoldConstantBeyondEnd(out) - phys <- toolHoldConstantBeyondEnd(phys) - } - - return(list(x = out, - weight = phys, - unit = "ratio", - description = description)) -} diff --git a/README.md b/README.md index e2b7e7b..1b846da 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-07-04}, + date = {2026-08-06}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, note = {Version: 1.8.0}, diff --git a/man/calcMulticroppingOld.Rd b/man/calcMulticroppingOld.Rd deleted file mode 100644 index 98af158..0000000 --- a/man/calcMulticroppingOld.Rd +++ /dev/null @@ -1,41 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcMulticroppingOld.R -\name{calcMulticroppingOld} -\alias{calcMulticroppingOld} -\title{calcMulticroppingOld} -\usage{ -calcMulticroppingOld(extend_future = FALSE, factortype = "CI") -} -\arguments{ -\item{extend_future}{if TRUE} - -\item{factortype}{CI: cropping intensity factor calculated as ratio of - harvested to physical area where values above one - indicate multicropping, below one fallow land (default) -MC: multiple cropping factor indicating areas that are - harvested more than once in one year calculated taking - fallow land into account explicitly: - harvestedArea / (physicalArea - fallowLand)} -} -\value{ -List of magpie objects with results on country level, weight on country level, unit and description. -} -\description{ -calculates a multiple cropping factor based on area harvested, - physical cropland area (and optionally fallow land). This function - is an outdated version of calcMulticropping that is kept for - Legacy reasons only for calcCropareaFAOLUH -} -\examples{ -\dontrun{ -calcOutput("calcMulticroppingOld") -} - -} -\seealso{ -[calcFAOLand()], -[calcCroparea()] -} -\author{ -Benjamin Leon Bodirsky, David Chen, Felicitas Beier -} From c4f2035861772c761297b721b8e64f69b3ff39b0 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Thu, 6 Aug 2026 13:58:53 +0200 Subject: [PATCH 26/36] Revert "deleted old calcMulticroppingOld function" This reverts commit 87128ac98bebbdc0072601c464fbe7029de29f4d. --- .buildlibrary | 2 +- .github/workflows/pkgdown.yaml | 1 + CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcMulticroppingOld.R | 70 ++++++++++++++++++++++++++++++++++ README.md | 2 +- man/calcMulticroppingOld.Rd | 41 ++++++++++++++++++++ 7 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 R/calcMulticroppingOld.R create mode 100644 man/calcMulticroppingOld.Rd diff --git a/.buildlibrary b/.buildlibrary index 7e1ff8c..7cad537 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3720780' +ValidationKey: '3714840' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 59b443b..5fb9ac4 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -28,6 +28,7 @@ jobs: run: | options(repos = c(pikpiam = 'https://pik-piam.r-universe.dev', CRAN = Sys.getenv('RSPM'))) + pak::pak(".", dependencies = 'all') pak::pkg_install("tidyverse/tidytemplate") diff --git a/CITATION.cff b/CITATION.cff index caf1159..ecbf454 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -5,7 +5,7 @@ title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation version: 1.8.0 -date-released: '2026-08-06' +date-released: '2026-07-04' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index d0a3264..aa181d3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation Version: 1.8.0 -Date: 2026-08-06 +Date: 2026-07-04 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcMulticroppingOld.R b/R/calcMulticroppingOld.R new file mode 100644 index 0000000..6cbc998 --- /dev/null +++ b/R/calcMulticroppingOld.R @@ -0,0 +1,70 @@ +#' @title calcMulticroppingOld +#' @description calculates a multiple cropping factor based on area harvested, +#' physical cropland area (and optionally fallow land). This function +#' is an outdated version of calcMulticropping that is kept for +#' Legacy reasons only for calcCropareaFAOLUH +#' +#' @param extend_future if TRUE +#' @param factortype CI: cropping intensity factor calculated as ratio of +#' harvested to physical area where values above one +#' indicate multicropping, below one fallow land (default) +#' MC: multiple cropping factor indicating areas that are +#' harvested more than once in one year calculated taking +#' fallow land into account explicitly: +#' harvestedArea / (physicalArea - fallowLand) +#' @return List of magpie objects with results on country level, weight on country level, unit and description. +#' @author Benjamin Leon Bodirsky, David Chen, Felicitas Beier +#' @seealso +#' [calcFAOLand()], +#' [calcCroparea()] +#' @examples +#' \dontrun{ +#' calcOutput("calcMulticroppingOld") +#' } +#' +calcMulticroppingOld <- function(extend_future = FALSE, factortype = "CI") { # nolint + + # physical cropland area ("6620|Cropland") + phys <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6620", pmatch = TRUE]) + # harvested area + harv <- collapseNames(dimSums(calcOutput("CropareaFAOLUH", physical = FALSE, + aggregate = FALSE, sectoral = "kcr"), + dim = 3.1)) + + # match year dimension + phys <- phys[, intersect(getYears(phys), getYears(harv)), ] + harv <- harv[, intersect(getYears(phys), getYears(harv)), ] + + if (factortype == "CI") { + # Cropping intensity (>1: mulitple cropping dominates; <1: fallow land dominates) + out <- harv / phys + description <- "cropping intensity factor with values above one indicating multicropping, below one fallow land" + + } else if (factortype == "MC") { + # fallow land ("6640|Land with temporary fallow") + fallow <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6640", pmatch = TRUE]) + + # match year dimension + fallow <- fallow[, intersect(getYears(fallow), getYears(harv)), ] + + # Multiple cropping factor accounting for land that is left fallow + out <- harv / (phys - fallow) + description <- "multiple cropping factor explicitly accounting for fallow land" + + } else { + stop("Please select calculation method via the type argument in calcMulticropping") + } + + out[is.na(out)] <- 0 + out[out == Inf] <- 0 + + if (extend_future == TRUE) { + out <- toolHoldConstantBeyondEnd(out) + phys <- toolHoldConstantBeyondEnd(phys) + } + + return(list(x = out, + weight = phys, + unit = "ratio", + description = description)) +} diff --git a/README.md b/README.md index 1b846da..e2b7e7b 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-08-06}, + date = {2026-07-04}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, note = {Version: 1.8.0}, diff --git a/man/calcMulticroppingOld.Rd b/man/calcMulticroppingOld.Rd new file mode 100644 index 0000000..98af158 --- /dev/null +++ b/man/calcMulticroppingOld.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/calcMulticroppingOld.R +\name{calcMulticroppingOld} +\alias{calcMulticroppingOld} +\title{calcMulticroppingOld} +\usage{ +calcMulticroppingOld(extend_future = FALSE, factortype = "CI") +} +\arguments{ +\item{extend_future}{if TRUE} + +\item{factortype}{CI: cropping intensity factor calculated as ratio of + harvested to physical area where values above one + indicate multicropping, below one fallow land (default) +MC: multiple cropping factor indicating areas that are + harvested more than once in one year calculated taking + fallow land into account explicitly: + harvestedArea / (physicalArea - fallowLand)} +} +\value{ +List of magpie objects with results on country level, weight on country level, unit and description. +} +\description{ +calculates a multiple cropping factor based on area harvested, + physical cropland area (and optionally fallow land). This function + is an outdated version of calcMulticropping that is kept for + Legacy reasons only for calcCropareaFAOLUH +} +\examples{ +\dontrun{ +calcOutput("calcMulticroppingOld") +} + +} +\seealso{ +[calcFAOLand()], +[calcCroparea()] +} +\author{ +Benjamin Leon Bodirsky, David Chen, Felicitas Beier +} From aecb6997193c4ba8e775120dffd31aa343fff4ab Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Thu, 6 Aug 2026 16:37:09 +0200 Subject: [PATCH 27/36] remove physical argument from calcFallow, remove selectyears argument from calcForestArea to get consistent forest area (including no primary forest growth) for all function calls, --- .buildlibrary | 2 +- .github/workflows/pkgdown.yaml | 1 - CITATION.cff | 2 +- DESCRIPTION | 2 +- R/calcFallow.R | 20 ++++++++------------ R/calcForestArea.R | 26 +++++++++++++++----------- R/calcLanduseInitialisation.R | 2 +- R/calcLanduseInitialisationBase.R | 2 +- README.md | 2 +- man/calcFallow.Rd | 4 ++-- man/calcForestArea.Rd | 10 ++++------ man/calcLanduseInitialisation.Rd | 2 +- 12 files changed, 36 insertions(+), 39 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index 7cad537..7e1ff8c 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3714840' +ValidationKey: '3720780' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 5fb9ac4..59b443b 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -28,7 +28,6 @@ jobs: run: | options(repos = c(pikpiam = 'https://pik-piam.r-universe.dev', CRAN = Sys.getenv('RSPM'))) - pak::pak(".", dependencies = 'all') pak::pkg_install("tidyverse/tidytemplate") diff --git a/CITATION.cff b/CITATION.cff index ecbf454..caf1159 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -5,7 +5,7 @@ title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation version: 1.8.0 -date-released: '2026-07-04' +date-released: '2026-08-06' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index aa181d3..d0a3264 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -3,7 +3,7 @@ Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation Version: 1.8.0 -Date: 2026-07-04 +Date: 2026-08-06 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/R/calcFallow.R b/R/calcFallow.R index 90397a3..57011e0 100644 --- a/R/calcFallow.R +++ b/R/calcFallow.R @@ -1,15 +1,11 @@ #' @title calcFallow -#' @description -#' Returns fallow land of our default datasource. -#' The estimates are scaled to match our Land classes in the Croparea function. -#' Due to multiple cropping, harvested cropland area can be greater than non-fallow land area -#' and even greater than physical cropland area. -#' @param cellular TRUE for cellular outputs. -#' @param physical if TRUE it returns the physical area, with fallow of multicropping systems being -#' scaled to match physical areas of the rotation, if FALSE the area harvested -#' being fallow is returned +#' @description Returns fallow land calculated based on physical cropland extent +#' and harvested area output of default croparea datasource. +#' The resultant areas are scaled to match the land croparea land +#' class of calcLanduseInitialisation. +#' @param cellular TRUE for cellular outputs #' @return MAgPIE object containing fallow land in Mha -#' @author David Hoetten, Felicitas Beier +#' @author David Hoetten, Felicitas Beier, Benjamin Bodirsky #' @seealso #' \code{\link{readLandInG}} #' @examples @@ -19,10 +15,10 @@ #' @importFrom magclass dimSums mbind #' @importFrom madrat toolConditionalReplace #' -calcFallow <- function(cellular = FALSE, physical = TRUE) { +calcFallow <- function(cellular = FALSE) { fallow <- calcOutput("Croparea", cellular = cellular, - physical = physical, fallow = TRUE, + physical = TRUE, fallow = TRUE, irrigation = FALSE, aggregate = FALSE) fallow <- fallow[, , "fallow"] diff --git a/R/calcForestArea.R b/R/calcForestArea.R index 73ef467..4026e10 100644 --- a/R/calcForestArea.R +++ b/R/calcForestArea.R @@ -1,9 +1,9 @@ -#' calcForestArea +#' @title calcForestArea #' -#' Calculates consistent forest area and its subcategories based on FAO_FRA2015 -#' (and FAO_FRA2020 only for forest plantations) and LanduseInitialisation data. +#' @description Calculates consistent forest area and its subcategories based on +#' FAO_FRA2015 (and FAO_FRA2020 only for forest plantations) +#' and LanduseInitialisation data. #' -#' @param selectyears passed to magpiesets::findset #' @return List of magpie object with results on country level, weight, unit and description. #' #' @author Kristine Karstens, Jan Philipp Dietrich @@ -12,11 +12,14 @@ #' calcOutput("ForestArea") #' } #' @export -calcForestArea <- function(selectyears = "past_til2020") { +calcForestArea <- function() { - years <- sort(magpiesets::findset(selectyears, noset = "original")) + # set years for forest area calculation + years <- sort(magpiesets::findset("past_til2020")) - forest <- readSource("FAO_FRA2015", "fac")[, , c("Forest", "NatFor", "PrimFor", "NatRegFor", "PlantFor")] + forest <- readSource("FAO_FRA2015", "fac")[, , c("Forest", "NatFor", + "PrimFor", "NatRegFor", + "PlantFor")] # Plantation data is bit strange in FRA2015, we update this with FRA2020 data (but only till 2015) # We do this because FRA2020 has stopped reporting separately on primf and secdf @@ -25,7 +28,7 @@ calcForestArea <- function(selectyears = "past_til2020") { ## Overall FRA 2020 data fra2020 <- readSource("FRA2020", "forest_area") - ## Find which year is missing in FRA2020 data (which exisits in FRA2015) + ## Find which year is missing in FRA2020 data (which exists in FRA2015) missingYears <- setdiff(getYears(forest), getYears(fra2020)) ## Linear interpolation to missing year @@ -41,12 +44,12 @@ calcForestArea <- function(selectyears = "past_til2020") { # (sum of nat.reg.forest and planted forest) forest[, , "Forest"] <- forest[, , "NatFor"] + forest[, , "PlantFor"] - forest <- time_interpolate(forest, interpolated_year = years, integrate_interpolated_years = TRUE, + forest <- time_interpolate(forest, interpolated_year = years, + integrate_interpolated_years = TRUE, extrapolation_type = "constant")[, years, ] vcat(verbosity = 3, "Forest is interpolated for missing years and held constant for the period before FAO starts") ### fix know issues - forest["HND", , "PlantFor"] <- forest["HND", , "Forest"] - forest["HND", , "NatFor"] forest["IDN", , "Forest"] <- forest["IDN", , "NatFor"] + forest["IDN", , "PlantFor"] forest["FIN", , "NatRegFor"] <- forest["FIN", , "NatFor"] - forest["FIN", , "PrimFor"] @@ -120,7 +123,8 @@ calcForestArea <- function(selectyears = "past_til2020") { warning("There are inconsistencies within the forest area data set.") } - out <- mrdownscale::toolReplaceExpansion(out, "primforest", "secdforest", warnThreshold = 35) + out <- mrdownscale::toolReplaceExpansion(out, "primforest", "secdforest", + warnThreshold = 35) return(list(x = out, weight = NULL, diff --git a/R/calcLanduseInitialisation.R b/R/calcLanduseInitialisation.R index 3856eff..19dce86 100644 --- a/R/calcLanduseInitialisation.R +++ b/R/calcLanduseInitialisation.R @@ -20,7 +20,7 @@ #' @param selectyears default on "past" #' @return List of magpie object with results on country or cellular level, weight on cellular level, #' unit and description. -#' @author Jan Philipp Dietrich, Benjamin Leon Bodirsky, Kristine Karstens, Felcitas Beier, Patrick v. Jeetze +#' @author Jan Philipp Dietrich, Benjamin Leon Bodirsky, Kristine Karstens, Felicitas Beier, Patrick v. Jeetze calcLanduseInitialisation <- function(cellular = FALSE, nclasses = "seven", cells = "lpjcell", selectyears = "past_til2020", input_magpie = FALSE) { # nolint diff --git a/R/calcLanduseInitialisationBase.R b/R/calcLanduseInitialisationBase.R index 99e1465..8314d24 100644 --- a/R/calcLanduseInitialisationBase.R +++ b/R/calcLanduseInitialisationBase.R @@ -75,7 +75,7 @@ calcLanduseInitialisationBase <- function(cells = "lpjcell", selectyears = "past luh <- calcOutput("LUH3", landuseTypes = "LUH3", irrigation = FALSE, cellular = TRUE, yrs = selectyears, aggregate = FALSE) # country-level forest area - forestArea <- calcOutput("ForestArea", selectyears = selectyears, aggregate = FALSE) + forestArea <- calcOutput("ForestArea", years = selectyears, aggregate = FALSE) # rename categories and split secondary forest into secondary forest and forestry # based on forestArea information (area sizes kept as reported by luh) lu <- .luIni(luh, forestArea) diff --git a/README.md b/README.md index e2b7e7b..1b846da 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-07-04}, + date = {2026-08-06}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, note = {Version: 1.8.0}, diff --git a/man/calcFallow.Rd b/man/calcFallow.Rd index 39ed012..d0944e9 100644 --- a/man/calcFallow.Rd +++ b/man/calcFallow.Rd @@ -18,7 +18,7 @@ MAgPIE object containing fallow land in Mha } \description{ Returns fallow land of our default datasource. -The estimates are scaled to match our Land classes in the Croparea function. +The estimates are scaled to match the land classes in the calcCroparea function. Due to multiple cropping, harvested cropland area can be greater than non-fallow land area and even greater than physical cropland area. } @@ -31,5 +31,5 @@ calcOutput("Fallow") \code{\link{readLandInG}} } \author{ -David Hoetten, Felicitas Beier +David Hoetten, Felicitas Beier, Benjamin Bodirsky } diff --git a/man/calcForestArea.Rd b/man/calcForestArea.Rd index e78730c..fa01542 100644 --- a/man/calcForestArea.Rd +++ b/man/calcForestArea.Rd @@ -4,17 +4,15 @@ \alias{calcForestArea} \title{calcForestArea} \usage{ -calcForestArea(selectyears = "past_til2020") -} -\arguments{ -\item{selectyears}{passed to magpiesets::findset} +calcForestArea() } \value{ List of magpie object with results on country level, weight, unit and description. } \description{ -Calculates consistent forest area and its subcategories based on FAO_FRA2015 -(and FAO_FRA2020 only for forest plantations) and LanduseInitialisation data. +Calculates consistent forest area and its subcategories based on + FAO_FRA2015 (and FAO_FRA2020 only for forest plantations) + and LanduseInitialisation data. } \examples{ \dontrun{ diff --git a/man/calcLanduseInitialisation.Rd b/man/calcLanduseInitialisation.Rd index fbc65a5..2ae2a8e 100644 --- a/man/calcLanduseInitialisation.Rd +++ b/man/calcLanduseInitialisation.Rd @@ -43,5 +43,5 @@ Calculates the cellular MAgPIE landuse initialisation area. Most of the calculation happens in calcLanduseInitialisationBase. } \author{ -Jan Philipp Dietrich, Benjamin Leon Bodirsky, Kristine Karstens, Felcitas Beier, Patrick v. Jeetze +Jan Philipp Dietrich, Benjamin Leon Bodirsky, Kristine Karstens, Felicitas Beier, Patrick v. Jeetze } From 35478fb6f7413a26855230bd71bf790601254898 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Fri, 7 Aug 2026 11:10:35 +0200 Subject: [PATCH 28/36] calcFallow: declare isocountries conditionally on cellular (M10) calcFallow() defaults to cellular = FALSE and then returns country-level data, but hardcoded isocountries = FALSE. madrat therefore skipped its ISO-country validation and would have mis-handled country-to-region aggregation for any aggregate = TRUE caller. Set isocountries = !cellular, matching the sibling calcCroparea() and the other country-capable calc functions in this package. Behaviour change: the one live country-level caller, mrvalidation calcValidCroparea() (cellular = FALSE, aggregate = FALSE), now passes through madrat's ISO-set check. No caller uses aggregate = TRUE. The cellular = TRUE caller (calcValidCropareaDiversity) is unaffected. Addresses review finding M10. --- R/calcFallow.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calcFallow.R b/R/calcFallow.R index 57011e0..61406ee 100644 --- a/R/calcFallow.R +++ b/R/calcFallow.R @@ -26,6 +26,6 @@ calcFallow <- function(cellular = FALSE) { weight = NULL, description = "Fallow land", unit = "Mha", - isocountries = FALSE)) + isocountries = !cellular)) } From 0cb0c034da2097037696c2ec1165424ad5983744 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Fri, 7 Aug 2026 11:20:25 +0200 Subject: [PATCH 29/36] calcCropareaLandInG, calcFallowLandInG: declare isocountries conditionally Same defect class as M10: both functions aggregate to country level in their !cellular branch (dimSums over x/y followed by toolCountryFill), but hardcoded isocountries = FALSE. madrat therefore skipped its ISO-country validation on the country-level path and would have mis-handled country-to-region aggregation for any aggregate = TRUE caller. Set isocountries = !cellular in both, matching calcCroparea and calcFallow. calcCropareaLandInG defaults to cellular = FALSE, so this is a live change: the country-level callers mrvalidation calcValidCroparea() (line 35) and calcValidYield() (line 129) now pass through madrat's ISO-set check. All other callers in mrlandcore, mrland and mrwater pass cellular = TRUE explicitly and are unaffected. calcFallowLandInG defaults to cellular = TRUE, so its default behaviour is unchanged; both known callers (calcCropareaLandInG line 52, calcCroparea line 67) pass cellular = TRUE. This one is a latent fix. Found by grepping for the M10 pattern after fixing M10; not in the original review. --- R/calcCropareaLandInG.R | 2 +- R/calcFallowLandInG.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/calcCropareaLandInG.R b/R/calcCropareaLandInG.R index 02a7231..2386214 100644 --- a/R/calcCropareaLandInG.R +++ b/R/calcCropareaLandInG.R @@ -290,5 +290,5 @@ calcCropareaLandInG <- function(sectoral = "kcr", physical = TRUE, cellular = FA weight = NULL, description = "Croparea for different croptypes", unit = "Mha", - isocountries = FALSE)) + isocountries = !cellular)) } diff --git a/R/calcFallowLandInG.R b/R/calcFallowLandInG.R index 5db3258..cf0762b 100644 --- a/R/calcFallowLandInG.R +++ b/R/calcFallowLandInG.R @@ -47,6 +47,6 @@ calcFallowLandInG <- function(cellular = TRUE) { weight = NULL, description = "Fallow land", unit = "Mha", - isocountries = FALSE)) + isocountries = !cellular)) } From b7a8f7e59cd8b7a942f6c05ee1c0a61dae0ccb72 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Fri, 7 Aug 2026 11:44:56 +0200 Subject: [PATCH 30/36] calcCroparea: drop duplicated "including fallow land." in description The branch at calcCroparea.R:37 (irrigation && cellular && fallow) appended " including fallow land." to the description, and the unconditional block at lines 143-148 appended it again, since that branch requires fallow = TRUE. The description read "... including fallow land. including fallow land." Removed the redundant append; the later block covers every LandInG path. Cosmetic only - no effect on values, units, dimensions or weights. Addresses review finding m5. --- R/calcCroparea.R | 1 - 1 file changed, 1 deletion(-) diff --git a/R/calcCroparea.R b/R/calcCroparea.R index d047f1c..8b44ac9 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -117,7 +117,6 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, } croparea <- cropareaCalibrated - description <- paste0(description, " including fallow land.") } else { # To speed up, aggregation is done with recursively From 44f73cc11a619ace6ea8a78f10d8328600acc82a Mon Sep 17 00:00:00 2001 From: bodirsky Date: Fri, 7 Aug 2026 11:48:54 +0200 Subject: [PATCH 31/36] .. --- R/calcMulticroppingOld.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/calcMulticroppingOld.R b/R/calcMulticroppingOld.R index 6cbc998..0fd69bb 100644 --- a/R/calcMulticroppingOld.R +++ b/R/calcMulticroppingOld.R @@ -19,7 +19,7 @@ #' [calcCroparea()] #' @examples #' \dontrun{ -#' calcOutput("calcMulticroppingOld") +#' calcOutput("MulticroppingOld") #' } #' calcMulticroppingOld <- function(extend_future = FALSE, factortype = "CI") { # nolint From 6996beaebeb2bb95641658d713ee87e01fede0ff Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Fri, 7 Aug 2026 16:26:55 +0200 Subject: [PATCH 32/36] bugfix in calcLUH2MAgPIE due to wrongly resolved merge conflict. calcLUH2MAgPIE should be based on croparea from FAOLUH --- R/calcLUH2MAgPIE.R | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/R/calcLUH2MAgPIE.R b/R/calcLUH2MAgPIE.R index a263d65..a1aa9a5 100644 --- a/R/calcLUH2MAgPIE.R +++ b/R/calcLUH2MAgPIE.R @@ -48,16 +48,8 @@ calcLUH2MAgPIE <- function(share = "total", bioenergy = "ignore", warning("No missing data for total numbers assumed.") } - # BLB: Shouldnt this rather use FAOharmonized? - cropPrim <- readSource("FAO_online", "CropLive2010")[, , "area_harvested"] - # use linear_interpolate - fodder <- readSource("FAO", "Fodder")[, , "area_harvested"] - fodder <- toolExtrapolateFodder(fodder, endyear = max(getYears(cropPrim, as.integer = TRUE))) - data <- toolFAOcombine(cropPrim, fodder) / 10^6 # convert to Mha - data[is.na(data)] <- 0 - data <- collapseNames(data) - # not more precision than 1 ha needed. very small areas can make problems in some weighting scripts - FAOdata <- round(data, 6) + FAOdata <- calcOutput("CropareaFAOLUH", sectoral = "ProductionItem", # nolint : object_name_linter + physical = FALSE, aggregate = FALSE) if (rice == "non_flooded") { # Rice areas are pre-determined by areas reported as flooded in LUH. From 50161f15439f4747cadb9e162ce0c3fa0caa0c46 Mon Sep 17 00:00:00 2001 From: bodirsky Date: Mon, 10 Aug 2026 15:05:04 +0200 Subject: [PATCH 33/36] calcCroparea: document the multicropping cap and report when it bites The cap at 3 harvests per year is unchanged - the returned data is identical. What is new is a comment giving the rationale and a conditional message. Rationale, per the author: inconsistencies between the physical and the harvested area data set can produce unrealistically high multicropping factors in edge cases, and three harvests per year is the defensible ceiling. 10 Mha is where an edge case stops being an edge case. The physical area affected is measured from the uncapped factor, weighted by physical croparea, summed globally per year; if the maximum across years exceeds 10 Mha a vcat(1, ...) names that area and that year. Only reachable under physical = FALSE, where the factor is computed at all. na.rm = TRUE in the dimSums is load-bearing, not noise: dimSums defaults to na.rm = FALSE, so a single NA would make the total NA and the following if (max(...) > 10) would error inside calcCroparea. Addresses review finding m16. --- R/calcCroparea.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 8b44ac9..2796a5c 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -58,6 +58,20 @@ calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, multiFactor <- cropareaMulti / croparea multiFactor <- ifelse(test = is.nan(multiFactor), 1, multiFactor) + + # Cap the multicropping factor at 3 harvests per year. Inconsistencies + # between the physical and the harvested area data set can produce + # unrealistically high factors in edge cases, and three harvests per year + # is the defensible ceiling. Report when this stops being an edge case, + # i.e. when the physical area affected exceeds 10 Mha globally in a year. + cappedArea <- dimSums(croparea * (multiFactor > 3), dim = c(1, 3), na.rm = TRUE) + if (max(cappedArea) > 10) { + vcat(1, "Multicropping factor exceeded 3 on", + round(max(cappedArea), digits = 1), "Mha of physical cropland in", + getItems(cappedArea, dim = 2)[which.max(as.vector(cappedArea))], + "and was truncated to 3 \n") + } + multiFactor <- ifelse(test = multiFactor > 3, 3, multiFactor) multiFactor <- add_columns(x = multiFactor, addnm = "fallow", dim = "crop", fill = 1) From 4a9932c1a3c9ab3ced839ee277692a7b093a31ab Mon Sep 17 00:00:00 2001 From: bodirsky Date: Mon, 10 Aug 2026 15:39:38 +0200 Subject: [PATCH 34/36] .. --- .buildlibrary | 2 +- CITATION.cff | 4 ++-- DESCRIPTION | 4 ++-- README.md | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.buildlibrary b/.buildlibrary index eae929b..43b3d0b 100644 --- a/.buildlibrary +++ b/.buildlibrary @@ -1,4 +1,4 @@ -ValidationKey: '3741632' +ValidationKey: '3928250' AutocreateReadme: yes AcceptedWarnings: - 'Warning: package ''.*'' was built under R version' diff --git a/CITATION.cff b/CITATION.cff index 350436e..13d6bd9 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -4,8 +4,8 @@ type: software title: |- mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -version: 1.8.1 -date-released: '2026-08-07' +version: 1.9.0 +date-released: '2026-08-10' abstract: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation. authors: - family-names: Beier diff --git a/DESCRIPTION b/DESCRIPTION index 12d5473..3dbea57 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,8 +2,8 @@ Type: Package Package: mrlandcore Title: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -Version: 1.8.1 -Date: 2026-08-07 +Version: 1.9.0 +Date: 2026-08-10 Authors@R: c( person("Felicitas", "Beier", , "beier@pik-potsdam.de", role = c("aut", "cre")), person("Kristine", "Karstens", role = "aut"), diff --git a/README.md b/README.md index e179805..3ded40f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation -R package **mrlandcore**, version **1.8.1** +R package **mrlandcore**, version **1.9.0** [![R build status](https://github.com/pik-piam/mrlandcore/workflows/check/badge.svg)](https://github.com/pik-piam/mrlandcore/actions) [![codecov](https://codecov.io/gh/pik-piam/mrlandcore/branch/master/graph/badge.svg)](https://app.codecov.io/gh/pik-piam/mrlandcore) [![r-universe](https://pik-piam.r-universe.dev/badges/mrlandcore)](https://pik-piam.r-universe.dev/builds) @@ -39,7 +39,7 @@ In case of questions / problems please contact Felicitas Beier . +Beier F, Karstens K, Alves M, Philipp Dietrich J, Leon Bodirsky B, Hoetten D, Humpenoeder F, Heinke J, v. Jeetze P, Mishra A, Beier F, Wirth S, Chen D, Kreidenweis U, Sauer P (2026). "mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation." Version: 1.9.0, . A BibTeX entry for LaTeX users is @@ -48,9 +48,9 @@ A BibTeX entry for LaTeX users is title = {mrlandcore: Fundamental MAgPIE preprocessing functions including calcLanduseInitialisation}, author = {Felicitas Beier and Kristine Karstens and Marcos Alves and Jan {Philipp Dietrich} and Benjamin {Leon Bodirsky} and David Hoetten and Florian Humpenoeder and Jens Heinke and Patrick {v. Jeetze} and Abhijeet Mishra and Felcitas Beier and Stephen Wirth and David Chen and Ulrich Kreidenweis and Pascal Sauer}, - date = {2026-08-07}, + date = {2026-08-10}, year = {2026}, url = {https://github.com/pik-piam/mrlandcore}, - note = {Version: 1.8.1}, + note = {Version: 1.9.0}, } ``` From e9a8e58ffefa0c89661376018102fdb61597ee7d Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Mon, 10 Aug 2026 18:06:06 +0200 Subject: [PATCH 35/36] bugfix in calcForest area: needs yearly time steps to be able to be used in calcCroparea and to ensure that primary forest doesn't grow --- R/calcForestArea.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/calcForestArea.R b/R/calcForestArea.R index 4026e10..3c134dc 100644 --- a/R/calcForestArea.R +++ b/R/calcForestArea.R @@ -14,8 +14,11 @@ #' @export calcForestArea <- function() { - # set years for forest area calculation - years <- sort(magpiesets::findset("past_til2020")) + # set yearly time steps for historical trime frame for forest area calculation + histyears <- sort(magpiesets::findset("past_til2020")) + startyear <- as.integer(gsub("y", "", histyears[[1]])) + endyear <- as.integer(gsub("y", "", tail(histyears, 1))) + years <- paste0("y", seq(from = startyear, to = endyear, by = 1)) forest <- readSource("FAO_FRA2015", "fac")[, , c("Forest", "NatFor", "PrimFor", "NatRegFor", From 5e0ccbd5e427ad50e4c6bbf62bd9e644d9abbd15 Mon Sep 17 00:00:00 2001 From: FelicitasBeier Date: Mon, 10 Aug 2026 18:53:03 +0200 Subject: [PATCH 36/36] start years in 1960 because calcCroparea calls calcLanduseInitialisation from 1960 onwards --- R/calcForestArea.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/calcForestArea.R b/R/calcForestArea.R index 3c134dc..5b65300 100644 --- a/R/calcForestArea.R +++ b/R/calcForestArea.R @@ -14,9 +14,9 @@ #' @export calcForestArea <- function() { - # set yearly time steps for historical trime frame for forest area calculation + # set yearly time steps for historical time frame for forest area calculation histyears <- sort(magpiesets::findset("past_til2020")) - startyear <- as.integer(gsub("y", "", histyears[[1]])) + startyear <- 1960 endyear <- as.integer(gsub("y", "", tail(histyears, 1))) years <- paste0("y", seq(from = startyear, to = endyear, by = 1))