## ----plot-options, include=FALSE----------------------------------------------
knitr::opts_chunk$set(fig.bg = "white", dev.args = list(bg = "white"))

## ----setup--------------------------------------------------------------------
library(hydromeso)

## ----hecras-inputs, fig.width=10, fig.height=4.8, fig.bg="white", fig.cap="Depth and velocity surfaces derived from the 31 May 2022 HEC-RAS output at the Big Blue-Kansas Rivers confluence."----
paths <- mesohabitat_example_rasters(paths = TRUE)
basename(unlist(paths))

h <- mesohabitat_example_rasters()
terra::compareGeom(h$depth, h$velocity)

old_par <- graphics::par(mfrow = c(1, 2), mar = c(1, 1, 3, 4))
terra::plot(h$depth, col = grDevices::hcl.colors(40, "Blues 3"),
            main = "Depth (m)", axes = FALSE)
terra::plot(h$velocity, col = grDevices::hcl.colors(40, "YlOrRd"),
            main = "Velocity (m/s)", axes = FALSE)
graphics::par(old_par)

## ----hecras-classification, fig.width=10, fig.height=6.2, fig.bg="white", fig.cap="Eight-class hydraulic mesohabitats derived from the real HEC-RAS sample."----
classes <- classify_mesohabitat_raster(h$depth, h$velocity)
class_colors <- mesohabitat_palette()
plot_mesohabitat(
  classes,
  main = "Big Blue-Kansas Rivers mesohabitats — 31 May 2022",
  axes = FALSE,
  palette = class_colors
)

## ----hecras-aoi, fig.width=10, fig.height=5.2, fig.bg="white", fig.cap="Mesohabitat output cropped to a central area of interest."----
e <- terra::ext(h$depth)
dx <- terra::xmax(e) - terra::xmin(e)
dy <- terra::ymax(e) - terra::ymin(e)
aoi <- terra::as.polygons(
  terra::ext(
    terra::xmin(e) + 0.18 * dx, terra::xmax(e) - 0.18 * dx,
    terra::ymin(e) + 0.12 * dy, terra::ymax(e) - 0.12 * dy
  ),
  crs = terra::crs(h$depth)
)
cropped <- classify_mesohabitat_raster(h$depth, h$velocity, aoi = aoi)
plot_mesohabitat(
  cropped, axes = FALSE, palette = class_colors
)

## ----hecras-export------------------------------------------------------------
series <- classify_mesohabitat_series(
  list(`2022-05-31` = h$depth),
  list(`2022-05-31` = h$velocity)
)
out <- file.path(tempdir(), "hecras_mesohabitat.tif")
write_mesohabitat(series[[1]], out, sidecar = TRUE, overwrite = TRUE)

