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


# these functions are used to format the SDM-predictions of species to be used to project the random forest-model

# functions included in this file
#     - loadSDMpredictions
#     - FormatSDMs_forRF


loadSDMpredictions <- function(SDM_file,AddCoordinates){
  # load SDM-range of first species
  r_SDMprediction <- rast(SDM_file)

  # only for testing the RF-model: make dataset smaller.
  if(modeltesting == TRUE){
    r_SDMprediction <- crop( r_SDMprediction,ext(4000000,5000000,2000000,3000000))
  }
  
  # make a table of the range and include the x&y-coordinates
  dt_SDMprediction <- as.data.table(as.data.frame(r_SDMprediction, na.rm=TRUE, xy=AddCoordinates))
  
  rm(r_SDMprediction)
  gc()
  
  return(dt_SDMprediction)
  
}

FormatSDMs_forRF <- function(Scenario){
  WriteLogFile(ln= paste0("2. Start loading SDM predictions of ", Scenario),
               file.path(user_dir,base_dir,species_out_dir,LogFile))
  
  setwd(file.path(user_dir,base_dir,binarized_SDMs,Scenario))
  # list files of SDM-predictions
  SDMpredictions <- list.files()
  
  # load raster of first species, including coordinates
  Range_species1 <- loadSDMpredictions(SDM_file = SDMpredictions[1],
                                       AddCoordinates = TRUE)
  
  
  # load rasters of other species, excluding coordinates
  ### Start snowfall
  sfInit(parallel = TRUE, cpus = parallel::detectCores() -1 )
  
  # Export packages
  sfLibrary('data.table', character.only = TRUE)
  sfLibrary('terra', character.only = TRUE)
  
  # Export global variables
  sfExport("loadSDMpredictions")
  sfExport("SDMpredictions")
  sfExport("modeltesting")
  
  # Do the run
  dt_Range_species <- sfClusterApplyLB(2:length(SDMpredictions), function(i){
    Range_species <- loadSDMpredictions(SDM_file = SDMpredictions[i],
                                        AddCoordinates = FALSE)
    
    return(Range_species)
  })
  
  ### Stop snowfall
  sfStop(nostop = FALSE)
  
  setwd(file.path(user_dir,base_dir))
  
  # combine table with coordinates & first species with table with all other species
  dt_Range_species <- bind_cols(Range_species1,dt_Range_species)
  # change names of species so that the model can run.
  dt_Range_species <- FormatSpeciesNames_forRF(dt_Range_species)
  
  dt_xy <- dt_Range_species[,.(x,y)]
  dt_Range_species[,':=' (x = NULL, y= NULL)]
  
  return(list(
    coordinates = dt_xy,
    SpeciesPredictions = dt_Range_species
  ))

  
}




# ====================================================================
#
# 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.
#
# 
# ====================================================================

