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


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

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

## 3. Loading libraries
loadlibraries(library_list)

## 4. Creating directories 
modelsetup <- create_species_directories(user_dir,base_dir, species_in_dir, species_out_dir, settings_system)

## 5. read all other bioscore functions saved in source files
LoadFunctions()

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

# create a logfile
LogFile <- OpenLogFile()

setwd(file.path(user_dir,base_dir))

#============3. LOAD ENVIRONMENTAL DATA
#===================================================================================================

# remove plots which are located in maritime wetlands, fresh water, marine water, man-made or unknown habtiats 
# and remove plots measured before 1990.
# and remove plots with a known location uncertainty larger than 1 km.
PlotLocations <- SelectPlots(plots)


# load all environmental rasters used to fit the regressions 
# and extract the value of environmental variables at a point location and add it to the coordinates
PlotLocations_Env <- ExtractVarValues(TableWithOverviewVariables )
  
# calculate all correlations between the variables and
# remove variables from the analysis which have a VIF value larger then the threshold value
VariableData <- VIFanalysis(VIFthreshold)


# clean memory
CleanMemory()


#============4. START SPECIES SELECTION
#===================================================================================================
SpeciesData <- Selectspecies(taxo_group, Drophabitatspecies = Drophabitat_species)


#======================================================================================================================
# Run BioScore for all species 

#RespNames <- SpeciesData$RespNames[1:2]

RunSDMs(RespNames, SDMfunction)
  
#============14. SAVE ALL METRICS FILE  
#================================================================================================+

SaveAllMetrics(AllMetricsSave,Removeoriginal,RespNames)

### END OF SCRIPT


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

