Skip to content

Commit

Permalink
resolve conflicts master
Browse files Browse the repository at this point in the history
  • Loading branch information
BERTHET Clement (Externe) committed Oct 31, 2023
2 parents 51a19a2 + 918bc0d commit fd9377a
Show file tree
Hide file tree
Showing 100 changed files with 1,016 additions and 507 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: antaresRead
Type: Package
Title: Import, Manipulate and Explore the Results of an 'Antares' Simulation
Version: 2.5.2
Version: 2.6.1
Authors@R: c(
person("Tatiana", "Vargas", email = "[email protected]", role = c("aut", "cre")),
person("Jalal-Edine", "ZAWAM", role = "aut"),
Expand Down Expand Up @@ -48,6 +48,7 @@ Suggests:
parallel,
htmltools
RoxygenNote: 7.2.2
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
Encoding: UTF-8
biocViews: Infrastructure, DataImport
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export(readAntaresClusters)
export(readBindingConstraints)
export(readClusterDesc)
export(readClusterResDesc)
export(readClusterSTDesc)
export(readDigestFile)
export(readIni)
export(readIniAPI)
Expand Down
39 changes: 35 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
> Copyright © 2016 RTE Réseau de transport d’électricité

# antaresRead 2.5.3
# antaresRead 2.7.0

### Breaking changes (Antares v8.7) :

* Existing function `readBindingConstraints()` (cf. Antares v8.7 changelog)

# antaresRead 2.5.2

BREAKING CHANGES:
# antaresRead 2.6.1 (devlopment)

BUGFIXES :

* `setSimulationPathAPI()` :
- returns an API exception if the requested study ID is incorrect
- `simulation` the simulation parameter works with negative values within the limit of the number of simulations
* correction in `readClusterDesc()` calls to add "opts"


# antaresRead 2.6.0

NEW FEATURES (Antares v8.6, cf. Antares v8.6 changelog) :

* `readClusterSTDesc()` read "short-term storage" clusters parameters (input files of an antares study)


BREAKING CHANGES (Antares v8.6) :

* `readInputTS()` is now compatible to read time series with :
- "short-term storage"
- "mingen" (pmin hydro value)
* `setSimulationPath()` has new parameter `areasWithSTClusters` (name of area with "st-storage" cluster)


BUGFIXES :

* `setSimulationPathAPI` generate new global parameter `sleep` to add timer to API request
* Correction of `.importOutput()` to use `readAntares()` with `parallel == TRUE` in shiny application
* `setSimulationPathAPI()` delete a redundant API request
* `readClusterDesc()` minor fix in API mode + fix if no cluster exists => return specific error message
* `readIniAPI()` read well file `generaldata` for sections "playlist" and "variables selection"


DATA :

* A test study in tar.gz format is available in version `v8.6.0`


# antaresRead 2.5.1
Expand Down
19 changes: 17 additions & 2 deletions R/API-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,16 @@ api_get <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
#fix for skipping 404 when some output is missing
url_elements <- strsplit(result$url, "%2F")[[1]]
condition_status_check <- !(!is.na(url_elements[4]) & url_elements[4] %in% c("economy","adequacy") & result$status_code == 404)
if (condition_status_check) stop_for_status(result) else warn_for_status(result)
if(condition_status_check){
mess_error <- content(result)
if(!is.null(names(mess_error)))
mess_error <- paste0("\n[Description] : ", mess_error$description,
"\n[Exception] : ", mess_error$exception)
else
mess_error <- NULL
stop_for_status(result, task = mess_error)
}else
warn_for_status(result)
content(result)
}

Expand Down Expand Up @@ -90,7 +99,13 @@ api_post <- function(opts, endpoint, ..., default_endpoint = "v1/studies") {
config = config,
...
)
stop_for_status(result)
api_content <- content(result)
if(!is.null(names(api_content)))
api_content <- paste0("\n[Description] : ", api_content$description,
"\n[Exception] : ", api_content$exception)
else
api_content <- NULL
stop_for_status(result, task = api_content)
content(result)
}

Expand Down
2 changes: 1 addition & 1 deletion R/giveSize.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ setRam <- function(x){
clusWithData <- data.table()
if(!is.null(clusters))
{
clusWithData <- tryCatch(readClusterDesc(), error = function(e) data.table())
clusWithData <- tryCatch(readClusterDesc(opts = opts), error = function(e) data.table())
if("all" %in% clusters){
enabled <- TRUE
if("enabled" %in% names(clusWithData))
Expand Down
97 changes: 97 additions & 0 deletions R/importInput.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
inputTimeStep = "hourly", type = "matrix")
}

# "mingen" (v860)
.importmingen <- function(area, timeStep, opts, ...){
.importInputTS(area, timeStep, opts, "hydro/series/%s/mingen.txt", "mingen",
inputTimeStep = "hourly", type = "matrix")
}

.importHydroStorageInput <- function(area, timeStep, opts, ...) {
inputTimeStepV <- ifelse(opts$antaresVersion >= 650, yes = "daily", no = "monthly")
.importInputTS(area, timeStep, opts, "hydro/series/%s/mod.txt", "hydroStorage",
Expand Down Expand Up @@ -421,3 +427,94 @@
# out <- gsub(" ", "%20", out)
# }


# "st-storage" (v860)
.importSTStorage <- function(area, timeStep, opts, ...){

if (!area %in% opts$areasWithSTClusters)
return(NULL)

if(!"api" %in% opts$typeLoad){
clusters <- list.files(
file.path(opts$inputPath,
"st-storage/series",
area)
)

# "st-storage" have 5 txt files output for each cluster
list_names_txt_files <- unique(
list.files(
file.path(opts$inputPath,
"st-storage/series",
area,
clusters)
)
)

list_names_less_txt <- sub(pattern = ".txt",
replacement = "",
x = list_names_txt_files)

} else {
list_info_clusters <- read_secure_json(
file.path(opts$inputPath,
"st-storage/series",
area),
token = opts$token,
timeout = opts$timeout,
config = opts$httr_config
)

clusters <- names(list_info_clusters)

files_names <- names(list_info_clusters[[1]])

list_names_txt_files <- paste0(files_names, ".txt")
list_names_less_txt <- files_names
}

# read TS for every cluster
ldply(clusters, function(cl) {
pattern <- paste0("%s/%s/%%s/",
list_names_txt_files)
filePatterns <- sprintf(pattern,
"st-storage/series", area)

res <- lapply(filePatterns,
function(.x){
index_name_file <- which(filePatterns %in% .x)

res_temp <- .importInputTS(area= cl,
timeStep= timeStep,
opts= opts,
fileNamePattern= .x,
colnames= "st-storage",
inputTimeStep = "hourly",
type = "matrix")
res_temp$name_file <- list_names_less_txt[index_name_file]
res_temp
})

res <- rbindlist(res)

if (is.null(res))
return(NULL)

res$area <- area
res$cluster <- cl

setcolorder(res,
c("area",
"cluster",
"timeId",
setdiff(
names(res),
c("area",
"cluster",
"timeId")))
)
})

# added a column "name_file" to tag the file name

}
2 changes: 1 addition & 1 deletion R/importOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
res <- llply(
1:nrow(args),
function(i) {
incProgress(1/n, detail = paste0("Importing ", folder, " data"))
if(showProgress){ incProgress(1/n, detail = paste0("Importing ", folder, " data")) }
data <- NULL
try({
if (!sameNames) {
Expand Down
2 changes: 1 addition & 1 deletion R/readAntaresClusters.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ readAntaresClusters <- function(clusters, selected = c("production", "NP Cost",
stop("Cannot use 'readAntaresClusters' in 'Input' mode.")

##Add check control for all
allClusters <- readClusterDesc()[, c("area","cluster")]
allClusters <- readClusterDesc(opts = opts)[, c("area","cluster")]
ind_cluster <- which(tolower(allClusters$cluster) %in% .checkArg(tolower(clusters),
tolower(unique(allClusters$cluster)),
"clusters %s do not exist in the simulation."))
Expand Down
24 changes: 22 additions & 2 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#' \code{readClusterDesc} : read thermal clusters
#'
#' \code{readClusterResDesc} : read renewable clusters (Antares >= V8.1)
#'
#' \code{readClusterSTDesc} : read st-storage clusters (Antares >= V8.6)
#'
#' @examples
#'
Expand All @@ -43,6 +45,9 @@
#' # renewable
#' readClusterResDesc()
#'
#' # st-storage
#' readClusterSTDesc()
#'
#' # By default, the function reads cluster descriptions for the default study,
#' # but it is possible to specify another study with parameter "opts"
#' sim1 <- setSimulationPath()
Expand All @@ -69,10 +74,22 @@ readClusterResDesc <- function(opts = simOptions()) {
is.null(opts$parameters$`other preferences`$`renewable-generation-modelling`)){
stop("readClusterDesc is available only on studies with 'renewable-generation-modelling' = 'clusters' (and Antares >= 8.1)", call. = FALSE)
}
.readClusterDesc(opts = simOptions(), dir = "renewables/clusters")
.readClusterDesc(opts = opts, dir = "renewables/clusters")
}


#' @export
#'
#' @rdname readClusterDesc
readClusterSTDesc <- function(opts = simOptions()) {
if (opts$antaresVersion < 860) {
stop("readClusterSTDesc is available only on Antares >= 8.6)", call. = FALSE)
}
.readClusterDesc(opts = opts, dir = "st-storage/clusters")
}



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

Expand Down Expand Up @@ -104,7 +121,10 @@ readClusterResDesc <- function(opts = simOptions()) {
if(nrow(clusters)==0)return(NULL)
clusters$area <- Y1
clusters[, .SD, .SDcols = order(names(clusters))]
},jsoncld, names(jsoncld)), fill = TRUE)
},jsoncld, names(jsoncld), SIMPLIFY = FALSE), fill = TRUE)

if(length(res) == 0)
stop("Cannot find cluster description.", call. = FALSE)

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

Expand Down
48 changes: 47 additions & 1 deletion R/readIniFile.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,58 @@ readIniFile <- function(file, stringsAsFactors = FALSE) {
#' @export
#' @rdname read-ini
readIniAPI <- function(study_id, path, host, token = NULL) {
api_get(
api_get_ini_file <- api_get(
opts = list(host = host, token = token),
endpoint = paste0(study_id, "/raw"),
query = list(
path = path,
formatted = TRUE
)
)
# reformat list contains unnamed list
.format_list(api_get_ini_file)
}

# reformat list from JSON format
.format_list <- function(list_x){
# check list to find sub list
check_class_list <- lapply(list_x, function(x)
lapply(x, class) %in% "list")

is_true_list <- lapply(check_class_list, function(x)
any(x %in% TRUE))

index_true <- which(is_true_list %in% TRUE)

# reformat sub list
list_to_reformat <- list_x[index_true]

list_to_reformat <- lapply(list_to_reformat, function(x){
index_list <- which(
lapply(x, class) %in% "list")

# reformat only unnamed list
if(is.name(x[index_list]))
return(x)
else{
elements <- unlist(x[index_list], use.names = FALSE)
if(class(elements)%in%"character"){
elements <- paste("'", elements, "'", sep="", collapse=",")
elements <- paste0("[", elements, "]")
}else{
elements <- paste0(elements, collapse= ",")
elements <- paste0("[", elements, "]")
}
x[index_list] <- elements
x
}
})

# return original list
if(identical(index_true, integer(0)))
list_x
else
# return list reformated
append(list_x[-index_true],
list_to_reformat)
}
4 changes: 2 additions & 2 deletions R/readInputClusters.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ readInputThermal <- function(clusters = NULL, thermalModulation = FALSE, thermal
if (!foreach::getDoParRegistered()) stop("Parallelized importation impossible. Please register a parallel backend, for instance with function 'registerDoParallel'")
}

allAreasClusters <- readClusterDesc()[area %in% opts$areasWithClusters, c("area", "cluster")]
allAreasClusters <- readClusterDesc(opts = opts)[area %in% opts$areasWithClusters, c("area", "cluster")]
allClusters <- unique(allAreasClusters$cluster)
# Manage special value "all"
if(identical(clusters, "all")) clusters <- allClusters
Expand Down Expand Up @@ -139,7 +139,7 @@ readInputRES <- function(clusters = NULL, opts = simOptions(),
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")]
allAreasClusters <- readClusterResDesc(opts = opts)[area %in% opts$areasWithResClusters, c("area", "cluster")]
allClusters <- unique(allAreasClusters$cluster)
# Manage special value "all"
if(identical(clusters, "all")) clusters <- allClusters
Expand Down
Loading

0 comments on commit fd9377a

Please sign in to comment.