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


# note: the terms vegetation type, habitat type and habitat are used interchangeably in this script.


#============1. START MODEL SET UP 
#===================================================================================================

## Read model file from github repository
source(paste0(github_dir, "/VegetationTypes/01_Model-setup.R"))
source(file.path(github_dir, settings_system))
source(file.path(github_dir, settings_model))

## Loading libraries
loadlibraries(library_list)

### read all other functions needed for the habitat model
LoadFunctions()

# make output directory
dir.create(file.path(user_dir,base_dir,species_out_dir))
dir.create(file.path(user_dir,base_dir,species_out_dir,"RangeMaps"))

#============2. START CREATING LOGFILE
#===================================================================================================

# create a logfile
LogFile <- OpenLogFile()

#============3. START FORMATTING INPUT FOR FITTING RF
#===================================================================================================
#	select plots and duplicate plots with multiple EUNIS habitat types
PlotLocations <- SelectPlots_forRF(DropHabitats_plots)

# load species observation data
dt_species_data <- SelectSpecies_forRF()

# format input so that it corresponds with the fitRF-tool
dt_Observations_forRF <- FormatObservations_forRF(PlotLocations,dt_species_data)


#============4. START FITTING RF AND EVALUATE WITH CROSS-VALIDATION
#===================================================================================================

# read from hard disk the cross validated AUC values
# they should have been calculated with the same script, settings and input data.
if(file.exists(file.path(user_dir, base_dir, species_out_dir, "Habitats_CV_AUC.csv"))) {
  dt_AUC.cv <- fread(file.path(user_dir, base_dir, species_out_dir, "Habitats_CV_AUC.csv"))
} else { # Fit RF with a 5-fold cross validation
  dt_AUC.cv <- FitRF_forCVs(dt_Observations_forRF)
}


#============5. START FITTING RF WITH ALL DATA AND PROJECT FOR SCENARIOS
#===================================================================================================

# make a list of all vegetation type names for which a model could be fit
HabitatNames <- dt_AUC.cv[MeanAUC >= 0 & MeanAUC <= 1, HabitatNames]

# load the table with all characteristic species per vegetation type
dt_charact_species <- fread(species_special)

# per scenario
for(Scenario in ProjectScenarios){
    
  CleanMemory() 
    
  # create file directories
  PathScenario <- file.path(user_dir,base_dir,species_out_dir,"RangeMaps",Scenario)
  if(!file.exists(PathScenario)) dir.create(PathScenario)
  PathPNG <- file.path(user_dir,base_dir,species_out_dir,"RangeMaps","png") 
  if(!file.exists(PathPNG)) dir.create(PathPNG)
  # create file directories
  PathScenario_IntRes <- file.path(PathScenario,"IntermediateResults")
  if(!file.exists(PathScenario_IntRes)) dir.create(PathScenario_IntRes)
  
    
  # format binarized SDM-predictions
  dt_SDMs_forRF <- FormatSDMs_forRF(Scenario)
    
  # save dt_SDMs_forRF to hard disk
#  dt_SDMs <- cbind(dt_SDMs_forRF$coordinates, dt_SDMs_forRF$SpeciesPredictions)
#  write.csv(dt_SDMs, file.path(user_dir, base_dir, species_out_dir, "dt_speciesPredictions.csv"))
    
  # per habitat
  for(HabitatName in HabitatNames){
      
    # Fit RF with 100%
    RFmodel <- FitRF_forProjections(dt_Observations_forRF,HabitatName)
    
    # project RF on SDMs
    predicted_RF <- Project_RF_withSDMs(dt_SDMs_forRF, Scenario, HabitatName)
    
    # clean memory
    rm(RFmodel)
    gc()
    
  }
}


  #============6. START POST PROCESSING OF HABITAT RANGE MAPS
  #===================================================================================================

# per scenario
for(Scenario in ProjectScenarios){
  
  # list directories
  PathScenario <- file.path(user_dir,base_dir,species_out_dir,"RangeMaps",Scenario)
  PathPNG <- file.path(user_dir,base_dir,species_out_dir,"RangeMaps","png") 
  PathScenario_IntRes <- file.path(PathScenario,"IntermediateResults")
  
  
  # PostProcess_Habitats: 
  #   - probility values are binarized
  #   - Vegetation types with a cross-validated AUC or a AUC of the projected vegetation type distribution lower than 0.7 are removed
  # and save the resulting habitat range map to the hard disk
  PostProcess_Habitats(Scenario)  
}
  






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

