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

update code+doc

parent 95b44d75
Package: era5spoblobs
Type: Package
Version: 0.0.1
Version: 0.0.2
Authors@R: c(
person(given="Jean-Marie","Lepioufle", role = c("cre"), email="jml@nilu.no")
)
Title: Era5spoblobs
Description: Get Timeseries From Era5spoblobs.
Description: Get Timeseries From Era5spo Blob.
Depends:
R (>= 4.1.0)
Imports:
data.table
data.table,
R.utils
License: MIT + file LICENSE
Suggests:
knitr,
rmarkdown
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
VignetteBuilder: knitr
# Generated by roxygen2: do not edit by hand
export(era5spoblobs)
export(get_day)
export(get_hour)
export(get_isdst)
export(get_minute)
export(get_month)
export(get_second)
export(get_tzone)
export(get_wday)
export(get_x)
export(get_yday)
export(get_year)
# Generated by roxygen2: do not edit by hand
export(era5spoblobs)
export(get_day)
export(get_hour)
export(get_isdst)
export(get_minute)
export(get_month)
export(get_second)
export(get_tzone)
export(get_wday)
export(get_x)
export(get_yday)
export(get_year)
#' era5spoblobs
#'
#' era5spoblobs
#' @keywords era5spoblobs
#' @export
#' @examples
#' \dontrun{
#' era5spoblobs(path)
#' }
era5spoblobs <- function(path_metadata_url){
era5spoblobs_object(path_metadata_url)
}
era5spoblobs_object <- function(path_metadata_url){
# load metadata_url
metadata_url <- data.table::fread(path_metadata_url)
object <- local({
meta <- function(stationIDs=NULL,west=NULL,south=NULL,east=NULL,north=NULL,elementIDs=NULL){
if (!is.null(stationIDs) && (is.null(north) && is.null(south) && is.null(east) && is.null(west)) ) {
res <- unique(metadata[metadata$AirQualityStationEoICode %in% stationIDs, ])
} else if (is.null(stationIDs) && (!is.null(north) && !is.null(south) && !is.null(east) && !is.null(west)) ) {
res <- unique(metadata[(as.numeric(metadata$lon)>=west & as.numeric(metadata$lon)<=east & as.numeric(metadata$lat)>=south & as.numeric(metadata$lat)<=north) , ])
} else res <- metadata
if (!is.null(elementIDs)){
res <- res[res$AirPollutant %in% elementIDs,]
}
return(res)
}
ts <- function(fromDateTime,toDateTime,stationIDs,elementIDs,levelIDs){
# get the years and months between start_date and end_date
date_seq <- as.POSIXlt(seq(as.POSIXlt(fromDateTime,tz="UTC"),as.POSIXlt(toDateTime,tz="UTC"),by="month"))
years_months <- unique(paste0(get_year(date_seq,CHAR=TRUE),"_",get_month(date_seq,CHAR=TRUE),".csv"))
metadata_target <- unique(metadata_url[metadata_url$station %in% stationIDs & metadata_url$variable %in% elementIDs & metadata_url$pressure_level %in% levelIDs & grepl(paste(years_months,collapse="|"),metadata_url$url), ])
res = data.table::data.table(date = as.POSIXct(integer(0),tz="UTC"), values = numeric(0),variable = character(0),level = character(0),unit = character(0),
SamplingPoint = character(0), station = character(0),
stringsAsFactors=FALSE)
# Access and join SPO timeseries
for (i in 1:nrow(metadata_target)){
tmp <- data.table::fread(metadata_target$url[i])
tmp$SamplingPoint <- rep(metadata_target$spo[i],nrow(tmp))
tmp$station <- rep(metadata_target$station[i],nrow(tmp))
res <- rbind(res,tmp)
}
return(res)
}
environment()
})
lockEnvironment(object, TRUE)
structure(object, class=c("era5spoblobs", class(object)))
}
#' era5spoblobs
#'
#' era5spoblobs
#' @param path_metadata path or url of metadata
#' @param path_metadata_url path or url of metadata_url
#' @keywords era5spoblobs
#' @export
#' @examples
#' \dontrun{
#' era5spoblobs(path)
#' }
era5spoblobs <- function(path_metadata,path_metadata_url){
era5spoblobs_object(path_metadata,path_metadata_url)
}
era5spoblobs_object <- function(path_metadata,path_metadata_url){
# load metadatas
metadata <- data.table::fread(path_metadata)
metadata_url <- data.table::fread(path_metadata_url)
object <- local({
meta <- function(stationIDs=NULL,west=NULL,south=NULL,east=NULL,north=NULL,elementIDs=NULL){
if (!is.null(stationIDs) && (is.null(north) && is.null(south) && is.null(east) && is.null(west)) ) {
res <- unique(metadata[metadata$AirQualityStationEoICode %in% stationIDs, ])
} else if (is.null(stationIDs) && (!is.null(north) && !is.null(south) && !is.null(east) && !is.null(west)) ) {
res <- unique(metadata[(as.numeric(metadata$lon)>=west & as.numeric(metadata$lon)<=east & as.numeric(metadata$lat)>=south & as.numeric(metadata$lat)<=north) , ])
} else res <- metadata
if (!is.null(elementIDs)){
res <- res[res$AirPollutant %in% elementIDs,]
}
return(res)
}
ts <- function(fromDateTime,toDateTime,stationIDs,elementIDs,levelIDs){
# get the years and months between start_date and end_date
date_seq <- as.POSIXlt(seq(as.POSIXlt(fromDateTime,tz="UTC"),as.POSIXlt(toDateTime,tz="UTC"),by="month"))
years_months <- unique(paste0(get_year(date_seq,CHAR=TRUE),"_",get_month(date_seq,CHAR=TRUE),".csv"))
metadata_target <- unique(metadata_url[metadata_url$station %in% stationIDs & metadata_url$variable %in% elementIDs & metadata_url$pressure_level %in% levelIDs & grepl(paste(years_months,collapse="|"),metadata_url$url), ])
res = data.table::data.table(date = as.POSIXct(integer(0),tz="UTC"), values = numeric(0),variable = character(0),level = character(0),unit = character(0),
SamplingPoint = character(0), station = character(0),
stringsAsFactors=FALSE)
# Access and join SPO timeseries
for (i in 1:nrow(metadata_target)){
tmp <- data.table::fread(metadata_target$url[i])
tmp$SamplingPoint <- rep(metadata_target$spo[i],nrow(tmp))
tmp$station <- rep(metadata_target$station[i],nrow(tmp))
res <- rbind(res,tmp)
}
return(res)
}
environment()
})
lockEnvironment(object, TRUE)
structure(object, class=c("era5spoblobs", class(object)))
}
#' Get the year
#'
#' Get the year
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_year <- function(date,CHAR=FALSE){
return(get_x(date,"year",CHAR))
}
#' Get the month
#'
#' Get the month
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_month <- function(date,CHAR=FALSE){
return(get_x(date,"month",CHAR))
}
#' Get the day
#'
#' Get the day
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_day <- function(date,CHAR=FALSE){
return(get_x(date,"day",CHAR))
}
#' Get the hour
#'
#' Get the hour
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_hour <- function(date,CHAR=FALSE){
return(get_x(date,"hour",CHAR))
}
#' Get the minute
#'
#' Get the minute
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_minute <- function(date,CHAR=FALSE){
return(get_x(date,"minute",CHAR))
}
#' Get the second
#'
#' Get the second
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_second <- function(date,CHAR=FALSE){
return(get_x(date,"second",CHAR))
}
#' Get the wday
#'
#' Get the wday
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_wday <- function(date,CHAR=FALSE){
return(get_x(date,"wday",CHAR))
}
#' Get the yday
#'
#' Get the yday
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_yday <- function(date,CHAR=FALSE){
return(get_x(date,"yday",CHAR))
}
#' Get the isdst
#'
#' Get the isdst
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_isdst <- function(date,CHAR=FALSE){
return(get_x(date,"isdst",CHAR))
}
#' Get the tzone
#'
#' Get the tzone
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_tzone <- function(date,CHAR=FALSE){
return(get_x(date,"tzone",CHAR))
}
#' Get x
#'
#' Get x
#' @param date POSIXlt
#' @param part character 'year', 'month',....
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_x <- function(date,part,CHAR=FALSE){
res <- switch(part,
"year" = 1900+date$year,
"month" = 1+date$mon,
"day" = date$mday,
"hour" = date$hour,
"minute" = date$min,
"second" = date$sec,
"wday" = date$wday,
"yday" = date$yday,
"isdst" = date$isdst,
"tzone" = attributes(date)$tzone,
(message=paste0("part:", part," not taken into account "))
)
if ((CHAR) && is.numeric(res)) {
lgth <- length(res)
if (lgth) {
indice <- which(res<10)
res[which(res<10)] <- paste0("0",as.character(res[which(res<10)]))
} else if (lgth==1){
if (res<10) res <- paste0("0",as.character(res))
}
}
return(res)
}
#' Get the year
#'
#' Get the year
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_year <- function(date,CHAR=FALSE){
return(get_x(date,"year",CHAR))
}
#' Get the month
#'
#' Get the month
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_month <- function(date,CHAR=FALSE){
return(get_x(date,"month",CHAR))
}
#' Get the day
#'
#' Get the day
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_day <- function(date,CHAR=FALSE){
return(get_x(date,"day",CHAR))
}
#' Get the hour
#'
#' Get the hour
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_hour <- function(date,CHAR=FALSE){
return(get_x(date,"hour",CHAR))
}
#' Get the minute
#'
#' Get the minute
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_minute <- function(date,CHAR=FALSE){
return(get_x(date,"minute",CHAR))
}
#' Get the second
#'
#' Get the second
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_second <- function(date,CHAR=FALSE){
return(get_x(date,"second",CHAR))
}
#' Get the wday
#'
#' Get the wday
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_wday <- function(date,CHAR=FALSE){
return(get_x(date,"wday",CHAR))
}
#' Get the yday
#'
#' Get the yday
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_yday <- function(date,CHAR=FALSE){
return(get_x(date,"yday",CHAR))
}
#' Get the isdst
#'
#' Get the isdst
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_isdst <- function(date,CHAR=FALSE){
return(get_x(date,"isdst",CHAR))
}
#' Get the tzone
#'
#' Get the tzone
#' @param date POSIXlt
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_tzone <- function(date,CHAR=FALSE){
return(get_x(date,"tzone",CHAR))
}
#' Get x
#'
#' Get x
#' @param date POSIXlt
#' @param part character 'year', 'month',....
#' @param CHAR boolean if output is character or not
#' @keywords era5spoblobs
#' @export
get_x <- function(date,part,CHAR=FALSE){
res <- switch(part,
"year" = 1900+date$year,
"month" = 1+date$mon,
"day" = date$mday,
"hour" = date$hour,
"minute" = date$min,
"second" = date$sec,
"wday" = date$wday,
"yday" = date$yday,
"isdst" = date$isdst,
"tzone" = attributes(date)$tzone,
(message=paste0("part:", part," not taken into account "))
)
if ((CHAR) && is.numeric(res)) {
lgth <- length(res)
if (lgth) {
indice <- which(res<10)
res[which(res<10)] <- paste0("0",as.character(res[which(res<10)]))
} else if (lgth==1){
if (res<10) res <- paste0("0",as.character(res))
}
}
return(res)
}
#' metadata
#'
#' metadata
#'
#' @docType data
#' @name metadata
#' @format A rda file
#' @keywords era5spoblobs
#' @references EEA
#' @source EEA
NULL
# era5spoblobs
Get AQ data timeseries from EEA ERA5 SPO blobs
## Installation
```r
path <- 'S:/Common workspace/Air/AQeReporting/Technical/SandboxTemp/ETC-ACM/lepioufle_sandbox/packages'
pkg <- c("era5spoblobs")
setwd(file.path(path,pkg))
#devtools::document()
setwd(path)
install.packages(file.path(path,pkg),repos=NULL,type="source",dependencies = "Imports")
setwd("~")
```
## Usage
See vignettes
# era5spoblobs
Get AQ data timeseries from EEA ERA5 SPO blobs
## Installation
### Git and R
In your [git] terminal write:
```shell
cd ~/
# if windows cd ~/Documents
mkdir tmp
cd tmp
git clone git@git.nilu.no:r_eea/era5spoblobs.git
```
In R, write:
```r
install.packages("data.table")
install.packages("R.utils")
install.packages("~/tmp/era5spoblobs",repos=NULL,type="source",dependencies = "Imports")
```
In a terminal, write:
```shell
cd ..
rm -r tmp
```
## Usage
See vignettes
library(data.table)
library(R.utils) # read gz files
metadata_url <- data.table::fread("https://aqblobs.blob.core.windows.net/airquality-e/E2aInventory.csv.gz")
#save
save(metadata_url,file = file.path(tempdir(),paste0("metadata_url.rda")),compress="xz")
library(data.table)
load(file.path("path/to/source","aq.e","data","metadata.rda")
tmp <- data.table::as.data.table(metadata)
rm(metadata)
metadata <- tmp[,c("Country","City","AirQualityStationEoICode","SamplingPoint","AirPollutant","AirQualityStationType","Longitude","Latitude","Altitude")]
metadata[,Longitude:=as.numeric(Longitude)]
metadata[,Latitude:=as.numeric(Latitude)]
metadata[, ':=' (lon = mean(Longitude,na.rm=TRUE),lat = mean(Latitude,na.rm=TRUE)), by = "SamplingPoint" ]
metadata[, c("Longitude","Latitude"):=NULL]
metadata <- unique(metadata)
save(metadata,file = file.path(tempdir(),paste0("metadata.rda")),compress="xz")
......@@ -4,7 +4,12 @@
\alias{era5spoblobs}
\title{era5spoblobs}
\usage{
era5spoblobs(path_metadata_url)
era5spoblobs(path_metadata, path_metadata_url)
}
\arguments{
\item{path_metadata}{path or url of metadata}
\item{path_metadata_url}{path or url of metadata_url}
}
\description{
era5spoblobs
......
......@@ -14,4 +14,4 @@ get_day(date, CHAR = FALSE)
\description{
Get the day
}
\keyword{aqblobs}
\keyword{era5spoblobs}
......@@ -14,4 +14,4 @@ get_hour(date, CHAR = FALSE)
\description{
Get the hour
}
\keyword{aqblobs}
\keyword{era5spoblobs}
......@@ -14,4 +14,4 @@ get_isdst(date, CHAR = FALSE)
\description{