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


# function to postprocess the predicted distributions of the vegetation types, 
# specifically the selection of vegetation types with a good model performance, 
# the binarisatioon of the distributions and the calculation of the range sizes.

PostProcess_Habitats <-  function(Scenario){
 
  # calculate the cut-off values and the performance of the maps
  if(Scenario == "current"){
    dt_AUC_SDMs <- Evaluate_RFpredictions(PlotLocations)
  } else {
    dt_AUC_SDMs <- fread(file.path(user_dir, base_dir, species_out_dir, "Habitats_predictions_AUC.csv"))
  }
  
  WriteLogFile(ln= paste0("7. Start binarizing PoOs of the habitat ranges"),
               file.path(user_dir,base_dir,species_out_dir,LogFile))
  
  
  # read the cross-validated AUC values and select the habitats with CV-AUC values above the threshold
  Threshold <- 0.7
  dt_AUC_CV <- fread(file.path(user_dir, base_dir, species_out_dir, "Habitats_CV_AUC.csv"))
  SelectedHabitats_1 <- dt_AUC_CV[MeanAUC >= Threshold, HabitatNames]
  # select the habitats with AUC values from the SDM-predictions above the threshold
  SelectedHabitats_2 <- dt_AUC_SDMs[AUC >= Threshold, HabitatNames]
  # remove habitat range maps which had a MCC value <0.3 (for fitting or projecting)
  SelectedHabitats <- intersect(SelectedHabitats_1, SelectedHabitats_2)

  ### Start snowfall
  sfInit(parallel = TRUE, cpus =  min(10, parallel::detectCores()))
  
  # Export packages
  sfLibrary('data.table', character.only = TRUE)
  sfLibrary('raster', character.only = TRUE)
  sfLibrary('terra', character.only = TRUE)
  sfLibrary('ggplot2', character.only = TRUE)

  # Export global variables
  sfExport("dt_AUC_SDMs")
  sfExport("PathScenario_IntRes")
  sfExport("user_dir")
  sfExport("base_dir")
  sfExport("species_out_dir")
  sfExport("Scenario")
  
  
  dt_RangeSizes <- sfClusterApplyLB(SelectedHabitats, function(HabitatName){
    
    # Set PoO value below calculated cutoff-value to 0
    predicted_RF <- fread(file.path(PathScenario_IntRes,paste0(HabitatName,".csv")))   
    
    Cutoff <- as.numeric(dt_AUC_SDMs[HabitatNames == HabitatName, cutoff] )
    HTvalues <- unlist(predicted_RF$predicted_RF)
    # binarize the range map
    HTvalues <- as.numeric(HTvalues > Cutoff) 
    predicted_RF[,predicted_RF := HTvalues]
    
    # save range maps to hard disk
    KeepColumns <- c("x","y","predicted_RF")
    df_xyz <- predicted_RF[,..KeepColumns]
    df_xyz$predicted_RF <- as.factor(df_xyz$predicted_RF)
    r_Habitat <- rast(df_xyz,type = "xyz")

    # save range as image to hard disk
    pdf(file.path(user_dir,base_dir,species_out_dir,"RangeMaps","png",paste0(HabitatName,"_",Scenario,".pdf")),
        width = 6, height = 6, onefile = FALSE)
    
    print(ggplot(df_xyz, aes(x=x,y=y,z=predicted_RF))+
      geom_raster(aes(fill=predicted_RF)) +
      scale_fill_manual(values = c("yellow","#00468B"), name = NULL) +  
      labs(title = paste0("range of ", HabitatName, " in ", Scenario, " climate conditions")) +
      theme_minimal() +
      theme(panel.grid = element_blank()) )
    
    dev.off()
    
    # save as tif-file to hard disk
    writeRaster(r_Habitat,file.path(user_dir,base_dir,species_out_dir,"RangeMaps",Scenario,paste0(HabitatName,".tif")), 
                options=tifoptions, overwrite = TRUE)
    
    # calculate range sizes per habitat 
    RangeSize <- sum(HTvalues)
    # save MCC values and cutoff values in table
    dt_RangeSize <-  as.data.table(matrix(c(as.character(HabitatName), Scenario, round(RangeSize )),
                                    nrow = 1, ncol=3,
                                    dimnames = list(NULL, c("HabitatNames","Scenario","RangeSize_km2"))))
    
    return(dt_RangeSize )
  })
  
  ### Stop snowfall
  sfStop(nostop = FALSE)
  
  # write as a table to the hard drive
  dt_RangeSizes <- rbindlist(dt_RangeSizes)
  write.csv(dt_RangeSizes,file.path(user_dir,base_dir,species_out_dir,paste0("RangeSizes_",Scenario,".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.
#
# 
# ====================================================================


