Commit 8b651051 authored by Dorchies David's avatar Dorchies David
Browse files

feat: add Strickler redefinition at geometry importation

Fix #14
1 merge request!10Resolve "Crash de Talweg sur les distances de lit majeur"
Pipeline #34225 passed with stage
in 2 minutes and 33 seconds
Showing with 104 additions and 6 deletions
+104 -6
......@@ -30,6 +30,7 @@ export(sic_run_steady)
export(sic_run_unsteady)
export(sic_write_par)
export(split_reach)
export(update_portion_abscissas)
import(magrittr)
import(utils)
import(xml2)
......
......@@ -15,8 +15,9 @@
#' 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.
#' 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].
#'
......@@ -34,6 +35,8 @@
#' - `INTERF`: default `INTERF` parameter injected in command line arguments of TALWEG, FLUVIA, SIRENE
#' - `project`
#' - `path`: Path to the XML project file (should be defined by `xml_path` parameter or in the user config file)
#' - `stricklers`: A vector of 2 [numeric] values representing respectively
#' the default minor and medium bed Strickler coefficients to apply with [sic_import_reaches]
#'
#' @export
#'
......
......@@ -55,14 +55,70 @@ sic_import_reaches <- function(reaches, import_mode = "ImportXml_UPDATE", cfg =
type = "cmd2"
)
logger::log_debug(cmd_line)
shell(
ret <- shell(
cmd_line,
wait = T,
translate = T
)
update_portion_abscissas(cfg)
return(ret)
}
write_reach_txt <- function(file, reach) {
s <- unlist(reach)
writeLines(s, file)
}
#' Update abscissas of Strickler portions in a SIC model
#'
#' @description
#' As [sic_import_reaches] redefines abscissas in the reaches, and as the Stricklers
#' are defined with abscissas, geometry importation can lead to an inconsistent model.
#'
#' So the aim of this function is to reset Strickler definitions of the whole model
#' to a default value with one Strickler portion by reach with correct abscissas.
#'
#' @template param_cfg
#' @param stricklers 2-length [numeric], Strickler coefficient to apply for minor bed and medium bed respectively
#'
#' @return Use for side effect on the XML project file.
#'
#' @family sic_import_reaches
#'
#' @export
#'
#' @examples
#' \dontrun{
#' cfg <- cfg_tmp_project()
#' update_portion_abscissas(cfg, KMin = 50)
#'
#' # Display first Strickler definitions encountered in the XML file
#' x <- read_xml(cfg$project$path)
#' xml_find_first(x, "//Stricklers")
#' }
update_portion_abscissas <- function(cfg,
stricklers = cfg$project$stricklers) {
x <- read_xml(cfg$project$path)
x_biefs <- xml_find_all(x, "//Bief")
lapply(x_biefs, function(x) {
XD <- x %>% xml_find_first(".//Liste_Sections/SectionMin") %>% xml_attr("abscisse")
XF <- x %>% xml_find_last(".//Liste_Sections/SectionMin") %>% xml_attr("abscisse")
x_stricklers <- xml_find_all(x, ".//Stricklers")
lapply(x_stricklers, function(x) {
x_portion <- xml_find_first(x, "./Portion")
xml_attr(x_portion, "XD") <- XD
xml_attr(x_portion, "XF") <- XF
xml_set_text(xml_child(x_portion, "KMin"), as.character(stricklers[1]))
xml_set_text(xml_child(x_portion, "KMoy"), as.character(stricklers[2]))
# Delete other defined portions in the reach
other_portions <- xml_siblings(x_portion)
lapply(other_portions, xml_remove)
})
})
write_xml(x, cfg$project$path)
}
xml_find_last <- function(x, path) {
x_all <- xml_find_all(x, path)
return(x_all[length(x_all)])
}
......@@ -11,3 +11,4 @@ default:
INTERF: "0"
project:
path: "*** The path of your XML project file should be defined ***"
stricklers: [40, 40]
......@@ -44,6 +44,8 @@ Configuration of RSIC2 as the following structure:
\item \code{project}
\itemize{
\item \code{path}: Path to the XML project file (should be defined by \code{xml_path} parameter or in the user config file)
\item \code{stricklers}: A vector of 2 \link{numeric} values representing respectively
the default minor and medium bed Strickler coefficients to apply with \link{sic_import_reach}
}
}
}
......@@ -57,8 +59,9 @@ 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.
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)
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/merge_reaches.R
% Please edit documentation in R/merge.ReachTxt.R
\name{merge.ReachTxt}
\alias{merge.ReachTxt}
\title{Merge several \emph{ReachTxt} objects into one}
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sic_import_reaches.R
\name{update_portion_abscissas}
\alias{update_portion_abscissas}
\title{Update abscissas of Strickler portions in a SIC model}
\usage{
update_portion_abscissas(cfg, stricklers = cfg$project$stricklers)
}
\arguments{
\item{cfg}{a \link{config} object. Configuration to use. See \link{loadConfig} for details}
\item{stricklers}{2-length \link{numeric}, Strickler coefficient to apply for minor bed and medium bed respectively}
}
\value{
Use for side effect on the XML project file.
}
\description{
As \link{sic_import_reaches} redefines abscissas in the reaches, and as the Stricklers
are defined with abscissas, geometry importation can lead to an inconsistent model.
So the aim of this function is to reset Strickler definitions of the whole model
to a default value with one Strickler portion by reach with correct abscissas.
}
\examples{
\dontrun{
cfg <- cfg_tmp_project()
update_portion_abscissas(cfg, KMin = 50)
# Display first Strickler definitions encountered in the XML file
x <- read_xml(cfg$project$path)
xml_find_first(x, "//Stricklers")
}
}
\concept{sic_import_reaches}
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