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

first

parents
Package: era5spoblobs
Type: Package
Version: 0.0.1
Authors@R: c(
person(given="Jean-Marie","Lepioufle", role = c("cre"), email="jml@nilu.no")
)
Title: Era5spoblobs
Description: Get Timeseries From Era5spoblobs.
Depends:
R (>= 4.1.0)
Imports:
data.table
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.2
VignetteBuilder: knitr
The MIT License (MIT)
Copyright © 2020 EEA
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# 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)))
}
#' 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
´´´R
library(era5spoblobs)
hello <- era5spoblobs::era5spoblobs("H:/02-data/ecmwf/era5_at_NO2stations/metadata_url.csv")
meta <- hello$meta(stationIDs="NO0088A",elementIDs="NO2")
study <- hello$ts(fromDateTime="2015-10-01 00:00:00",
toDateTime = "2016-11-30 23:00:00",
stationIDs = unique(meta$AirQualityStationEoICode),
elementIDs = "2m_temperature")
´´´
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")
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/era5spoblobs.R
\name{era5spoblobs}
\alias{era5spoblobs}
\title{era5spoblobs}
\usage{
era5spoblobs(path_metadata_url)
}
\description{
era5spoblobs
}
\examples{
\dontrun{
era5spoblobs(path)
}
}
\keyword{era5spoblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_day}
\alias{get_day}
\title{Get the day}
\usage{
get_day(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the day
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_hour}
\alias{get_hour}
\title{Get the hour}
\usage{
get_hour(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the hour
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_isdst}
\alias{get_isdst}
\title{Get the isdst}
\usage{
get_isdst(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the isdst
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_minute}
\alias{get_minute}
\title{Get the minute}
\usage{
get_minute(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the minute
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_month}
\alias{get_month}
\title{Get the month}
\usage{
get_month(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the month
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_second}
\alias{get_second}
\title{Get the second}
\usage{
get_second(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the second
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_tzone}
\alias{get_tzone}
\title{Get the tzone}
\usage{
get_tzone(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the tzone
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_wday}
\alias{get_wday}
\title{Get the wday}
\usage{
get_wday(date, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get the wday
}
\keyword{aqblobs}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/get_date_parts.R
\name{get_x}
\alias{get_x}
\title{Get x}
\usage{
get_x(date, part, CHAR = FALSE)
}
\arguments{
\item{date}{POSIXlt}
\item{part}{character 'year', 'month',....}
\item{CHAR}{boolean if output is character or not}
}
\description{
Get x
}
\keyword{aqblobs}
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