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 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/.gitignore b/.gitignore index 3dc02f4..2224bc8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .RData .Ruserdata .Rprofile +.positai 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 5e87ec5..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"), @@ -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), diff --git a/R/calcCroparea.R b/R/calcCroparea.R index 4f3fe37..2796a5c 100644 --- a/R/calcCroparea.R +++ b/R/calcCroparea.R @@ -1,223 +1,203 @@ #' @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 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) +#' 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 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, Benjamin Leon Bodirsky #' -#' @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 - -calcCroparea <- 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") +#' @importFrom magpiesets findset + +calcCroparea <- function(sectoral = "kcr", physical = TRUE, fallow = FALSE, + cellular = FALSE, + irrigation = FALSE, datasource = "LandInG") { + + if (datasource == "LandInG") { + # 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, + 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) } - } 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") - } + if (!physical) { + # calculate crop and irrigation-specific multicropping factor + cropareaMulti <- calcOutput("CropareaLandInG", sectoral = sectoral, physical = FALSE, + cellular = TRUE, irrigation = TRUE, + aggregate = FALSE) - # 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 (!any(grepl("y2020", getItems(cropareaMulti, dim = 2)))) { + cropareaMulti <- toolHoldConstant(cropareaMulti, years = selectyears) + } - if (sectoral == "kcr") { - # LUH related data input on cell level - luhWeights <- calcOutput("LUH2MAgPIE", share = "MAGofLUH", - missing = "fill", rice = "non_flooded", aggregate = 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") + } - luhCroptypes <- c("c3ann", "c4ann", "c3per", "c4per", "c3nfx") + multiFactor <- ifelse(test = multiFactor > 3, 3, multiFactor) + multiFactor <- add_columns(x = multiFactor, addnm = "fallow", + dim = "crop", fill = 1) - luhCroparea <- calcOutput("LUH3", landuseTypes = "LUH3", aggregate = FALSE, - irrigation = irrigation, cellular = TRUE)[, , luhCroptypes] + } - commonYears <- intersect(getYears(luhWeights), getYears(luhCroparea)) - luhWeights <- luhWeights[, commonYears, ] - luhCroparea <- luhCroparea[, commonYears, ] + fallowLand <- calcOutput("FallowLandInG", cellular = TRUE, + aggregate = FALSE) - commonCountries <- intersect(getItems(luhWeights, dim = "ISO"), getItems(luhCroparea, dim = "iso")) + if (!any(grepl("y2020", getItems(fallowLand, dim = 2)))) { + fallowLand <- toolHoldConstant(fallowLand, years = selectyears) + } - # corrected rice area (in Mha) - ricearea <- calcOutput("Ricearea", cellular = TRUE, - share = FALSE, aggregate = FALSE) + ### Integrate fallow into physical land + # implicit assumption: fallow is the same in physical and harvested area, + # as multicropping with fallow is not multicropping - commonYears <- intersect(getYears(ricearea), getYears(luhCroparea)) - luhWeights <- luhWeights[, commonYears, ] - luhCroparea <- luhCroparea[, commonYears, ] - ricearea <- ricearea[, commonYears, ] + fallowLand <- setNames(fallowLand[, selectyears, ], "fallow") - # irrigation - if (irrigation == TRUE) { - # for check - luhCropareaTotal <- dimSums(luhCroparea, dim = 3) + totalCroparea <- dimSums(croparea, dim = "crop") + irrigationShare <- (totalCroparea[, , c("rainfed", "irrigated")] / + dimSums(totalCroparea, dim = "irrigation")) + irrigationShare <- ifelse(is.nan(irrigationShare), 0, irrigationShare) + fallowLandIrrigated <- irrigationShare * fallowLand - # 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) + cropareaWithFallow <- mbind(croparea, fallowLandIrrigated) - irrigShr[, , "irrigated"] <- collapseNames(ifelse(luhCroparea[, , "total"] > 0, - luhCroparea[, , "irrigated"] / luhCroparea[, , "total"], 0)) - irrigShr[, , "rainfed"] <- 1 - collapseNames(irrigShr[, , "irrigated"]) + ### Correction to match LanduseInitialisation cropland area - # flooded rice areas - floodedRice <- collapseNames(ricearea[, , "flooded"] * irrigShr[, , "c3ann"]) + # 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) - luhCroparea <- luhCroparea[, , "total", invert = TRUE] + # for correction: read LanduseInitialisation cropland area + landuseIni <- calcOutput(type = "LanduseInitialisation", aggregate = FALSE, + cellular = TRUE, selectyears = selectyears) - } else { - # for check - luhCropareaTotal <- dimSums(luhCroparea, dim = 3) + landuseIniCrop <- collapseNames(landuseIni[, , "crop"]) - # flooded rice areas - floodedRice <- collapseNames(ricearea[, , "flooded"]) + # rescale + cropareaCalibrated <- cropareaShareGrid * landuseIniCrop + ### Check physical cropland matching ### + # 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.") } - # 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!") + if (!physical) { + cropareaCalibrated <- cropareaCalibrated * multiFactor } - data <- collapseNames(magCroparea) + croparea <- cropareaCalibrated - } 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 { + # To speed up, aggregation is done with recursively + 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 irrigation dimension + croparea <- dimSums(croparea, dim = "irrigation") + } + } + # return croparea (and optionally fallow land) + if (!fallow) { + croparea <- croparea[, , "fallow", invert = TRUE] + description <- paste0(description, " excluding fallow land.") } else { - stop("Not possible (for now) for the given item set (sectoral)!") + description <- paste0(description, " including fallow land.") } - 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), ] + if (fallow) { + stop("fallow not implemented for FAOLUH") + } + ### For backwards compatibility only ### + # This chunk can be deleted when croparea update is completed. + + # read in croparea + croparea <- calcOutput("CropareaFAOLUH", sectoral = sectoral, physical = physical, + cellular = cellular, irrigation = irrigation, + aggregate = 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.") } - } - - data <- collapseNames(data) - # not more precision than 1 ha needed. very small areas can make problems in some weighting scripts - data <- round(data, 6) + } 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..01487f7 --- /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("CropareaFAOLUH", 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("CropareaFAOLUH", 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("MulticroppingOld", 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/R/calcCropareaLandInG.R b/R/calcCropareaLandInG.R index a6fa3ec..2386214 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") { @@ -50,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, cellular = TRUE) # year selection if (any(selectyears == "all")) { @@ -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 } @@ -300,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/calcFallow.R b/R/calcFallow.R new file mode 100644 index 0000000..61406ee --- /dev/null +++ b/R/calcFallow.R @@ -0,0 +1,31 @@ +#' @title calcFallow +#' @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, Benjamin Bodirsky +#' @seealso +#' \code{\link{readLandInG}} +#' @examples +#' \dontrun{ +#' calcOutput("Fallow") +#' } +#' @importFrom magclass dimSums mbind +#' @importFrom madrat toolConditionalReplace +#' +calcFallow <- function(cellular = FALSE) { + + fallow <- calcOutput("Croparea", cellular = cellular, + physical = TRUE, fallow = TRUE, + irrigation = FALSE, aggregate = FALSE) + fallow <- fallow[, , "fallow"] + + return(list(x = fallow, + weight = NULL, + description = "Fallow land", + unit = "Mha", + isocountries = !cellular)) + +} diff --git a/R/calcFallowLand.R b/R/calcFallowLandInG.R similarity index 91% rename from R/calcFallowLand.R rename to R/calcFallowLandInG.R index 168460e..cf0762b 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") @@ -47,6 +47,6 @@ calcFallowLand <- function(cellular = TRUE) { weight = NULL, description = "Fallow land", unit = "Mha", - isocountries = FALSE)) + isocountries = !cellular)) } diff --git a/R/calcForestArea.R b/R/calcForestArea.R index 73ef467..5b65300 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,17 @@ #' calcOutput("ForestArea") #' } #' @export -calcForestArea <- function(selectyears = "past_til2020") { +calcForestArea <- function() { - years <- sort(magpiesets::findset(selectyears, noset = "original")) + # set yearly time steps for historical time frame for forest area calculation + histyears <- sort(magpiesets::findset("past_til2020")) + startyear <- 1960 + 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", "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 +31,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 +47,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 +126,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/calcLUH2MAgPIE.R b/R/calcLUH2MAgPIE.R index 1e5be25..a1aa9a5 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), @@ -20,7 +20,6 @@ calcLUH2MAgPIE <- function(share = "total", bioenergy = "ignore", rice = "non_flooded", missing = "ignore") { - # proxy filling map in the case of `missing = "fill"` proxyMapping <- c( # Polar or Sub-polar with Iceland/Norway @@ -49,8 +48,8 @@ calcLUH2MAgPIE <- function(share = "total", bioenergy = "ignore", warning("No missing data for total numbers assumed.") } - FAOdata <- calcOutput("Croparea", sectoral = "ProductionItem", # nolint : object_name_linter. - physical = FALSE, aggregate = FALSE) + 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. 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/R/calcMulticropping.R b/R/calcMulticropping.R index a2576ad..1f973d0 100644 --- a/R/calcMulticropping.R +++ b/R/calcMulticropping.R @@ -1,68 +1,35 @@ #' @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 physical 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) +#' @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, David Chen, Felicitas Beier +#' @author Benjamin Leon Bodirsky, Felicitas Beier #' @seealso -#' [calcFAOLand()], #' [calcCroparea()] #' @examples #' \dontrun{ #' calcOutput("Multicropping") #' } #' -calcMulticropping <- function(extend_future = FALSE, factortype = "CI") { # nolint +calcMulticropping <- function(irrigation = TRUE) { - # physical cropland area ("6620|Cropland") - phys <- collapseNames(calcOutput("FAOLand", aggregate = FALSE)[, , "6620", pmatch = TRUE]) - # harvested area - harv <- collapseNames(dimSums(calcOutput("Croparea", physical = FALSE, - aggregate = FALSE, sectoral = "kcr"), - 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) - # match year dimension - phys <- phys[, intersect(getYears(phys), getYears(harv)), ] - harv <- harv[, intersect(getYears(phys), getYears(harv)), ] + out[is.na(out)] <- 1 - 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) - } + out <- toolHoldConstantBeyondEnd(out) + phys <- toolHoldConstantBeyondEnd(phys) return(list(x = out, weight = phys, + aggregationArguments = list(zeroWeight = "fix"), unit = "ratio", - description = description)) + description = "Ratio of area harvested to phyiscal area, excluding fallow land", + isocountries = FALSE)) } diff --git a/R/calcMulticroppingOld.R b/R/calcMulticroppingOld.R new file mode 100644 index 0000000..0fd69bb --- /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("MulticroppingOld") +#' } +#' +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/R/calcRicearea.R b/R/calcRicearea.R index e56aa1f..8f35600 100644 --- a/R/calcRicearea.R +++ b/R/calcRicearea.R @@ -24,8 +24,9 @@ calcRicearea <- function(cellular = FALSE, share = TRUE) { # Country-level LUH flooded areas floodedLUHiso <- collapseNames(calcOutput("LUH3flood", aggregate = FALSE)) - # FAO rice areas (physical to be comparable with LUH) - riceareaFAOiso <- collapseNames(calcOutput("Croparea", sectoral = "kcr", physical = TRUE, + # FAO rice areas according to FAO (physical to be comparable with LUH) + riceareaFAOiso <- collapseNames(calcOutput("CropareaFAOLUH", + sectoral = "kcr", physical = TRUE, cellular = FALSE, irrigation = FALSE, aggregate = FALSE)[, , "rice_pro"]) 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}, } ``` diff --git a/man/calcCroparea.Rd b/man/calcCroparea.Rd index f4ce18b..a5a05bd 100644 --- a/man/calcCroparea.Rd +++ b/man/calcCroparea.Rd @@ -7,17 +7,19 @@ calcCroparea( sectoral = "kcr", physical = TRUE, + fallow = FALSE, cellular = FALSE, - irrigation = FALSE + irrigation = FALSE, + 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} +\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} \item{cellular}{if TRUE: calculates cellular MAgPIE crop area for all magpie croptypes. Crop area from LUH3 crop types (c3ann, c4ann, c3per, c4per, cnfx) @@ -27,17 +29,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{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 } \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, Benjamin Leon Bodirsky } 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/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} diff --git a/man/calcFallow.Rd b/man/calcFallow.Rd new file mode 100644 index 0000000..4d49fd4 --- /dev/null +++ b/man/calcFallow.Rd @@ -0,0 +1,31 @@ +% 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) +} +\arguments{ +\item{cellular}{TRUE for cellular outputs} +} +\value{ +MAgPIE object containing fallow land in Mha +} +\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. +} +\examples{ +\dontrun{ +calcOutput("Fallow") +} +} +\seealso{ +\code{\link{readLandInG}} +} +\author{ +David Hoetten, Felicitas Beier, Benjamin Bodirsky +} diff --git a/man/calcFallowLand.Rd b/man/calcFallowLandInG.Rd similarity index 78% rename from man/calcFallowLand.Rd rename to man/calcFallowLandInG.Rd index 55d73de..fccdeb0 100644 --- a/man/calcFallowLand.Rd +++ b/man/calcFallowLandInG.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/calcFallowLand.R -\name{calcFallowLand} -\alias{calcFallowLand} -\title{calcFallowLand} +% Please edit documentation in R/calcFallowLandInG.R +\name{calcFallowLandInG} +\alias{calcFallowLandInG} +\title{calcFallowLandInG} \usage{ -calcFallowLand(cellular = TRUE) +calcFallowLandInG(cellular = TRUE) } \arguments{ \item{cellular}{TRUE for cellular outputs.} @@ -25,7 +25,7 @@ Thus, the results can only be considered a rough estimate of fallow land area. } \examples{ \dontrun{ -calcOutput("FallowLand") +calcOutput("FallowLandInG") } } \seealso{ 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/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/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 } diff --git a/man/calcMulticropping.Rd b/man/calcMulticropping.Rd index 6d33ccb..6afe625 100644 --- a/man/calcMulticropping.Rd +++ b/man/calcMulticropping.Rd @@ -4,25 +4,18 @@ \alias{calcMulticropping} \title{calcMulticropping} \usage{ -calcMulticropping(extend_future = FALSE, factortype = "CI") +calcMulticropping(irrigation = TRUE) } \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)} +\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{ -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 physical area. Fallow lands are not counted in, so the factor +is always larger than 1. } \examples{ \dontrun{ @@ -31,9 +24,8 @@ calcOutput("Multicropping") } \seealso{ -[calcFAOLand()], [calcCroparea()] } \author{ -Benjamin Leon Bodirsky, David Chen, Felicitas Beier +Benjamin Leon Bodirsky, Felicitas Beier } diff --git a/man/calcMulticroppingOld.Rd b/man/calcMulticroppingOld.Rd new file mode 100644 index 0000000..b951d51 --- /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("MulticroppingOld") +} + +} +\seealso{ +[calcFAOLand()], +[calcCroparea()] +} +\author{ +Benjamin Leon Bodirsky, David Chen, Felicitas Beier +} 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