-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
41 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Type: Package | ||
Package: magpie4 | ||
Title: MAgPIE outputs R package for MAgPIE version 4.x | ||
Version: 1.206.21 | ||
Date: 2024-04-17 | ||
Version: 1.206.22 | ||
Date: 2024-04-19 | ||
Authors@R: c( | ||
person("Benjamin Leon", "Bodirsky", , "[email protected]", role = c("aut", "cre")), | ||
person("Florian", "Humpenoeder", , "[email protected]", role = "aut"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,165 +1,50 @@ | ||
#' @title ForestYield | ||
#' @description reads timber yield out of a MAgPIE gdx file | ||
#' | ||
#' @description calculates timber yield out of a MAgPIE gdx file | ||
#' | ||
#' @export | ||
#' | ||
#' @param gdx GDX file | ||
#' @param file a file name the output should be written to using write.magpie | ||
#' @param level Level of regional aggregation; "cell", "reg" (regional), "glo" (global), "regglo" (regional and global) or any secdforest aggregation level defined in superAggregate | ||
#' @details Forest yield for timber production | ||
#' @return Forest yield for timber production | ||
#' @author Abhijeet Mishra | ||
#' @return Forest yield for timber production in tDM per ha per year | ||
#' @author Abhijeet Mishra, Florian Humpenoeder | ||
#' @importFrom gdx readGDX out | ||
#' @importFrom magclass clean_magpie dimSums collapseNames setYears write.magpie | ||
#' @importFrom luscale superAggregate | ||
#' @examples | ||
#' | ||
#' | ||
#' \dontrun{ | ||
#' x <- ForestYield(gdx) | ||
#' } | ||
|
||
ForestYield <- function(gdx, file=NULL, level="cell"){ | ||
a <- NULL | ||
|
||
if(as.numeric(readGDX(gdx, "s32_hvarea")) > 0 & as.numeric(readGDX(gdx, "s35_hvarea")) > 0) { | ||
if (level == "cell") { | ||
#### Production and harvest area calculations | ||
ov73_prod_forestry <-dimSums(readGDX(gdx,"ov_prod_forestry","ov73_prod_forestry",select = list(type="level")),dim=3) | ||
ov73_hvarea_forestry <- dimSums(readGDX(gdx,"ov32_hvarea_forestry","ov73_hvarea_forestry","ov_hvarea_forestry",react = "silent",select = list(type="level")),dim=3) | ||
|
||
ov73_prod_natveg_secdf <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level"))[,,"secdforest"],dim=3) | ||
ov_hvarea_secdf <- dimSums(readGDX(gdx,"ov35_hvarea_secdforest","ov_hvarea_secdforest",select = list(type="level")),dim=3) | ||
|
||
ov73_prod_natveg_primf <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level"))[,,"primforest"],dim=3) | ||
ov_hvarea_primf <- dimSums(readGDX(gdx,"ov35_hvarea_primforest","ov_hvarea_primforest",select = list(type="level")),dim=3) | ||
|
||
ov73_prod_natveg_other <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level"))[,,"other"],dim=3) | ||
ov_hvarea_other <- dimSums(readGDX(gdx,"ov35_hvarea_other","ov73_hvarea_other","ov_hvarea_other",react = "silent",select = list(type="level")),dim=3) | ||
|
||
#### Yield calculations | ||
|
||
## Plantations | ||
yield_forestry <- ov73_prod_forestry/ov73_hvarea_forestry | ||
if(any(is.na(range(yield_forestry)))){ | ||
yield_forestry[is.na(yield_forestry)] <- 0 | ||
} | ||
if(any(is.infinite(range(yield_forestry)))){ | ||
div0 <- where(ov73_prod_forestry != 0 & ov73_hvarea_forestry == 0)$true$regions | ||
yield_forestry[is.infinite(yield_forestry)] <- 0 | ||
} | ||
|
||
## Secondary forest | ||
yield_secdf <- ov73_prod_natveg_secdf/ov_hvarea_secdf | ||
if(any(is.na(range(yield_secdf)))){ | ||
yield_secdf[is.na(yield_secdf)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_secdf)))){ | ||
div0 <- where(ov73_prod_natveg_secdf != 0 & ov_hvarea_secdf == 0)$true$regions | ||
yield_secdf[is.infinite(yield_secdf)] <- 0 | ||
} | ||
|
||
## Primary forest | ||
yield_primf <- ov73_prod_natveg_primf/ov_hvarea_primf | ||
if(any(is.na(range(yield_primf)))){ | ||
yield_primf[is.na(yield_primf)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_primf)))){ | ||
div0 <- where(ov73_prod_natveg_primf != 0 & ov_hvarea_primf == 0)$true$regions | ||
yield_primf[is.infinite(yield_primf)] <- 0 | ||
} | ||
|
||
## Other land | ||
yield_other <- ov73_prod_natveg_other/ov_hvarea_other | ||
if(any(is.na(range(yield_other)))){ | ||
yield_other[is.na(yield_other)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_other)))){ | ||
div0 <- where(ov73_prod_natveg_other != 0 & ov_hvarea_other == 0)$true$regions | ||
yield_other[is.infinite(yield_other)] <- 0 | ||
} | ||
|
||
a <- mbind(setNames(yield_forestry,"Forestry"), | ||
setNames(yield_secdf,"Secondary forest"), | ||
setNames(yield_primf,"Primary forest"), | ||
setNames(yield_other,"Other land")) | ||
} else if (level == "regglo" | level == "reg"){ | ||
#### Production and harvest area calculations | ||
ov73_prod_forestry <- dimSums(readGDX(gdx,"ov_prod_forestry","ov73_prod_forestry",select = list(type="level")),dim=3) | ||
ov73_prod_forestry <- superAggregate(data = ov73_prod_forestry,aggr_type = "sum",level = level) | ||
ov73_hvarea_forestry <- dimSums(readGDX(gdx,"ov32_hvarea_forestry","ov73_hvarea_forestry","ov_hvarea_forestry",react = "silent",select = list(type="level")),dim=3) | ||
ov73_hvarea_forestry <- superAggregate(data = ov73_hvarea_forestry ,aggr_type = "sum",level = level) | ||
|
||
ov73_prod_natveg_secdf <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level"))[,,"secdforest"],dim=3) | ||
ov73_prod_natveg_secdf <- superAggregate(data = ov73_prod_natveg_secdf,aggr_type = "sum",level = level) | ||
ov_hvarea_secdf <- dimSums(readGDX(gdx,"ov35_hvarea_secdforest","ov_hvarea_secdforest",select = list(type="level")),dim=3) | ||
ov_hvarea_secdf <- superAggregate(data = ov_hvarea_secdf ,aggr_type = "sum",level = level) | ||
|
||
ov73_prod_natveg_primf <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level"))[,,"primforest"],dim=3) | ||
ov73_prod_natveg_primf <- superAggregate(data = ov73_prod_natveg_primf,aggr_type = "sum",level = level) | ||
ov_hvarea_primf <- dimSums(readGDX(gdx,"ov35_hvarea_primforest","ov_hvarea_primforest",select = list(type="level")),dim=3) | ||
ov_hvarea_primf <- superAggregate(data = ov_hvarea_primf ,aggr_type = "sum",level = level) | ||
|
||
ov73_prod_natveg_other <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level"))[,,"other"],dim=3) | ||
ov73_prod_natveg_other <- superAggregate(data = ov73_prod_natveg_other,aggr_type = "sum",level = level) | ||
ov_hvarea_other <- dimSums(readGDX(gdx,"ov35_hvarea_other","ov73_hvarea_other","ov_hvarea_other",react = "silent",select = list(type="level")),dim=3) | ||
ov_hvarea_other <- superAggregate(data = ov_hvarea_other ,aggr_type = "sum",level = level) | ||
|
||
#### Yield calculations | ||
|
||
## Plantations | ||
yield_forestry <- ov73_prod_forestry/ov73_hvarea_forestry | ||
if(any(is.na(range(yield_forestry)))){ | ||
yield_forestry[is.na(yield_forestry)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_forestry)))){ | ||
div0 <- where(ov73_prod_forestry != 0 & ov73_hvarea_forestry == 0)$true$regions | ||
yield_forestry[is.infinite(yield_forestry)] <- 0 | ||
} | ||
|
||
## Secondary forest | ||
yield_secdf <- ov73_prod_natveg_secdf/ov_hvarea_secdf | ||
if(any(is.na(range(yield_secdf)))){ | ||
yield_secdf[is.na(yield_secdf)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_secdf)))){ | ||
div0 <- where(ov73_prod_natveg_secdf != 0 & ov_hvarea_secdf == 0)$true$regions | ||
yield_secdf[is.infinite(yield_secdf)] <- 0 | ||
} | ||
|
||
## Primary forest | ||
yield_primf <- ov73_prod_natveg_primf/ov_hvarea_primf | ||
if(any(is.na(range(yield_primf)))){ | ||
yield_primf[is.na(yield_primf)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_primf)))){ | ||
div0 <- where(ov73_prod_natveg_primf != 0 & ov_hvarea_primf == 0)$true$regions | ||
yield_primf[is.infinite(yield_primf)] <- 0 | ||
} | ||
|
||
## Other land | ||
yield_other <- ov73_prod_natveg_other/ov_hvarea_other | ||
if(any(is.na(range(yield_other)))){ | ||
yield_other[is.na(yield_other)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield_other)))){ | ||
div0 <- where(ov73_prod_natveg_other != 0 & ov_hvarea_other == 0)$true$regions | ||
yield_other[is.infinite(yield_other)] <- 0 | ||
} | ||
|
||
a <- mbind(setNames(yield_forestry,"Forestry"), | ||
setNames(yield_secdf,"Secondary forest"), | ||
setNames(yield_primf,"Primary forest"), | ||
setNames(yield_other,"Other land")) | ||
} else {stop("Resolution not recognized. Select cell or reg or regglo as level. NULL returned.")} | ||
|
||
|
||
#### get annual production and annual harvested area | ||
ov73_prod_forestry <- setNames(dimSums(readGDX(gdx,"ov_prod_forestry","ov73_prod_forestry",select = list(type="level")),dim=3), "Forestry") | ||
ov73_prod_natveg <- dimSums(readGDX(gdx,"ov_prod_natveg","ov73_prod_natveg",select = list(type="level")),dim="kforestry") | ||
ov73_prod_natveg <- setNames(ov73_prod_natveg,c("Primary forest","Secondary forest","Other land")) | ||
ov73_prod <- mbind(ov73_prod_forestry, ov73_prod_natveg) | ||
ov73_prod <- gdxAggregate(gdx, ov73_prod, to = level) | ||
ov73_hvarea <- harvested_area_timber(gdx, level = level) | ||
|
||
#### Yield calculations | ||
yield <- ov73_prod / ov73_hvarea | ||
if(any(is.na(range(yield)))){ | ||
yield[is.na(yield)] <- 0 | ||
} | ||
|
||
if(any(is.infinite(range(yield)))){ | ||
div0 <- where(ov73_prod != 0 & ov73_hvarea == 0)$true$regions | ||
yield[is.infinite(yield)] <- 0 | ||
} | ||
|
||
a <- yield | ||
|
||
} else {message("Disabled (no dynamic forestry) ", appendLF = FALSE)} | ||
|
||
out(a,file) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.