## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)

## ----setup, message = FALSE---------------------------------------------------
library(blueterra)

## ----catalog------------------------------------------------------------------
catalog <- metric_catalog()
catalog[, c("metric", "label", "process_group", "scale_sensitive")]

## ----assign-------------------------------------------------------------------
bathy <- read_bathy(blueterra_example("hitw"))
prepared <- prepare_bathy(bathy, depth_range = c(-220, -25), smooth = TRUE)
terrain <- derive_terrain(
  prepared,
  metrics = c("slope", "aspect", "northness", "eastness", "tri", "bpi",
              "curvature", "surface_area_ratio")
)

assign_process_groups(terrain)
summarize_process_groups(terrain)

## ----names--------------------------------------------------------------------
standardize_metric_names(c("Slope degrees", "Broad BPI", "Curvature index"))
rename_metric_layers(c("slope_old", "curv_old"), c(slope_old = "slope_deg"))

## ----reps---------------------------------------------------------------------
select_process_representatives(metrics_available = names(terrain))
select_process_representatives(
  representatives = c(seafloor_aspect = "northness", slope_gradient = "slope_deg")
)

## ----pca, eval=requireNamespace("ggplot2", quietly = TRUE), fig.alt="PCA scores for sampled terrain metrics."----
cells <- sample_terrain_cells(
  terrain[[c("slope_deg", "tri", "bpi_3x3", "curvature")]],
  size = 50,
  method = "regular"
)
pca <- terrain_pca(cells, vars = c("slope_deg", "tri", "bpi_3x3", "curvature"))
pca_axis_labels(pca)
plot_process_pca(pca, title = "Terrain PCA with Dominant Loading Labels")

## ----custom-------------------------------------------------------------------
slope_tri <- derive_custom_metric(
  terrain,
  name = "slope_tri_index",
  expression = quote(slope_deg * tri)
)

extended <- add_metric_layers(terrain, slope_tri)

custom_catalog <- extend_metric_catalog(
  metric_catalog(),
  create_metric_catalog(
    metric = "slope_tri_index",
    label = "Slope-TRI index",
    process_group = "custom_relief",
    description = "Product of local slope and terrain ruggedness index.",
    units = "index",
    source_function = "derive_custom_metric",
    interpretation_notes = "Example index; define custom metrics from an explicit process model."
  )
)

assign_process_groups(extended, catalog = custom_catalog)
summarize_process_groups(extended, catalog = custom_catalog)

