Skip to content

Commit

Permalink
read meteologica
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Feb 14, 2018
1 parent 5800aa9 commit fc587bf
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(read_forecast_power)
export(read_forfait_oa)
export(read_hydro_usine)
export(read_hydro_vallee)
export(read_meteologica)
export(read_planning)
export(read_planning_edf)
export(read_thermal_tranche)
Expand All @@ -34,6 +35,9 @@ importFrom(readxl,read_excel)
importFrom(stats,setNames)
importFrom(stringr,str_detect)
importFrom(stringr,str_extract)
importFrom(stringr,str_replace)
importFrom(stringr,str_subset)
importFrom(stringr,str_which)
importFrom(utils,choose.dir)
importFrom(xml2,read_xml)
importFrom(xml2,xml_attr)
Expand Down
86 changes: 86 additions & 0 deletions R/read-meteologica.R
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
}
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ choose_path <- function() {
}


select_file <- function(path, pattern = "Hydrauliques", fileext = "\\.xml$", multiple = FALSE, verbose = TRUE) {
select_file <- function(path, pattern = "Hydrauliques", fileext = "\\.xml$", multiple = FALSE, recursive = FALSE, verbose = TRUE) {
if (dir.exists(path)) {
path <- list.files(path = path, pattern = fileext, full.names = TRUE)
path <- list.files(path = path, pattern = fileext, full.names = TRUE, recursive = recursive)
path <- path[!grepl(pattern = "/~\\$", x = path)]
path <- grep(pattern = pattern, x = path, value = TRUE)
if (length(path) < 1)
Expand Down
42 changes: 42 additions & 0 deletions man/read_meteologica.Rd

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

0 comments on commit fc587bf

Please sign in to comment.