-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
#' Read Meteologica file | ||
#' | ||
#' @param path path to a directory containing forecast files or a single file. | ||
#' @param country filter files to read onlyone country | ||
#' | ||
#' @return a \code{data.table} | ||
#' @export | ||
#' | ||
#' @importFrom stringr str_which str_replace str_extract | ||
#' @importFrom data.table rbindlist | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' | ||
#' # one file | ||
#' one_file <- read_meteologica( | ||
#' path = "20180214/PhotovoltaicPower/country-type-code-code_000000_00.csv" | ||
#' ) | ||
#' | ||
#' # filter with a country | ||
#' country <- read_meteologica(path = "20180214/PhotovoltaicPower/", country = "France") | ||
#' | ||
#' | ||
#' # all files in subdir PhotovoltaicPower/ | ||
#' all_pp <- read_meteologica(path = "20180214/PhotovoltaicPower/") | ||
#' all_pp[, .N, by = list(type, country)] | ||
#' | ||
#' | ||
#' # all files in subdir 20180214/ | ||
#' all <- read_meteologica(path = "20180214/") | ||
#' all[, .N, by = list(type, country)] | ||
#' | ||
#' } | ||
read_meteologica <- function(path, country = NULL) { | ||
if (missing(path)) { | ||
path <- choose_path() | ||
} | ||
patterns <- c("PhotovoltaicPower", "WindPower", "PowerDemand") | ||
path <- select_file( | ||
path = path, | ||
pattern = paste(patterns, collapse = "|"), | ||
fileext = "\\.csv$", | ||
multiple = TRUE, | ||
recursive = TRUE, | ||
verbose = FALSE | ||
) | ||
if (!is.null(country)) { | ||
path <- path[str_which(string = tolower(path), pattern = tolower(country))] | ||
} | ||
res <- lapply( | ||
X = path, | ||
FUN = function(x) { | ||
message(paste("Reading file:", x)) | ||
dat <- read_meteologica_file(x) | ||
type <- str_which(string = x, pattern = patterns) | ||
type <- patterns[type] | ||
dat$type <- type | ||
country <- str_replace(string = x, pattern = ".*/", replacement = "") | ||
country <- str_extract(string = country, pattern = sprintf(".+(?=-%s)", type)) | ||
dat$country <- country | ||
dat | ||
} | ||
) | ||
rbindlist(l = res) | ||
} | ||
|
||
#' @importFrom stringr str_extract str_subset | ||
#' @importFrom data.table fread setnames := | ||
read_meteologica_file <- function(path) { | ||
dat <- fread(file = path, sep = ",", fill = TRUE) | ||
names_ <- names(dat) | ||
names_ <- str_subset(string = names_, pattern = "ENS") | ||
setnames( | ||
x = dat, | ||
old = names_, | ||
new = str_extract( | ||
string = names_, | ||
pattern = "ENS\\d{2}" | ||
) | ||
) | ||
setnames(x = dat, old = "From (UTC)", new = "datetime") | ||
dat <- dat[, .SD, .SDcols = c("datetime", sprintf("ENS%02d", 0:50))] | ||
dat <- dat[, datetime := as.POSIXct(datetime, tz = "Europe/Paris")] | ||
dat | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.