## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>", fig.bg = "white",
  dev.args = list(bg = "white"), fig.width = 8, fig.height = 6,
  dpi = 144, fig.align = "center"
)

## ----surface------------------------------------------------------------------
library(potentiomap)
data("synthetic_wells")
points <- ps_make_points(
  synthetic_wells, "x", "y", "gw_elevation", "well_id", "EPSG:26916"
)
aoi <- ps_sample_aoi()
surface <- suppressWarnings(ps_interpolate(
  points, methods = "TPS", grid_res = 150, mask = aoi, padding = 0
)$TPS)
head_cols <- hcl.colors(64, "RdYlBu", rev = TRUE)

## ----contours-----------------------------------------------------------------
contours <- ps_contours(
  surface, interval = 1, return = "result"
)
contours$manifest

## ----contour-map, fig.alt="Synthetic thin-plate-spline potentiometric surface shaded blue at lower heads and red at higher heads, with one-unit dark contours and groundwater observation wells."----
par(bg = "white")
terra::plot(
  surface, col = head_cols,
  main = "TPS surface with one-unit contours"
)
terra::plot(contours$contours, add = TRUE, col = "#263845", lwd = 1.2)
terra::plot(points, add = TRUE, pch = 21, bg = "white", cex = 0.75)

## ----contour-support----------------------------------------------------------
support <- ps_prediction_support(points, surface = surface)
classified <- suppressWarnings(ps_contour_support(
  contours$contours, support = support,
  supported_distance = 500, approximate_distance = 1200,
  require_inside_hull = TRUE
))
classified$summary

## ----contour-support-map, fig.width=8, fig.height=6, fig.alt="Synthetic potentiometric contours divided into green solid supported sections and orange dashed approximate sections, with groundwater observation wells shown as points."----
support_colors <- c(supported = "#176b4d", approximate = "#c56a00")
support_lty <- c(supported = 1, approximate = 2)
segment_values <- terra::values(classified$segments)

par(bg = "white", fg = "#263845")
terra::plot(
  aoi, col = "#f7f5ef", border = "#8b969c",
  main = "Contour support from user-defined criteria"
)
for (support_class in names(support_lty)) {
  keep <- segment_values$support_class == support_class
  if (!any(keep)) next
  terra::plot(
    classified$segments[keep], add = TRUE,
    col = support_colors[support_class],
    lty = support_lty[support_class], lwd = 2
  )
}
terra::plot(points, add = TRUE, pch = 21, bg = "white", cex = 0.7)

## ----arrows-------------------------------------------------------------------
flow <- ps_flow_arrows(
  surface, scale = 25, res_factor = 8, endpoint_action = "shorten"
)
flow$validation_summary
head(flow$validation)

## ----arrow-map, fig.alt="Synthetic TPS potentiometric surface with one-unit contours, groundwater observation wells, and short sparse arrows pointing toward decreasing modeled head."----
bases <- ps_arrow_vertices(flow$arrows, "first")
tips <- ps_arrow_vertices(flow$arrows, "last")
b <- terra::crds(bases)
t <- terra::crds(tips)

par(bg = "white")
terra::plot(
  surface, col = head_cols,
  main = "Inferred negative-gradient direction"
)
terra::plot(contours$contours, add = TRUE, col = "#52646d", lwd = 0.9)
arrows(
  b[, 1], b[, 2], t[, 1], t[, 2],
  length = 0.06, col = "#111111"
)
terra::plot(points, add = TRUE, pch = 21, bg = "white", cex = 0.65)

