Commit e7d4d39c authored by Jean-Marie Lepioufle's avatar Jean-Marie Lepioufle
Browse files

use object

parent 5d622c9b
......@@ -12,11 +12,9 @@ Description: Get luftkval dataset as a timeserie data.frame easy to work with.
Depends:
R (>= 3.2.0)
Imports:
timeManip,
luftkvalr,
friendlyr
Remotes:
jeanmarie-eu/timeManip,
git::https://git.nilu.no/rdbnilu/luftkvalr.git,
git::https://git.nilu.no/rfriendlyts/friendlyr.git
License: GPL-3
......
friendly_df <- function(x,res){
if (x$dateStructObj$COL_DATE) {
tmp <- friendly_df_(x=x,ugly_df=res)
} else if (!x$dateStructObj$COL_DATE){
tmp <- friendlyr::checkDate(df = friendly_df_(x=x,ugly_df=res), timeObj=x$timeObj, missingValues = NA)
} else stop("COL_DATE in the right format")
return(tmp)
}
friendly_df_ <- function(x,ugly_df){
output <- data.frame(date = x$timeObj$seqPeriod())
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]
tmp <- friendlyr::checkDate(df=ugly_df[indice,],timeObj = x$timeObj, missingValues=NA)[,"TE_VALUE"]
df <- tmp
if (length(x$stationObj$station_id)>2) {
while (i_s< length(x$stationObj$station_id)) {
indice <- NULL
while ( (!length(indice)) && (i_s< length(x$stationObj$station_id))) {
indice <- which( (ugly_df["ST_ID"] == x$stationObj$station_id[i_s]) & (ugly_df["CO_NAME"] == x$elementObj$element_id[i_e]))
i_s <- i_s+1
}
stations_id <- c(stations_id,x$stationObj$station_id[i_s])
tmp <- friendlyr::checkDate(df=ugly_df[indice,],timeObj = x$timeObj, missingValues=NA)[,"TE_VALUE"]
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
return(friendlyr::flatten(output))
}
get_data <- function(x,auth) {
db <- luftkvalr::luftkvalr()
db$start(auth)
tmp_st <- db$GetAQStations(stationIDs=x$stationObj$station_id)
tmp_ts <- db$GetAQTimeseries(stationIDs=tmp_st$ST_ID,componentName=x$elementObj$element_id)
res <- db$GetAQData(stationIDs=tmp_st$ST_ID,timeSeriesIDs=tmp_ts$TS_ID,fromDateTime=as.character(x$timeObj$fromPeriod()),toDateTime=as.character(x$timeObj$toPeriod()))
db$stop()
#Missing values
res[which(res$TE_VALUE<0),"TE_VALUE"]<-NA
return(res)
}
......@@ -2,51 +2,15 @@
#' luftlyr
#' luftlyr
#' @param fromDate from "2016/11/01 00:00:00"
#' @param toDate to "2017/05/01 00:00:00"
#' @param precision precision second, minute, houly, daily, monthly, yearly
#' @param timeResolution time resolution second, minute, houly, daily, monthly, yearly
#' @param element_id element_id "NO2" "O3", c(...),...
#' @param station_id station ids "665", c(...),...
#' @param COL_DATE FALSE by default: wday,YYYY,mm,dd,HH,MM,SS by default
#' @param format format of the result
#' @param EXPORT yes if TRUE, FALSE by default
#' @param ... other options
#' @param authenticate authentication options
#' @keywords luftlyr
#' @export
#' @examples
#' \dontrun{
#' luftlyr()
#' }
luftlyr <- function(fromDate,toDate,precision,timeResolution,element_id,station_id,COL_DATE=FALSE,format="rda", EXPORT=FALSE,...){
luftlyr <- function(authenticate){
fromDateTime <- timeManip::YYYYmmddHHMMSS2_chr(fromDate)
toDateTime <- timeManip::YYYYmmddHHMMSS2_chr(toDate)
# Get data
db <- luftkvalr::luftkvalr()
db$start(...)
tmp_st <- db$GetAQStations(stationIDs=station_id)
tmp_ts <- db$GetAQTimeseries(stationIDs=tmp_st$ST_ID,componentName=element_id)
res <- db$GetAQData(stationIDs=tmp_st$ST_ID,timeSeriesIDs=tmp_ts$TS_ID,fromDateTime=as.character(fromDateTime),toDateTime=as.character(toDateTime))
db$stop()
#Missing values
res[which(res$TE_VALUE<0),"TE_VALUE"]<-NA
# into a friendly data.frame #
if (COL_DATE) {
tmp <- friendlyr::friendly_df(fromDateTime,toDateTime,timeResolution,element_id=element_id,station_id=station_id,ugly_df=res)
} else if (!COL_DATE){
tmp <- friendlyr::checkDate(df=friendlyr::friendly_df(fromDateTime,toDateTime,timeResolution,element_id=element_id,station_id=station_id,ugly_df=res),
precision=precision,timeResolution=timeResolution, fromPeriod=fromDateTime, toPeriod=toDateTime, missingValues=NA)
} else stop("COL_DATE in the right format")
if (EXPORT) {
v_name <- paste0("luftkval_",paste(station_id,collapse="_"),"_",paste(element_id,collapse="_"),".",format)
} else v_name <- NULL
return(friendlyr::export(df=tmp,filename=v_name,format=format))
return(friendlyr::friendlyr(session_name="luftkvalitet", fn_get=get_data, fn_friendly=friendly_df,authenticate=authenticate))
}
......@@ -8,7 +8,7 @@ luftlyr deals with getting luftkval dataset as a timeserie data.frame easy to wo
1. get the data from the database
1. putting the data into a friendly data.frame format
3. will deal with large dataset (later)
## Prerequisite
......@@ -27,9 +27,10 @@ remotes::install_git("https://git.nilu.no/rfriendlyts/luftlyr.git")
## Usage
```R
values <- luftlyr::luftlyr(fromDate="2016/11/01 00:00:00",toDate="2017/05/01 00:00:00",
precision="hourly",timeResolution="hourly",
element_id=c("NO2","NO"),
station_id="665",
username="******",password="******")
test <- luftlyr::luftlyr(username="******",password="******")
test$data(fromDate="2016/11/01 00:00:00",toDate="2017/05/01 00:00:00",
precision="hourly",timeResolution="hourly",
element_id=c("NO2","NO"),
station_id="665",
```
......@@ -5,29 +5,10 @@
\title{luftlyr
luftlyr}
\usage{
luftlyr(fromDate, toDate, precision, timeResolution, element_id, station_id,
COL_DATE = FALSE, format = "rda", EXPORT = FALSE, ...)
luftlyr(authenticate)
}
\arguments{
\item{fromDate}{from "2016/11/01 00:00:00"}
\item{toDate}{to "2017/05/01 00:00:00"}
\item{precision}{precision second, minute, houly, daily, monthly, yearly}
\item{timeResolution}{time resolution second, minute, houly, daily, monthly, yearly}
\item{element_id}{element_id "NO2" "O3", c(...),...}
\item{station_id}{station ids "665", c(...),...}
\item{COL_DATE}{FALSE by default: wday,YYYY,mm,dd,HH,MM,SS by default}
\item{format}{format of the result}
\item{EXPORT}{yes if TRUE, FALSE by default}
\item{...}{other options}
\item{authenticate}{authentication options}
}
\description{
luftlyr
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment