diff --git a/DESCRIPTION b/DESCRIPTION index cafc108..403e657 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: antaresEditObject Type: Package Title: Edit an 'Antares' Simulation -Version: 0.7.1 +Version: 0.8.0.9000 Authors@R: c( person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")), person("Frederic", "Breant", role = "ctb"), @@ -53,3 +53,5 @@ Suggests: knitr, rmarkdown VignetteBuilder: knitr +Remotes: + rte-antares-rpackage/antaresRead diff --git a/NEWS.md b/NEWS.md index 08ab744..c073378 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,13 @@ +> Copyright © 2016 RTE Reseau de transport d’electricite + +# antaresEditObject 0.8.0.9000 +(cf. Antares v9 changelog) + +NEW FEATURES (Antares v9.0) : +* `createStudy()` takes into account the new format of Antares studies (e.g. 9.0, 9.15 instead of 900, 915) + + + # antaresEditObject 0.7.1 ### Breaking changes : diff --git a/R/createStudy.R b/R/createStudy.R index 376e0a8..afc66d3 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,23 @@ #' @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) + + # check format version >= 9 + is_new_version <- .is_version_9(version = antares_version) + if (!dir.exists(path)) { dir.create(path = path, recursive = TRUE) } else { @@ -35,6 +52,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 +70,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) } - antares <- paste(readLines(con = file.path(path, "study.antares")), collapse = "\n") + + # read ".antares" file to update + antares <- paste(readLines(con = file.path(path, "study.antares")), + collapse = "\n") + + # specific format from version 9 + if(!is_new_version) + version_to_write <- gsub(pattern = ".", + replacement = "", + x = antares_version, + fixed = TRUE) + else + version_to_write <- antares_version + + # template for file "study.antares" antares <- whisker::whisker.render( template = antares, data = list( - version = gsub(pattern = ".", replacement = "", x = antares_version, fixed = TRUE), + version = version_to_write, 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 +104,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) } @@ -168,3 +207,33 @@ deleteStudy <- function(opts = simOptions(), prompt_validation = FALSE, simulati "Study"))) } + +#' function that ensures the transition to 9 +#' @description basic tests on the version's writing format +#' +#' @param version `character` (eg, "8.2.0" or "9.0") +#' @return `logical` if version >=9 +#' @keywords internal +.is_version_9 <- function(version){ + # Split major and minor parts + antares_version_splitted <- unlist( + strsplit( + as.character(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) + } + + return(is_new_version) +} + diff --git a/man/create-study.Rd b/man/create-study.Rd index a274b96..f2ce352 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/man/dot-is_version_9.Rd b/man/dot-is_version_9.Rd new file mode 100644 index 0000000..deebbb5 --- /dev/null +++ b/man/dot-is_version_9.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/createStudy.R +\name{.is_version_9} +\alias{.is_version_9} +\title{function that ensures the transition to 9} +\usage{ +.is_version_9(version) +} +\arguments{ +\item{version}{\code{character} (eg, "8.2.0" or "9.0")} +} +\value{ +\code{logical} if version >=9 +} +\description{ +basic tests on the version's writing format +} +\keyword{internal} diff --git a/tests/testthat/test-createStudy.R b/tests/testthat/test-createStudy.R index b2a4d73..e8cf304 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")