From a83f35a840784054d7a711e5e4b9d0e5e7d85277 Mon Sep 17 00:00:00 2001 From: "MAHOUDI Abdallah (Externe)" Date: Thu, 28 Mar 2024 11:02:18 +0100 Subject: [PATCH 1/3] fix bug readClusterDesc when no cluster + cover ST and RES case --- R/readClusterDesc.R | 53 ++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index 15bc403..7ed300b 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -89,7 +89,6 @@ readClusterSTDesc <- function(opts = simOptions()) { } - .readClusterDesc <- function(opts = simOptions(), dir = "thermal/clusters") { @@ -107,13 +106,10 @@ readClusterSTDesc <- function(opts = simOptions()) { path <- file.path(opts$inputPath, dir) - columns = c("group","enabled","must_run","unit_count","nominal_capacity", - "min_stable_power","spinning","min_up_time","min_down_time", - "co2","marginal_cost","fixed_cost","startup_cost","market_bid_cost", - "spread_cost","ts_gen","volatility_forced","volatility_planned", - "law_forced","law_planned") + columns <- .generate_columns_by_type(dir = dir) if(opts$typeLoad == 'api'){ + jsoncld <- read_secure_json(paste0(path, "&depth=4"), token = opts$token, timeout = opts$timeout, config = opts$httr_config) res <- rbindlist(mapply(function(X1, Y1){ clusters <- rbindlist( @@ -131,10 +127,15 @@ readClusterSTDesc <- function(opts = simOptions()) { if(length(res) == 0){ warning("No cluster description available.", call. = FALSE) - res <- setNames(data.table(matrix(nrow = 0, ncol = 22)), c("area", "cluster", columns)) + res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) + }else{ + + clusters <- rbindlist(list_clusters, idcol = "cluster") + newcol <- data.table() + newcol <- newcol[, c("area", "cluster") := tstrsplit(clusters$cluster, " / ", fixed = TRUE, keep = 1:2)] + res <- data.table(newcol,clusters[,-"cluster"]) } - - res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] + }else{ @@ -152,17 +153,33 @@ readClusterSTDesc <- function(opts = simOptions()) { clusters[, c(ncol(clusters), 1:(ncol(clusters) - 1))] }) + if(length(res) == 0){ + warning("No cluster description available.", call. = FALSE) + res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) + }else{ + res <- as.data.table(res) + setnames(res, "name", "cluster") + + res$cluster <- as.factor(tolower(res$cluster)) + } } - if(length(res) == 0){ - warning("No cluster description available.", call. = FALSE) - res <- setNames(data.table(matrix(nrow = 0, ncol = 22)), c("area", "cluster", columns)) - } - - res <- as.data.table(res) - setnames(res, "name", "cluster") + res +} + +.generate_columns_by_type <- function(dir = c("thermal/clusters", "renewables/clusters", "st-storage/clusters")) { - res$cluster <- as.factor(tolower(res$cluster)) - res + columns <- switch( + dir, + "thermal/clusters" = c("group","enabled","must_run","unit_count","nominal_capacity", + "min_stable_power","spinning","min_up_time","min_down_time", + "co2","marginal_cost","fixed_cost","startup_cost","market_bid_cost", + "spread_cost","ts_gen","volatility_forced","volatility_planned", + "law_forced","law_planned"), + + "renewables/clusters" = c("group","ts_interpretation","enabled","unit_count","nominal_capacity") + #"st-storage/clusters" = #ATTENTE DEV COTé API + ) + return(columns) } From bc6a38b588d3c3282aa7a89e0e3b6ca20a56cdfd Mon Sep 17 00:00:00 2001 From: "MAHOUDI Abdallah (Externe)" Date: Thu, 28 Mar 2024 11:12:19 +0100 Subject: [PATCH 2/3] correct bug --- R/readClusterDesc.R | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index 7ed300b..71caec8 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -129,11 +129,7 @@ readClusterSTDesc <- function(opts = simOptions()) { warning("No cluster description available.", call. = FALSE) res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns)) }else{ - - clusters <- rbindlist(list_clusters, idcol = "cluster") - newcol <- data.table() - newcol <- newcol[, c("area", "cluster") := tstrsplit(clusters$cluster, " / ", fixed = TRUE, keep = 1:2)] - res <- data.table(newcol,clusters[,-"cluster"]) + res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] } From 8464e6023f16b59cd5c38dd29a5e1ade0ccadfaa Mon Sep 17 00:00:00 2001 From: "MAHOUDI Abdallah (Externe)" Date: Thu, 28 Mar 2024 11:34:57 +0100 Subject: [PATCH 3/3] news.Md --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7c2bef3..4cd7050 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,7 +15,8 @@ BUGFIXES : BREAKING CHANGES : * `api_get()` has a new parameter to control JSON file parsing -* `readClusterDesc()` return empty dataTable and warning if no cluster in Antares study. +* `readClusterDesc()`/ `readClusterRESDesc()` / `readClusterSTDesc()` +return empty dataTable and warning if no cluster in Antares study. # antaresRead 2.6.0