# ====================================================================
#
# Copyright 2025, PBL Netherlands Environmental Assessment Agency
# See the copyright notice at the end of this file.
#
# ====================================================================


# 1. F_binarize_PoOs:       function which applies the threshold value to the probability map to binarise the PoO-maps or to set only the values below the threshold to zero.

F_binarize_PoOs  <- function(SetValuesUnderThresholdToZero, SetValuesAboveThresholdToOne, DispersalAssumption, Scenario, 
                             RespName, Species_RangeMap){
  
  if(DispersalAssumption == "realistic dispersal" & Scenario == "current"){
    # create a temporary folders in which the continuous and binarized species range maps under current climatic conditions are saved. 
    # This is necessary to be able to run the function F_DispersalAssumption when DispersalAssumption == "realistic dispersal" 
    result_path <- file.path(user_dir, base_dir, result_dir, RespName)
    dir.create(result_path, showWarnings = FALSE)
    suppressWarnings(dir.create(file.path(user_dir,base_dir,IntermediateResults,"Binary_maps")))
    #Choose species specific cut off value
    cutoff <- dt_cutoffs[species == RespName,cutoff.ens]
    # save the continuous species range map to hard disk
    terra::crs(Species_RangeMap) <- "EPSG:3035"
    terra::writeRaster(Species_RangeMap, file.path(user_dir, base_dir, result_dir, RespName, paste0("2004", "_ensembled.grd")), overwrite = TRUE)
    # save the Binary  species range map to hard disk
    Species_RangeMap_Bin <- Species_RangeMap
    Species_RangeMap_Bin[Species_RangeMap_Bin >= 0 & Species_RangeMap_Bin < cutoff] <- 0
    Species_RangeMap_Bin[Species_RangeMap_Bin >= cutoff] <- 1
    terra::writeRaster(Species_RangeMap_Bin, file.path(user_dir, base_dir, result_dir, RespName, paste0("2004", "_binary.grd")), overwrite = TRUE)
    terra::writeRaster(Species_RangeMap_Bin, file.path(user_dir, base_dir, IntermediateResults, "Binary_maps", paste0(RespName, ".tif")), overwrite = TRUE)
  } 
  if(DispersalAssumption == "realistic dispersal" & Scenario != "current"){
    # create a temporary folders in which the continuous and binarized species range maps under future climatic conditions are saved. 
    # This is necessary to be able to run the function F_DispersalAssumption when DispersalAssumption == "realistic dispersal" 
    result_path <- file.path(user_dir, base_dir, result_dir, RespName)
    dir.create(file.path(result_path, Scenario), showWarnings = FALSE)
    cutoff <- dt_cutoffs[species == RespName,cutoff.ens]
    # save the continuous species range map to hard disk
    terra::crs(Species_RangeMap) <- "EPSG:3035"
    terra::writeRaster(Species_RangeMap, file.path(user_dir, base_dir, result_dir, RespName, Scenario, paste0("2085_",Scenario,"_ensembled.grd")), overwrite = TRUE)
    # save the Binary  species range map to hard disk
    Species_RangeMap_Bin <- Species_RangeMap
    Species_RangeMap_Bin[Species_RangeMap_Bin >= 0 & Species_RangeMap_Bin < cutoff] <- 0
    Species_RangeMap_Bin[Species_RangeMap_Bin >= cutoff] <- 1
    terra::writeRaster(Species_RangeMap_Bin, file.path(user_dir, base_dir, result_dir, RespName, Scenario, paste0("2085_",Scenario,"_binary.grd")), overwrite = TRUE)
  }
  if(SetValuesUnderThresholdToZero == TRUE){
    cutoff <- dt_cutoffs[species == RespName,cutoff.ens]
    Species_RangeMap[Species_RangeMap < cutoff] <- 0 
  }
  if(SetValuesAboveThresholdToOne == TRUE){
    cutoff <- dt_cutoffs[species == RespName,cutoff.ens]
    Species_RangeMap[Species_RangeMap >= cutoff] <- 1 
  }
  return(Species_RangeMap)
}




# ====================================================================
#
# Copyright 2025, PBL Netherlands Environmental Assessment Agency
# 
# This source code of the BioScore model is owned by PBL Netherlands Environmental Assessment Agency. 
# It is not permitted to copy, redistribute, remix, transform, and build upon the material without written approval of PBL. 
# Permission for commercial purposes will not be granted. 
# This code is published to improve the transparency of the models used by PBL, 
# but without any warranty for fitness for any other purpose. 
# After approval of PBL to use the code, PBL will not provide any support.
#
# 
# ====================================================================

