Skip to content

Commit 09d9b82

Browse files
authored
Merge pull request #167 from rte-antares-rpackage/dev_api_enr
Dev api enr
2 parents 3a41e75 + 36271ac commit 09d9b82

18 files changed

+1214
-622
lines changed

Diff for: DESCRIPTION

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ Imports:
3030
shiny,
3131
pbapply,
3232
doParallel,
33-
jsonlite
33+
jsonlite,
34+
httr,
35+
utils
3436
Suggests:
3537
rhdf5 (>= 2.24.0),
3638
testthat,

Diff for: NAMESPACE

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,21 @@ export(setAlias)
5252
export(setHvdcAreas)
5353
export(setRam)
5454
export(setSimulationPath)
55+
export(setSimulationPathAPI)
56+
export(setTimeoutAPI)
5557
export(showAliases)
5658
export(simOptions)
5759
export(viewAntares)
5860
export(writeAntaresH5)
5961
import(bit64)
6062
import(data.table)
63+
import(httr)
64+
import(jsonlite)
6165
import(parallel)
6266
import(plyr)
6367
importFrom(doParallel,registerDoParallel)
6468
importFrom(grDevices,col2rgb)
6569
importFrom(grDevices,rgb)
66-
importFrom(jsonlite,read_json)
6770
importFrom(methods,is)
6871
importFrom(pbapply,pblapply)
6972
importFrom(pbapply,pboptions)
@@ -75,6 +78,7 @@ importFrom(stats,setNames)
7578
importFrom(stats,weighted.mean)
7679
importFrom(stringr,str_match)
7780
importFrom(stringr,str_replace)
81+
importFrom(utils,URLencode)
7882
importFrom(utils,View)
7983
importFrom(utils,read.table)
8084
importFrom(utils,setTxtProgressBar)

Diff for: R/api.R

-20
This file was deleted.

Diff for: R/importInput.R

+63-42
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#'
3535
.importInputTS <- function(area, timeStep, opts, fileNamePattern, colnames,
3636
inputTimeStep, fun = "sum", type = "simple", colSelect = NULL, ...) {
37-
37+
3838
path <- file.path(opts$inputPath, sprintf(fileNamePattern, area))
3939

4040
# if(!is.null(colSelect)){
@@ -48,48 +48,52 @@
4848
daily=range(.getTimeId(opts$timeIdMin:opts$timeIdMax, "daily", opts)),
4949
monthly=range(.getTimeId(opts$timeIdMin:opts$timeIdMax, "monthly", opts)))
5050

51-
if(opts$typeLoad == "api"){
52-
path <- .changeNameInput(path, opts)
53-
}
54-
5551

56-
57-
5852
if (opts$typeLoad == 'api' || (file.exists(path) && !file.size(path) == 0)) {
5953
if(is.null(colSelect))
6054
{
61-
inputTS <- fread(path, integer64 = "numeric", header = FALSE, showProgress = FALSE)
62-
}else{
63-
inputTS <- fread(path, integer64 = "numeric", header = FALSE, select = colSelect, showProgress = FALSE)
55+
# inputTS <- fread(path, integer64 = "numeric", header = FALSE, showProgress = FALSE)
56+
inputTS <- fread_antares(opts = opts, file = path, integer64 = "numeric", header = FALSE, showProgress = FALSE)
57+
} else {
58+
# inputTS <- fread(path, integer64 = "numeric", header = FALSE, select = colSelect, showProgress = FALSE)
59+
inputTS <- fread_antares(opts = opts, file = path, integer64 = "numeric", header = FALSE, select = colSelect, showProgress = FALSE)
6460
}
6561

66-
if (opts$typeLoad == 'api' || opts$antaresVersion < 650) {
67-
68-
inputTS <- .reorderInputTSHydroStorage(inputTS, path, opts)
62+
# browser()
63+
if (opts$antaresVersion < 650) {
64+
if(!is.null(inputTS)){
65+
inputTS <- .reorderInputTSHydroStorage(inputTS, path, opts)
66+
}
6967
}
7068

71-
inputTS <- inputTS[timeRange[1]:timeRange[2]]
69+
if(!is.null(inputTS)){
70+
inputTS <- inputTS[timeRange[1]:timeRange[2]]
71+
} else {
72+
if (type == "matrix") return(NULL)
73+
inputTS <- data.table(matrix(0L, timeRange[2] - timeRange[1] + 1,length(colnames)))
74+
}
7275
} else {
7376
if (type == "matrix") return(NULL)
7477
inputTS <- data.table(matrix(0L, timeRange[2] - timeRange[1] + 1,length(colnames)))
7578
}
7679

77-
# Add area and timeId columns and put it at the begining of the table
78-
inputTS$area <- area
79-
inputTS$timeId <- timeRange[1]:timeRange[2]
80-
.reorderCols(inputTS)
81-
82-
inputTS <- changeTimeStep(inputTS, timeStep, inputTimeStep, fun = fun, opts = opts)
83-
84-
# If the data is a matrix of time series melt the data
85-
if (type == "matrix") {
86-
colnames <- c("tsId", colnames)
87-
inputTS <- melt(inputTS, id.vars = c("area", "timeId"))
88-
inputTS$variable <- as.integer(substring(inputTS$variable, 2))
80+
if(!is.null(inputTS)){
81+
# Add area and timeId columns and put it at the begining of the table
82+
inputTS$area <- area
83+
inputTS$timeId <- timeRange[1]:timeRange[2]
84+
.reorderCols(inputTS)
85+
86+
inputTS <- changeTimeStep(inputTS, timeStep, inputTimeStep, fun = fun, opts = opts)
87+
88+
# If the data is a matrix of time series melt the data
89+
if (type == "matrix") {
90+
colnames <- c("tsId", colnames)
91+
inputTS <- melt(inputTS, id.vars = c("area", "timeId"))
92+
inputTS$variable <- as.integer(substring(inputTS$variable, 2))
93+
}
94+
95+
setnames(inputTS, names(inputTS), c("area", "timeId", colnames))
8996
}
90-
91-
setnames(inputTS, names(inputTS), c("area", "timeId", colnames))
92-
9397
inputTS
9498
}
9599

@@ -101,8 +105,13 @@
101105
.importThermalAvailabilities <- function(area, timeStep, opts, ...) {
102106
if (!area %in% opts$areasWithClusters) return(NULL)
103107

104-
clusters <- list.files(file.path(opts$inputPath, "thermal/series", area))
105-
108+
if(!"api" %in% opts$typeLoad){
109+
clusters <- list.files(file.path(opts$inputPath, "thermal/series", area))
110+
} else {
111+
clusters <- names(read_secure_json(file.path(opts$inputPath, "thermal/series", area),
112+
token = opts$token, timeout = opts$timeout))
113+
}
114+
106115
ldply(clusters, function(cl) {
107116
filePattern <- sprintf("%s/%s/%%s/series.txt", "thermal/series", area)
108117
res <- .importInputTS(cl, timeStep, opts, filePattern, "ThermalAvailabilities",
@@ -119,10 +128,16 @@
119128
}
120129

121130
.importResProduction <- function(area, timeStep, opts, ...) {
131+
122132
if (!area %in% opts$areasWithResClusters) return(NULL)
123133

124-
clusters <- list.files(file.path(opts$inputPath, "renewables/series", area))
125-
134+
if(!"api" %in% opts$typeLoad){
135+
clusters <- list.files(file.path(opts$inputPath, "renewables/series", area))
136+
} else {
137+
clusters <- names(read_secure_json(file.path(opts$inputPath, "renewables/series", area),
138+
token = opts$token, timeout = opts$timeout))
139+
}
140+
126141
ldply(clusters, function(cl) {
127142
filePattern <- sprintf("%s/%s/%%s/series.txt", "renewables/series", area)
128143
res <- .importInputTS(cl, timeStep, opts, filePattern, "production",
@@ -147,7 +162,7 @@
147162
.importInputTS(area, timeStep, opts, "hydro/series/%s/mod.txt", "hydroStorage",
148163
inputTimeStep = inputTimeStepV, type = "matrix")
149164
}
150-
165+
151166
.importHydroStorageMaxPower <- function(area, timeStep, opts, unselect = NULL, ...) {
152167

153168
unselect = unselect$areas
@@ -267,10 +282,14 @@
267282
unselect <- unselect$areas
268283
path <- file.path(opts$inputPath, "thermal/prepro", area)
269284

270-
clusters <- list.files(path)
285+
if(!"api" %in% opts$typeLoad){
286+
clusters <- list.files(path)
287+
} else {
288+
clusters <- names(read_secure_json(path, token = opts$token, timeout = opts$timeout))
289+
}
271290

272291
beginName <- c("marginalCostModulation", "marketBidModulation",
273-
"capacityModulation", "minGenModulation")
292+
"capacityModulation", "minGenModulation")
274293
if(!is.null(unselect)){
275294
colSelect <- which(!beginName%in%unselect)
276295
names <- beginName[colSelect]
@@ -283,9 +302,11 @@
283302
res <- ldply(clusters, function(cl) {
284303
if(is.null(colSelect))
285304
{
286-
modulation <- fread(file.path(path, cl, "modulation.txt"), colClasses = "numeric")
305+
# modulation <- fread(file.path(path, cl, "modulation.txt"), colClasses = "numeric")
306+
modulation <- fread_antares(opts = opts, file = file.path(path, cl, "modulation.txt"), colClasses = "numeric")
287307
}else{
288-
modulation <- fread(file.path(path, cl, "modulation.txt"), select = colSelect, colClasses = "numeric")
308+
# modulation <- fread(file.path(path, cl, "modulation.txt"), select = colSelect, colClasses = "numeric")
309+
modulation <- fread_antares(opts = opts, file = file.path(path, cl, "modulation.txt"), select = colSelect, colClasses = "numeric")
289310
}
290311

291312
setnames(modulation,
@@ -305,7 +326,7 @@
305326
})
306327
}
307328

308-
.changeNameInput <- function(path, opts){
309-
out <- sub(pattern = "studies", "file", path)
310-
out <- gsub(" ", "%20", out)
311-
}
329+
# .changeNameInput <- function(path, opts){
330+
# out <- sub(pattern = "studies", "file", path)
331+
# out <- gsub(" ", "%20", out)
332+
# }

0 commit comments

Comments
 (0)