## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.align = "center")

## -----------------------------------------------------------------------------
library(climniche)

case_path <- system.file("extdata/mediterranean_anchovy", package = "climniche")

case_cells <- read.csv(
  file.path(case_path, "anchovy_climniche_input_sample.csv"),
  check.names = FALSE
)
layer_manifest <- read.csv(
  file.path(case_path, "anchovy_biooracle_layer_manifest.csv")
)
sdm_settings <- read.csv(
  file.path(case_path, "anchovy_presence_background_sdm_settings.csv")
)
metric_weights <- read.csv(
  file.path(case_path, "anchovy_climniche_sensitivity_weights.csv")
)

## -----------------------------------------------------------------------------
variable_table <- subset(
  layer_manifest,
  retained_for_climniche,
  c("variable", "label", "depth", "current_time", "future_time")
)
variable_table[["future_scenario"]] <- "SSP2-4.5"
variable_table

## -----------------------------------------------------------------------------
weight_table <- merge(
  variable_table[c("variable", "label")],
  metric_weights[c("variable", "sensitivity_weight")],
  by = "variable",
  sort = FALSE
)
names(weight_table) <- c(
  "Variable", "Climate variable", "Climatic metric weight"
)
weight_table

## -----------------------------------------------------------------------------
climate_variables <- variable_table[["variable"]]
current_columns <- paste0(climate_variables, "_current")
future_columns <- paste0(climate_variables, "_future")

current_climate <- as.matrix(case_cells[current_columns])
future_climate <- as.matrix(case_cells[future_columns])
colnames(current_climate) <- climate_variables
colnames(future_climate) <- climate_variables

weight_row <- sdm_settings[["setting"]] == "sdm_threshold"
sdm_threshold <- as.numeric(sdm_settings[weight_row, "value"])
climatic_weights <- setNames(
  metric_weights[["sensitivity_weight"]],
  metric_weights[["variable"]]
)

## -----------------------------------------------------------------------------
anchovy_fit <- fit_climniche(
  current = current_climate,
  future = future_climate,
  occupied = case_cells[["sdm_suitability"]],
  occupied_threshold = sdm_threshold,
  sensitivity = climatic_weights,
  preprocess = FALSE
)

climniche_summary(anchovy_fit)

## -----------------------------------------------------------------------------
geometry_error <- with(
  anchovy_fit,
  climate_change_amount^2 -
    niche_distance_change^2 -
    climate_reconfiguration^2
)
max(abs(geometry_error))

## ----mediterranean-maps-code, eval = FALSE------------------------------------
# spatial_fit <- fit_climniche_terra(
#   current = current_layers[[climate_variables]],
#   future = future_layers[[climate_variables]],
#   occupied = anchovy_suitability,
#   occupied_threshold = sdm_threshold,
#   domain = mediterranean_sea_mask,
#   sensitivity = climatic_weights,
#   preprocess = FALSE
# )
# 
# metric_maps <- plot_climniche_maps(
#   spatial_fit,
#   occupied = anchovy_suitability,
#   occupied_only = TRUE,
#   occupied_threshold = sdm_threshold,
#   study_region = mediterranean_boundary,
#   degree_labels = "hemisphere",
#   legend_title = FALSE,
#   legend_position = "bottom"
# )
# 
# metric_maps

## ----mediterranean-maps-figure, echo = FALSE, out.width = "100%"--------------
knitr::include_graphics("figures/anchovy-mediterranean-maps.png")

## ----summary-figure-code, eval = FALSE----------------------------------------
# variable_labels <- setNames(
#   variable_table[["label"]],
#   variable_table[["variable"]]
# )
# 
# summary_figure <- plot_climniche_summary_figure(
#   spatial_fit,
#   scope = "current",
#   top_variables = 6,
#   variable_labels = variable_labels,
#   title = NULL
# )
# 
# summary_figure

## ----summary-figure-output, echo = FALSE, out.width = "95%"-------------------
knitr::include_graphics("figures/anchovy-climniche-summary.png")

