luftlyr_df.R 2.28 KB
Newer Older
1
#' luftlyr_df_data
2

3
#' luftlyr_df_data
4
#' @param x request object
5
#' @param res resulted data.frame
6
7
8
9
#' @keywords luftlyr
#' @export
#' @examples
#' \dontrun{
10
#' luftlyr_df_data()
11
#' }
12
luftlyr_df_data <- function(x,res){
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
13

Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
14
  tmp <- df_data(x=x,ugly_df=res)
15

Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
16
17
  if (x$dateStructObj$COL_DATE) {
     # ..
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
18
  } else if (!x$dateStructObj$COL_DATE){
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
19
    tmp <- timelyr::checkDate(df = tmp, timeObj=x$timeObj, missingValues = NA)
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
20
21
22
23
24
25

  } else stop("COL_DATE in the right format")

  return(tmp)
}

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#' luftlyr_df_qa

#' luftlyr_df_qa
#' @param x request object
#' @param res resulted data.frame
#' @keywords luftlyr
#' @export
#' @examples
#' \dontrun{
#' luftlyr_df_qa()
#' }
luftlyr_df_qa <- function(x,res){
  luftlyr_df_data(x=x,res=res)
}



43
df_data <- function(x,ugly_df){
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
44
45
46

  output <- data.frame(date = x$timeObj$seqPeriod())

47
48
  target <- setdiff(names(ugly_df), c("YEAR_UTC","MONTH_UTC","DAY_UTC","HOUR_UTC","ST_ID","ST_NAME","CO_NAME"))

Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
49
50
51
52
53
54
55
56
57
58
59
  for (i_e in 1:length(x$elementObj$element_id)) {

     if (length(x$stationObj$station_id)>0) {

       indice <- NULL
       i_s <- 0
       while ( (!length(indice)) && (i_s< length(x$stationObj$station_id))) {
         i_s <- i_s+1
         indice <- which( (ugly_df["ST_ID"] == x$stationObj$station_id[i_s]) & (ugly_df["CO_NAME"] == x$elementObj$element_id[i_e]))
       }
       stations_id <- x$stationObj$station_id[i_s]
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
60

61
       tmp <- timelyr::checkDate(df=ugly_df[indice,],timeObj = x$timeObj, missingValues=NA)[,target]
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
62
63
       df <- tmp

Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
64
       if (length(x$stationObj$station_id)>1) {
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
65
66
67
68
         while  (i_s< length(x$stationObj$station_id)) {
           indice <- NULL
           while ( (!length(indice)) && (i_s< length(x$stationObj$station_id))) {
             i_s <- i_s+1
Jean-Marie Lepioufle's avatar
typo    
Jean-Marie Lepioufle committed
69
             indice <- which( (ugly_df["ST_ID"] == x$stationObj$station_id[i_s]) & (ugly_df["CO_NAME"] == x$elementObj$element_id[i_e]))
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
70
71
           }
           stations_id <- c(stations_id,x$stationObj$station_id[i_s])
72
           tmp <- timelyr::checkDate(df=ugly_df[indice,],timeObj = x$timeObj, missingValues=NA)[,target]
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
73
74
75
76
77
78
79
80
81
82
83
84
85
           df <- cbind(df,tmp)
         }

       }
       output$element <- data.frame(df)
       colnames(output$element) <- (stations_id)
     }

     colnames(output) <- c("date",x$elementObj$element_id[1:i_e])

  }

  # flatten the multi-data.frame
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
86
  return(timelyr::flatten(output))
Jean-Marie Lepioufle's avatar
Jean-Marie Lepioufle committed
87
88

}