Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New argument fun in readInputTS and readAntares #138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions R/importInput.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

if(is.null(colSelect))
{
inputTS <- fread(path, integer64 = "numeric", header = FALSE)
inputTS <- fread(path, integer64 = "numeric", header = FALSE)
}else{
inputTS <- fread(path, integer64 = "numeric", header = FALSE, select = colSelect)
}
Expand All @@ -72,7 +72,7 @@
inputTS$timeId <- timeRange[1]:timeRange[2]
.reorderCols(inputTS)

inputTS <- changeTimeStep(inputTS, timeStep, inputTimeStep, fun = fun, opts = opts)
inputTS <- changeTimeStep(inputTS, timeStep, inputTimeStep, fun = get("fun", envir = parent.frame()), opts = opts)

# If the data is a matrix of time series melt the data
if (type == "matrix") {
Expand All @@ -86,20 +86,20 @@
inputTS
}

.importLoad <- function(area, timeStep, opts, ...) {
.importLoad <- function(area, timeStep, opts, fun, ...) {
.importInputTS(area, timeStep, opts, "load/series/load_%s.txt", "load",
inputTimeStep = "hourly", type = "matrix")
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), type = "matrix")
}

.importThermalAvailabilities <- function(area, timeStep, opts, ...) {
.importThermalAvailabilities <- function(area, timeStep, opts, fun, ...) {
if (!area %in% opts$areasWithClusters) return(NULL)

clusters <- list.files(file.path(opts$inputPath, "thermal/series", area))

ldply(clusters, function(cl) {
filePattern <- sprintf("%s/%s/%%s/series.txt", "thermal/series", area)
res <- .importInputTS(cl, timeStep, opts, filePattern, "ThermalAvailabilities",
inputTimeStep = "hourly", type = "matrix")
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), type = "matrix")

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

Expand All @@ -111,18 +111,18 @@

}

.importROR <- function(area, timeStep, opts, ...) {
.importROR <- function(area, timeStep, opts, fun, ...) {
.importInputTS(area, timeStep, opts, "hydro/series/%s/ror.txt", "ror",
inputTimeStep = "hourly", type = "matrix")
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), type = "matrix")
}

.importHydroStorageInput <- function(area, timeStep, opts, ...) {
.importHydroStorageInput <- function(area, timeStep, opts, fun, ...) {
inputTimeStepV <- ifelse(opts$antaresVersion >= 650, yes = "hourly", no = "monthly")
.importInputTS(area, timeStep, opts, "hydro/series/%s/mod.txt", "hydroStorage",
inputTimeStep = inputTimeStepV, type = "matrix")
inputTimeStep = inputTimeStepV, fun = get("fun", envir = parent.frame()), type = "matrix")
}
.importHydroStorageMaxPower <- function(area, timeStep, opts, unselect = NULL, ...) {

.importHydroStorageMaxPower <- function(area, timeStep, opts, unselect = NULL, fun, ...) {

unselect = unselect$areas
if (opts$antaresVersion >= 650) {
Expand All @@ -146,17 +146,17 @@

}

.importWind <- function(area, timeStep, opts, ...) {
.importWind <- function(area, timeStep, opts, fun, ...) {
.importInputTS(area, timeStep, opts, "wind/series/wind_%s.txt", "wind",
inputTimeStep = "hourly", type = "matrix")
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), type = "matrix")
}

.importSolar <- function(area, timeStep, opts, ...) {
.importSolar <- function(area, timeStep, opts, fun, ...) {
.importInputTS(area, timeStep, opts, "solar/series/solar_%s.txt", "solar",
inputTimeStep = "hourly", type = "matrix")
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), type = "matrix")
}

.importMisc <- function(area, timeStep, opts, colSelect = NULL, names = NULL, unselect = NULL, ...) {
.importMisc <- function(area, timeStep, opts, colSelect = NULL, names = NULL, unselect = NULL, fun, ...) {

unselect = unselect$areas
if(!is.null(unselect)){
Expand All @@ -173,11 +173,11 @@
}
.importInputTS(area, timeStep, opts, "misc-gen/miscgen-%s.txt",
colnames=names,
inputTimeStep = "hourly", colSelect = colSelect)
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), colSelect = colSelect)

}

.importReserves <- function(area, timeStep, opts, colSelect = NULL, names = NULL, unselect =NULL, ...) {
.importReserves <- function(area, timeStep, opts, colSelect = NULL, names = NULL, unselect =NULL, fun, ...) {
beginName <- c("primaryRes", "strategicRes", "DSM", "dayAhead")
unselect = unselect$areas

Expand All @@ -191,11 +191,11 @@

.importInputTS(area, timeStep, opts, "reserves/%s.txt",
colnames=names,
inputTimeStep = "hourly", colSelect = colSelect)
inputTimeStep = "hourly", fun = get("fun", envir = parent.frame()), colSelect = colSelect)

}

.importLinkCapacity <- function(link, timeStep, opts, unselect = NULL, ...) {
.importLinkCapacity <- function(link, timeStep, opts, unselect = NULL, fun, ...) {

areas <- strsplit(link, " - ")[[1]]

Expand Down Expand Up @@ -242,7 +242,7 @@
clusters <- list.files(path)

beginName <- c("marginalCostModulation", "marketBidModulation",
"capacityModulation", "minGenModulation")
"capacityModulation", "minGenModulation")
if(!is.null(unselect)){
colSelect <- which(!beginName%in%unselect)
names <- beginName[colSelect]
Expand All @@ -255,7 +255,7 @@
res <- ldply(clusters, function(cl) {
if(is.null(colSelect))
{
modulation <- fread(file.path(path, cl, "modulation.txt"), colClasses = "numeric")
modulation <- fread(file.path(path, cl, "modulation.txt"), colClasses = "numeric")
}else{
modulation <- fread(file.path(path, cl, "modulation.txt"), select = colSelect, colClasses = "numeric")
}
Expand Down
105 changes: 56 additions & 49 deletions R/readAntares.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
#' Should reserve be imported ?
#' @param linkCapacity
#' Should link capacities be imported ?
#' @param fun
#' Aggregation function, among c("sum", "mean", "min", "max"). NB: \code{mean}
#' is always used for \code{ThermalModulation}, \code{LinkCapacity} and
#' \code{HydroStorageMaxPower}.
#' @param mustRun
#' Should must run productions be added to the result? If TRUE,
#' then four columns are added: \code{mustRun} contains the production of
Expand Down Expand Up @@ -203,6 +207,7 @@ readAntares <- function(areas = NULL, links = NULL, clusters = NULL,
select = NULL,
mcYears = NULL,
timeStep = c("hourly", "daily", "weekly", "monthly", "annual"),
fun = "sum",
opts = simOptions(),
parallel = FALSE, simplify = TRUE, showProgress = TRUE) {

Expand Down Expand Up @@ -251,23 +256,23 @@ readAntares <- function(areas = NULL, links = NULL, clusters = NULL,

if(.requireRhdf5_Antares(stopP = FALSE)){
return(.h5ReadAntares(path = opts$h5path,
areas = areas,
links = links,
clusters = clusters,
districts = districts,
misc = misc,
thermalAvailabilities = thermalAvailabilities,
hydroStorage = hydroStorage,
hydroStorageMaxPower = hydroStorageMaxPower,
reserve = reserve,
linkCapacity = linkCapacity,
mustRun = mustRun,
thermalModulation = thermalModulation,
select = select,
mcYears = mcYears,
timeStep = timeStep[1],
showProgress = showProgress,
simplify = simplify))
areas = areas,
links = links,
clusters = clusters,
districts = districts,
misc = misc,
thermalAvailabilities = thermalAvailabilities,
hydroStorage = hydroStorage,
hydroStorageMaxPower = hydroStorageMaxPower,
reserve = reserve,
linkCapacity = linkCapacity,
mustRun = mustRun,
thermalModulation = thermalModulation,
select = select,
mcYears = mcYears,
timeStep = timeStep[1],
showProgress = showProgress,
simplify = simplify))
} else {
stop(rhdf5_message)
}
Expand Down Expand Up @@ -358,14 +363,16 @@ readAntares <- function(areas = NULL, links = NULL, clusters = NULL,
colSelect = NULL
res <- list()
# local function that add a type of output to the object "res"
.addOutputToRes <- function(name, ids, outputFun, select, ts = timeStep) {
.addOutputToRes <- function(name, ids, outputFun, select, ts = timeStep,
fun = get("fun", envir = parent.frame())) {
if (is.null(ids) | length(ids) == 0) return(NULL)

if (showProgress) cat(sprintf("Importing %s\n", name))

tmp <- suppressWarnings(
llply(ids, function(x, ...) outputFun(x, ...),
synthesis=synthesis, mcYears=mcYears,timeStep=ts,
fun = get("fun", envir = parent.frame()),
opts=opts, select = select,
colSelect = colSelect,
names = names,
Expand Down Expand Up @@ -414,47 +421,47 @@ readAntares <- function(areas = NULL, links = NULL, clusters = NULL,
}
}
else{
res$clusters <- .importOutputForClusters(clustersAugmented, timeStep, NULL, mcYears,
showProgress, opts, mustRun = TRUE, parallel = parallel)

if (!is.null(res$areas)) {
tmp <- copy(res$clusters)
tmp[, cluster := NULL]
tmp <- tmp[,.(thermalPmin = sum(thermalPmin),
mustRun = sum(mustRun),
mustRunPartial = sum(mustRunPartial),
mustRunTotal = sum(mustRunTotal)),
keyby = c(.idCols(tmp))]
res$areas <- .mergeByRef(res$areas, tmp)
res$clusters <- .importOutputForClusters(clustersAugmented, timeStep, NULL, mcYears,
showProgress, opts, mustRun = TRUE, parallel = parallel)

res$areas[is.na(mustRunTotal), c("thermalPmin","mustRun", "mustRunPartial", "mustRunTotal") := 0]
}

if (!is.null(districts)) {
if(nrow(districts ) > 0)
{
if (!is.null(res$areas)) {
tmp <- copy(res$clusters)
tmp <- merge(tmp, districts, by = "area", allow.cartesian = TRUE)
tmp[, area := NULL]
tmp[, cluster := NULL]
tmp <- tmp[,.(thermalPmin = sum(thermalPmin),
mustRun = sum(mustRun),
mustRunPartial = sum(mustRunPartial),
mustRunTotal = sum(mustRunTotal)),
keyby = c(.idCols(tmp))]
res$districts <- .mergeByRef(res$districts, tmp)
res$districts[is.na(mustRunTotal), c("thermalPmin", "mustRun", "mustRunPartial", "mustRunTotal") := 0]
res$areas <- .mergeByRef(res$areas, tmp)

res$areas[is.na(mustRunTotal), c("thermalPmin","mustRun", "mustRunPartial", "mustRunTotal") := 0]
}

if (!is.null(districts)) {
if(nrow(districts ) > 0)
{
tmp <- copy(res$clusters)
tmp <- merge(tmp, districts, by = "area", allow.cartesian = TRUE)
tmp[, area := NULL]
tmp[, cluster := NULL]
tmp <- tmp[,.(thermalPmin = sum(thermalPmin),
mustRun = sum(mustRun),
mustRunPartial = sum(mustRunPartial),
mustRunTotal = sum(mustRunTotal)),
keyby = c(.idCols(tmp))]
res$districts <- .mergeByRef(res$districts, tmp)
res$districts[is.na(mustRunTotal), c("thermalPmin", "mustRun", "mustRunPartial", "mustRunTotal") := 0]
}
}

# Suppress that has not been asked
if (is.null(clusters)) {
res$clusters <- NULL
} else if (length(clustersAugmented) > length(clusters)) {
res$clusters <- res$clusters[area %in% clusters]
}

}

# Suppress that has not been asked
if (is.null(clusters)) {
res$clusters <- NULL
} else if (length(clustersAugmented) > length(clusters)) {
res$clusters <- res$clusters[area %in% clusters]
}

}
}

# Add input time series
Expand Down
7 changes: 5 additions & 2 deletions R/readInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#' vector of areas names for which reserve time series must be read
#' @param linkCapacity
#' vector of links names for which links characteristics time series must be read
#' @param fun
#' agregation function, among c("sum", "mean", "min", "max")
#' @inheritParams readAntares
#'
#' @return
Expand Down Expand Up @@ -74,6 +76,7 @@ readInputTS <- function(load = NULL, thermalAvailabilities = NULL, ror = NULL,
wind = NULL, solar = NULL, misc = NULL,
reserve = NULL, linkCapacity = NULL, opts = simOptions(),
timeStep = c("hourly", "daily", "weekly", "monthly", "annual"),
fun = "sum",
simplify = TRUE, parallel = FALSE,
showProgress = TRUE) {

Expand All @@ -100,12 +103,12 @@ readInputTS <- function(load = NULL, thermalAvailabilities = NULL, ror = NULL,
res <- list() # Object the function will return

# local function that add a type of output to the object "res"
.addOutputToRes <- function(name, ids, outputFun, ts = timeStep) {
.addOutputToRes <- function(name, ids, outputFun, ts = timeStep, fun = get("fun", envir = parent.frame())) {
if (is.null(ids) | length(ids) == 0) return(NULL)
if (showProgress) cat(sprintf("Importing %s\n", name))

tmp <- llply(ids, function(x, ...) outputFun(x, ...),
timeStep=ts, opts=opts,
timeStep=ts, fun = get("fun", envir = parent.frame()), opts=opts,
.progress = ifelse(showProgress, "text", "none"),
.parallel = parallel,
.paropts = list(.packages="antaresRead"))
Expand Down
6 changes: 4 additions & 2 deletions man/readAntares.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/readInputTS.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-readInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ test_that("Wind importation works", {
expect_is(input, "antaresDataTable")
expect_gt(nrow(input), 0)
expect_equal(nrow(input) %% (24 * 7 * nweeks), 0)

input_mean_hourly <- readInputTS(wind = "all", fun = "mean", showProgress = FALSE)
expect_identical(input, input_mean_hourly)

input_mean_weekly <- suppressWarnings(
readInputTS(wind = "all", timeStep = "weekly",
fun = "mean", showProgress = FALSE)
)
expect_false(identical(input, input_mean_weekly))
expect_gt(nrow(input_mean_weekly), 0)
expect_equal(nrow(input) %% nweeks, 0)
})

test_that("Solar importation works", {
Expand Down