From 26824a9f5fd0e226ab912f089231bab15b611549 Mon Sep 17 00:00:00 2001 From: berthetclement Date: Tue, 15 Oct 2024 17:21:51 +0200 Subject: [PATCH] update createStudy() txt mode to create study v9 + doc + tests --- R/createStudy.R | 71 +++++++++++++++++++++++++++---- man/create-study.Rd | 17 +++++++- tests/testthat/test-createStudy.R | 35 +++++++++++++++ 3 files changed, 114 insertions(+), 9 deletions(-) diff --git a/R/createStudy.R b/R/createStudy.R index 376e0a80..b0798032 100644 --- a/R/createStudy.R +++ b/R/createStudy.R @@ -7,6 +7,11 @@ #' if it doesn't exist, it'll be created. #' @param study_name Name of the study. #' @param antares_version Antares number version. +#' +#' @section Warning: +#' From **Antares version 9.0** onwards, versioning is only done with one number +#' for the major version number and a two-digit number for the minor +#' version number (e.g. 9.0, 9.12, 10.58, ...). #' #' @return Result of [antaresRead::setSimulationPath()] or [antaresRead::setSimulationPathAPI()] accordingly. #' @export @@ -20,11 +25,39 @@ #' @examples #' \dontrun{ #' -#' createStudy("path/to/simulation") +#' # with default values +#' createStudy("path/to/simulation", +#' study_name = "my_study", +#' antares_version = "8.2.0") +#' +#' # with Antares study version >= 9 (max 2 digits, ex : "9.15") +#' createStudy("path/to/simulation", +#' study_name = "my_study", +#' antares_version = "9.15") #' #' } createStudy <- function(path, study_name = "my_study", antares_version = "8.2.0") { antares_version <- as.numeric_version(antares_version) + + # Split major and minor parts + antares_version_splitted <- unlist( + strsplit( + as.character(antares_version), + split = "\\.")) + major <- antares_version_splitted[1] + + # check from version 9, minor max two digits + is_new_version <- as.numeric(major)>=9 + if(is_new_version){ + minor <- antares_version_splitted[2] + if(length(antares_version_splitted)>2) + stop("From Antares version 9, put version like this : '9.0' or '9.12' or '10.25'", + call. = FALSE) + if (nchar(minor) > 2) + stop("Invalid antares_version format, good format is like '9.99' (two digits on minor)", + call. = FALSE) + } + if (!dir.exists(path)) { dir.create(path = path, recursive = TRUE) } else { @@ -35,6 +68,8 @@ createStudy <- function(path, study_name = "my_study", antares_version = "8.2.0" } } } + + # choose template if (antares_version < as.numeric_version("6.5.0")) { file.copy( from = list.files(path = system.file("newStudy", package = "antaresEditObject"), full.names = TRUE), @@ -51,15 +86,31 @@ createStudy <- function(path, study_name = "my_study", antares_version = "8.2.0" } else { unzip(zipfile = system.file("template-antares/antares-study-v800.zip", package = "antaresEditObject"), exdir = path) } + + # read ".antares" file to update antares <- paste(readLines(con = file.path(path, "study.antares")), collapse = "\n") - antares <- whisker::whisker.render( - template = antares, - data = list( - version = gsub(pattern = ".", replacement = "", x = antares_version, fixed = TRUE), - study_name = study_name, - date_created = floor(as.numeric(Sys.time())) + + # specific format from version 9 + if(is_new_version) + antares <- whisker::whisker.render( + template = antares, + data = list( + version = antares_version, + study_name = study_name, + date_created = floor(as.numeric(Sys.time())) + ) ) - ) + else + antares <- whisker::whisker.render( + template = antares, + data = list( + version = gsub(pattern = ".", replacement = "", x = antares_version, fixed = TRUE), + study_name = study_name, + date_created = floor(as.numeric(Sys.time())) + ) + ) + + # write meta data writeLines(text = antares, con = file.path(path, "study.antares")) desktop <- paste(readLines(con = file.path(path, "Desktop.ini")), collapse = "\n") desktop <- whisker::whisker.render( @@ -69,7 +120,11 @@ createStudy <- function(path, study_name = "my_study", antares_version = "8.2.0" ) ) writeLines(text = desktop, con = file.path(path, "Desktop.ini")) + + # read study to create meta data object to return then opts <- setSimulationPath(path = path) + + # add specific directory and files according to version of study to create if (antares_version >= as.numeric_version("8.1.0")) { activateRES(opts = opts) } diff --git a/man/create-study.Rd b/man/create-study.Rd index a274b962..f2ce352d 100644 --- a/man/create-study.Rd +++ b/man/create-study.Rd @@ -36,10 +36,25 @@ Result of \code{\link[antaresRead:setSimulationPath]{antaresRead::setSimulationP \description{ Create study on disk or with AntaREST server through the API. } +\section{Warning}{ + +From \strong{Antares version 9.0} onwards, versioning is only done with one number +for the major version number and a two-digit number for the minor +version number (e.g. 9.0, 9.12, 10.58, ...). +} + \examples{ \dontrun{ -createStudy("path/to/simulation") +# with default values +createStudy("path/to/simulation", + study_name = "my_study", + antares_version = "8.2.0") + +# with Antares study version >= 9 (max 2 digits, ex : "9.15") +createStudy("path/to/simulation", + study_name = "my_study", + antares_version = "9.15") } } diff --git a/tests/testthat/test-createStudy.R b/tests/testthat/test-createStudy.R index b2a4d736..e8cf304e 100644 --- a/tests/testthat/test-createStudy.R +++ b/tests/testthat/test-createStudy.R @@ -2,6 +2,41 @@ # create ---- + ## v9.0---- +test_that("Create a new v9.0 study", { + path <- file.path(tempdir(), "tests_createStudy") + suppressWarnings( + opts <- createStudy(path, antares_version = "9.0") + ) + properties <- antaresRead:::readIniFile(file.path(path, "study.antares")) + expect_identical(properties$antares$version, 9) + unlink(path, recursive = TRUE) +}) + +test_that("Create a new v9.15 (2 digits) study", { + path <- file.path(tempdir(), "tests_createStudy") + suppressWarnings( + opts <- createStudy(path, antares_version = "9.15") + ) + properties <- antaresRead:::readIniFile(file.path(path, "study.antares")) + expect_identical(properties$antares$version, 9.15) + unlink(path, recursive = TRUE) +}) + +test_that("Create a new v9.15.2 (error bad format version) study", { + path <- file.path(tempdir(), "tests_createStudy") + + expect_error( + opts <- createStudy(path, antares_version = "9.15.2"), + regexp = 'From Antares version 9, put version like this : \'9.0\' or') + + expect_error( + opts <- createStudy(path, antares_version = "9.153"), + regexp = "Invalid antares_version format, good format is like \'9.99\' \\(two digits on minor\\)" ) + + unlink(path, recursive = TRUE) +}) + ## v8.7.0---- test_that("Create a new v8.7.0 study", { path <- file.path(tempdir(), "tests_createStudy")