Skip to content

Commit

Permalink
Use data.table::fwrite instead of utils::write.table and use lower na…
Browse files Browse the repository at this point in the history
…me for area and cluster_name
  • Loading branch information
kemihak committed Feb 22, 2024
1 parent 73d2ab0 commit 21345c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
29 changes: 15 additions & 14 deletions R/createCluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ createClusterRES <- function(area,
)
}


#' @importFrom data.table fwrite
.createCluster <- function(area,
cluster_name,
...,
Expand Down Expand Up @@ -301,6 +301,7 @@ createClusterRES <- function(area,
if (add_prefix)
cluster_name <- paste(area, cluster_name, sep = "_")
params_cluster$name <- cluster_name
lower_cluster_name <- tolower(cluster_name)

# v860 pollutants
if(opts$antaresVersion >= 860)
Expand Down Expand Up @@ -338,7 +339,7 @@ createClusterRES <- function(area,
currPath <- ifelse(identical(cluster_type, "renewables"), "input/renewables/series/%s/%s/series", "input/thermal/series/%s/%s/series")
cmd <- api_command_generate(
action = "replace_matrix",
target = sprintf(currPath, area, tolower(cluster_name)),
target = sprintf(currPath, area, lower_cluster_name),
matrix = time_series
)
api_command_register(cmd, opts = opts)
Expand All @@ -359,15 +360,15 @@ createClusterRES <- function(area,
# params_cluster <- stats::setNames(object = list(params_cluster), nm = cluster_name)

# path to ini file containing clusters' name and parameters
path_clusters_ini <- file.path(inputPath, cluster_type, "clusters", tolower(area), "list.ini")
path_clusters_ini <- file.path(inputPath, cluster_type, "clusters", area, "list.ini")

# read previous content of ini
previous_params <- readIniFile(file = path_clusters_ini)

if (tolower(cluster_name) %in% tolower(names(previous_params)) & !overwrite){
if (lower_cluster_name %in% tolower(names(previous_params)) & !overwrite){
stop(paste(cluster_name, "already exist"))
} else if (tolower(cluster_name) %in% tolower(names(previous_params)) & overwrite){
ind_cluster <- which(tolower(names(previous_params)) %in% tolower(cluster_name))[1]
} else if (lower_cluster_name %in% tolower(names(previous_params)) & overwrite){
ind_cluster <- which(tolower(names(previous_params)) %in% lower_cluster_name)[1]
previous_params[[ind_cluster]] <- params_cluster
names(previous_params)[[ind_cluster]] <- cluster_name
} else {
Expand All @@ -385,7 +386,7 @@ createClusterRES <- function(area,

# initialize series
dir.create(
path = file.path(inputPath, cluster_type, "series", tolower(area), tolower(cluster_name)),
path = file.path(inputPath, cluster_type, "series", area, lower_cluster_name),
recursive = TRUE, showWarnings = FALSE
)

Expand All @@ -401,33 +402,33 @@ createClusterRES <- function(area,
time_series <- rbind(time_series, fill_mat)
}

utils::write.table(
fwrite(
x = time_series, row.names = FALSE, col.names = FALSE, sep = "\t",
file = file.path(inputPath, cluster_type, "series", tolower(area), tolower(cluster_name), "series.txt")
file = file.path(inputPath, cluster_type, "series", area, lower_cluster_name, "series.txt")
)


# prepro
if (identical(cluster_type, "thermal")) {
dir.create(
path = file.path(inputPath, cluster_type, "prepro", tolower(area), tolower(cluster_name)),
path = file.path(inputPath, cluster_type, "prepro", area, lower_cluster_name),
recursive = TRUE, showWarnings = FALSE
)

if (is.null(prepro_data))
prepro_data <- matrix(data = c(rep(1, times = 365 * 2), rep(0, times = 365 * 4)), ncol = 6)
utils::write.table(
fwrite(
x = prepro_data, row.names = FALSE, col.names = FALSE, sep = "\t",
file = file.path(inputPath, cluster_type, "prepro", tolower(area), tolower(cluster_name), "data.txt")
file = file.path(inputPath, cluster_type, "prepro", area, lower_cluster_name, "data.txt")
)


if (is.null(prepro_modulation))
prepro_modulation <- matrix(data = c(rep(1, times = 365 * 24 * 3), rep(0, times = 365 * 24 * 1)), ncol = 4)

utils::write.table(
fwrite(
x = prepro_modulation, row.names = FALSE, col.names = FALSE, sep = "\t",
file = file.path(inputPath, cluster_type, "prepro", tolower(area), tolower(cluster_name), "modulation.txt")
file = file.path(inputPath, cluster_type, "prepro", area, lower_cluster_name, "modulation.txt")
)
}

Expand Down
30 changes: 16 additions & 14 deletions R/editCluster.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ editClusterRES <- function(area,
)
}


#' @importFrom data.table fwrite
.editCluster <- function(area,
cluster_name,
...,
Expand All @@ -108,6 +108,8 @@ editClusterRES <- function(area,
if (add_prefix)
cluster_name <- paste(area, cluster_name, sep = "_")

lower_cluster_name <- tolower(cluster_name)

# v860 pollutants
if(opts$antaresVersion >= 860)
params_cluster <- append(params_cluster, list_pollutants)
Expand All @@ -117,8 +119,8 @@ editClusterRES <- function(area,
opts= opts))

if (!cluster_name %in% clusters){
if (tolower(cluster_name) %in% tolower(clusters)){
cluster_idx <- which(tolower(clusters) %in% tolower(cluster_name))
if (lower_cluster_name %in% tolower(clusters)){
cluster_idx <- which(tolower(clusters) %in% lower_cluster_name)
cluster_name <- clusters[cluster_idx]
if (length(cluster_name) > 1)
warning("detected multiple clusters : ", do.call(paste, as.list(cluster_name)), ", only the first one will be edited.")
Expand Down Expand Up @@ -160,7 +162,7 @@ editClusterRES <- function(area,
if (!identical(cluster_type, "renewables") && !is.null(prepro_modulation)) {
cmd <- api_command_generate(
action = "replace_matrix",
target = sprintf("input/thermal/prepro/%s/%s/modulation", area, tolower(cluster_name)),
target = sprintf("input/thermal/prepro/%s/%s/modulation", area, lower_cluster_name),
matrix = prepro_modulation
)
api_command_register(cmd, opts = opts)
Expand All @@ -175,7 +177,7 @@ editClusterRES <- function(area,
if (!identical(cluster_type, "renewables") && !is.null(prepro_data)) {
cmd <- api_command_generate(
action = "replace_matrix",
target = sprintf("input/thermal/prepro/%s/%s/data", area, tolower(cluster_name)),
target = sprintf("input/thermal/prepro/%s/%s/data", area, lower_cluster_name),
matrix = prepro_data
)
api_command_register(cmd, opts = opts)
Expand All @@ -191,7 +193,7 @@ editClusterRES <- function(area,
currPath <- ifelse(identical(cluster_type, "renewables"), "input/renewables/series/%s/%s/series", "input/thermal/series/%s/%s/series")
cmd <- api_command_generate(
action = "replace_matrix",
target = sprintf(currPath, area, tolower(cluster_name)),
target = sprintf(currPath, area, lower_cluster_name),
matrix = time_series
)
api_command_register(cmd, opts = opts)
Expand All @@ -210,14 +212,14 @@ editClusterRES <- function(area,


# path to ini file
path_clusters_ini <- file.path(inputPath, cluster_type, "clusters", tolower(area), "list.ini")
path_clusters_ini <- file.path(inputPath, cluster_type, "clusters", area, "list.ini")
if (!file.exists(path_clusters_ini))
stop("'", cluster_name, "' in area '", area, "' doesn't seems to exist.")

# read previous content of ini
previous_params <- readIniFile(file = path_clusters_ini)

if (!tolower(cluster_name) %in% tolower(names(previous_params))){
if (!lower_cluster_name %in% tolower(names(previous_params))){
stop(
"'", cluster_name, "' doesn't exist, it can't be edited. You can create cluster with createCluster().",
call. = FALSE
Expand All @@ -238,23 +240,23 @@ editClusterRES <- function(area,
# datas associated with cluster

if (!is.null(time_series)) {
utils::write.table(
fwrite(
x = time_series, row.names = FALSE, col.names = FALSE, sep = "\t",
file = file.path(inputPath, cluster_type, "series", tolower(area), tolower(cluster_name), "series.txt")
file = file.path(inputPath, cluster_type, "series", area, lower_cluster_name, "series.txt")
)
}

if (!is.null(prepro_data)) {
utils::write.table(
fwrite(
x = prepro_data, row.names = FALSE, col.names = FALSE, sep = "\t",
file = file.path(inputPath, cluster_type, "prepro", tolower(area), tolower(cluster_name), "data.txt")
file = file.path(inputPath, cluster_type, "prepro", area, lower_cluster_name, "data.txt")
)
}

if (!is.null(prepro_modulation)) {
utils::write.table(
fwrite(
x = prepro_modulation, row.names = FALSE, col.names = FALSE, sep = "\t",
file = file.path(inputPath, cluster_type, "prepro", tolower(area), tolower(cluster_name), "modulation.txt")
file = file.path(inputPath, cluster_type, "prepro", area, lower_cluster_name, "modulation.txt")
)
}

Expand Down

0 comments on commit 21345c0

Please sign in to comment.