R/exploitablefuelwoodvolume.R

Defines functions exploitablefuelwoodvolume

Documented in exploitablefuelwoodvolume

#' Compute the exploitable fuel wood volume
#'
#' @param inventory Input inventory (see the inputs formats and metadata in the
#'   \code{\link{vignette}}) (data.frame)
#'
#' @param scenario Logging scenario: "RIL1", "RIL2broken", "RIL2", "RIL3",
#'   "RIL3fuel", "RIL3fuelhollow" or "manual"(character) (see the
#'   \code{\link{vignette}})
#'
#'@param fuel Fuel wood exploitation: no exploitation = "0", exploitation of
#'   damage and unused part of logged trees for fuel = "1", exploitation of
#'   hollow trees, damage and and unused part of the log for fuel = "2"
#'
#' @param TimberLoggedVolume All the logged volume (in m^3) (numeric)
#'
#' @param NoHollowTimberLoggedVolume The healthy logged volume (in m^3)
#'   (numeric)
#'
#' @param advancedloggingparameters Other parameters of the logging simulator
#'   \code{\link{loggingparameters}} (list)
#'
#' @return A list with the damage volume, and the fuel wood volume when fuel
#'   wood exploitation is chosen.
#' @export
#'
#' @details By default, within a hollow tree, 2/3 of the log will be usable as
#'   timber, 1/3 as fuel wood ('TreeHollowPartForFuel'). For dead trees from the
#'   operation (trails, secondary windfall), the whole trunk will be exploitable
#'   as fuel wood.
#'
#' @importFrom dplyr filter
#'
#' @examples
#' data(Paracou6_2016)
#' data(DTMParacou)
#' data(PlotSlope)
#' data(MainTrails)
#' data(HarvestablePolygons)
#'
#' pol1 <- list(matrix(c(286503, 583134,
#'                       286503, 583240,
#'                       286507, 583240,
#'                       286507, 583134,
#'                       286503, 583134) # the return
#'                    ,ncol=2, byrow=TRUE))
#' pol2 <- list(matrix(c(286650, 583134,
#'                       286650, 583240,
#'                       286654, 583240,
#'                       286654, 583134,
#'                       286650, 583134) # the return
#'                    ,ncol=2, byrow=TRUE))
#'
#' PolList = list(pol1,pol2) #list of lists of numeric matrices
#' MainTrail <- ScndTrail <- sf::st_multipolygon(PolList)
#'
#' inventory <- addtreedim(inventorycheckformat(Paracou6_2016),
#' volumeparameters = ForestZoneVolumeParametersTable)
#'
#' inventory <- suppressMessages(treeselection(inventory, objective = 20, scenario ="manual",
#'  fuel = "2", diversification = TRUE, specieslax = FALSE, maintrails = MainTrails,
#'  harvestablepolygons = HarvestablePolygons,
#'  objectivelax = TRUE, topography = DTMParacou, plotslope = PlotSlope,
#'  speciescriteria = SpeciesCriteria,
#'  advancedloggingparameters = loggingparameters())$inventory)
#'
#' if (!("DeathCause" %in% names(inventory))){
#'   inventory <- inventory %>%
#'     add_column(DeathCause = NA) # if "DeathCause" column doesnt exist create it
#' }
#' inventory$DeathCause[1] <- "maintrail"
#' inventory$DeathCause[2] <- "2ndtrail"
#' inventory$DeathCause[3] <- "treefall2nd"
#' inventory$DeathCause[4] <- "landing"
#'
#' TimberV <- timberharvestedvolume(inventory, scenario = "manual", fuel = "2",
#' advancedloggingparameters = loggingparameters())
#'
#' TimberLoggedVolume <- TimberV$TimberLoggedVolume
#' NoHollowTimberLoggedVolume <- TimberV$NoHollowTimberLoggedVolume
#'
#' exploitablefuelwoodvolume(inventory, scenario = "manual", fuel = "2",
#' TimberLoggedVolume = TimberLoggedVolume, NoHollowTimberLoggedVolume = NoHollowTimberLoggedVolume,
#' advancedloggingparameters = loggingparameters())
#'
exploitablefuelwoodvolume <- function(
  inventory,
  scenario,
  fuel = NULL,
  TimberLoggedVolume,
  NoHollowTimberLoggedVolume,
  advancedloggingparameters = loggingparameters()
){
  # Global variables
  Accessible <- Circ <- CircCorr <- CodeAlive <- Commercial <- NULL
  Commercial.genus <- Commercial.species <- Condition <- DBH <- NULL
  DeathCause <- DistCrit <- Family <- NULL
  ForestZoneVolumeParametersTable <- Genus <- Logged <- NULL
  LoggingStatus <- MaxFD <- MaxFD.genus <- NULL
  MaxFD.species <- MinFD <- MinFD.genus <- MinFD.species <- NULL
  ParamCrownDiameterAllometry <- PlotSlope <- NULL
  PlotTopo <- ProbedHollow <- ProbedHollowProba <- ScientificName <- NULL
  Selected <- Slope <- SlopeCrit <- Species <- Species.genus <- NULL
  SpeciesCriteria  <- geometry <- idTree <- NULL
  TreeFellingOrientationSuccess <- TreeHarvestableVolume <- NULL
  TreeHeight <- TrunkHeight <- Up <- UpMinFD  <- NULL
  VernName.species <- VolumeCumSum <- Xutm <- Yutm <- aCoef <- NULL

  # Arguments check
  if(!inherits(inventory, "data.frame"))
    stop("The 'inventory' argument of the 'exploitablefuelwoodvolume' function must be a data.frame")

  if (!any(scenario == "RIL1" || scenario == "RIL2broken"|| scenario == "RIL2"||
           scenario == "RIL3"|| scenario == "RIL3fuel"||
           scenario == "RIL3fuelhollow"|| scenario =="manual"))
    stop("The 'scenario' argument of the 'exploitablefuelwoodvolume' function must be
         'RIL1', 'RIL2broken', 'RIL2', 'RIL3', 'RIL3fuel', 'RIL3fuelhollow' or 'manual'")

  if (!any(fuel == "0" || fuel == "1"|| fuel == "2"|| is.null(fuel)))
    stop("The 'fuel' argument of the 'exploitablefuelwoodvolume' function must be '0', '1', '2' or NULL")

  if(!inherits(advancedloggingparameters, "list"))
    stop("The 'advancedloggingparameters' argument
         of the 'exploitablefuelwoodvolume' function must be a list")

  if(!all(unlist(lapply(list(TimberLoggedVolume, NoHollowTimberLoggedVolume), inherits, "numeric"))))
    stop("The 'TimberLoggedVolume' and 'NoHollowTimberLoggedVolume' arguments
         of the 'exploitablefuelwoodvolume' function must be numeric")

  # Redefinition of the parameters according to the chosen scenario
  scenariosparameters <- scenariosparameters(scenario = scenario, fuel = fuel)

  fuel <- scenariosparameters$fuel


  # Function
  DamageTable <- inventory %>%
    dplyr::filter(DeathCause == "maintrail" |
             DeathCause == "2ndtrail" |
             DeathCause == "treefall2nd" |
             DeathCause == "landing")

  DamageVolume <- sum(DamageTable$TreeHarvestableVolume) # only damage (without purge and hollow trees)

  if(fuel == "0"){

    FuelVolume <- NULL
  }

  if(fuel != "0"){ # fuel wood exploitation

    DamagePurge <- sum(DamageVolume + advancedloggingparameters$Purge * TimberLoggedVolume)

    if(fuel == "1"){
      # Damage and purge (0.14)
      FuelVolume <- DamagePurge
    }

    if(fuel == "2"){

      HollowTable <- inventory %>%
        dplyr::filter(DeathCause == "hollowfuel")

      if(nrow(HollowTable) > 0){
        # damage + 1/3 of hollow trees volume + purge (0.14) of the healthy trees
        FuelVolume <- sum(DamageVolume +
                            advancedloggingparameters$TreeHollowPartForFuel *
                            sum(HollowTable$TreeHarvestableVolume) +
                            advancedloggingparameters$Purge * NoHollowTimberLoggedVolume)

      }else if(nrow(HollowTable) == 0){

        FuelVolume <- DamagePurge # no probed hollow trees
      }
    }

  }

  outputs <- list(DamageVolume = DamageVolume, # only damage (without purge and hollow trees)
                  FuelVolume = FuelVolume)

  return(outputs)

}
thomasgaquiere/Maria documentation built on Dec. 24, 2021, 1:20 a.m.