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


# these functions are used to project the random forest-models

# functions included in this file
# 
#     - Project_RF
#     - Project_RF_withSDMs

Project_RF <- function(RF.model, Projection_data){
  # Predict cross-validated RF on validation set of observations
  predicted_RF <- predict.rfsrc(RF.model, Projection_data)
  RF_Obs_Habitats <- as.vector(predicted_RF$yvar)
  RF_Pred_PoOs <- as.vector(predicted_RF$predicted)
  
  #clean memory
  rm(predicted_RF)
  gc()
  
  if(is.null(RF_Obs_Habitats) == TRUE){
   return(
     RF_Pred_PoOs = RF_Pred_PoOs
   )} else {
    return(list(
      RF_Obs_Habitats = RF_Obs_Habitats,  
      RF_Pred_PoOs = RF_Pred_PoOs
    ))  
  }
}

Project_RF_withSDMs <- function(dt_SDMs_forRF, Scenario, HabitatName){
 
  WriteLogFile(ln= paste0("4. Start projecting RF on SDM predictions for ", Scenario),
               file.path(user_dir,base_dir,species_out_dir,LogFile))
  
  # select only the characteristic species from the input table
    dt_Habitat <- Select_CharacterSpecies(dt_charact_species, 
                                        HabitatName = HabitatName, 
                                        dt_Input_forRF = dt_SDMs_forRF$SpeciesPredictions)
  
  ### split the data into segments to lower computational costs
  n <- ceiling(nrow(dt_Habitat)/ 10)
  rownrs <- 1:nrow(dt_Habitat)
  rownrs_split <- split(rownrs,ceiling(seq_along(rownrs)/n))
  
  # Project RF
  predicted_RF <- lapply(1:length(rownrs_split),  function(i){
    
    
    dt_subset <-  dt_Habitat[rownrs_split[[i]],]

    Project_RF(RF.model = RFmodel, 
               Projection_data = dt_subset)

  })
  
  predicted_RF <- unlist(predicted_RF)
  predicted_RF <- cbind(dt_SDMs_forRF$coordinates, predicted_RF)
  
  # test results
  # summary(predicted_RF )
  #KeepColumns <- c("x","y","predicted_RF")
  #df_xyz <- predicted_RF[,..KeepColumns]
  #r_Habitat <- rast(df_xyz,type = "xyz")
  #plot(r_Habitat)

  write.csv(predicted_RF, file.path(PathScenario_IntRes,paste0(HabitatName,".csv")))  
}




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


