Skip to content

Commit

Permalink
Changed species suitability and species_landuse_suitability to be abl…
Browse files Browse the repository at this point in the history
…e to interpret more parameters and values
  • Loading branch information
derek-corcoran-barrios committed Dec 12, 2023
1 parent 9925798 commit de9499e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
9 changes: 5 additions & 4 deletions R/species_landuse_suitability.R
Expand Up @@ -6,6 +6,7 @@
#' @param Rasterspecieslanduse a list of species suitability for each landuse
#' @param species_names character vector of species names
#' @param landuses character vector with all landuses
#' @param parameter The name of the parameter to use
#' @param name The name of the output file
#' @param verbose Logical whether messages will be written while the
#' function is generating calculations, defaults to FALSE
Expand All @@ -26,16 +27,16 @@
#' @importFrom terra as.data.frame
#

species_suitability_landuse <- function(Rasterspecieslanduse, species_names, landuses, name = "Problem", verbose = FALSE){
species_suitability_landuse <- function(Rasterspecieslanduse, species_names, landuses, parameter = "SpeciesSuitabilityLanduse", name = "Problem", verbose = FALSE){

if(file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = T)
cat("param SpeciesSuitabilityLanduse default 0 :=")
cat(paste("param", parameter, "default 0 :="))
sink()
}
if(!file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = F)
cat("param SpeciesSuitabilityLanduse default 0 :=")
cat(paste("param", parameter, "default 0 :="))
sink()
}

Expand Down Expand Up @@ -63,7 +64,7 @@ species_suitability_landuse <- function(Rasterspecieslanduse, species_names, lan
}

paste_suitabilities_landuse <- function(df, species, colname){
filtered_df <- df[df[[colname]] == 1, ]
filtered_df <- df[df[[colname]] >= 0, ]
paste0(paste0("[", species, ",", colname, ","), paste0(filtered_df$cell, "]", " ", as.vector(filtered_df[colname][,1])))
}

Expand Down
7 changes: 4 additions & 3 deletions R/species_suitability.R
Expand Up @@ -5,6 +5,7 @@
#' line terminated by a newline character.
#' @param Rastercurrent raster object of current suitability
#' @param species_names character vector of species names
#' @param parameter The name of the parameter to use
#' @param name The name of the output file
#' @param verbose Logical whether messages will be written while the
#' function is generating calculations, defaults to FALSE
Expand All @@ -22,11 +23,11 @@
#'
#' @importFrom terra as.data.frame
#'
species_suitability <- function(Rastercurrent, species_names, name = "Problem", verbose = FALSE){
species_suitability <- function(Rastercurrent, species_names, name = "Problem", parameter = "SpeciesSuitability", verbose = FALSE){
SuitabilityTemp <- terra::as.data.frame(Rastercurrent, cells = T)
colnames(SuitabilityTemp)[-1] <- species_names
result <- species_names |> purrr::map(~paste_suitabilities(df = SuitabilityTemp, colname = .x)) |> purrr::reduce(paste) |> paste(collapse = " ")
TempSpeciesNames <- paste(c("param SpeciesSuitability default 0 :=", result, ";"), collapse = " ")
TempSpeciesNames <- paste(paste("param", parameter, "default 0 :=", result, ";"), collapse = " ")
if(file.exists(paste0(name, ".dat"))){
sink(paste0(name, ".dat"), append = T)
cat(TempSpeciesNames)
Expand All @@ -48,7 +49,7 @@ species_suitability <- function(Rastercurrent, species_names, name = "Problem",


paste_suitabilities <- function(df, colname){
filtered_df <- df[df[[colname]] == 1, ]
filtered_df <- df[df[[colname]] > 0, ]
paste0(paste0("[", colname, ","), paste0(filtered_df$cell, "]", " ", as.vector(filtered_df[colname][,1])))
}

Expand Down
3 changes: 3 additions & 0 deletions man/species_suitability.Rd

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

3 changes: 3 additions & 0 deletions man/species_suitability_landuse.Rd

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

0 comments on commit de9499e

Please sign in to comment.