Skip to content

Commit

Permalink
For Review
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Aug 18, 2023
1 parent 683e11d commit 07923e7
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 83 deletions.
42 changes: 21 additions & 21 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ Depends:
R (>= 4.1)
Config/testthat/edition: 3
Imports:
magrittr (>= 2.0.1),
dplyr (>= 1.0.7),
tibble (>= 3.1.0),
tidyr (>= 1.1.2),
BIOMASS (>= 2.1.7),
rlang (>= 0.4.10),
raster (>= 3.5.29),
sp (>= 1.4.6),
sf (>= 1.0.5),
nngeo (>= 0.4.5),
fasterize (>= 1.0.3),
terra (>= 1.4-22),
methods (>= 3.6.1),
utils (>= 3.6.1),
matlib (>= 0.9.4),
gdistance(>= 1.3-6),
smoothr (>= 0.2.2),
lwgeom (>= 0.2-8),
units (>= 0.7.2),
stats (>= 3.6.1),
parallel (>= 3.6),
magrittr (>= 2.0.3),
dplyr (>= 1.1.2),
tibble (>= 3.2.1),
tidyr (>= 1.3.0),
BIOMASS (>= 2.1.8),
rlang (>= 1.1.1),
raster (>= 3.6.20),
sp (>= 1.6.0),
sf (>= 1.0.12),
nngeo (>= 0.4.7),
fasterize (>= 1.0.4),
terra (>= 1.7-29),
methods (>= 4.3.0),
utils (>= 4.3.0),
matlib (>= 0.9.6),
gdistance(>= 1.6.4),
smoothr (>= 1.0.1),
lwgeom (>= 0.2-11),
units (>= 0.8-2),
stats (>= 4.3.0),
parallel (>= 4.3.0),
doSNOW (>= 1.0.20),
foreach (>= 1.5.2),
tryCatchLog (>= 1.3.1)
Expand Down
12 changes: 7 additions & 5 deletions R/SpeciesCriteria.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#' \describe{
#' \item{CommercialName}{Vernacular/economic name (character)}
#' \item{Genus}{Genus associated to the vernacular name (character)}
#' \item{Species}{Species (without genus part) associated to the vernacular name (character)}
#' \item{CommercialLevel}{Economic interest level ("1" principal economic species,
#' "2" species logged in a objective of diversification, "0" species
#' whose genus is covered by a commercial name, but which are not logged) (factor)}
#' \item{Species}{Species (without genus part) associated to the vernacular
#' name (character)}
#' \item{CommercialLevel}{Economic interest level (1: principal economic species,
#' 2 or more: species logged in a objective of diversification, 0: species
#' whose genus is covered by a commercial name, but which are not logged) (numeric)}
#' \item{MinFD}{Minimum Felling Diameter, in centimeter (numeric)}
#' \item{UpMinFD}{Enhanced Minimum Felling Diameter (over-rich stand case), in centimeter (numeric)}
#' \item{UpMinFD}{Enhanced Minimum Felling Diameter (over-rich stand case), in
#' centimeter (numeric)}
#' \item{MaxFD}{Maximum Felling Diameter, in centimeter (numeric)}
#' \item{Aggregative}{Aggregative character of the species (logical)}
#' ...
Expand Down
8 changes: 6 additions & 2 deletions R/addtreedim.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#'Compute tree dimensions (tree, trunk and crown height, crown diameter,
#'harvestable volume, wood density, and AGB)
#'
#'@description Compute tree dimensions (tree, trunk and crown height, crown
#' diameter, harvestable volume, wood density, and AGB) from its Diameter at
#' Breast Height (DBH)
#'
#'@param inventory Input inventory (see the inputs formats and metadata in the
#' vignette) (data.frame)
#'
Expand All @@ -26,10 +30,10 @@
#'@details 'addtreedim' compute some tree dimensions with the
#' 'advancedloggingparameters' argument:
#' - Tree height (in m) ('TreeHeightAllometry')
#' - Tree harvestable volume (m3) ('TreeHarvestableVolumeAllometry')
#' - Tree harvestable volume (m3) ('TreeHarvestableVolumeAllometry')(French Guiana ONF)
#' - Trunk height (in m) ('TrunkHeightAllometry')
#' - Crown height (in m)
#' - Crown diameter (in m) ('CrownDiameterAllometry')
#' - Crown diameter (in m) ('CrownDiameterAllometry') (Aubry-Kientz et al.2019)
#' - Wood density (g/cm3) (by the BIOMASS package)
#' - Tree above-ground biomass (AGB) (in ton) (by the BIOMASS package)
#'
Expand Down
20 changes: 10 additions & 10 deletions R/commercialcriteriajoin.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ commercialcriteriajoin <- function(
mutate(CommercialName = ifelse(is.na(CommercialName.species), CommercialName.genus, CommercialName.species)) %>%
dplyr::select(-CommercialName.species, -CommercialName.genus) %>%

mutate(CommercialLevel.species = as.character(CommercialLevel.species)) %>%
mutate(CommercialLevel.genus = as.character(CommercialLevel.genus)) %>%
# mutate(CommercialLevel.species = as.character(CommercialLevel.species)) %>% # remove for reviewer
# mutate(CommercialLevel.genus = as.character(CommercialLevel.genus)) %>% # remove for reviewer

mutate(CommercialLevel = ifelse(is.na(CommercialLevel.species), CommercialLevel.genus, CommercialLevel.species)) %>%
mutate(MinFD = ifelse(is.na(MinFD.species), MinFD.genus, MinFD.species)) %>%
Expand All @@ -97,19 +97,19 @@ commercialcriteriajoin <- function(
dplyr::select(-CommercialLevel.species, -CommercialLevel.genus, -MinFD.species, -MinFD.genus,
-UpMinFD.species, -UpMinFD.genus, -MaxFD.species, -MaxFD.genus,
-Aggregative.species, -Aggregative.genus) %>%
mutate(CommercialLevel = as.character(CommercialLevel)) %>%
# mutate(CommercialLevel = as.character(CommercialLevel)) %>% # remove for reviewer

# Exceptions management (CommercialLevel == "0" in speciescriteria)
mutate(CommercialName = ifelse(CommercialLevel == "0", NA, CommercialName)) %>%
mutate(MinFD = ifelse(CommercialLevel == "0", NA, MinFD)) %>%
mutate(UpMinFD = ifelse(CommercialLevel == "0", NA, UpMinFD)) %>%
mutate(MaxFD = ifelse(CommercialLevel == "0", NA, MaxFD)) %>%
mutate(Aggregative = ifelse(CommercialLevel == "0", NA, Aggregative))
mutate(CommercialName = ifelse(CommercialLevel == 0, NA, CommercialName)) %>% # in numeric for reviewer
mutate(MinFD = ifelse(CommercialLevel == 0, NA, MinFD)) %>%
mutate(UpMinFD = ifelse(CommercialLevel == 0, NA, UpMinFD)) %>%
mutate(MaxFD = ifelse(CommercialLevel == 0, NA, MaxFD)) %>%
mutate(Aggregative = ifelse(CommercialLevel == 0, NA, Aggregative))


inventory <- inventory %>%
mutate(CommercialLevel = ifelse(is.na(CommercialLevel), "0", CommercialLevel)) %>%
mutate(CommercialLevel = factor(as.character(CommercialLevel)))
mutate(CommercialLevel = ifelse(is.na(CommercialLevel), 0, CommercialLevel)) # %>%
# mutate(CommercialLevel = factor(as.character(CommercialLevel))) # remove for reviewer

if(nrow(inventory) != nrow(inventory0))
stop("The number of rows between the input inventory and the output inventory
Expand Down
2 changes: 1 addition & 1 deletion R/futurereserve.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ futurereserve <- function(
#Future: select essence and diameters

inventory <- inventory %>%
mutate(LoggingStatus = ifelse(CommercialLevel == "1" & Selected != "1" & HarvestableZone == TRUE &
mutate(LoggingStatus = ifelse(CommercialLevel == 1 & Selected != "1" & HarvestableZone == TRUE &
((Up == "0" &
(DBH >= advancedloggingparameters$FutureTreesMinDiameter & DBH < MinFD))
| (Up == "1" &
Expand Down
10 changes: 5 additions & 5 deletions R/harvestable.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ harvestable <- function(

# Calculation of spatial information (distance and slope)
SpatInventory <- inventory %>%
dplyr::filter(CommercialLevel!= "0") %>% # only take commercial sp, the calculation time is long enough
dplyr::filter(CommercialLevel!= 0) %>% # only take commercial sp, the calculation time is long enough
dplyr::filter(DBH >= MinFD & DBH <= MaxFD) # already selected commercial DBHs

coordinates(SpatInventory) <- ~ Xutm + Yutm # transform the inventory into a spatial object by informing the coordinates
Expand Down Expand Up @@ -263,9 +263,9 @@ harvestable <- function(
# Essences selection
HarverstableConditions <- # = 1 boolean vector
if (diversification || (!diversification && specieslax)) {
inventory$CommercialLevel =="1"| inventory$CommercialLevel == "2" # now or maybe after we will diversify
inventory$CommercialLevel >0 # now or maybe after we will diversify
} else if (!diversification && !specieslax) {
inventory$CommercialLevel == "1" # We will never diversify
inventory$CommercialLevel == 1 # We will never diversify
}


Expand All @@ -286,14 +286,14 @@ harvestable <- function(
inventory <- inventory %>%
mutate(LoggingStatus = ifelse(HarverstableConditions, #Under the above criteria, designate the harvestable species
"harvestable", "non-harvestable")) %>%
mutate(LoggingStatus = ifelse(CommercialLevel == "0", #The non-commercial species are non-harvestable.
mutate(LoggingStatus = ifelse(CommercialLevel == 0, #The non-commercial species are non-harvestable.
"non-harvestable", LoggingStatus)) %>%

mutate(LoggingStatus = ifelse(
!diversification &
specieslax & #designate the secondarily harvestable species, because diversification only if necessary
LoggingStatus == "harvestable" &
CommercialLevel == "2",
CommercialLevel >1,
"harvestable2nd", LoggingStatus))


Expand Down
6 changes: 3 additions & 3 deletions R/loggingparameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
#'@param CrownPartForFuel Proportion of the tree crown biomass used as fuel wood.
#' Default = 2/3 (double) (Branches diameter >= 5 cm) (Eleotério et al. 2019)
#'
#'@param Purge Part of the harvested log no used for timber, can be used for fuel
#'@param Purge Part of the harvested log not used for timber, can be used for fuel
#' wood. Default = 0.14, in m3 of purge/m3 of volume of timber harvested.
#' (double)
#'
Expand All @@ -94,8 +94,8 @@
#' - "SecondTrails" (default = 0.1)
#'
#'@param TreeHarvestableVolumeAllometry By default, allometry of tree
#' harvestable volume, French Guiana ONF formula: aCoef + bCoef * (DBH/100)^2,
#' aCoef and bCoef depend on the forest location, stored in
#' harvestable volume, French Guiana ONF formula: aCoef + bCoef * (DBH/100)^2.
#' With aCoef and bCoef depending on the forest location, stored in
#' \code{\link{ForestZoneVolumeParametersTable}}, DBH in cm. (function)
#'
#'@param TrunkHeightAllometry Allometry of trunk height, based on the cylinder
Expand Down
17 changes: 13 additions & 4 deletions R/selected.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,18 @@
#' @examples
#' data(Paracou6_2016)
#' data(DTMParacou)
#' data(PlotMask)
#' data(HarvestableAreaOutputsCable)
#' data(MainTrails)
#' data(ForestZoneVolumeParametersTable)
#'
#' inventory <- addtreedim(cleaninventory(Paracou6_2016, PlotMask),
#' volumeparameters = ForestZoneVolumeParametersTable)
#'
#' inventory <- commercialcriteriajoin(inventory, SpeciesCriteria)
#'
#' harvestableOutputs <- harvestable(inventory, topography = DTMParacou,
#' diversification = TRUE, specieslax = FALSE,
#' diversification = FALSE, specieslax = TRUE,
#' plotslope = HarvestableAreaOutputsCable$PlotSlope,
#' maintrails = MainTrails,
#' harvestablepolygons = HarvestableAreaOutputsCable$HarvestablePolygons,
Expand All @@ -105,7 +107,7 @@
#'
#' selecInventory <- selected(inventory, topography = DTMParacou,
#' scenario = "manual", fuel = "2", diversification = TRUE,
#' VO = 125, HVinit = HVinit, specieslax = FALSE, objectivelax = TRUE,
#' VO = 20, HVinit = HVinit, specieslax = TRUE, objectivelax = TRUE,
#' advancedloggingparameters = loggingparameters())$inventory
#'
selected <- function(
Expand Down Expand Up @@ -213,6 +215,7 @@ selected <- function(
inventory <- inventory %>%
mutate(Condition = ifelse(LoggingStatus == "harvestable2nd"|LoggingStatus == "harvestable",TRUE, FALSE)) %>%
group_by(Condition) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume)) %>%
mutate(VolumeCumSum = cumsum(TreeHarvestableVolume)) %>%
ungroup() %>%
Expand Down Expand Up @@ -250,6 +253,7 @@ selected <- function(
inventory <- inventory %>%
mutate(Condition = ifelse(LoggingStatus == "harvestable",TRUE, FALSE)) %>%
group_by(Condition) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume)) %>%
mutate(VolumeCumSum = cumsum(TreeHarvestableVolume)) %>%
ungroup() %>%
Expand Down Expand Up @@ -283,7 +287,7 @@ selected <- function(

inventory <- inventory %>%
mutate(LoggingStatus = ifelse(LoggingStatus == "harvestable" &
CommercialLevel == "1" & (DBH >= UpMinFD & DBH <= MaxFD), #designate the bigger individuals, when the plot is species-rich.
CommercialLevel == 1 & (DBH >= UpMinFD & DBH <= MaxFD), #designate the bigger individuals, when the plot is species-rich.
"harvestableUp", LoggingStatus))

if (!diversification) {
Expand All @@ -309,6 +313,7 @@ selected <- function(
inventory <- inventory %>%
mutate(Condition = ifelse(LoggingStatus == "harvestableUp", TRUE, FALSE)) %>%
group_by(Condition) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume)) %>%
mutate(VolumeCumSum = cumsum(TreeHarvestableVolume)) %>%
ungroup() %>%
Expand All @@ -331,6 +336,7 @@ selected <- function(
inventory <- inventory %>%
mutate(Condition = ifelse(LoggingStatus == "harvestableUp"|LoggingStatus == "harvestable",TRUE,FALSE)) %>%
group_by(Condition) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume)) %>%
mutate(VolumeCumSum = cumsum(TreeHarvestableVolume)) %>%
ungroup() %>%
Expand All @@ -353,7 +359,7 @@ selected <- function(

# Increase the MinFD of the other economic species
inventory <- inventory %>%
mutate(LoggingStatus = ifelse(LoggingStatus == "harvestable" & CommercialLevel == "2" &
mutate(LoggingStatus = ifelse(LoggingStatus == "harvestable" & CommercialLevel >1 &
(DBH >= UpMinFD & DBH <= MaxFD), #designate preferred individuals of 2nd economic rank species too, when the plot is species-rich.
"harvestableUp", LoggingStatus))

Expand All @@ -377,6 +383,7 @@ selected <- function(
inventory <- inventory %>%
mutate(Condition = ifelse(LoggingStatus == "harvestableUp"|LoggingStatus == "harvestable",TRUE,FALSE)) %>%
group_by(Condition) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume)) %>%
mutate(VolumeCumSum = cumsum(TreeHarvestableVolume)) %>%
ungroup() %>%
Expand Down Expand Up @@ -413,6 +420,7 @@ selected <- function(
inventory <- inventory %>%
mutate(Condition = ifelse(LoggingStatus == "harvestable", TRUE, FALSE)) %>%
group_by(Condition) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume)) %>%
mutate(VolumeCumSum = cumsum(TreeHarvestableVolume)) %>%
ungroup() %>%
Expand All @@ -435,6 +443,7 @@ selected <- function(
(TreeHarvestableVolume > min & TreeHarvestableVolume <= MissingVolume),
TRUE, FALSE)) %>%
group_by(Crumbs) %>%
arrange(CommercialLevel) %>% # add for reviewer
arrange(desc(TreeHarvestableVolume))

if(any(inventory$Crumbs)){
Expand Down
12 changes: 7 additions & 5 deletions man/SpeciesCriteria.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/selected.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions tests/testthat/test-commercialcriteriajoin.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ test_that("commercialcriteriajoin", {
function(element) expect_type(element, "double"))

expect_type(testinventory$CommercialName, "character")
expect_s3_class(testinventory$CommercialLevel, "factor")
expect_type(testinventory$CommercialLevel, "double")

# Check that commercial sp have logging info, and non-commercial have not:
TestCommercial <- testinventory %>%
filter(CommercialLevel == "0")
filter(CommercialLevel == 0)

TestList <- list( # list the variables to check
TestCommercial$CommercialName,
Expand All @@ -56,7 +56,7 @@ test_that("commercialcriteriajoin", {
function(element) expect_true(all(is.na(element))))

TestCommercial <- testinventory %>%
filter(CommercialLevel != "0")
filter(CommercialLevel != 0)

TestList <- list( # list the variables to check
TestCommercial$CommercialName,
Expand Down Expand Up @@ -91,8 +91,8 @@ test_that("commercialcriteriajoin", {


# check colonnes présentes et classe:
# CommercialName(character), CommercialLevel(factor), MinFD(numeric), UpMinFD(numeric), MaxFD(numeric)
# Quand CommercialLevel diff de "0" : CommercialName, MinFD, UpMinFD, MaxFD dif de NA
# CommercialName(character), CommercialLevel(numeric), MinFD(numeric), UpMinFD(numeric), MaxFD(numeric)
# Quand CommercialLevel diff de 0 : CommercialName, MinFD, UpMinFD, MaxFD dif de NA
# Check species attribution exceptions

# Quand CommercialLevel == "0" : CommercialName, MinFD, UpMinFD, MaxFD == NA
# Quand CommercialLevel == 0 : CommercialName, MinFD, UpMinFD, MaxFD == NA
Loading

0 comments on commit 07923e7

Please sign in to comment.