From 1a3a2187418a96d055aa2a286c3919bc41d309f3 Mon Sep 17 00:00:00 2001 From: MANSOURI Assil Ext Date: Thu, 9 Feb 2023 21:43:47 +0100 Subject: [PATCH 1/6] added thermalData option in readInputThermal --- R/importInput.R | 46 ++++++++++++++++++++++++++ R/readInputThermal.R | 17 ++++++++-- tests/testthat/test-readInputThermal.R | 8 +++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/R/importInput.R b/R/importInput.R index 29d499e8..bf99ab6a 100644 --- a/R/importInput.R +++ b/R/importInput.R @@ -324,6 +324,52 @@ } + + + +.importThermalData <- function(area, opts, timeStep, unselect = NULL, ...) { + if (!area %in% opts$areasWithClusters) return(NULL) + unselect <- unselect$areas + path <- file.path(opts$inputPath, "thermal/prepro", area) + + if(!"api" %in% opts$typeLoad){ + clusters <- list.files(path) + } else { + clusters <- names(read_secure_json(path, token = opts$token, timeout = opts$timeout, config = opts$httr_config)) + } + + beginName <- c("FODuration", "PODuration", "FORate", "PORate", "NPOMin", "NPOMax") + if(!is.null(unselect)){ + colSelect <- which(!beginName%in%unselect) + names <- beginName[colSelect] + }else{ + colSelect <- NULL + names <- beginName + } + + + res <- ldply(clusters, function(cl) { + if(is.null(colSelect)) + { + # data <- fread(file.path(path, cl, "data.txt"), colClasses = "numeric") + data <- fread_antares(opts = opts, file = file.path(path, cl, "data.txt"), colClasses = "numeric") + }else{ + # data <- fread(file.path(path, cl, "data.txt"), select = colSelect, colClasses = "numeric") + data <- fread_antares(opts = opts, file = file.path(path, cl, "data.txt"), select = colSelect, colClasses = "numeric") + } + + setnames(data, + names(data), names) + + data$area <- area + data$cluster <- cl + data <- data[opts$timeIdMin:opts$timeIdMax] + data$timeId <- opts$timeIdMin:opts$timeIdMax + + changeTimeStep(data, timeStep, "hourly", fun = "mean") + }) +} + .importThermalModulation <- function(area, opts, timeStep, unselect = NULL, ...) { if (!area %in% opts$areasWithClusters) return(NULL) unselect <- unselect$areas diff --git a/R/readInputThermal.R b/R/readInputThermal.R index 0b74d6c5..c699bd09 100644 --- a/R/readInputThermal.R +++ b/R/readInputThermal.R @@ -9,11 +9,12 @@ #' #' @param clusters vector of clusters names for which thermal time series must be read. #' @param thermalModulation if TRUE, return thermalModulation data +#' @param thermalData if TRUE, return thermalData from prepro #' @inheritParams readAntares #' #' @return -#' If thermalModulation is TRUE, an object of class "antaresDataList" is returned. It is a list of -#' data.tables for thermalAvailabilities and thermalModulation +#' If thermalModulation or thermalData is TRUE, an object of class "antaresDataList" is returned. It is a list of +#' data.tables for selected input #' #' Else the result is a data.table with class "antaresDataTable". #' @@ -26,7 +27,7 @@ #' \code{\link{getAreas}}, \code{\link{getLinks}} #' #' @export -readInputThermal <- function(clusters = NULL, thermalModulation = FALSE, +readInputThermal <- function(clusters = NULL, thermalModulation = FALSE, thermalData = FALSE, opts = simOptions(), timeStep = c("hourly", "daily", "weekly", "monthly", "annual"), simplify = TRUE, parallel = FALSE, @@ -86,6 +87,16 @@ readInputThermal <- function(clusters = NULL, thermalModulation = FALSE, if (nrow(thermalMod) > 0) res$thermalModulation <- thermalMod } + + # thermalData processing + if (thermalData){ + areas <- unique(allAreasClusters[cluster %in% clusters]$area) + thermalDat <- as.data.table(ldply(areas, .importThermalData, opts = opts, timeStep = timeStep)) + thermalDat <- thermalDat[cluster %in% clusters] + setcolorder(thermalDat, c("area", "cluster", "timeId", setdiff(names(thermalDat), c("area", "cluster", "timeId")))) + + if (nrow(thermalDat) > 0) res$thermalData <- thermalDat + } if (length(res) == 0) stop("At least one argument of readInputTS has to be defined.") diff --git a/tests/testthat/test-readInputThermal.R b/tests/testthat/test-readInputThermal.R index 043cf63c..7d45eafd 100644 --- a/tests/testthat/test-readInputThermal.R +++ b/tests/testthat/test-readInputThermal.R @@ -23,5 +23,13 @@ sapply(studyPathS, function(studyPath){ expect_equal(nrow(input$thermalModulation) %% (24 * 7 * nweeks), 0) }) + test_that("Thermal data importation works", { + input <- readInputThermal(clusters = "peak_must_run_partial", thermalModulation = TRUE, showProgress = FALSE) + expect_is(input, "antaresDataList") + expect_is(input$thermalModulation, "antaresDataTable") + expect_gt(nrow(input$thermalModulation), 0) + expect_equal(nrow(input$thermalModulation) %% (24 * 7 * nweeks), 0) + }) + } }) From 5db46503320f1b7f5bf35f03f9e8f5a6d03a23fb Mon Sep 17 00:00:00 2001 From: MANSOURI Assil Ext Date: Thu, 9 Feb 2023 22:47:20 +0100 Subject: [PATCH 2/6] support for 8760 series when available in input --- R/changeTimeStep.R | 6 ++++-- R/importInput.R | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/R/changeTimeStep.R b/R/changeTimeStep.R index 0bea010c..fd8fa394 100644 --- a/R/changeTimeStep.R +++ b/R/changeTimeStep.R @@ -88,11 +88,13 @@ changeTimeStep <- function(x, newTimeStep, oldTimeStep, fun = "sum", opts=simOpt if (!is.null(x$week)) x$week <- NULL if (!is.null(x$month)) x$month <- NULL + if(!is.null(x)) realTimeIdMax <- ifelse(nrow(x) == 8760, 8760, opts$timeIdMax) + # Strategy: if oldTimeStep is not hourly, first desagregate data at hourly # level. Then, in all cases aggregate hourly data at the desired level. refTime <- data.table( - oldTimeId = .getTimeId(opts$timeIdMin:opts$timeIdMax, oldTimeStep, opts), - timeId = .getTimeId(opts$timeIdMin:opts$timeIdMax, newTimeStep, opts) + oldTimeId = .getTimeId(opts$timeIdMin:realTimeIdMax, oldTimeStep, opts), + timeId = .getTimeId(opts$timeIdMin:realTimeIdMax, newTimeStep, opts) ) x <- copy(x) diff --git a/R/importInput.R b/R/importInput.R index bf99ab6a..e4169988 100644 --- a/R/importInput.R +++ b/R/importInput.R @@ -43,10 +43,6 @@ # If file does not exists or is empty, but we know the columns, then we # create a table filled with 0. Else we return NULL - timeRange <- switch(inputTimeStep, - hourly=c(opts$timeIdMin, opts$timeIdMax), - daily=range(.getTimeId(opts$timeIdMin:opts$timeIdMax, "daily", opts)), - monthly=range(.getTimeId(opts$timeIdMin:opts$timeIdMax, "monthly", opts))) if (opts$typeLoad == 'api' || (file.exists(path) && !file.size(path) == 0)) { @@ -66,6 +62,13 @@ } } + if(!is.null(inputTS)) realTimeIdMax <- ifelse(nrow(inputTS) == 8760, 8760, opts$timeIdMax) + + timeRange <- switch(inputTimeStep, + hourly=c(opts$timeIdMin, realTimeIdMax), + daily=range(.getTimeId(opts$timeIdMin:realTimeIdMax, "daily", opts)), + monthly=range(.getTimeId(opts$timeIdMin:realTimeIdMax, "monthly", opts))) + if(!is.null(inputTS)){ inputTS <- inputTS[timeRange[1]:timeRange[2]] } else { @@ -361,10 +364,12 @@ setnames(data, names(data), names) + if(!is.null(data)) realTimeIdMax <- ifelse(nrow(data) == 8760, 8760, opts$timeIdMax) + data$area <- area data$cluster <- cl - data <- data[opts$timeIdMin:opts$timeIdMax] - data$timeId <- opts$timeIdMin:opts$timeIdMax + data <- data[opts$timeIdMin:realTimeIdMax] + data$timeId <- opts$timeIdMin:realTimeIdMax changeTimeStep(data, timeStep, "hourly", fun = "mean") }) @@ -410,10 +415,12 @@ if (all(modulation$minGenModulation == 0)) modulation[, minGenModulation := NA_real_] + if(!is.null(modulation)) realTimeIdMax <- ifelse(nrow(modulation) == 8760, 8760, opts$timeIdMax) + modulation$area <- area modulation$cluster <- cl - modulation <- modulation[opts$timeIdMin:opts$timeIdMax] - modulation$timeId <- opts$timeIdMin:opts$timeIdMax + modulation <- modulation[opts$timeIdMin:realTimeIdMax] + modulation$timeId <- opts$timeIdMin:realTimeIdMax changeTimeStep(modulation, timeStep, "hourly", fun = "mean") }) From 62450dcd52655105636f2802f1225ef0ea90bdc3 Mon Sep 17 00:00:00 2001 From: MANSOURI Assil Ext Date: Thu, 9 Feb 2023 23:22:29 +0100 Subject: [PATCH 3/6] added readInputRES() --- R/importInput.R | 2 +- R/{readInputThermal.R => readInputClusters.R} | 78 +++++++++++++++++++ ...nputThermal.R => test-readInputClusters.R} | 0 3 files changed, 79 insertions(+), 1 deletion(-) rename R/{readInputThermal.R => readInputClusters.R} (60%) rename tests/testthat/{test-readInputThermal.R => test-readInputClusters.R} (100%) diff --git a/R/importInput.R b/R/importInput.R index e4169988..1f6b8142 100644 --- a/R/importInput.R +++ b/R/importInput.R @@ -371,7 +371,7 @@ data <- data[opts$timeIdMin:realTimeIdMax] data$timeId <- opts$timeIdMin:realTimeIdMax - changeTimeStep(data, timeStep, "hourly", fun = "mean") + changeTimeStep(data, timeStep, "daily", fun = "mean") }) } diff --git a/R/readInputThermal.R b/R/readInputClusters.R similarity index 60% rename from R/readInputThermal.R rename to R/readInputClusters.R index c699bd09..d902c274 100644 --- a/R/readInputThermal.R +++ b/R/readInputClusters.R @@ -100,6 +100,84 @@ readInputThermal <- function(clusters = NULL, thermalModulation = FALSE, thermal if (length(res) == 0) stop("At least one argument of readInputTS has to be defined.") + # Class and attributes + res <- .addClassAndAttributes(res, NULL, timeStep, opts, simplify) + addDateTimeColumns(res) +} + + + + +#' Read Input RES time series +#' +#' @description +#' \code{readInputRes} is a function that reads renewable time series from an antares +#' project. But contrary to \code{\link{readAntares}}, it only reads time series +#' stored in the input folder, so it can work in "input" mode. +#' +#' @param clusters vector of RES clusters names for which renewable time series must be read. +#' @inheritParams readAntares +#' +#' @return +#' data.table with class "antaresDataTable". +#' +#' @seealso +#' \code{\link{setSimulationPath}}, \code{\link{readAntares}}, +#' \code{\link{getAreas}}, \code{\link{getLinks}} +#' +#' @export +readInputRES <- function(clusters = NULL, opts = simOptions(), + timeStep = c("hourly", "daily", "weekly", "monthly", "annual"), + simplify = TRUE, parallel = FALSE, + showProgress = TRUE) { + + timeStep <- match.arg(timeStep) + + # Can the importation be parallelized ? + if (parallel) { + if(!requireNamespace("foreach")) stop("Parallelized importation impossible. Please install the 'foreach' package and a parallel backend provider like 'doParallel'.") + if (!foreach::getDoParRegistered()) stop("Parallelized importation impossible. Please register a parallel backend, for instance with function 'registerDoParallel'") + } + + allAreasClusters <- readClusterResDesc()[area %in% opts$areasWithResClusters, c("area", "cluster")] + allClusters <- unique(allAreasClusters$cluster) + # Manage special value "all" + if(identical(clusters, "all")) clusters <- allClusters + + if (length(setdiff(tolower(clusters), tolower(allClusters))) > 0){ + cat(c("the following clusters are not available : ",setdiff(tolower(clusters), tolower(allClusters)))) + stop("Some clusters are not available in the areas specified") + } + + ind_cluster <- which(tolower(allClusters) %in% tolower(clusters)) + clusters <- unique(allClusters[ind_cluster]) + res <- list() # Object the function will return + + ResTS <- as.data.table(ldply(clusters, function(cl) { + + area <- unique(allAreasClusters[cluster == cl]$area) + if (length(area) > 1) warning(cl," is in more than one area") + resCl <- ldply(area, function(x){ + filePattern <- sprintf("%s/%s/%%s/series.txt", "renewables/series", x) + mid <- .importInputTS(cl, timeStep, opts, filePattern, "production", + inputTimeStep = "hourly", type = "matrix") + if (is.null(mid)) return (data.table()) + mid$area <- x + mid$cluster <- cl + mid + }) + + resCl <- dcast(as.data.table(resCl), area + cluster + timeId ~ tsId, value.var = "production") + })) + + tsCols <- setdiff(colnames(ResTS), c("area", "cluster", "timeId")) + setnames(ResTS, tsCols, paste0("ts",tsCols)) + setcolorder(ResTS, c("area", "cluster", "timeId", setdiff(names(ResTS), c("area", "cluster", "timeId")))) + + if (nrow(ResTS) > 0) res$ResProduction <- ResTS + + if (length(res) == 0) stop("At least one argument of readInputRes has to be defined.") + # Class and attributes res <- .addClassAndAttributes(res, NULL, timeStep, opts, simplify) addDateTimeColumns(res) diff --git a/tests/testthat/test-readInputThermal.R b/tests/testthat/test-readInputClusters.R similarity index 100% rename from tests/testthat/test-readInputThermal.R rename to tests/testthat/test-readInputClusters.R From b27afcd79eae25d957cada3ab2af0dbc7e718d77 Mon Sep 17 00:00:00 2001 From: MANSOURI Assil Ext Date: Mon, 13 Feb 2023 21:57:01 +0100 Subject: [PATCH 4/6] geographic trimming function --- NAMESPACE | 1 + NEWS.md | 9 +- R/getGeographicTrimming.R | 49 +++++++++ docs/articles/antaresH5.html | 2 +- docs/articles/antaresRead.html | 2 +- docs/news/index.html | 9 +- docs/pkgdown.yml | 2 +- docs/reference/getGeographicTrimming.html | 116 ++++++++++++++++++++ docs/reference/index.html | 4 + docs/sitemap.xml | 3 + man/getGeographicTrimming.Rd | 21 ++++ tests/testthat/test-getGeographicTrimming.R | 15 +++ 12 files changed, 221 insertions(+), 12 deletions(-) create mode 100644 R/getGeographicTrimming.R create mode 100644 docs/reference/getGeographicTrimming.html create mode 100644 man/getGeographicTrimming.Rd create mode 100644 tests/testthat/test-getGeographicTrimming.R diff --git a/NAMESPACE b/NAMESPACE index a2617a6c..8a6af079 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -37,6 +37,7 @@ export(extractDataList) export(getAlias) export(getAreas) export(getDistricts) +export(getGeographicTrimming) export(getIdCols) export(getLinks) export(hvdcModification) diff --git a/NEWS.md b/NEWS.md index 76ec5abb..3106dd29 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,9 +4,8 @@ NEW FEATURES: -Function `getLinks()` now has a new argument **withTransmission**. if TRUE, return additional -column with type of transmission capacities - +* New function `getGeographicTrimming()` returns filtering options for selected areas (links optional). +* Existing function `getLinks()` now has a new argument **withTransmission**. if TRUE, return additional column with type of transmission capacities. @@ -38,11 +37,11 @@ Major upgrade to `aggregateResult()` and `parAggregateMCall()` : NEW FEATURES: -added "profit by cluster" when reading cluster data +added "profit by cluster" when reading cluster data. BUGFIXES: -Fix for 404 error when some output is missing in API mode(#188) +Fix for 404 error when some output is missing in API mode(#188). diff --git a/R/getGeographicTrimming.R b/R/getGeographicTrimming.R new file mode 100644 index 00000000..135d5767 --- /dev/null +++ b/R/getGeographicTrimming.R @@ -0,0 +1,49 @@ +#Copyright © 2016 RTE Réseau de transport d’électricité + +#' Read geographic trimming (filtering) options +#' +#' @param areas Character. vector of areas +#' @param links Logical. if TRUE, return filtering options for all links starting from selected areas +#' @param opts List. simulation options +#' +#' @return list of filtering options for areas and links +#' +#' @export +getGeographicTrimming <- function(areas = NULL, links = TRUE, opts = simOptions()){ + if (is.null(areas)) stop("You need to select at least one area.") + if (areas == "all") areas <- opts$areaList + + res <- list() + + if(!is.null(areas)){ + areaData <- lapply(as.list(areas), .readPropertiesFunction, opts2 = opts, type = "areas") + names(areaData) <- areas + res$areas <- areaData + } + + if (links){ + linkData <- lapply(as.list(areas), .readPropertiesFunction, opts2 = opts, type = "links") + names(linkData) <- areas + linkData <- unlist(linkData, recursive = F) + names(linkData) <- gsub("\\.", " - ", names(linkData)) + res$links <- linkData + } + + res +} + +.readPropertiesFunction <- function(x, opts2, type){ + if (type == "areas"){ + if (opts2$typeLoad != "api"){ + inputPath <- file.path(opts2$inputPath, "areas", x, "optimization.ini") + readIniFile(inputPath)$filtering + } else readIni(file.path("input", "areas", x, "optimization"))$filtering + + } else if (type == "links"){ + if (opts2$typeLoad != "api"){ + inputPath <- file.path(opts2$inputPath, "links", x, "properties.ini") + lapply(as.list(readIniFile(inputPath)), function(x){x[grep("filter", names(x))]}) + } else lapply(as.list(readIni(file.path("input", "links", x, "properties"))), function(x){x[grep("filter", names(x))]}) + } +} + diff --git a/docs/articles/antaresH5.html b/docs/articles/antaresH5.html index e1e80217..cd3646da 100644 --- a/docs/articles/antaresH5.html +++ b/docs/articles/antaresH5.html @@ -84,7 +84,7 @@

Use h5 file format with ‘antaresRead’

Titouan Robert

-

2023-02-10

+

2023-02-13

Source: vignettes/antaresH5.Rmd diff --git a/docs/articles/antaresRead.html b/docs/articles/antaresRead.html index faff9422..7d3e993a 100644 --- a/docs/articles/antaresRead.html +++ b/docs/articles/antaresRead.html @@ -84,7 +84,7 @@

The ‘antaresRead’ Package

François Guillem

-

2023-02-10

+

2023-02-13

Source: vignettes/antaresRead.Rmd diff --git a/docs/news/index.html b/docs/news/index.html index 50d0f504..ffd6f0db 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -63,8 +63,9 @@

Changelog

NEW FEATURES:

-

Function getLinks() now has a new argument withTransmission. if TRUE, return additional column with type of transmission capacities

-
+
  • New function getGeographicTrimming() returns filtering options for selected areas (links optional).
  • +
  • Existing function getLinks() now has a new argument withTransmission. if TRUE, return additional column with type of transmission capacities.
  • +

NEW FEATURES:

@@ -78,9 +79,9 @@