Skip to content

Commit

Permalink
Split .importOutputForClusters modulation vs no modulation, lowerize …
Browse files Browse the repository at this point in the history
…columns, integrate endpoint mc-ind
  • Loading branch information
kemihak committed Nov 12, 2024
1 parent 8378b64 commit 8318486
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 76 deletions.
198 changes: 123 additions & 75 deletions R/importOutput.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@
cols_to_integer <- intersect(cols_to_integer, res_cols)
res[,(cols_to_integer):=lapply(.SD, as.integer), .SDcols=cols_to_integer]

res[,(cols_to_factor):=lapply(.SD, tolower), .SDcols=cols_to_factor]

return(res)
}

Expand Down Expand Up @@ -421,6 +423,13 @@
}


#' Read output data for clusters if mustRun is disabled
#'
#' @return
#' a data.table
#'
#' @noRd
#'
.importOutputForClusters_wo_mustrun <- function(areas, timeStep, select = NULL, mcYears = NULL,
showProgress, opts, parallel){
if (is_api_study(opts)) {
Expand All @@ -446,11 +455,121 @@
}


#' Read output data for clusters if modulation is computed as inactive
#'
#' @return
#' a data.table
#'
#' @noRd
#'
.importOutputForClusters_wo_modulation <- function(areas, timeStep, select = NULL, mcYears = NULL,
showProgress, opts, reshapeFun, parallel) {

# We should not \o/
if (is_api_study(opts)) {
res <- .api_get_aggregate_areas(areas = areas,
timeStep = timeStep,
query_file = "details",
select = select,
mcYears = mcYears,
synthesis = FALSE,
opts = opts
)
res <- .format_api_aggregate_result(res)
} else {
res <- suppressWarnings(
suppressWarnings(
.importOutput("areas", "details", "area", areas, timeStep, NULL,
mcYears, showProgress, opts, reshapeFun, sameNames = FALSE,
objectDisplayName = "cluster", parallel = parallel)
)
)
}

res[, mustRunPartial := 0L]
}


#' Read output data for clusters if modulation is computed as active
#'
#' @return
#' a data.table
#'
#' @noRd
#'
.importOutputForClusters_w_modulation <- function(areas, timeStep, select = NULL, mcYears = NULL,
showProgress, opts, reshapeFun, parallel, mod, clusterDesc) {

if(timeStep!="hourly"){
warning('Hourly data will be imported to compute partial must run min(production_t, capacity * minGenModulation_t). These data will be aggregated at the desired `timeStep`. ')

#copy of warning in ChangeTimeStep
warning('Aggregation will be perform approximatively because optimization variables in ANTARES are doubles but ANTARES write only integers in TXT files, with this transformation we lose precision. If you want accurate data then you must import the corresponding data with `readAntares`')

messageWarningMcYears<-paste0("When mcYears is set to all or NULL : ", mcYears, " and timeStep is set to : " ,timeStep , " result for mustRun are not accurate. Hourly `synthetic` or `details` results will be aggregated at the desired `timeStep`. " )

if( is.null(mcYears) ){
warning(messageWarningMcYears, call. = FALSE)
}else if (is.character(mcYears)){
if (mcYears=="all"){
warning(messageWarningMcYears, call. = FALSE)
}
}else if (length(mcYears) > 1){
warning(messageWarningMcYears, call. = FALSE)
}
}

mod[is.na(minGenModulation), minGenModulation := 0]

.mergeByRef(mod, clusterDesc)
mod[, mustRunPartial := minGenModulation * capacity]

setkey(mod, area, cluster, timeId)

if (is_api_study(opts)) {
res <- .api_get_aggregate_areas(areas = areas,
timeStep = "hourly",
query_file = "details",
select = select,
mcYears = mcYears,
synthesis = FALSE,
opts = opts
)
res <- .format_api_aggregate_result(res)
mustRunPartial <- mod[J(res$area, res$cluster, res$timeId), mustRunPartial]
res[, mustRunPartial := pmin(production, mustRunPartial)]
res <- changeTimeStep(res, timeStep, "hourly", fun = "sum", opts = opts)
} else {
processFun <- function(x) {
x <- reshapeFun(x)
mustRunPartial <- mod[J(x$area, x$cluster, x$timeId), mustRunPartial]
x[, mustRunPartial := pmin(production, mustRunPartial)]
changeTimeStep(x, timeStep, "hourly", fun = "sum", opts = opts)
}
res <- suppressWarnings(
.importOutput("areas", "details", "area", areas, "hourly", NULL,
mcYears, showProgress, opts, processFun,
sameNames = FALSE, objectDisplayName = "cluster",
parallel = parallel)

)

}

return(res)
}


#' Read output data for clusters if mustRun is enabled
#'
#' @return
#' a data.table
#'
#' @noRd
#'
.importOutputForClusters_w_mustrun <- function(areas, timeStep, select = NULL, mcYears = NULL,
showProgress, opts, parallel) {

is_api <- is_api_study(opts)

reshapeFun <- function(x){
.reshape_details_file(x,file_type="details",opts=opts)
}
Expand Down Expand Up @@ -486,81 +605,10 @@
# Should we compute the partial must run ?
if (is.null(mod$minGenModulation) || all(is.na(mod$minGenModulation) | mod$minGenModulation == 0)) {
# We should not \o/
if (is_api) {
res <- .api_get_aggregate_areas(areas = areas,
timeStep = timeStep,
query_file = "details",
select = select,
mcYears = mcYears,
synthesis = FALSE,
opts = opts
)
res <- .format_api_aggregate_result(res)
} else {
res <- suppressWarnings(
suppressWarnings(
.importOutput("areas", "details", "area", areas, timeStep, NULL,
mcYears, showProgress, opts, reshapeFun, sameNames = FALSE,
objectDisplayName = "cluster", parallel = parallel)
)
)
}
res[, mustRunPartial := 0L]
res <- .importOutputForClusters_wo_modulation(areas, timeStep, select, mcYears, showProgress, opts, reshapeFun, parallel)
} else {

# Worst case ! We have to !
#
if(timeStep!="hourly"){
warning('Hourly data will be imported to compute partial must run min(production_t, capacity * minGenModulation_t). These data will be aggregated at the desired `timeStep`. ')

#copy of warning in ChangeTimeStep
warning('Aggregation will be perform approximatively because optimization variables in ANTARES are doubles but ANTARES write only integers in TXT files, with this transformation we lose precision. If you want accurate data then you must import the corresponding data with `readAntares`')

messageWarningMcYears<-paste0("When mcYears is set to all or NULL : ", mcYears, " and timeStep is set to : " ,timeStep , " result for mustRun are not accurate. Hourly `synthetic` or `details` results will be aggregated at the desired `timeStep`. " )

if( is.null(mcYears) ){
warning(messageWarningMcYears, call. = FALSE)
}else if (is.character(mcYears)){
if (mcYears=="all"){
warning(messageWarningMcYears, call. = FALSE)
}
}else if (length(mcYears) > 1){
warning(messageWarningMcYears, call. = FALSE)
}
}

mod[is.na(minGenModulation), minGenModulation := 0]

.mergeByRef(mod, clusterDesc)
mod[, mustRunPartial := minGenModulation * capacity]

setkey(mod, area, cluster, timeId)

if (is_api) {
res <- .api_get_aggregate_areas(areas = areas,
timeStep = "hourly",
query_file = "details",
select = select,
mcYears = mcYears,
synthesis = FALSE,
opts = opts
)
res <- .format_api_aggregate_result(res)
} else {
processFun <- function(x) {
x <- reshapeFun(x)
mustRunPartial <- mod[J(x$area, x$cluster, x$timeId), mustRunPartial]
x[, mustRunPartial := pmin(production, mustRunPartial)]
changeTimeStep(x, timeStep, "hourly", fun = "sum", opts = opts)
}
res <- suppressWarnings(
.importOutput("areas", "details", "area", areas, "hourly", NULL,
mcYears, showProgress, opts, processFun,
sameNames = FALSE, objectDisplayName = "cluster",
parallel = parallel)
)
}

res <- .importOutputForClusters_w_modulation(areas, timeStep, select, mcYears, showProgress, opts, reshapeFun, parallel, mod, clusterDesc)
}

.mergeByRef(res, clusterDesc[,.(area, cluster, `must-run`, `min-stable-power`)])
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ utils::globalVariables(
"Folder", "Mode", "Stats", "Name", "progNam", "mrgprice", "isLOLD_cum",
"...To", "upstream", "downstream", "LOLD", "LOLD_data", "LOLP", "warn_for_status",
"MRG. PRICE", "H. LEV", "V2", "V1", "size", "ORDINAL_POSITION_BY_TOPIC",
"DETAILS_FILES_TYPE","ANTARES_DISPLAYED_NAME")
"DETAILS_FILES_TYPE","ANTARES_DISPLAYED_NAME", "time")
)

## INPUT Properties REF ----
Expand Down

0 comments on commit 8318486

Please sign in to comment.