diff --git a/DESCRIPTION b/DESCRIPTION index 2135331f..06997b20 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.6.1 +Version: 2.6.2 Authors@R: c( person("Tatiana", "Vargas", email = "tatiana.vargas@rte-france.com", role = c("aut", "cre")), person("Jalal-Edine", "ZAWAM", role = "aut"), @@ -13,6 +13,7 @@ Authors@R: c( person("Etienne", "Sanchez", role = "ctb"), person("Assil", "Mansouri", role = "ctb"), person("Clement", "Berthet", role = "ctb"), + person("Kamel", "Kemiha", role = "ctb"), person("RTE", role = "cph") ) Description: Import, manipulate and explore results generated by 'Antares', a diff --git a/NEWS.md b/NEWS.md index 3b41e5e8..7d6a3faf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,13 @@ > Copyright © 2016 RTE Réseau de transport d’électricité +# antaresRead 2.6.2 (devlopment) + +BUGFIXES : + +* `readAntares()` : + - returns the right column names for details-timeStep.txt and details-res-timeStep.txt + + # antaresRead 2.6.1 (devlopment) BUGFIXES : diff --git a/R/importOutput.R b/R/importOutput.R index f2ae8b60..2cb25a80 100644 --- a/R/importOutput.R +++ b/R/importOutput.R @@ -46,6 +46,7 @@ #' is determined by the arguments "folder" and "file" #' - "areas", "values" => areas #' - "areas", "details" => clusters +#' - "areas", "details-res" => renewables clusters #' - "links", "values" => links #' #' @return @@ -277,6 +278,62 @@ ) } + +#' .get_value_columns_details_file +#' +#' Private function used to get the column names for the details-timeStep.txt or details-res-timeStep.txt. +#' Used in .importOutputForClusters() and .importOutputForResClusters() +#' From the opts, we detect which outputs the user decides to take +#' +#' @return +#' a vector +#' +#' @noRd +#' +.get_value_columns_details_file <- function(opts, type) { + + if(type == "details") { + # Order is important. There is a correspondance between elements. + all_thematic_variables <- c("DTG by plant", "NP Cost by plant", "NODU by plant") + colNames <- c("production", "NP Cost", "NODU") + if (opts$antaresVersion >= 830){ + all_thematic_variables <- c(all_thematic_variables, "Profit by plant") + colNames <- c(colNames, "profit") + } + } else if(type == "details-res") { + # Order is important. There is a correspondance between elements. + all_thematic_variables <- c("RES generation by plant") + colNames <- c("production") + } + # With thematic-trimming enabled + if (opts$parameters$general$`thematic-trimming`) { + if ("variables selection" %in% names(opts$parameters)) { + var_selection <- opts$parameters$`variables selection` + selection_type <- unique(names(var_selection)) + allowed_selection_type <- c("select_var -", "select_var +") + # Filter the vector to avoid other properties (for example : selected_vars_reset) + selection_type <- intersect(selection_type, allowed_selection_type) + # List with a repeated name + var_selection <- var_selection[which(names(var_selection) == selection_type)] + selected_variables <- unlist(var_selection, use.names = FALSE) + # Index of the variables found in the section "variables selection" + idx_vars <- which(all_thematic_variables %in% selected_variables) + if (length(idx_vars) > 0) { + if (selection_type == "select_var -") { + # vars to remove + colNames <- colNames[-idx_vars] + } else if (selection_type == "select_var +") { + # vars to keep + colNames <- colNames[idx_vars] + } + } + } + } + + return(colNames) +} + + #' .importOutputForClusters #' #' Private function used to import the output for the thermal clusters of one area @@ -295,6 +352,7 @@ # To improve greatly the performance we use our knowledge of the position of # the columns instead of using more general functions like dcast. reshapeFun <- function(x) { + # Get cluster names n <- names(x) idx <- ! n %in% pkgEnv$idVars @@ -304,28 +362,19 @@ idVarsId <- which(!idx) idVarsNames <- n[idVarsId] - # Get final value columns - if (sum(idx) / length(clusterNames) == 4) { - colNames <- c("production", "NP Cost", "NODU", "profit") - } else if (sum(idx) / length(clusterNames) == 3) { - colNames <- c("production", "NP Cost", "NODU") - } else if (sum(idx) / length(clusterNames) == 2) { - colNames <- c("production", "NP Cost") - } else { - colNames <- c("production") - } + # Column names of the output table + colNames <- .get_value_columns_details_file(opts, "details") # Loop over clusters nclusters <- length(clusterNames) - ncols <- length(colNames) res <- llply(1:nclusters, function(i) { - dt <- x[, c(nclusters * 0:(ncols - 1) + i, idVarsId), with = FALSE] + dt <- x[, c(nclusters * 0:(length(colNames) - 1) + i, idVarsId), with = FALSE] setnames(dt, c(colNames, idVarsNames)) dt[, cluster := as.factor(clusterNames[i])] dt }) - + rbindlist(res) } @@ -436,9 +485,9 @@ res } - } + #' .importOutputForResClusters #' #' Private function used to import the output for the renewable clusters of one area @@ -457,6 +506,7 @@ # To improve greatly the performance we use our knowledge of the position of # the columns instead of using more general functions like dcast. reshapeFun <- function(x) { + # Get cluster names n <- names(x) idx <- ! n %in% pkgEnv$idVars @@ -466,23 +516,14 @@ idVarsId <- which(!idx) idVarsNames <- n[idVarsId] - # Get final value columns - # Get final value columns - # colNames <- c("resProduction") - if (sum(idx) / length(clusterNames) == 3) { - colNames <- c("production", "NP Cost", "NODU") - } else if (sum(idx) / length(clusterNames) == 2) { - colNames <- c("production", "NP Cost") - } else { - colNames <- c("production") - } + # Column names of the output table + colNames <- .get_value_columns_details_file(opts, "details-res") # Loop over clusters nclusters <- length(clusterNames) - ncols <- length(colNames) res <- llply(1:nclusters, function(i) { - dt <- x[, c(nclusters * 0:(ncols - 1) + i, idVarsId), with = FALSE] + dt <- x[, c(nclusters * 0:(length(colNames) - 1) + i, idVarsId), with = FALSE] setnames(dt, c(colNames, idVarsNames)) dt[, cluster := as.factor(clusterNames[i])] dt @@ -496,10 +537,9 @@ mcYears, showProgress, opts, reshapeFun, sameNames = FALSE, objectDisplayName = "clustersRe", parallel = parallel) ) - - } + #' .importOutputForBindingConstraints #' #' Private function used to import the output for binding constraints. diff --git a/tests/testthat/test-readAntares.R b/tests/testthat/test-readAntares.R index 611db921..a5fef918 100644 --- a/tests/testthat/test-readAntares.R +++ b/tests/testthat/test-readAntares.R @@ -28,6 +28,16 @@ sapply(studyPathS, function(studyPath){ expect_equal(nrow(clusters), 24 * 7 * nweeks * nrow(readClusterDesc())) }) + test_that("Clusters importation column names are ok", { + clusters <- readAntares(clusters = opts$areasWithClusters, + timeStep = "hourly", + mcYears = "all", + opts = opts, + showProgress = FALSE) + expect_is(clusters, "data.table") + expect_equal(setdiff(colnames(clusters),pkgEnv$idVars), c("production", "NP Cost", "NODU")) + }) + test_that("importation of different objects works", { out <- readAntares(areas = opts$areaList, links=opts$linkList, clusters=opts$areasWithClusters, showProgress= FALSE, timeStep = "annual")