get_data.R 2.05 KB
Newer Older
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#' get_short_data

#' get_short_data
#' @param stations station ids
#' @param referencetimeFrom referencetimeFrom
#' @param referencetimeTo referencetimeTo
#' @param timeResolution timeResolution
#' @param elements elements
#' @keywords data.synthaq.oslo10.seed123
#' @export
get_short_data <- function(stations=NULL,referencetimeFrom=NULL,referencetimeTo=NULL,timeResolution=NULL,elements=NULL) {

   res <- short_data_init()
   if (!is.null(stations) && !is.null(referencetimeFrom) && !is.null(referencetimeTo) && !is.null(timeResolution) && !is.null(elements)) {
     res <- build_data_df(station=stations,referencetimeFrom=referencetimeFrom,referencetimeTo=referencetimeTo,timeResolution=timeResolution,element=elements)
   }

  res <- tibble::as_tibble(res)
  return(res)

}


build_data_df <- function(station,referencetimeFrom,referencetimeTo,timeResolution,element){

  tr_file <- switch(timeResolution,
  #"yearly"  = "yly",
  #"monthly" = "mly",
  #"daily"   = "dly",
  "hourly"  = "hourly",
  stop("no data or not taken into account yet"))

Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
33
  path <- normalizePath(file.path(.libPaths()[1],"data.synthaq.oslo10.seed123","data","aqoslosynth.rda"),mustWork=FALSE)
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

  # load the timeseries
  ld <- try(load(path),silent=TRUE)
  if (!inherits(ld,"try-error")) {

    tmp <- get("aqoslosynth",envir=environment())

    # build the results
    if (!is.null(element) ) {
      res <- tmp %>% filter(date>=as.POSIXct(referencetimeFrom,tz="UTC"),date<=as.POSIXct(referencetimeTo,tz="UTC"),ID %in% station,CO_NAME %in% element)  %>% select(date,ID,CO_NAME,timeResolution,unit,value)
    } else {
       res <- short_data_init()
    }
    names(res) <- c("date","ID","CO_NAME","timeResolution","unit","value")

    rm(tmp)

  } else {
    res <- short_data_init()
  }

  return(res)
}

short_data_init <- function(){
  df <- data.frame(date  = character(0),
                   ID  = character(0),
                   CO_NAME  = character(0),
                   timeResolution = character(0),
                   unit = character(0),
                   value = numeric(0)
                 )
  return(df)
}