Skip to content

Commit b85dbaf

Browse files
New function read_storages_constraints() to read properties and TS constraints from a st-storage
1 parent 53d02ae commit b85dbaf

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

R/read_st_constraints.R

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#'
2+
#'
3+
#'
4+
#'
5+
# RULES :
6+
# read only written files values
7+
# no default values for TS => return data.table() empty
8+
read_storages_constraints <- function(opts=simOptions()){
9+
browser()
10+
11+
assertthat::assert_that(inherits(opts, "simOptions"))
12+
stopifnot(opts$antaresVersion>=920)
13+
14+
##
15+
# API bloc
16+
##
17+
if(is_api_study(opts = opts)){}
18+
19+
##
20+
# Desktop
21+
##
22+
path <- file.path(opts$inputPath,
23+
"st-storage",
24+
"constraints")
25+
26+
# scan current directory
27+
current_files <- data.table(
28+
full_path = list.files(path, recursive = TRUE, full.names = TRUE),
29+
content_data = list.files(path, recursive = TRUE))
30+
31+
# split and structure data
32+
splited_values <- strsplit(current_files$content_data, split = "/")
33+
34+
list_structured <- lapply(splited_values, function(x){
35+
df <- data.table(area=x[1],
36+
cluster_name=x[2],
37+
file=x[3])
38+
df
39+
})
40+
41+
df_structured <- rbindlist(list_structured)
42+
current_files$content_data <- NULL
43+
44+
df_structured <- cbind(current_files, df_structured)
45+
46+
# read properties
47+
df_prop_filtered <- df_structured[grep(pattern = ".ini", x = file)]
48+
49+
# TODO split/filter by area
50+
51+
list_prop <- lapply(seq(1, nrow(df_prop_filtered)), function(x){
52+
clust_name <- df_structured[x, cluster_name]
53+
area <- df_structured[x, area]
54+
55+
ll <- list(readIniFile(df_structured[x, full_path]))
56+
names(ll) <- clust_name
57+
ll
58+
})
59+
60+
names(list_prop) <- df_prop_filtered[, area]
61+
62+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
2+
3+
# v920 ----
4+
# st-storage ----
5+
6+
# build structure directory
7+
# input/st-storage/clusters/list.ini
8+
# input/st-storage/series/{area}/{cluster}/{ts.txt}
9+
10+
# input/st-storage/constraints/{area}/{cluster}/additional-constraints.ini
11+
# input/st-storage/constraints/{area}/{cluster}/rhs_{constaints}.txt
12+
13+
# mock study
14+
areas <- c("fr", "be")
15+
opts <- list(
16+
"studyPath" = tempdir(),
17+
"inputPath" = file.path(tempdir(), "input"),
18+
"typeLoad"= "txt",
19+
"areaList" = areas,
20+
"antaresVersion" = 920
21+
)
22+
23+
class(opts) <- c("simOptions")
24+
25+
# properties
26+
fr <- c(
27+
"[withdrawal-1]",
28+
"variable = withdrawal",
29+
"operator = equal",
30+
"hours = [1,3,5], [120,121,122,123,124,125,126,127,128]",
31+
"",
32+
"[withdrawal-2]",
33+
"variable = withdrawal",
34+
"operator = equal",
35+
"hours = [1,3,5], [120,121,122,123,124]"
36+
)
37+
38+
be <- c(
39+
"[netting-1]",
40+
"variable = netting",
41+
"operator = less",
42+
"hours = [1, 168]",
43+
"",
44+
"[netting-2]",
45+
"variable = netting",
46+
"operator = less",
47+
"hours = [1, 167,168,169]"
48+
)
49+
50+
list_properties <- list(fr, be)
51+
52+
# TS (8760, 1 column)
53+
TS_VALUE <- matrix(2, 8760)
54+
55+
# create dir with properties
56+
cluster_names <- c("storage_1", "storage_2")
57+
comb <- merge(areas, cluster_names)
58+
59+
dir_path <- file.path(tempdir(),
60+
"input",
61+
"st-storage",
62+
"constraints",
63+
areas,
64+
paste0(areas, "_", cluster_names))
65+
66+
# dir_path <- file.path(tempdir(),
67+
# "st-storage",
68+
# "constraints",
69+
# areas,
70+
# paste0(comb$x, "_", comb$y))
71+
72+
lapply(dir_path, dir.create, recursive = TRUE, showWarnings = FALSE)
73+
74+
75+
# write properties
76+
lapply(seq(1, length(list_properties)), function(x){
77+
writeLines(list_properties[[x]],
78+
file.path(dir_path[x], "additional-constraints.ini"))
79+
})
80+
81+
# write TS
82+
names_constraints_1 <- c("withdrawal-1", "withdrawal-2")
83+
names_constraints_2 <- c("netting-1", "netting-2")
84+
85+
lapply(names_constraints_1, function(x){
86+
write.table(TS_VALUE,
87+
file = file.path(dir_path[1],
88+
paste0("rhs_",
89+
x,
90+
".txt")),
91+
row.names = FALSE,
92+
col.names = FALSE)
93+
})
94+
95+
lapply(names_constraints_2, function(x){
96+
write.table(TS_VALUE,
97+
file = file.path(dir_path[2],
98+
paste0("rhs_",
99+
x,
100+
".txt")),
101+
row.names = FALSE,
102+
col.names = FALSE)
103+
})
104+
105+
106+
test_that("Read without dir 'constraints'",{
107+
108+
# when
109+
read_storages_constraints(opts = opts)
110+
})
111+
112+
113+

0 commit comments

Comments
 (0)