Commit aa5a8ad4 authored by Dorchies David's avatar Dorchies David
Browse files

Merge branch '3-export-simulation-results-and-import-them-into-r' into 'main'

Resolve "Export simulation results and import them into R"

Closes #3

See merge request !3
1 merge request!3Resolve "Export simulation results and import them into R"
Showing with 152 additions and 2 deletions
+152 -2
...@@ -48,3 +48,4 @@ vignettes/*.pdf ...@@ -48,3 +48,4 @@ vignettes/*.pdf
/tests/testthat/config.yml /tests/testthat/config.yml
*.INI *.INI
*.ini *.ini
*.log
...@@ -3,4 +3,5 @@ ...@@ -3,4 +3,5 @@
export(convert_sic_params) export(convert_sic_params)
export(loadConfig) export(loadConfig)
export(set_initial_conditions) export(set_initial_conditions)
export(sic_run_export)
export(sic_run_fortran) export(sic_run_fortran)
#' 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)
}
#' Read default configuration of the package and complete it with eventual user config #' 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 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) #' @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]. #' @return A configuration as it is returned by [config::get].
#' #'
#' Configuration of RSIC2 as the following structure: #' Configuration of RSIC2 as the following structure:
...@@ -30,6 +45,10 @@ ...@@ -30,6 +45,10 @@
#' str(cfg) #' str(cfg)
loadConfig <- function(sic_path = NULL, xml_path = NULL, userFile = "config.yml", pathDefaultCfg = system.file("config.yml", package = "rsic2")) { loadConfig <- function(sic_path = NULL, xml_path = NULL, userFile = "config.yml", pathDefaultCfg = system.file("config.yml", package = "rsic2")) {
cfg <- config::get(file = pathDefaultCfg) 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)) { if (file.exists(userFile)) {
message("Reading user configuration from ", userFile) message("Reading user configuration from ", userFile)
cfg = config::merge(cfg,config::get(file = userFile)) cfg = config::merge(cfg,config::get(file = userFile))
......
#' 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"))
}
% 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.
}
...@@ -12,6 +12,10 @@ loadConfig( ...@@ -12,6 +12,10 @@ loadConfig(
) )
} }
\arguments{ \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{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)} \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: ...@@ -46,6 +50,16 @@ Configuration of RSIC2 as the following structure:
\description{ \description{
Read default configuration of the package and complete it with eventual user config 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{ \examples{
library(rsic2) library(rsic2)
sic_path <- tempdir(check = TRUE) sic_path <- tempdir(check = TRUE)
......
% 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))
}
}
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
\alias{sic_run_fortran} \alias{sic_run_fortran}
\title{Run Talweg, Fluvia or Sirene} \title{Run Talweg, Fluvia or Sirene}
\usage{ \usage{
sic_run_fortran(prog, params, cfg = loadConfig()) sic_run_fortran(prog, params = list(), cfg = loadConfig())
} }
\arguments{ \arguments{
\item{prog}{\link{character}, the program to run. Should be one of "talweg"} \item{prog}{\link{character}, the program to run. Should be one of "talweg"}
......
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"))
})
skip_on_ci() skip_on_ci()
cfg <- loadLocalConfig()
test_that("fluvia on SCE=1 should create a binary result file", { test_that("fluvia on SCE=1 should create a binary result file", {
cfg <- loadLocalConfig()
sic_run_fortran("fluvia", list(SCE = 1), cfg = cfg) 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.res", cfg$project$path)))
expect_true(file.exists(gsub("\\.xml", "_1_0.rci", cfg$project$path))) expect_true(file.exists(gsub("\\.xml", "_1_0.rci", cfg$project$path)))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment