Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion rQSWATPlus/R/workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@
#' @param target_hrus Integer or NULL. Target number of HRUs per
#' subbasin when `hru_method = "target"`. Default NULL.
#' @param use_gwflow Logical. Enable gwflow groundwater modelling.
#' Default FALSE.
#' When `TRUE`, [qswat_setup_gwflow()] is called automatically after
#' [qswat_write_database()] to initialise the gwflow tables in the
#' project database. Default FALSE.
#' @param gwflow_config A named list of gwflow settings as returned by
#' [qswat_read_gwflow_config()]. Only used when `use_gwflow = TRUE`.
#' If `NULL` the bundled `gwflow.ini` defaults are used.
#' @param use_aquifers Logical. Create SWAT+ aquifer objects. Default
#' TRUE.
#' @param n_processes Integer. Number of MPI processes. Default 1.
Expand Down Expand Up @@ -91,6 +96,7 @@ qswat_run <- function(project_dir,
area_threshold = 0,
target_hrus = NULL,
use_gwflow = FALSE,
gwflow_config = NULL,
use_aquifers = TRUE,
n_processes = 1L,
quiet = FALSE,
Expand Down Expand Up @@ -151,6 +157,13 @@ qswat_run <- function(project_dir,
if (!quiet) message("\n=== Step 5/5: Writing database ===")
project <- qswat_write_database(project, db_file = db_file, overwrite = TRUE)

# Optional Step 6: Set up gwflow tables
if (isTRUE(use_gwflow)) {
if (!quiet) message("\n=== Step 6/6: Setting up gwflow tables ===")
project <- qswat_setup_gwflow(project, gwflow_config = gwflow_config,
overwrite = TRUE)
}

if (!quiet) message("\n=== QSWATPlus workflow complete! ===")
return(project)
}
9 changes: 8 additions & 1 deletion rQSWATPlus/man/qswat_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions rQSWATPlus/tests/testthat/test-workflow.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,57 @@ test_that("qswat_run works", {
expect_true(file.exists(project$db_file))
})

test_that("qswat_run with use_gwflow=TRUE initialises gwflow tables", {
skip_if_no_taudem()

dem <- system.file("extdata", "ravn_dem.tif", package = "rQSWATPlus")
landuse <- system.file("extdata", "ravn_landuse.tif", package = "rQSWATPlus")
soil <- system.file("extdata", "ravn_soil.tif", package = "rQSWATPlus")
lu_lookup <- system.file("extdata", "ravn_landuse.csv", package = "rQSWATPlus")
soil_lookup <- system.file("extdata", "ravn_soil.csv", package = "rQSWATPlus")
outlet <- system.file("extdata", "ravn_outlet.shp", package = "rQSWATPlus")
usersoil <- system.file("extdata", "ravn_usersoil.csv", package = "rQSWATPlus")

skip_if(dem == "", "Example data not available")

proj_dir <- file.path(tempdir(), "ravn_gwflow")
on.exit(unlink(proj_dir, recursive = TRUE), add = TRUE)

project <- qswat_run(
project_dir = proj_dir,
dem_file = dem,
landuse_file = landuse,
landuse_lookup = lu_lookup,
soil_file = soil,
soil_lookup = soil_lookup,
usersoil = usersoil,
outlet_file = outlet,
threshold = 500,
slope_breaks = c(0, 5, 15, 9999),
landuse_threshold = 5,
soil_threshold = 5,
use_gwflow = TRUE,
quiet = TRUE
)

expect_true(isTRUE(project$use_gwflow))

con <- DBI::dbConnect(RSQLite::SQLite(), project$db_file)
on.exit(DBI::dbDisconnect(con), add = TRUE)

pc <- DBI::dbGetQuery(con, "SELECT use_gwflow FROM project_config")
expect_equal(pc$use_gwflow, 1L, label = "project_config.use_gwflow = 1")

tbls <- DBI::dbListTables(con)
gwflow_tables <- c("gwflow_base", "gwflow_zone", "gwflow_grid",
"gwflow_out_days", "gwflow_obs_locs", "gwflow_solutes",
"gwflow_init_conc", "gwflow_hrucell", "gwflow_fpcell",
"gwflow_rivcell", "gwflow_lsucell", "gwflow_rescell")
for (tbl in gwflow_tables) {
expect_true(tbl %in% tbls, label = paste0("table '", tbl, "' exists"))
}
})

# Full end-to-end integration test: build project and verify SWAT+ Editor readiness
test_that("example dataset produces a SWAT+ Editor-ready database", {
skip_if_no_taudem()
Expand Down