diff --git a/DESCRIPTION b/DESCRIPTION index e3dba3c..b7ea97d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: antaresThermalTS Title: Extended Shutdown Simulation with 'Antares' for Power Groups -Version: 0.0.4 +Version: 0.0.5 Authors@R: c( person("Victor", "Perrier", email = "victor.perrier@dreamRs.fr", role = c("aut", "cre")), person("Fabiola", "Aravena-Rojas", email = "fabiola.aravena-rojas@rte-france.com", role = c("aut")), diff --git a/NAMESPACE b/NAMESPACE index 1f4d3e7..4fceb9a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(create_clusters_edf) export(create_clusters_nuclear) export(create_clusters_other) export(mean_thermal_ts) +export(mean_thermal_ts2) export(msp_run_clusters) export(pcn_stop_clusters) export(read_calendar) diff --git a/R/mean_thermal_ts.R b/R/mean_thermal_ts.R index 39b861e..62ec4aa 100644 --- a/R/mean_thermal_ts.R +++ b/R/mean_thermal_ts.R @@ -55,3 +55,79 @@ mean_thermal_ts <- function(first_weekday = 1, opts = simOptions()) { } +#' Mean of thermal timeseries by weeks +#' +#' @param first_weekday The first day to use for starting a week, default to \code{1} (monday). +#' @param opts +#' List of simulation parameters returned by the function +#' \code{setSimulationPath} +#' +#' @return a \code{data.table}. +#' @export +#' +#' @importFrom antaresRead readClusterDesc +#' @importFrom lubridate wday hour isoweek +#' @importFrom data.table first := rbindlist +#' @importFrom anytime anydate +#' @importFrom progress progress_bar +#' +mean_thermal_ts2 <- function(first_weekday = 1, opts = simOptions()) { + clusters <- readClusterDesc(opts = opts) + clusters[, area := as.character(area)] + clusters[, cluster := as.character(cluster)] + clusters[, group := as.character(group)] + clusters <- clusters[area == "fr"] + pb <- progress_bar$new( + format = " Reading clusters time-series [:bar] :percent", + total = nrow(clusters), clear = FALSE + ) + ts_clus <- mapply( + FUN = function(area, cluster) { + pb$tick() + path <- file.path(opts$inputPath, "thermal/series", area, cluster, "series.txt") + if (file.size(path) == 0) { + return(NULL) + } + ts <- fread(file = path) + ts[, time := seq.POSIXt(from = opts$start, by = "hour", length.out = 8760)] + ts[, date := anytime::anydate(time)] + ts[, WED19 := FALSE] + ts[lubridate::wday(time, week_start = 1) == 3 & lubridate::hour(time) == 19, WED19 := TRUE] + ts <- melt( + data = ts, + id.vars = c("time", "date", "WED19"), + variable.factor = FALSE, + variable.name = "tsId", + value.name = "ThermalAvailabilities" + ) + ts <- ts[, list( + ThermalAvailabilities = mean(ThermalAvailabilities[WED19 == TRUE]) + ), by = date] + ts[, week := lubridate::wday(date, week_start = first_weekday)] + ts[week > 1, week := 0] + ts[, week := cumsum(week)] + ts[lubridate::wday(date, week_start = 1) == 1, nweek := lubridate::isoweek(date)] + ts[, nweek := nweek[!is.na(nweek)][1], by = week] + tsweek <- ts[, list( + ThermalAvailabilities = mean(ThermalAvailabilities, na.rm = TRUE), + date = first(date), + nweek = first(nweek), + n = .N + ), by = list(week)] + tsweek <- tsweek[n == 7] + tsweek[, n := NULL] + tsweek[, area := area] + tsweek[, cluster := cluster] + }, + area = clusters$area, + cluster = clusters$cluster, + SIMPLIFY = FALSE + ) + ts_clus <- rbindlist(ts_clus) + ts_clus <- merge( + x = ts_clus, + y = clusters[, list(area, cluster, group)], + by = c("area", "cluster") + ) + ts_clus[] +} diff --git a/man/mean_thermal_ts2.Rd b/man/mean_thermal_ts2.Rd new file mode 100644 index 0000000..8fca188 --- /dev/null +++ b/man/mean_thermal_ts2.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mean_thermal_ts.R +\name{mean_thermal_ts2} +\alias{mean_thermal_ts2} +\title{Mean of thermal timeseries by weeks} +\usage{ +mean_thermal_ts2(first_weekday = 1, opts = simOptions()) +} +\arguments{ +\item{first_weekday}{The first day to use for starting a week, default to \code{1} (monday).} + +\item{opts}{List of simulation parameters returned by the function +\code{setSimulationPath}} +} +\value{ +a \code{data.table}. +} +\description{ +Mean of thermal timeseries by weeks +}