Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ant1795 #192

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -53,3 +53,5 @@ Suggests:
knitr,
rmarkdown
VignetteBuilder: knitr
Remotes:
rte-antares-rpackage/antaresRead@feature/ant1795v2
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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 :
Expand Down
75 changes: 72 additions & 3 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,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 {
Expand All @@ -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),
Expand All @@ -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")

KKamel67 marked this conversation as resolved.
Show resolved Hide resolved
# 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(
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}

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.

18 changes: 18 additions & 0 deletions man/dot-is_version_9.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
Loading