Skip to content

Commit

Permalink
update createStudy() txt mode to create study v9 + doc + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
berthetclement committed Oct 15, 2024
1 parent 92d92b0 commit 26824a9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 9 deletions.
71 changes: 63 additions & 8 deletions R/createStudy.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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(
Expand All @@ -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)
}
Expand Down
17 changes: 16 additions & 1 deletion man/create-study.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/testthat/test-createStudy.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 26824a9

Please sign in to comment.