Skip to content

Commit

Permalink
fix bug readClusterDesc when no cluster + cover ST and RES case (#241)
Browse files Browse the repository at this point in the history
* fix bug readClusterDesc when no cluster + cover ST and RES case
  • Loading branch information
Nekmek7 authored Mar 28, 2024
1 parent c14be11 commit e54b25f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 31 additions & 18 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ readClusterSTDesc <- function(opts = simOptions()) {
}



.readClusterDesc <- function(opts = simOptions(),
dir = "thermal/clusters") {

Expand All @@ -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(
Expand All @@ -131,10 +127,11 @@ 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{
res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])]
}

res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])]


}else{

Expand All @@ -152,17 +149,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)
}

0 comments on commit e54b25f

Please sign in to comment.