diff --git a/.gitignore b/.gitignore index ca6a3415b84a3e5753023de0b956f5db3537f2b1..8bb9f713ec339abbe7e6ecbf03da1f3040bcfb14 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ vignettes/*.pdf /tests/testthat/config.yml *.INI *.ini +*.log diff --git a/NAMESPACE b/NAMESPACE index fd18f048c231f40a475228ee3fcbd607952b14f8..9c9f593b412bc9409a914d835c57d4d9d0967625 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,4 +3,5 @@ export(convert_sic_params) export(loadConfig) export(set_initial_conditions) +export(sic_run_export) export(sic_run_fortran) diff --git a/R/file.pathwin.R b/R/file.pathwin.R new file mode 100644 index 0000000000000000000000000000000000000000..0212200eb54883f05a280543082e7b1f78cb7dbe --- /dev/null +++ b/R/file.pathwin.R @@ -0,0 +1,13 @@ +#' file.path for Windows +#' +#' Same behavior as [file.path] but remove duplicated slashes and use backslashes as the path separator. +#' +#' @param ... Arguments sent to [file.path] +#' +file.pathwin <- function(...) { + sPath = file.path(...) + sPath = gsub("\\\\","/",sPath) # Remplace les antislashes présents (bug avec la command system) + sPath = gsub("//","/",sPath) #Suppression des séparateurs en double + sPath = gsub("/","\\\\",sPath) #Remplacement des slashs par des anti-slashes + return(sPath) +} diff --git a/R/loadConfig.R b/R/loadConfig.R index 40001e999fc05110067fef933518138792f9345f..98ea65c4c6360e6a55759caf733437f1da216871 100644 --- a/R/loadConfig.R +++ b/R/loadConfig.R @@ -1,8 +1,23 @@ #' Read default configuration of the package and complete it with eventual user config #' +#' @param sic_path [character], the path to an installation of SIC (see details) +#' @param xml_path [character], the path of the XML project file (see details) #' @param userFile location of the user config YML file #' @param pathDefaultCfg The location of the default configuration (located in "inst/config.yml" of the package by default) #' +#' @details +#' The path to SIC and to the XML project file can be defined in the user XML file by defining the appropriate items: +#' +#' ```yaml +#' sic: +#' path: Path/To/SIC/installation +#' project: +#' path: Path/To/Xml/Project/File.xml +#' ``` +#' +#' Moreover, the `sic_path` can be defined with the environment variable "SICPATH". This setting is a default which is overwritten +#' by the path defined in the user YAML file, which is also overwritten by the one define by the `sic_path` argument. +#' #' @return A configuration as it is returned by [config::get]. #' #' Configuration of RSIC2 as the following structure: @@ -30,6 +45,10 @@ #' str(cfg) loadConfig <- function(sic_path = NULL, xml_path = NULL, userFile = "config.yml", pathDefaultCfg = system.file("config.yml", package = "rsic2")) { cfg <- config::get(file = pathDefaultCfg) + if (Sys.getenv("SICPATH") != "" & is.null(sic_path)) { + cfg$sic$path <- Sys.getenv("SICPATH") + message("`sic_path` defined by environment variable SICPATH=", cfg$sic$path) + } if (file.exists(userFile)) { message("Reading user configuration from ", userFile) cfg = config::merge(cfg,config::get(file = userFile)) diff --git a/R/sic_run_export.R b/R/sic_run_export.R new file mode 100644 index 0000000000000000000000000000000000000000..bf56ec78ccdec5faaab735c7569e733dcb90a139 --- /dev/null +++ b/R/sic_run_export.R @@ -0,0 +1,43 @@ +#' Run SicExport and read the exported file +#' +#' @details +#' `params` parameter is a list representing parameters available in \url{https://sic.g-eau.fr/sicexport-utilitaire-d-exportation} to set the model network location of exported results. The string parameter `/x=n /yy=ii` in the command line is here represented by `list(xxx = nnn, yy = ii)`. +#' +#' @param scenario [numeric], the scenario to read +#' @param variant [numeric], the variant to read +#' @param params [list] location parameters of the result, see details. +#' @template param_cfg +#' +#' @return [matrix] with the read result +#' @export +#' +#' @examples +#' \dontrun{ +#' params <- list(SCE=1) +#' sic_run_fortran("fluvia", params) +#' # For exporting result in sections at time 0 +#' sic_run_export(scenario = 1, params = list(t = 0)) +#' } +sic_run_export <- function(scenario, variant = 0, params, cfg = loadConfig()) { + if (is.list(params)) params <- unlist(params) + params <- paste(paste0("/", names(params)), params, sep = "=", collapse = " ") + + file <- tempfile("sicexport", fileext = ".tsv") + cmd_line <- + paste( + shQuote(file.pathwin(cfg$sic$path, cfg$sic$export), type = "cmd"), + shQuote(cfg$project$path, type = "cmd"), + paste0("/sce=", scenario), + paste0("/var=", variant), + params, + "/quiet=1", + paste0("/out=", shQuote(file, type = "cmd")) + ) + logger::log_debug(cmd_line) + shell( + shQuote(cmd_line, type = "cmd2"), + wait = T, + translate = F + ) + as.matrix(read.csv(file, sep = "\t")) +} diff --git a/man/file.pathwin.Rd b/man/file.pathwin.Rd new file mode 100644 index 0000000000000000000000000000000000000000..18bcf2ec8d2c3430a4d4c8ace3e2e758713da2f8 --- /dev/null +++ b/man/file.pathwin.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/file.pathwin.R +\name{file.pathwin} +\alias{file.pathwin} +\title{file.path for Windows} +\usage{ +file.pathwin(...) +} +\arguments{ +\item{...}{Arguments sent to \link{file.path}} +} +\description{ +Same behavior as \link{file.path} but remove duplicated slashes and use backslashes as the path separator. +} diff --git a/man/loadConfig.Rd b/man/loadConfig.Rd index 9106a7da1d7b52a50e6f7d838cb0925faccc2b33..76e890f4b558c89319b012cf9033b8469e5a9c2f 100644 --- a/man/loadConfig.Rd +++ b/man/loadConfig.Rd @@ -12,6 +12,10 @@ loadConfig( ) } \arguments{ +\item{sic_path}{\link{character}, the path to an installation of SIC (see details)} + +\item{xml_path}{\link{character}, the path of the XML project file (see details)} + \item{userFile}{location of the user config YML file} \item{pathDefaultCfg}{The location of the default configuration (located in "inst/config.yml" of the package by default)} @@ -46,6 +50,16 @@ Configuration of RSIC2 as the following structure: \description{ Read default configuration of the package and complete it with eventual user config } +\details{ +The path to SIC and to the XML project file can be defined in the user XML file by defining the appropriate items:\if{html}{\out{<div class="yaml">}}\preformatted{sic: + path: Path/To/SIC/installation +project: + path: Path/To/Xml/Project/File.xml +}\if{html}{\out{</div>}} + +Moreover, the \code{sic_path} can be defined with the environment variable "SICPATH". This setting is a default which is overwritten +by the path defined in the user YAML file, which is also overwritten by the one define by the \code{sic_path} argument. +} \examples{ library(rsic2) sic_path <- tempdir(check = TRUE) diff --git a/man/sic_run_export.Rd b/man/sic_run_export.Rd new file mode 100644 index 0000000000000000000000000000000000000000..aea06aa93fd29e4b58d06c14abf93851960e54e8 --- /dev/null +++ b/man/sic_run_export.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/sic_run_export.R +\name{sic_run_export} +\alias{sic_run_export} +\title{Run SicExport and read the exported file} +\usage{ +sic_run_export(scenario, variant = 0, params, cfg = loadConfig()) +} +\arguments{ +\item{scenario}{\link{numeric}, the scenario to read} + +\item{variant}{\link{numeric}, the variant to read} + +\item{params}{\link{list} location parameters of the result, see details.} + +\item{cfg}{a \link{config} object. Configuration to use. See \link{loadConfig} for details} +} +\value{ +\link{matrix} with the read result +} +\description{ +Run SicExport and read the exported file +} +\details{ +\code{params} parameter is a list representing parameters available in \url{https://sic.g-eau.fr/sicexport-utilitaire-d-exportation} to set the model network location of exported results. The string parameter \verb{/x=n /yy=ii} in the command line is here represented by \code{list(xxx = nnn, yy = ii)}. +} +\examples{ +\dontrun{ +params <- list(SCE=1) +sic_run_fortran("fluvia", params) +# For exporting result in sections at time 0 +sic_run_export(scenario = 1, params = list(t = 0)) +} +} diff --git a/man/sic_run_fortran.Rd b/man/sic_run_fortran.Rd index d08a6251ef96e7d70a163f9747bbcd001b48b718..3ed6001297e9ebb0d986bd042dcb6642be878bb4 100644 --- a/man/sic_run_fortran.Rd +++ b/man/sic_run_fortran.Rd @@ -4,7 +4,7 @@ \alias{sic_run_fortran} \title{Run Talweg, Fluvia or Sirene} \usage{ -sic_run_fortran(prog, params, cfg = loadConfig()) +sic_run_fortran(prog, params = list(), cfg = loadConfig()) } \arguments{ \item{prog}{\link{character}, the program to run. Should be one of "talweg"} diff --git a/tests/testthat/test-sic_run_export.R b/tests/testthat/test-sic_run_export.R new file mode 100644 index 0000000000000000000000000000000000000000..d2b413da70ac8de7a524058162390344d60e3fd1 --- /dev/null +++ b/tests/testthat/test-sic_run_export.R @@ -0,0 +1,10 @@ +skip_on_ci() + +cfg <- loadLocalConfig() + +test_that("multiplication works", { + sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg) + m <- sic_run_export(scenario = 1, params = list(t = 0), cfg = cfg) + expect_type(m, "double") + expect_equal(colnames(m)[1:3], c("Bief", "Section", "Abscisse")) +}) diff --git a/tests/testthat/test-sic_run_fortran.R b/tests/testthat/test-sic_run_fortran.R index faa6e29555edd6f9d37b437f7928ab4c130f86d8..15b5bcc6832957743d84a811eb5e32239d07f3aa 100644 --- a/tests/testthat/test-sic_run_fortran.R +++ b/tests/testthat/test-sic_run_fortran.R @@ -1,7 +1,8 @@ skip_on_ci() +cfg <- loadLocalConfig() + test_that("fluvia on SCE=1 should create a binary result file", { - cfg <- loadLocalConfig() sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg) expect_true(file.exists(gsub("\\.xml", "_1_0.res", cfg$project$path))) expect_true(file.exists(gsub("\\.xml", "_1_0.rci", cfg$project$path)))