R/run_model.R

Defines functions run_model

Documented in run_model

#' Run Model
#'
#' @param exe_path path to Hydrus1D executable v4.17.040 (default: as retrieved
#' by \code{check_hydrus_exe})
#' @param model_path path to model directory (default: system.file("extdata/model/test",
#' package = "kwb.hydrus1d"))
#' @param ... additional arguments passed to \code{shell}
#' @return runs HYDRUS 1D model
#' @export
#' @importFrom fs file_copy path_abs
#' @examples
#' run_model()
run_model <- function(
  exe_path = check_hydrus_exe(),
  model_path = system.file("extdata/model/test", package = "kwb.hydrus1d"),
  ...
)
{
  exe_name <- basename(exe_path)
  target_dir <- dirname(model_path)

  fs::file_copy(
    path = exe_path,
    new_path = fs::path_abs(file.path(target_dir, exe_name)),
    overwrite = TRUE
  )

  file <- fs::path_abs(file.path(target_dir, "LEVEL_01.DIR"))

  writeLines(c(fs::path_abs(model_path), ""), file)

  shell(
    cmd = sprintf("cd %s && %s", fs::path_abs(target_dir), exe_name),
    intern = FALSE,
    ...
  )
}
KWB-R/kwb.hydrus1d documentation built on Nov. 21, 2022, 4:54 a.m.