R/f_csv_multivar.R

Defines functions f_csv_multivar

Documented in f_csv_multivar

#' @title Build a multivariable table file in csv format
#'
#' @author P. Chevallier - Jan-Feb 2022
#'
#' @param files list of hts files
#' @param daily default = TRUE
#' @param fileo name of the output file (without extension)
#' 
#' @details The function build a cvs file with values extracted from several hts files at the same date. 
#' So, it's better to run `h_common` before to apply `f_csv_multivar`
#'  
#' If daily is TRUE, only the date is taking into account, not the time.
#' 
#' @return A csv table, where the first field is a date and the next fields values 
#'
#'


# fonction

f_csv_multivar <- function(files, daily = TRUE, fileo = "fileo")
{

# tableau
  x <- tstab <- Value <- NA
  for (i in 1:length(files)){
    load (files[i])
    colab <- paste0(as.character(tstab$Station[1]),"_", as.character(tstab$Sensor[1]))
    y <- select (tstab, Date, Value)
    if (daily) y <- transmute (y, Date = lubridate::as_date(Date), Value) else 
      y <- transmute (y, Date = as.character(lubridate::ymd_hms(as_datetime(Date))), Value)
    colnames(y) <- c("Date", colab)
    if (i==1) x <- y else x <- left_join(x, y, by="Date")
  }

# ecriture
  fileo <- paste0(fileo,".csv")
  write.csv(x, file=fileo, row.names=FALSE)
  message("File ", fileo, " written")
  return(fileo)
}

#FIN

Try the htsr package in your browser

Any scripts or data that you put into this service are public.

htsr documentation built on Oct. 13, 2023, 5:10 p.m.