From b3d4bd6a0bac49e65628d644585b258351285642 Mon Sep 17 00:00:00 2001 From: vargastat <130074062+vargastat@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:36:04 +0200 Subject: [PATCH] Feature/ant1795v2 (#261) * transform_antares_version added to setSimulationPath.R * transform_antares_version method added at setSimulationPath.R * library(stringr) removed from test-setSimulationPath.R * update version to dev + TODO * reforge function .transform_antares_version() + tests + integration in setsimulationPath + tests * fix to 2 digits for minor --- DESCRIPTION | 2 +- NEWS.md | 9 ++- R/setSimulationPath.R | 36 ++++++++++++ .../antares_version_float.antares | 6 ++ .../antares_version_float_2digit.antares | 6 ++ man/dot-transform_antares_version.Rd | 18 ++++++ tests/testthat/test-setSimulationPath.R | 57 +++++++++++++++++++ 7 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 inst/test_files/antares_version_files/antares_version_float.antares create mode 100644 inst/test_files/antares_version_files/antares_version_float_2digit.antares create mode 100644 man/dot-transform_antares_version.Rd diff --git a/DESCRIPTION b/DESCRIPTION index a98f14b5..12c426c7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: antaresRead Type: Package Title: Import, Manipulate and Explore the Results of an 'Antares' Simulation -Version: 2.7.2 +Version: 2.8.0.9000 Authors@R: c( person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")), person("Jalal-Edine", "ZAWAM", role = "aut"), diff --git a/NEWS.md b/NEWS.md index 0dd7634d..a360c034 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,11 +1,16 @@ > Copyright © 2016 RTE Réseau de transport d’électricité -# antaresRead 2.7.3 -BUGFIXES : +# antaresRead 2.8.0.9000 +(cf. Antares v9 changelog) +BUGFIXES : * `setSimulationPathAPI()` : encode URL before reading the data in simulation mode +BREAKING CHANGES : +* `setSimulationPathAPI()` : reads and returns the new converted study version format (ex : 9.0 => 900) + + # antaresRead 2.7.2 NEW FEATURES: diff --git a/R/setSimulationPath.R b/R/setSimulationPath.R index 9b5498e5..9ac791d1 100644 --- a/R/setSimulationPath.R +++ b/R/setSimulationPath.R @@ -350,6 +350,10 @@ setSimulationPath <- function(path, simulation = NULL) { linksDef <- data.table(linksDef) antaresVersion <- readIniFile(file.path(studyPath, "study.antares"))$antares$version + # Convert the Antares number version (from 9.0) + if(antaresVersion<600 & antaresVersion>=9) + antaresVersion <- .transform_antares_version(antares_version = antaresVersion) + params <- readIniFile(file.path(studyPath, "settings/generaldata.ini")) # Areas with thermal clusters @@ -679,3 +683,35 @@ setSimulationPath <- function(path, simulation = NULL) { return(list(binding = df_groups)) } } + +#' @title Convert the Antares number version +#' @description From V9.0, system version is 9.0 (2 digit for minor) instead of 900 +#' +#' @param antares_version ``numeric` Antares number version. +#' +#' @return `numeric` value according to study version +#' +#' @keywords internal +.transform_antares_version <- function(antares_version) { + antares_version <- format(antares_version, nsmall = 2) + + # Split major and minor parts + antares_version_splitted <- unlist(strsplit(antares_version, split = "\\.")) + + major <- antares_version_splitted[1] + minor <- antares_version_splitted[2] + + # max 1 digit for minor + if (nchar(minor) > 2) + stop("Invalid antares_version format, good format is like '9.99'", + call. = FALSE) + + # convert to numeric for package understanding + num_version <- as.numeric(antares_version)*100 + + return(num_version) +} + + + + diff --git a/inst/test_files/antares_version_files/antares_version_float.antares b/inst/test_files/antares_version_files/antares_version_float.antares new file mode 100644 index 00000000..5e5f6162 --- /dev/null +++ b/inst/test_files/antares_version_files/antares_version_float.antares @@ -0,0 +1,6 @@ +[antares] +version = 9.0 +caption = study_name +created = 1702567142 +lastsave = 1702634579 +author = Unknown diff --git a/inst/test_files/antares_version_files/antares_version_float_2digit.antares b/inst/test_files/antares_version_files/antares_version_float_2digit.antares new file mode 100644 index 00000000..faeda75a --- /dev/null +++ b/inst/test_files/antares_version_files/antares_version_float_2digit.antares @@ -0,0 +1,6 @@ +[antares] +version = 9.99 +caption = study_name +created = 1702567142 +lastsave = 1702634579 +author = Unknown diff --git a/man/dot-transform_antares_version.Rd b/man/dot-transform_antares_version.Rd new file mode 100644 index 00000000..70e14796 --- /dev/null +++ b/man/dot-transform_antares_version.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/setSimulationPath.R +\name{.transform_antares_version} +\alias{.transform_antares_version} +\title{Convert the Antares number version} +\usage{ +.transform_antares_version(antares_version) +} +\arguments{ +\item{antares_version}{``numeric` Antares number version.} +} +\value{ +\code{numeric} value according to study version +} +\description{ +From V9.0, system version is 9.0 (2 digit for minor) instead of 900 +} +\keyword{internal} diff --git a/tests/testthat/test-setSimulationPath.R b/tests/testthat/test-setSimulationPath.R index ae9bbb09..5caac160 100644 --- a/tests/testthat/test-setSimulationPath.R +++ b/tests/testthat/test-setSimulationPath.R @@ -207,3 +207,60 @@ test_that("New meta data for group dimension of binding constraints", { expect_is(opts_study_test$binding, "data.table") }) + +# v900---- +## test private ---- +test_that("read new format version from .antares file", { + test_path_files <- system.file("test_files", package = "antaresRead") + + # "readIniFile" conversion problem (9.0 => 9) + antares_file <- file.path(test_path_files, + "antares_version_files", + "antares_version_float.antares") + + file_to_read <- readIniFile(file = antares_file, stringsAsFactors = TRUE) + + version_value <- file_to_read$antares$version + + # test right conversion for package + expect_equal(.transform_antares_version(version_value), 900) + + # exception max digit minor + expect_error(.transform_antares_version(9.111), + regexp = "Invalid antares_version format") + + # read right format file (9.99) + antares_file <- file.path(test_path_files, + "antares_version_files", + "antares_version_float_2digit.antares") + + file_to_read <- readIniFile(file = antares_file, stringsAsFactors = TRUE) + + version_value <- file_to_read$antares$version + + # test right conversion for package + expect_equal(.transform_antares_version(version_value), 999) +}) + +## study ---- +test_that("read new format version from study", { + path <- setup_study_empty(dir_path = sourcedir_empty_study) + opts_study_test <- setSimulationPath(path) + + # "hack" study and paste test file with version "9.0" + test_path_files <- system.file("test_files", package = "antaresRead") + antares_file <- file.path(test_path_files, + "antares_version_files", + "antares_version_float.antares") + + # delete "study.antares" + file_to_remove <- file.path(opts_study_test$studyPath, "study.antares") + file.remove(file_to_remove) + file.copy(from = antares_file, to = file_to_remove) + + # read study + study <- setSimulationPath(path) + + # test right conversion for package + expect_equal(study$antaresVersion, 900) +})